]> git.r.bdr.sh - rbdr/mobius/blob - hotline/tracker.go
97ca1080e4e4abcc1f6856d60a096b6478395a78
[rbdr/mobius] / hotline / tracker.go
1 package hotline
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "fmt"
7 "github.com/jhalter/mobius/concat"
8 "net"
9 "strconv"
10 "time"
11 )
12
13 type TrackerRegistration struct {
14 Port []byte // Server’s listening UDP port number TODO: wat?
15 UserCount int // Number of users connected to this particular server
16 PassID []byte // Random number generated by the server
17 Name string // Server’s name
18 Description string // Description of the server
19 }
20
21 func (tr *TrackerRegistration) Payload() []byte {
22 userCount := make([]byte, 2)
23 binary.BigEndian.PutUint16(userCount, uint16(tr.UserCount))
24
25 return concat.Slices(
26 []byte{0x00, 0x01},
27 tr.Port,
28 userCount,
29 []byte{0x00, 0x00},
30 tr.PassID,
31 []byte{uint8(len(tr.Name))},
32 []byte(tr.Name),
33 []byte{uint8(len(tr.Description))},
34 []byte(tr.Description),
35 )
36 }
37
38 func register(tracker string, tr TrackerRegistration) error {
39 conn, err := net.Dial("udp", tracker)
40 if err != nil {
41 return err
42 }
43
44 if _, err := conn.Write(tr.Payload()); err != nil {
45 return err
46 }
47
48 return nil
49 }
50
51 type ServerListing struct {
52 }
53
54 const trackerTimeout = 5 * time.Second
55
56 // All string values use 8-bit ASCII character set encoding.
57 // Client Interface with Tracker
58 // After establishing a connection with tracker, the following information is sent:
59 // Description Size Data Note
60 // Magic number 4 ‘HTRK’
61 // Version 2 1 or 2 Old protocol (1) or new (2)
62
63 // Reply received from the tracker starts with a header:
64 //
65
66 type TrackerHeader struct {
67 Protocol [4]byte // "HTRK" 0x4854524B
68 Version [2]byte // Old protocol (1) or new (2)
69 }
70
71 //Message type 2 1 Sending list of servers
72 //Message data size 2 Remaining size of this request
73 //Number of servers 2 Number of servers in the server list
74 //Number of servers 2 Same as previous field
75 type ServerInfoHeader struct {
76 MsgType [2]byte // always has value of 1
77 MsgDataSize [2]byte // Remaining size of request
78 SrvCount [2]byte // Number of servers in the server list
79 SrvCountDup [2]byte // Same as previous field ¯\_(ツ)_/¯
80 }
81
82 type ServerRecord struct {
83 IPAddr []byte
84 Port []byte
85 NumUsers []byte // Number of users connected to this particular server
86 Unused []byte
87 NameSize byte // Length of name string
88 Name []byte // Server’s name
89 DescriptionSize byte
90 Description []byte
91 }
92
93 func GetListing(addr string) ([]ServerRecord, error) {
94 conn, err := net.DialTimeout("tcp", addr, trackerTimeout)
95 if err != nil {
96 return nil, err
97 }
98 //spew.Dump(conn)
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) // handshakes are always 12 bytes in length
112 var readLen int
113 if readLen, err = conn.Read(buf); err != nil {
114 return nil, err
115 }
116 totalRead += readLen
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 if totalRead < payloadSize {
131 for {
132 //fmt.Printf("totalRead: %v", totalRead)
133 //fmt.Printf("readLen: %v Payload size: %v, Server count: %x\n", readLen, payloadSize, info.SrvCount)
134
135 if readLen, err = conn.Read(buf); err != nil {
136 return nil, err
137 }
138 totalRead += readLen
139 if totalRead >= payloadSize {
140 break
141 }
142 }
143 }
144 //fmt.Println("passed read")
145 totalSrv := int(binary.BigEndian.Uint16(info.SrvCount[:]))
146
147 //fmt.Printf("readLen: %v Payload size: %v, Server count: %x\n", readLen, payloadSize, info.SrvCount)
148 srvBuf := buf[14:totalRead]
149
150 totalRead += readLen
151
152 var servers []ServerRecord
153 for {
154 var srv ServerRecord
155 n, _ := srv.Read(srvBuf)
156 servers = append(servers, srv)
157
158 srvBuf = srvBuf[n:]
159 // fmt.Printf("srvBuf len: %v\n", len(srvBuf))
160 if len(servers) == totalSrv {
161 return servers, nil
162 }
163
164 if len(srvBuf) == 0 {
165 if readLen, err = conn.Read(buf); err != nil {
166 return nil, err
167 }
168 srvBuf = buf[8:readLen]
169 }
170 }
171
172 return servers, nil
173 }
174
175 func (s *ServerRecord) Read(b []byte) (n int, err error) {
176 s.IPAddr = b[0:4]
177 s.Port = b[4:6]
178 s.NumUsers = b[6:8]
179 s.NameSize = b[10]
180 nameLen := int(b[10])
181 s.Name = b[11 : 11+nameLen]
182 s.DescriptionSize = b[11+nameLen]
183 s.Description = b[12+nameLen : 12+nameLen+int(s.DescriptionSize)]
184
185 return 12 + nameLen + int(s.DescriptionSize), nil
186 }
187
188 func (s *ServerRecord) PortInt() int {
189 data := binary.BigEndian.Uint16(s.Port)
190 return int(data)
191 }
192
193 func (s *ServerRecord) Addr() string {
194 return fmt.Sprintf("%s:%s",
195 net.IP(s.IPAddr),
196 strconv.Itoa(int(binary.BigEndian.Uint16(s.Port))),
197 )
198 }