]> git.r.bdr.sh - rbdr/mobius/blob - hotline/tracker.go
Fix refuse private chat error messaging
[rbdr/mobius] / hotline / tracker.go
1 package hotline
2
3 import (
4 "bufio"
5 "encoding/binary"
6 "fmt"
7 "github.com/jhalter/mobius/concat"
8 "net"
9 "strconv"
10 "time"
11 )
12
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
20 }
21
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))
26
27 return concat.Slices(
28 []byte{0x00, 0x01},
29 tr.Port[:],
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
40 func register(tracker string, tr *TrackerRegistration) error {
41 conn, err := net.Dial("udp", tracker)
42 if err != nil {
43 return err
44 }
45
46 b := tr.Read()
47
48 if _, err := conn.Write(b); err != nil {
49 return err
50 }
51
52 return nil
53 }
54
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:
65 type TrackerHeader struct {
66 Protocol [4]byte // "HTRK" 0x4854524B
67 Version [2]byte // Old protocol (1) or new (2)
68 }
69
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 ¯\_(ツ)_/¯
79 }
80
81 type ServerRecord struct {
82 IPAddr [4]byte
83 Port [2]byte
84 NumUsers [2]byte // Number of users connected to this particular server
85 Unused [2]byte
86 NameSize byte // Length of name string
87 Name []byte // Server name
88 DescriptionSize byte
89 Description []byte
90 }
91
92 func GetListing(addr string) ([]ServerRecord, error) {
93 conn, err := net.DialTimeout("tcp", addr, trackerTimeout)
94 if err != nil {
95 return []ServerRecord{}, err
96 }
97 defer func() { _ = conn.Close() }()
98
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
109 var th TrackerHeader
110 if err := binary.Read(conn, binary.BigEndian, &th); err != nil {
111 return nil, err
112 }
113
114 var info ServerInfoHeader
115 if err := binary.Read(conn, binary.BigEndian, &info); err != nil {
116 return nil, err
117 }
118
119 totalSrv := int(binary.BigEndian.Uint16(info.SrvCount[:]))
120
121 scanner := bufio.NewScanner(conn)
122 scanner.Split(serverScanner)
123
124 var servers []ServerRecord
125 for {
126 scanner.Scan()
127 var srv ServerRecord
128 _, _ = srv.Read(scanner.Bytes())
129
130 servers = append(servers, srv)
131 if len(servers) == totalSrv {
132 break
133 }
134 }
135
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 |
148 func serverScanner(data []byte, _ bool) (advance int, token []byte, err error) {
149 if len(data) < 10 {
150 return 0, nil, nil
151 }
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
161 }
162
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])
169
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
177 func (s *ServerRecord) Addr() string {
178 return fmt.Sprintf("%s:%s",
179 net.IP(s.IPAddr[:]),
180 strconv.Itoa(int(binary.BigEndian.Uint16(s.Port[:]))),
181 )
182 }