7 "github.com/jhalter/mobius/concat"
13 // TrackerRegistration represents the payload a Hotline server sends to a Tracker to register
14 type TrackerRegistration struct {
15 Port [2]byte // Server listening port number
16 UserCount int // Number of users connected to this particular server
17 PassID []byte // Random number generated by the server
18 Name string // Server name
19 Description string // Description of the server
22 // TODO: reimplement as io.Reader
23 func (tr *TrackerRegistration) Read() []byte {
24 userCount := make([]byte, 2)
25 binary.BigEndian.PutUint16(userCount, uint16(tr.UserCount))
33 []byte{uint8(len(tr.Name))},
35 []byte{uint8(len(tr.Description))},
36 []byte(tr.Description),
40 func register(tracker string, tr *TrackerRegistration) error {
41 conn, err := net.Dial("udp", tracker)
48 if _, err := conn.Write(b); err != nil {
55 const trackerTimeout = 5 * time.Second
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)
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)
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 ¯\_(ツ)_/¯
81 type ServerRecord struct {
84 NumUsers [2]byte // Number of users connected to this particular server
86 NameSize byte // Length of name string
87 Name []byte // Server name
92 func GetListing(addr string) ([]ServerRecord, error) {
93 conn, err := net.DialTimeout("tcp", addr, trackerTimeout)
95 return []ServerRecord{}, err
97 defer func() { _ = conn.Close() }()
101 0x48, 0x54, 0x52, 0x4B, // HTRK
102 0x00, 0x01, // Version
110 if err := binary.Read(conn, binary.BigEndian, &th); err != nil {
114 var info ServerInfoHeader
115 if err := binary.Read(conn, binary.BigEndian, &info); err != nil {
119 totalSrv := int(binary.BigEndian.Uint16(info.SrvCount[:]))
121 scanner := bufio.NewScanner(conn)
122 scanner.Split(serverScanner)
124 var servers []ServerRecord
128 _, _ = srv.Read(scanner.Bytes())
130 servers = append(servers, srv)
131 if len(servers) == totalSrv {
139 // serverScanner implements bufio.SplitFunc for parsing the tracker list into ServerRecords tokens
141 // 00000000 18 05 30 63 15 7c 00 02 00 00 10 54 68 65 20 4d |..0c.|.....The M|
142 // 00000010 6f 62 69 75 73 20 53 74 72 69 70 40 48 6f 6d 65 |obius Strip@Home|
143 // 00000020 20 6f 66 20 74 68 65 20 4d 6f 62 69 75 73 20 48 | of the Mobius H|
144 // 00000030 6f 74 6c 69 6e 65 20 73 65 72 76 65 72 20 61 6e |otline server an|
145 // 00000040 64 20 63 6c 69 65 6e 74 20 7c 20 54 52 54 50 48 |d client | TRTPH|
146 // 00000050 4f 54 4c 2e 63 6f 6d 3a 35 35 30 30 2d 4f 3a b2 |OTL.com:5500-O:.|
147 // 00000060 15 7c 00 00 00 00 08 53 65 6e 65 63 74 75 73 20 |.|.....Senectus |
148 func serverScanner(data []byte, _ bool) (advance int, token []byte, err error) {
153 // A server entry has two variable length fields: the name and description.
154 // To get the token length, we first need the name length from the 10th byte:
155 nameLen := int(data[10])
157 // Next we need the description length from the 11+nameLen byte:
158 descLen := int(data[11+nameLen])
160 return 12 + nameLen + descLen, data[0 : 12+nameLen+descLen], nil
163 // Read implements io.Reader for ServerRecord
164 func (s *ServerRecord) Read(b []byte) (n int, err error) {
165 copy(s.IPAddr[:], b[0:4])
166 copy(s.Port[:], b[4:6])
167 copy(s.NumUsers[:], b[6:8])
168 nameLen := int(b[10])
170 s.Name = b[11 : 11+nameLen]
171 s.DescriptionSize = b[11+nameLen]
172 s.Description = b[12+nameLen : 12+nameLen+int(s.DescriptionSize)]
174 return 12 + nameLen + int(s.DescriptionSize), nil
177 func (s *ServerRecord) Addr() string {
178 return fmt.Sprintf("%s:%s",
180 strconv.Itoa(int(binary.BigEndian.Uint16(s.Port[:]))),