]> git.r.bdr.sh - rbdr/mobius/blame - hotline/tracker.go
Use strings.ReplaceAll method
[rbdr/mobius] / hotline / tracker.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
8ff2b66d 4 "bufio"
6988a057
JH
5 "encoding/binary"
6 "fmt"
7 "github.com/jhalter/mobius/concat"
8 "net"
9 "strconv"
10 "time"
11)
12
8ff2b66d 13// TrackerRegistration represents the payload a Hotline server sends to a Tracker to register
6988a057 14type TrackerRegistration struct {
40414f92
JH
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
6988a057
JH
20}
21
8ff2b66d
JH
22// TODO: reimplement as io.Reader
23func (tr *TrackerRegistration) Read() []byte {
6988a057
JH
24 userCount := make([]byte, 2)
25 binary.BigEndian.PutUint16(userCount, uint16(tr.UserCount))
26
27 return concat.Slices(
28 []byte{0x00, 0x01},
40414f92 29 tr.Port[:],
6988a057
JH
30 userCount,
31 []byte{0x00, 0x00},
32 tr.PassID,
33 []byte{uint8(len(tr.Name))},
34 []byte(tr.Name),
35 []byte{uint8(len(tr.Description))},
36 []byte(tr.Description),
37 )
38}
39
e42888eb 40func register(tracker string, tr *TrackerRegistration) error {
6988a057
JH
41 conn, err := net.Dial("udp", tracker)
42 if err != nil {
43 return err
44 }
45
8ff2b66d
JH
46 b := tr.Read()
47
48 if _, err := conn.Write(b); err != nil {
6988a057
JH
49 return err
50 }
51
52 return nil
53}
54
6988a057
JH
55const 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
65type 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
74type 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
81type 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
92func 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
8ff2b66d 128 _, _ = srv.Read(scanner.Bytes())
0203a8c5 129
8ff2b66d 130 servers = append(servers, srv)
6988a057 131 if len(servers) == totalSrv {
8ff2b66d 132 break
6988a057 133 }
8ff2b66d 134 }
6988a057 135
8ff2b66d
JH
136 return servers, nil
137}
138
139// serverScanner implements bufio.SplitFunc for parsing the tracker list into ServerRecords tokens
140// Example payload:
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 |
148func serverScanner(data []byte, _ bool) (advance int, token []byte, err error) {
149 if len(data) < 10 {
150 return 0, nil, nil
6988a057 151 }
8ff2b66d
JH
152
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])
156
157 // Next we need the description length from the 11+nameLen byte:
158 descLen := int(data[11+nameLen])
159
160 return 12 + nameLen + descLen, data[0 : 12+nameLen+descLen], nil
6988a057
JH
161}
162
8ff2b66d 163// Read implements io.Reader for ServerRecord
6988a057 164func (s *ServerRecord) Read(b []byte) (n int, err error) {
c5d9af5a
JH
165 copy(s.IPAddr[:], b[0:4])
166 copy(s.Port[:], b[4:6])
167 copy(s.NumUsers[:], b[6:8])
6988a057 168 nameLen := int(b[10])
c5d9af5a 169
6988a057
JH
170 s.Name = b[11 : 11+nameLen]
171 s.DescriptionSize = b[11+nameLen]
172 s.Description = b[12+nameLen : 12+nameLen+int(s.DescriptionSize)]
173
174 return 12 + nameLen + int(s.DescriptionSize), nil
175}
176
6988a057
JH
177func (s *ServerRecord) Addr() string {
178 return fmt.Sprintf("%s:%s",
c5d9af5a
JH
179 net.IP(s.IPAddr[:]),
180 strconv.Itoa(int(binary.BigEndian.Uint16(s.Port[:]))),
6988a057
JH
181 )
182}