]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/tracker.go
Account for the root
[rbdr/mobius] / hotline / tracker.go
index 8f8ddd15e259fd925c14523833e0d3de5d36f346..52963bf1ba6fe34dae227aa4f7fca24db41eb575 100644 (file)
@@ -3,12 +3,12 @@ package hotline
 import (
        "bufio"
        "encoding/binary"
+       "errors"
        "fmt"
        "io"
        "net"
        "slices"
        "strconv"
-       "time"
 )
 
 // TrackerRegistration represents the payload a Hotline server sends to a Tracker to register
@@ -18,6 +18,7 @@ type TrackerRegistration struct {
        PassID      [4]byte // Random number generated by the server
        Name        string  // Server Name
        Description string  // Description of the server
+       Password    string  // Tracker password, if required by tracker
 
        readOffset int // Internal offset to track read progress
 }
@@ -37,6 +38,8 @@ func (tr *TrackerRegistration) Read(p []byte) (int, error) {
                []byte(tr.Name),
                []byte{uint8(len(tr.Description))},
                []byte(tr.Description),
+               []byte{uint8(len(tr.Password))},
+               []byte(tr.Password),
        )
 
        if tr.readOffset >= len(buf) {
@@ -49,10 +52,22 @@ func (tr *TrackerRegistration) Read(p []byte) (int, error) {
        return n, nil
 }
 
-func register(tracker string, tr *TrackerRegistration) error {
-       conn, err := net.Dial("udp", tracker)
+// Dialer interface to abstract the dialing operation
+type Dialer interface {
+       Dial(network, address string) (net.Conn, error)
+}
+
+// RealDialer is the real implementation of the Dialer interface
+type RealDialer struct{}
+
+func (d *RealDialer) Dial(network, address string) (net.Conn, error) {
+       return net.Dial(network, address)
+}
+
+func register(dialer Dialer, tracker string, tr io.Reader) error {
+       conn, err := dialer.Dial("udp", tracker)
        if err != nil {
-               return fmt.Errorf("failed to dial tracker: %w", err)
+               return fmt.Errorf("failed to dial tracker: %v", err)
        }
        defer conn.Close()
 
@@ -63,8 +78,6 @@ func register(tracker string, tr *TrackerRegistration) error {
        return nil
 }
 
-const trackerTimeout = 5 * time.Second
-
 // All string values use 8-bit ASCII character set encoding.
 // Client Interface with Tracker
 // After establishing a connection with tracker, the following information is sent:
@@ -72,23 +85,20 @@ const trackerTimeout = 5 * time.Second
 // Magic number        4       ‘HTRK’
 // Version     2       1 or 2  Old protocol (1) or new (2)
 
-// Reply received from the tracker starts with a header:
+// TrackerHeader is sent in reply Reply received from the tracker starts with a header:
 type TrackerHeader struct {
        Protocol [4]byte // "HTRK" 0x4854524B
        Version  [2]byte // Old protocol (1) or new (2)
 }
 
-// Message type                        2       1       Sending list of servers
-// Message data size   2               Remaining size of this request
-// Number of servers   2               Number of servers in the server list
-// Number of servers   2               Same as previous field
 type ServerInfoHeader struct {
-       MsgType     [2]byte // always has value of 1
+       MsgType     [2]byte // Always has value of 1
        MsgDataSize [2]byte // Remaining size of request
        SrvCount    [2]byte // Number of servers in the server list
        SrvCountDup [2]byte // Same as previous field ¯\_(ツ)_/¯
 }
 
+// ServerRecord is a tracker listing for a single server
 type ServerRecord struct {
        IPAddr          [4]byte
        Port            [2]byte
@@ -100,14 +110,10 @@ type ServerRecord struct {
        Description     []byte
 }
 
-func GetListing(addr string) ([]ServerRecord, error) {
-       conn, err := net.DialTimeout("tcp", addr, trackerTimeout)
-       if err != nil {
-               return []ServerRecord{}, err
-       }
+func GetListing(conn io.ReadWriteCloser) ([]ServerRecord, error) {
        defer func() { _ = conn.Close() }()
 
-       _, err = conn.Write(
+       _, err := conn.Write(
                []byte{
                        0x48, 0x54, 0x52, 0x4B, // HTRK
                        0x00, 0x01, // Version
@@ -135,8 +141,14 @@ func GetListing(addr string) ([]ServerRecord, error) {
        var servers []ServerRecord
        for {
                scanner.Scan()
+
+               // Make a new []byte slice and copy the scanner bytes to it.  This is critical as the
+               // scanner re-uses the buffer for subsequent scans.
+               buf := make([]byte, len(scanner.Bytes()))
+               copy(buf, scanner.Bytes())
+
                var srv ServerRecord
-               _, err = srv.Write(scanner.Bytes())
+               _, err = srv.Write(buf)
                if err != nil {
                        return nil, err
                }
@@ -188,9 +200,13 @@ func serverScanner(data []byte, _ bool) (advance int, token []byte, err error) {
 
 // Write implements io.Writer for ServerRecord
 func (s *ServerRecord) Write(b []byte) (n int, err error) {
+       if len(b) < 13 {
+               return 0, errors.New("too few bytes")
+       }
        copy(s.IPAddr[:], b[0:4])
        copy(s.Port[:], b[4:6])
        copy(s.NumUsers[:], b[6:8])
+       s.NameSize = b[10]
        nameLen := int(b[10])
 
        s.Name = b[11 : 11+nameLen]