]> git.r.bdr.sh - rbdr/mobius/blob - hotline/tracker.go
Backfill file path test
[rbdr/mobius] / hotline / tracker.go
1 package hotline
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "errors"
7 "fmt"
8 "github.com/jhalter/mobius/concat"
9 "net"
10 "strconv"
11 "time"
12 )
13
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 func (tr *TrackerRegistration) Payload() []byte {
23 userCount := make([]byte, 2)
24 binary.BigEndian.PutUint16(userCount, uint16(tr.UserCount))
25
26 return concat.Slices(
27 []byte{0x00, 0x01},
28 tr.Port[:],
29 userCount,
30 []byte{0x00, 0x00},
31 tr.PassID,
32 []byte{uint8(len(tr.Name))},
33 []byte(tr.Name),
34 []byte{uint8(len(tr.Description))},
35 []byte(tr.Description),
36 )
37 }
38
39 func register(tracker string, tr *TrackerRegistration) error {
40 conn, err := net.Dial("udp", tracker)
41 if err != nil {
42 return err
43 }
44
45 if _, err := conn.Write(tr.Payload()); err != nil {
46 return err
47 }
48
49 return nil
50 }
51
52 const trackerTimeout = 5 * time.Second
53
54 // All string values use 8-bit ASCII character set encoding.
55 // Client Interface with Tracker
56 // After establishing a connection with tracker, the following information is sent:
57 // Description Size Data Note
58 // Magic number 4 ‘HTRK’
59 // Version 2 1 or 2 Old protocol (1) or new (2)
60
61 // Reply received from the tracker starts with a header:
62 type TrackerHeader struct {
63 Protocol [4]byte // "HTRK" 0x4854524B
64 Version [2]byte // Old protocol (1) or new (2)
65 }
66
67 // Message type 2 1 Sending list of servers
68 // Message data size 2 Remaining size of this request
69 // Number of servers 2 Number of servers in the server list
70 // Number of servers 2 Same as previous field
71 type ServerInfoHeader struct {
72 MsgType [2]byte // always has value of 1
73 MsgDataSize [2]byte // Remaining size of request
74 SrvCount [2]byte // Number of servers in the server list
75 SrvCountDup [2]byte // Same as previous field ¯\_(ツ)_/¯
76 }
77
78 type ServerRecord struct {
79 IPAddr [4]byte
80 Port [2]byte
81 NumUsers [2]byte // Number of users connected to this particular server
82 Unused [2]byte
83 NameSize byte // Length of name string
84 Name []byte // Server name
85 DescriptionSize byte
86 Description []byte
87 }
88
89 func GetListing(addr string) ([]ServerRecord, error) {
90 conn, err := net.DialTimeout("tcp", addr, trackerTimeout)
91 if err != nil {
92 return []ServerRecord{}, err
93 }
94 defer func() { _ = conn.Close() }()
95
96 _, err = conn.Write(
97 []byte{
98 0x48, 0x54, 0x52, 0x4B, // HTRK
99 0x00, 0x01, // Version
100 },
101 )
102 if err != nil {
103 return nil, err
104 }
105
106 totalRead := 0
107
108 buf := make([]byte, 4096)
109 var readLen int
110 if readLen, err = conn.Read(buf); err != nil {
111 return nil, err
112 }
113 totalRead += readLen // 1514
114
115 var th TrackerHeader
116 if err := binary.Read(bytes.NewReader(buf[:6]), binary.BigEndian, &th); err != nil {
117 return nil, err
118 }
119
120 var info ServerInfoHeader
121 if err := binary.Read(bytes.NewReader(buf[6:14]), binary.BigEndian, &info); err != nil {
122 return nil, err
123 }
124
125 payloadSize := int(binary.BigEndian.Uint16(info.MsgDataSize[:]))
126
127 buf = buf[:readLen]
128 if totalRead < payloadSize {
129 for {
130 tmpBuf := make([]byte, 4096)
131 if readLen, err = conn.Read(tmpBuf); err != nil {
132 return nil, err
133 }
134 buf = append(buf, tmpBuf[:readLen]...)
135 totalRead += readLen
136 if totalRead >= payloadSize {
137 break
138 }
139 }
140 }
141 totalSrv := int(binary.BigEndian.Uint16(info.SrvCount[:]))
142
143 srvBuf := buf[14:totalRead]
144
145 var servers []ServerRecord
146
147 for {
148 var srv ServerRecord
149 n, _ := srv.Read(srvBuf)
150 servers = append(servers, srv)
151
152 srvBuf = srvBuf[n:]
153
154 if len(servers) == totalSrv {
155 return servers, nil
156 }
157
158 if len(srvBuf) == 0 {
159 return servers, errors.New("tracker sent too few bytes for server count")
160 }
161 }
162 }
163
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) PortInt() int {
178 data := binary.BigEndian.Uint16(s.Port[:])
179 return int(data)
180 }
181
182 func (s *ServerRecord) Addr() string {
183 return fmt.Sprintf("%s:%s",
184 net.IP(s.IPAddr[:]),
185 strconv.Itoa(int(binary.BigEndian.Uint16(s.Port[:]))),
186 )
187 }