8 "github.com/jhalter/mobius/concat"
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
22 func (tr *TrackerRegistration) Payload() []byte {
23 userCount := make([]byte, 2)
24 binary.BigEndian.PutUint16(userCount, uint16(tr.UserCount))
32 []byte{uint8(len(tr.Name))},
34 []byte{uint8(len(tr.Description))},
35 []byte(tr.Description),
39 func register(tracker string, tr TrackerRegistration) error {
40 conn, err := net.Dial("udp", tracker)
45 if _, err := conn.Write(tr.Payload()); err != nil {
52 type ServerListing struct {
55 const trackerTimeout = 5 * time.Second
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)
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)
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 ¯\_(ツ)_/¯
81 type ServerRecord struct {
84 NumUsers [2]byte // Number of users connected to this particular server
86 NameSize byte // Length of name string
87 Name []byte // Server name
92 func GetListing(addr string) ([]ServerRecord, error) {
93 conn, err := net.DialTimeout("tcp", addr, trackerTimeout)
95 return []ServerRecord{}, err
97 defer func() { _ = conn.Close() }()
101 0x48, 0x54, 0x52, 0x4B, // HTRK
102 0x00, 0x01, // Version
111 buf := make([]byte, 4096)
113 if readLen, err = conn.Read(buf); err != nil {
116 totalRead += readLen // 1514
119 if err := binary.Read(bytes.NewReader(buf[:6]), binary.BigEndian, &th); err != nil {
123 var info ServerInfoHeader
124 if err := binary.Read(bytes.NewReader(buf[6:14]), binary.BigEndian, &info); err != nil {
128 payloadSize := int(binary.BigEndian.Uint16(info.MsgDataSize[:]))
131 if totalRead < payloadSize {
133 tmpBuf := make([]byte, 4096)
134 if readLen, err = conn.Read(tmpBuf); err != nil {
137 buf = append(buf, tmpBuf[:readLen]...)
139 if totalRead >= payloadSize {
144 totalSrv := int(binary.BigEndian.Uint16(info.SrvCount[:]))
146 srvBuf := buf[14:totalRead]
148 var servers []ServerRecord
152 n, _ := srv.Read(srvBuf)
153 servers = append(servers, srv)
157 if len(servers) == totalSrv {
161 if len(srvBuf) == 0 {
162 return servers, errors.New("tracker sent too few bytes for server count")
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])
173 s.Name = b[11 : 11+nameLen]
174 s.DescriptionSize = b[11+nameLen]
175 s.Description = b[12+nameLen : 12+nameLen+int(s.DescriptionSize)]
177 return 12 + nameLen + int(s.DescriptionSize), nil
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 {
187 // copy(s.IPAddr[:], b[0:4])
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)]
199 func (s *ServerRecord) PortInt() int {
200 data := binary.BigEndian.Uint16(s.Port[:])
204 func (s *ServerRecord) Addr() string {
205 return fmt.Sprintf("%s:%s",
207 strconv.Itoa(int(binary.BigEndian.Uint16(s.Port[:]))),