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
22 readOffset int // Internal offset to track read progress
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))
31 []byte{0x00, 0x01}, // Magic number, always 1
34 []byte{0x00, 0x00}, // Magic number, always 0
36 []byte{uint8(len(tr.Name))},
38 []byte{uint8(len(tr.Description))},
39 []byte(tr.Description),
42 if tr.readOffset >= len(buf) {
43 return 0, io.EOF // All bytes have been read
46 n := copy(p, buf[tr.readOffset:])
52 // Dialer interface to abstract the dialing operation
53 type Dialer interface {
54 Dial(network, address string) (net.Conn, error)
57 // RealDialer is the real implementation of the Dialer interface
58 type RealDialer struct{}
60 func (d *RealDialer) Dial(network, address string) (net.Conn, error) {
61 return net.Dial(network, address)
64 func register(dialer Dialer, tracker string, tr io.Reader) error {
65 conn, err := dialer.Dial("udp", tracker)
67 return fmt.Errorf("failed to dial tracker: %w", err)
71 if _, err := io.Copy(conn, tr); err != nil {
72 return fmt.Errorf("failed to write to connection: %w", err)
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)
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)
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 ¯\_(ツ)_/¯
98 // ServerRecord is a tracker listing for a single server
99 type ServerRecord struct {
102 NumUsers [2]byte // Number of users connected to this particular server
104 NameSize byte // Length of Name string
105 Name []byte // Server Name
110 func GetListing(conn io.ReadWriteCloser) ([]ServerRecord, error) {
111 defer func() { _ = conn.Close() }()
113 _, err := conn.Write(
115 0x48, 0x54, 0x52, 0x4B, // HTRK
116 0x00, 0x01, // Version
124 if err := binary.Read(conn, binary.BigEndian, &th); err != nil {
128 var info ServerInfoHeader
129 if err := binary.Read(conn, binary.BigEndian, &info); err != nil {
133 totalSrv := int(binary.BigEndian.Uint16(info.SrvCount[:]))
135 scanner := bufio.NewScanner(conn)
136 scanner.Split(serverScanner)
138 var servers []ServerRecord
143 _, err = srv.Write(scanner.Bytes())
148 servers = append(servers, srv)
149 if len(servers) == totalSrv {
157 // serverScanner implements bufio.SplitFunc for parsing the tracker list into ServerRecords tokens
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.
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])
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 {
183 // Next we need the description length from the 11+nameLen byte:
184 descLen := int(data[11+nameLen])
186 if len(data) < 12+nameLen+descLen {
190 return 12 + nameLen + descLen, data[0 : 12+nameLen+descLen], nil
193 // Write implements io.Writer for ServerRecord
194 func (s *ServerRecord) Write(b []byte) (n int, err error) {
196 return 0, errors.New("too few bytes")
198 copy(s.IPAddr[:], b[0:4])
199 copy(s.Port[:], b[4:6])
200 copy(s.NumUsers[:], b[6:8])
202 nameLen := int(b[10])
204 s.Name = b[11 : 11+nameLen]
205 s.DescriptionSize = b[11+nameLen]
206 s.Description = b[12+nameLen : 12+nameLen+int(s.DescriptionSize)]
208 return 12 + nameLen + int(s.DescriptionSize), nil
211 func (s *ServerRecord) Addr() string {
212 return fmt.Sprintf("%s:%s",
214 strconv.Itoa(int(binary.BigEndian.Uint16(s.Port[:]))),