]> git.r.bdr.sh - rbdr/mobius/blob - hotline/tracker.go
4ee192a4ae398f4b1479cc81f88907bde4f8e48a
[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 []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 type ServerListing struct {
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 totalRead := 0
110
111 buf := make([]byte, 4096)
112 var readLen int
113 if readLen, err = conn.Read(buf); err != nil {
114 return nil, err
115 }
116 totalRead += readLen // 1514
117
118 var th TrackerHeader
119 if err := binary.Read(bytes.NewReader(buf[:6]), binary.BigEndian, &th); err != nil {
120 return nil, err
121 }
122
123 var info ServerInfoHeader
124 if err := binary.Read(bytes.NewReader(buf[6:14]), binary.BigEndian, &info); err != nil {
125 return nil, err
126 }
127
128 payloadSize := int(binary.BigEndian.Uint16(info.MsgDataSize[:]))
129
130 buf = buf[:readLen]
131 if totalRead < payloadSize {
132 for {
133 tmpBuf := make([]byte, 4096)
134 if readLen, err = conn.Read(tmpBuf); err != nil {
135 return nil, err
136 }
137 buf = append(buf, tmpBuf[:readLen]...)
138 totalRead += readLen
139 if totalRead >= payloadSize {
140 break
141 }
142 }
143 }
144 totalSrv := int(binary.BigEndian.Uint16(info.SrvCount[:]))
145
146 srvBuf := buf[14:totalRead]
147
148 var servers []ServerRecord
149
150 for {
151 var srv ServerRecord
152 n, _ := srv.Read(srvBuf)
153 servers = append(servers, srv)
154
155 srvBuf = srvBuf[n:]
156
157 if len(servers) == totalSrv {
158 return servers, nil
159 }
160
161 if len(srvBuf) == 0 {
162 return servers, errors.New("tracker sent too few bytes for server count")
163 }
164 }
165 }
166
167 func (s *ServerRecord) Read(b []byte) (n int, err error) {
168 copy(s.IPAddr[:], b[0:4])
169 copy(s.Port[:], b[4:6])
170 copy(s.NumUsers[:], b[6:8])
171 nameLen := int(b[10])
172
173 s.Name = b[11 : 11+nameLen]
174 s.DescriptionSize = b[11+nameLen]
175 s.Description = b[12+nameLen : 12+nameLen+int(s.DescriptionSize)]
176
177 return 12 + nameLen + int(s.DescriptionSize), nil
178 }
179
180 //
181 //func (s *ServerRecord) UnmarshalBinary(b []byte) (err error) {
182 // r := bytes.NewReader(b[:10])
183 // if err := binary.Read(r, binary.BigEndian, s); err != nil {
184 // return err
185 // }
186 //
187 // copy(s.IPAddr[:], b[0:4])
188 // s.Port = b[4:6]
189 // s.NumUsers = b[6:8]
190 // s.NameSize = b[10]
191 // nameLen := int(b[10])
192 // s.Name = b[11 : 11+nameLen]
193 // s.DescriptionSize = b[11+nameLen]
194 // s.Description = b[12+nameLen : 12+nameLen+int(s.DescriptionSize)]
195 //
196 // return nil
197 //}
198
199 func (s *ServerRecord) PortInt() int {
200 data := binary.BigEndian.Uint16(s.Port[:])
201 return int(data)
202 }
203
204 func (s *ServerRecord) Addr() string {
205 return fmt.Sprintf("%s:%s",
206 net.IP(s.IPAddr[:]),
207 strconv.Itoa(int(binary.BigEndian.Uint16(s.Port[:]))),
208 )
209 }