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