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 []byte // Number of users connected to this particular server
86 NameSize byte // Length of name string
87 Name []byte // Server’s name
92 func GetListing(addr string) ([]ServerRecord, error) {
93 conn, err := net.DialTimeout("tcp", addr, trackerTimeout)
94 defer func() { _ = conn.Close() }()
98 0x48, 0x54, 0x52, 0x4B, // HTRK
99 0x00, 0x01, // Version
108 buf := make([]byte, 4096)
110 if readLen, err = conn.Read(buf); err != nil {
113 totalRead += readLen // 1514
116 if err := binary.Read(bytes.NewReader(buf[:6]), binary.BigEndian, &th); err != nil {
120 var info ServerInfoHeader
121 if err := binary.Read(bytes.NewReader(buf[6:14]), binary.BigEndian, &info); err != nil {
125 payloadSize := int(binary.BigEndian.Uint16(info.MsgDataSize[:]))
128 if totalRead < payloadSize {
130 tmpBuf := make([]byte, 4096)
131 if readLen, err = conn.Read(tmpBuf); err != nil {
134 buf = append(buf, tmpBuf[:readLen]...)
136 if totalRead >= payloadSize {
141 totalSrv := int(binary.BigEndian.Uint16(info.SrvCount[:]))
143 srvBuf := buf[14:totalRead]
146 var servers []ServerRecord
150 n, _ := srv.Read(srvBuf)
151 servers = append(servers, srv)
155 if len(servers) == totalSrv {
159 if len(srvBuf) == 0 {
160 return servers, errors.New("tracker sent too few bytes for server count")
165 func (s *ServerRecord) Read(b []byte) (n int, err error) {
170 nameLen := int(b[10])
171 s.Name = b[11 : 11+nameLen]
172 s.DescriptionSize = b[11+nameLen]
173 s.Description = b[12+nameLen : 12+nameLen+int(s.DescriptionSize)]
175 return 12 + nameLen + int(s.DescriptionSize), nil
178 func (s *ServerRecord) PortInt() int {
179 data := binary.BigEndian.Uint16(s.Port)
183 func (s *ServerRecord) Addr() string {
184 return fmt.Sprintf("%s:%s",
186 strconv.Itoa(int(binary.BigEndian.Uint16(s.Port))),