]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | import ( | |
8ff2b66d | 4 | "bufio" |
6988a057 JH |
5 | "encoding/binary" |
6 | "fmt" | |
9a75b7cb | 7 | "io" |
6988a057 | 8 | "net" |
9a75b7cb | 9 | "slices" |
6988a057 JH |
10 | "strconv" |
11 | "time" | |
12 | ) | |
13 | ||
8ff2b66d | 14 | // TrackerRegistration represents the payload a Hotline server sends to a Tracker to register |
6988a057 | 15 | type TrackerRegistration struct { |
9a75b7cb | 16 | Port [2]byte // Server's listening TCP port number |
40414f92 | 17 | UserCount int // Number of users connected to this particular server |
9a75b7cb | 18 | PassID [4]byte // Random number generated by the server |
40414f92 JH |
19 | Name string // Server name |
20 | Description string // Description of the server | |
6988a057 JH |
21 | } |
22 | ||
9a75b7cb JH |
23 | // Read implements io.Reader to write tracker registration payload bytes to slice |
24 | func (tr *TrackerRegistration) Read(p []byte) (int, error) { | |
6988a057 JH |
25 | userCount := make([]byte, 2) |
26 | binary.BigEndian.PutUint16(userCount, uint16(tr.UserCount)) | |
27 | ||
9a75b7cb JH |
28 | return copy(p, slices.Concat( |
29 | []byte{0x00, 0x01}, // Magic number, always 1 | |
40414f92 | 30 | tr.Port[:], |
6988a057 | 31 | userCount, |
9a75b7cb JH |
32 | []byte{0x00, 0x00}, // Magic number, always 0 |
33 | tr.PassID[:], | |
6988a057 JH |
34 | []byte{uint8(len(tr.Name))}, |
35 | []byte(tr.Name), | |
36 | []byte{uint8(len(tr.Description))}, | |
37 | []byte(tr.Description), | |
9a75b7cb | 38 | )[:]), io.EOF |
6988a057 JH |
39 | } |
40 | ||
e42888eb | 41 | func register(tracker string, tr *TrackerRegistration) error { |
6988a057 JH |
42 | conn, err := net.Dial("udp", tracker) |
43 | if err != nil { | |
9a75b7cb | 44 | return fmt.Errorf("failed to dial tracker: %w", err) |
6988a057 | 45 | } |
9a75b7cb | 46 | defer conn.Close() |
6988a057 | 47 | |
9a75b7cb JH |
48 | if _, err := io.Copy(conn, tr); err != nil { |
49 | return fmt.Errorf("failed to write to connection: %w", err) | |
6988a057 JH |
50 | } |
51 | ||
52 | return nil | |
53 | } | |
54 | ||
6988a057 JH |
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: | |
6988a057 JH |
65 | type TrackerHeader struct { |
66 | Protocol [4]byte // "HTRK" 0x4854524B | |
67 | Version [2]byte // Old protocol (1) or new (2) | |
68 | } | |
69 | ||
aebc4d36 JH |
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 | |
6988a057 JH |
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 { | |
c5d9af5a JH |
82 | IPAddr [4]byte |
83 | Port [2]byte | |
84 | NumUsers [2]byte // Number of users connected to this particular server | |
85 | Unused [2]byte | |
6988a057 | 86 | NameSize byte // Length of name string |
c5d9af5a | 87 | Name []byte // Server name |
6988a057 JH |
88 | DescriptionSize byte |
89 | Description []byte | |
90 | } | |
91 | ||
92 | func GetListing(addr string) ([]ServerRecord, error) { | |
93 | conn, err := net.DialTimeout("tcp", addr, trackerTimeout) | |
9004987d JH |
94 | if err != nil { |
95 | return []ServerRecord{}, err | |
96 | } | |
0203a8c5 JH |
97 | defer func() { _ = conn.Close() }() |
98 | ||
6988a057 JH |
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 | ||
6988a057 | 109 | var th TrackerHeader |
8ff2b66d | 110 | if err := binary.Read(conn, binary.BigEndian, &th); err != nil { |
6988a057 JH |
111 | return nil, err |
112 | } | |
113 | ||
114 | var info ServerInfoHeader | |
8ff2b66d | 115 | if err := binary.Read(conn, binary.BigEndian, &info); err != nil { |
6988a057 JH |
116 | return nil, err |
117 | } | |
118 | ||
6988a057 JH |
119 | totalSrv := int(binary.BigEndian.Uint16(info.SrvCount[:])) |
120 | ||
8ff2b66d JH |
121 | scanner := bufio.NewScanner(conn) |
122 | scanner.Split(serverScanner) | |
6988a057 JH |
123 | |
124 | var servers []ServerRecord | |
125 | for { | |
8ff2b66d | 126 | scanner.Scan() |
6988a057 | 127 | var srv ServerRecord |
04f40273 JH |
128 | _, err = srv.Read(scanner.Bytes()) |
129 | if err != nil { | |
130 | return nil, err | |
131 | } | |
0203a8c5 | 132 | |
8ff2b66d | 133 | servers = append(servers, srv) |
6988a057 | 134 | if len(servers) == totalSrv { |
8ff2b66d | 135 | break |
6988a057 | 136 | } |
8ff2b66d | 137 | } |
6988a057 | 138 | |
8ff2b66d JH |
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) { | |
04f40273 JH |
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. | |
8ff2b66d JH |
154 | if len(data) < 10 { |
155 | return 0, nil, nil | |
6988a057 | 156 | } |
8ff2b66d JH |
157 | |
158 | // A server entry has two variable length fields: the name and description. | |
04f40273 | 159 | // To get the token length, we first need the name length from the 10th byte |
8ff2b66d JH |
160 | nameLen := int(data[10]) |
161 | ||
04f40273 JH |
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 | ||
8ff2b66d JH |
168 | // Next we need the description length from the 11+nameLen byte: |
169 | descLen := int(data[11+nameLen]) | |
170 | ||
04f40273 JH |
171 | if len(data) < 12+nameLen+descLen { |
172 | return 0, nil, nil | |
173 | } | |
174 | ||
8ff2b66d | 175 | return 12 + nameLen + descLen, data[0 : 12+nameLen+descLen], nil |
6988a057 JH |
176 | } |
177 | ||
8ff2b66d | 178 | // Read implements io.Reader for ServerRecord |
6988a057 | 179 | func (s *ServerRecord) Read(b []byte) (n int, err error) { |
c5d9af5a JH |
180 | copy(s.IPAddr[:], b[0:4]) |
181 | copy(s.Port[:], b[4:6]) | |
182 | copy(s.NumUsers[:], b[6:8]) | |
6988a057 | 183 | nameLen := int(b[10]) |
c5d9af5a | 184 | |
6988a057 JH |
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 | ||
6988a057 JH |
192 | func (s *ServerRecord) Addr() string { |
193 | return fmt.Sprintf("%s:%s", | |
c5d9af5a JH |
194 | net.IP(s.IPAddr[:]), |
195 | strconv.Itoa(int(binary.BigEndian.Uint16(s.Port[:]))), | |
6988a057 JH |
196 | ) |
197 | } |