]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "bufio" | |
5 | "encoding/binary" | |
6 | "fmt" | |
7 | "io" | |
8 | "net" | |
9 | "slices" | |
10 | "strconv" | |
11 | "time" | |
12 | ) | |
13 | ||
14 | // TrackerRegistration represents the payload a Hotline server sends to a Tracker to register | |
15 | type TrackerRegistration struct { | |
16 | Port [2]byte // Server's listening TCP port number | |
17 | UserCount int // Number of users connected to this particular server | |
18 | PassID [4]byte // Random number generated by the server | |
19 | Name string // Server Name | |
20 | Description string // Description of the server | |
21 | } | |
22 | ||
23 | // Read implements io.Reader to write tracker registration payload bytes to slice | |
24 | func (tr *TrackerRegistration) Read(p []byte) (int, error) { | |
25 | userCount := make([]byte, 2) | |
26 | binary.BigEndian.PutUint16(userCount, uint16(tr.UserCount)) | |
27 | ||
28 | return copy(p, slices.Concat( | |
29 | []byte{0x00, 0x01}, // Magic number, always 1 | |
30 | tr.Port[:], | |
31 | userCount, | |
32 | []byte{0x00, 0x00}, // Magic number, always 0 | |
33 | tr.PassID[:], | |
34 | []byte{uint8(len(tr.Name))}, | |
35 | []byte(tr.Name), | |
36 | []byte{uint8(len(tr.Description))}, | |
37 | []byte(tr.Description), | |
38 | )[:]), io.EOF | |
39 | } | |
40 | ||
41 | func register(tracker string, tr *TrackerRegistration) error { | |
42 | conn, err := net.Dial("udp", tracker) | |
43 | if err != nil { | |
44 | return fmt.Errorf("failed to dial tracker: %w", err) | |
45 | } | |
46 | defer conn.Close() | |
47 | ||
48 | if _, err := io.Copy(conn, tr); err != nil { | |
49 | return fmt.Errorf("failed to write to connection: %w", err) | |
50 | } | |
51 | ||
52 | return nil | |
53 | } | |
54 | ||
55 | const trackerTimeout = 5 * time.Second | |
56 | ||
57 | // All string values use 8-bit ASCII character set encoding. | |
58 | // Client Interface with Tracker | |
59 | // After establishing a connection with tracker, the following information is sent: | |
60 | // Description Size Data Note | |
61 | // Magic number 4 ‘HTRK’ | |
62 | // Version 2 1 or 2 Old protocol (1) or new (2) | |
63 | ||
64 | // Reply received from the tracker starts with a header: | |
65 | type TrackerHeader struct { | |
66 | Protocol [4]byte // "HTRK" 0x4854524B | |
67 | Version [2]byte // Old protocol (1) or new (2) | |
68 | } | |
69 | ||
70 | // Message type 2 1 Sending list of servers | |
71 | // Message data size 2 Remaining size of this request | |
72 | // Number of servers 2 Number of servers in the server list | |
73 | // Number of servers 2 Same as previous field | |
74 | type ServerInfoHeader struct { | |
75 | MsgType [2]byte // always has value of 1 | |
76 | MsgDataSize [2]byte // Remaining size of request | |
77 | SrvCount [2]byte // Number of servers in the server list | |
78 | SrvCountDup [2]byte // Same as previous field ¯\_(ツ)_/¯ | |
79 | } | |
80 | ||
81 | type ServerRecord struct { | |
82 | IPAddr [4]byte | |
83 | Port [2]byte | |
84 | NumUsers [2]byte // Number of users connected to this particular server | |
85 | Unused [2]byte | |
86 | NameSize byte // Length of Name string | |
87 | Name []byte // Server Name | |
88 | DescriptionSize byte | |
89 | Description []byte | |
90 | } | |
91 | ||
92 | func GetListing(addr string) ([]ServerRecord, error) { | |
93 | conn, err := net.DialTimeout("tcp", addr, trackerTimeout) | |
94 | if err != nil { | |
95 | return []ServerRecord{}, err | |
96 | } | |
97 | defer func() { _ = conn.Close() }() | |
98 | ||
99 | _, err = conn.Write( | |
100 | []byte{ | |
101 | 0x48, 0x54, 0x52, 0x4B, // HTRK | |
102 | 0x00, 0x01, // Version | |
103 | }, | |
104 | ) | |
105 | if err != nil { | |
106 | return nil, err | |
107 | } | |
108 | ||
109 | var th TrackerHeader | |
110 | if err := binary.Read(conn, binary.BigEndian, &th); err != nil { | |
111 | return nil, err | |
112 | } | |
113 | ||
114 | var info ServerInfoHeader | |
115 | if err := binary.Read(conn, binary.BigEndian, &info); err != nil { | |
116 | return nil, err | |
117 | } | |
118 | ||
119 | totalSrv := int(binary.BigEndian.Uint16(info.SrvCount[:])) | |
120 | ||
121 | scanner := bufio.NewScanner(conn) | |
122 | scanner.Split(serverScanner) | |
123 | ||
124 | var servers []ServerRecord | |
125 | for { | |
126 | scanner.Scan() | |
127 | var srv ServerRecord | |
128 | _, err = srv.Write(scanner.Bytes()) | |
129 | if err != nil { | |
130 | return nil, err | |
131 | } | |
132 | ||
133 | servers = append(servers, srv) | |
134 | if len(servers) == totalSrv { | |
135 | break | |
136 | } | |
137 | } | |
138 | ||
139 | return servers, nil | |
140 | } | |
141 | ||
142 | // serverScanner implements bufio.SplitFunc for parsing the tracker list into ServerRecords tokens | |
143 | // Example payload: | |
144 | // 00000000 18 05 30 63 15 7c 00 02 00 00 10 54 68 65 20 4d |..0c.|.....The M| | |
145 | // 00000010 6f 62 69 75 73 20 53 74 72 69 70 40 48 6f 6d 65 |obius Strip@Home| | |
146 | // 00000020 20 6f 66 20 74 68 65 20 4d 6f 62 69 75 73 20 48 | of the Mobius H| | |
147 | // 00000030 6f 74 6c 69 6e 65 20 73 65 72 76 65 72 20 61 6e |otline server an| | |
148 | // 00000040 64 20 63 6c 69 65 6e 74 20 7c 20 54 52 54 50 48 |d client | TRTPH| | |
149 | // 00000050 4f 54 4c 2e 63 6f 6d 3a 35 35 30 30 2d 4f 3a b2 |OTL.com:5500-O:.| | |
150 | // 00000060 15 7c 00 00 00 00 08 53 65 6e 65 63 74 75 73 20 |.|.....Senectus | | |
151 | func serverScanner(data []byte, _ bool) (advance int, token []byte, err error) { | |
152 | // The name length field is the 11th byte of the server record. If we don't have that many bytes, | |
153 | // return nil token so the Scanner reads more data and continues scanning. | |
154 | if len(data) < 10 { | |
155 | return 0, nil, nil | |
156 | } | |
157 | ||
158 | // A server entry has two variable length fields: the name and description. | |
159 | // To get the token length, we first need the name length from the 10th byte | |
160 | nameLen := int(data[10]) | |
161 | ||
162 | // The description length field is at the 12th + nameLen byte of the server record. | |
163 | // If we don't have that many bytes, return nil token so the Scanner reads more data and continues scanning. | |
164 | if len(data) < 11+nameLen { | |
165 | return 0, nil, nil | |
166 | } | |
167 | ||
168 | // Next we need the description length from the 11+nameLen byte: | |
169 | descLen := int(data[11+nameLen]) | |
170 | ||
171 | if len(data) < 12+nameLen+descLen { | |
172 | return 0, nil, nil | |
173 | } | |
174 | ||
175 | return 12 + nameLen + descLen, data[0 : 12+nameLen+descLen], nil | |
176 | } | |
177 | ||
178 | // Write implements io.Writer for ServerRecord | |
179 | func (s *ServerRecord) Write(b []byte) (n int, err error) { | |
180 | copy(s.IPAddr[:], b[0:4]) | |
181 | copy(s.Port[:], b[4:6]) | |
182 | copy(s.NumUsers[:], b[6:8]) | |
183 | nameLen := int(b[10]) | |
184 | ||
185 | s.Name = b[11 : 11+nameLen] | |
186 | s.DescriptionSize = b[11+nameLen] | |
187 | s.Description = b[12+nameLen : 12+nameLen+int(s.DescriptionSize)] | |
188 | ||
189 | return 12 + nameLen + int(s.DescriptionSize), nil | |
190 | } | |
191 | ||
192 | func (s *ServerRecord) Addr() string { | |
193 | return fmt.Sprintf("%s:%s", | |
194 | net.IP(s.IPAddr[:]), | |
195 | strconv.Itoa(int(binary.BigEndian.Uint16(s.Port[:]))), | |
196 | ) | |
197 | } |