|
| 1 | +package daemon |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/binary" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + |
| 9 | + "github.com/google/gousb" |
| 10 | + "github.com/princePal/droidtether/internal/rndis" |
| 11 | + "github.com/princePal/droidtether/internal/tun" |
| 12 | + "github.com/princePal/droidtether/internal/usb" |
| 13 | + "github.com/rs/zerolog/log" |
| 14 | +) |
| 15 | + |
| 16 | +// Relay handles the packet shuttle between USB Bulk and Tunnel interface. |
| 17 | +type Relay struct { |
| 18 | + dev *usb.Device |
| 19 | + tun tun.Interface |
| 20 | + ctx context.Context |
| 21 | + cancel context.CancelFunc |
| 22 | + |
| 23 | + usbIn *gousb.InEndpoint |
| 24 | + usbOut *gousb.OutEndpoint |
| 25 | + |
| 26 | + phoneMAC []byte |
| 27 | +} |
| 28 | + |
| 29 | +// NewRelay creates a new bidirectional relay. |
| 30 | +func NewRelay(dev *usb.Device, tun tun.Interface, phoneMAC []byte) (*Relay, error) { |
| 31 | + in, out, err := dev.OpenBulkEndpoints() |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + |
| 36 | + ctx, cancel := context.WithCancel(context.Background()) |
| 37 | + return &Relay{ |
| 38 | + dev: dev, |
| 39 | + tun: tun, |
| 40 | + ctx: ctx, |
| 41 | + cancel: cancel, |
| 42 | + usbIn: in, |
| 43 | + usbOut: out, |
| 44 | + phoneMAC: phoneMAC, |
| 45 | + }, nil |
| 46 | +} |
| 47 | + |
| 48 | +// Start spawns the relay goroutines and blocks until context is cancelled or an error occurs. |
| 49 | +func (r *Relay) Start() error { |
| 50 | + errChan := make(chan error, 2) |
| 51 | + |
| 52 | + // Mac -> Phone (Tunnel -> USB) |
| 53 | + go func() { |
| 54 | + buf := make([]byte, 2048) |
| 55 | + for { |
| 56 | + select { |
| 57 | + case <-r.ctx.Done(): |
| 58 | + return |
| 59 | + default: |
| 60 | + n, err := r.tun.Read(buf) |
| 61 | + if err != nil { |
| 62 | + if err != io.EOF { |
| 63 | + errChan <- fmt.Errorf("relay: tun read error: %w", err) |
| 64 | + } |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + // macOS utun header is 4 bytes [0 0 0 2] for IPv4. Strip it. |
| 69 | + if n < 4 { |
| 70 | + continue |
| 71 | + } |
| 72 | + rawIP := buf[4:n] |
| 73 | + |
| 74 | + // Construct Ethernet Header (L2) |
| 75 | + // [DstMAC(6)] [SrcMAC(6)] [Type(2)] |
| 76 | + eth := make([]byte, 14+len(rawIP)) |
| 77 | + copy(eth[0:6], r.phoneMAC) |
| 78 | + copy(eth[6:12], []byte{0x02, 0x00, 0x00, 0x00, 0x00, 0x01}) |
| 79 | + binary.BigEndian.PutUint16(eth[12:14], 0x0800) // IPv4 |
| 80 | + copy(eth[14:], rawIP) |
| 81 | + |
| 82 | + // Wrap in RNDIS (L1-ish) |
| 83 | + pkt := rndis.EncapsulatePacket(eth) |
| 84 | + _, err = r.usbOut.Write(pkt) |
| 85 | + if err != nil { |
| 86 | + errChan <- fmt.Errorf("relay: usb write error: %w", err) |
| 87 | + return |
| 88 | + } |
| 89 | + log.Info().Str("component", "relay").Int("bytes", len(pkt)).Msg("Sent packet to phone") |
| 90 | + } |
| 91 | + } |
| 92 | + }() |
| 93 | + |
| 94 | + |
| 95 | + // Phone -> Mac (USB -> Tunnel) |
| 96 | + go func() { |
| 97 | + buf := make([]byte, 16384) |
| 98 | + for { |
| 99 | + select { |
| 100 | + case <-r.ctx.Done(): |
| 101 | + return |
| 102 | + default: |
| 103 | + n, err := r.usbIn.Read(buf) |
| 104 | + if err != nil { |
| 105 | + errChan <- fmt.Errorf("relay: usb read error: %w", err) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + offset := 0 |
| 110 | + for offset < n { |
| 111 | + msg := buf[offset:] |
| 112 | + if len(msg) < 8 { |
| 113 | + break |
| 114 | + } |
| 115 | + msgType := binary.LittleEndian.Uint32(msg[0:4]) |
| 116 | + msgLen := int(binary.LittleEndian.Uint32(msg[4:8])) |
| 117 | + |
| 118 | + if msgType == rndis.MsgPacket && msgLen > 44 { |
| 119 | + ethPkt, err := rndis.DecapsulatePacket(msg[:msgLen]) |
| 120 | + if err == nil && len(ethPkt) > 14 { |
| 121 | + // Strip Ethernet header (14 bytes) |
| 122 | + rawIP := ethPkt[14:] |
| 123 | + |
| 124 | + // Prepend macOS utun header [0 0 0 2] |
| 125 | + outBuf := make([]byte, 4+len(rawIP)) |
| 126 | + binary.BigEndian.PutUint32(outBuf[0:4], 2) |
| 127 | + copy(outBuf[4:], rawIP) |
| 128 | + |
| 129 | + _, _ = r.tun.Write(outBuf) |
| 130 | + log.Info().Str("component", "relay").Int("bytes", len(outBuf)).Msg("Received packet from phone") |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if msgLen == 0 { |
| 135 | + break |
| 136 | + } |
| 137 | + offset += msgLen |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + }() |
| 142 | + |
| 143 | + |
| 144 | + log.Info().Str("component", "relay").Msg("Bidirectional packet relay started") |
| 145 | + |
| 146 | + // Wait for error or shutdown |
| 147 | + select { |
| 148 | + case err := <-errChan: |
| 149 | + r.cancel() |
| 150 | + return err |
| 151 | + case <-r.ctx.Done(): |
| 152 | + return nil |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// Stop shuts down the relay. |
| 157 | +func (r *Relay) Stop() { |
| 158 | + r.cancel() |
| 159 | +} |
0 commit comments