Files
usb-server/internal/relay/server.go
T
duffyduckandClaude Opus 5 9ed473a965 Fix HID transfers, harden the tunnel, add E2E crypto and direct peers
The HID failure came down to the endpoint type map being indexed by
endpoint number without the direction bit. A composite device can have
endpoint 1 as both interrupt IN (0x81) and bulk OUT (0x01); the last one
read won, so interrupt URBs were submitted as bulk and the kernel rejected
them. The device attached and stayed silent.

Endpoint data now comes from the raw descriptors read from /dev/bus/usb
rather than sysfs, which only ever exposes the active alternate setting —
a webcam's isochronous endpoints are invisible there because they only
exist after SET_INTERFACE. Two sysfs parsing bugs fell out of that too:
the numeric endpoint attributes are hex without a prefix (wMaxPacketSize
"0040" was read as 40, not 64), and bInterval was never read at all.

Reliability: three places could freeze the whole process. The share path
fed io.Pipe from the WebSocket read loop, so one slow USB transfer stalled
every tunnel and the keepalives with them. The relay wrote to client
sockets while holding the hub lock, so one peer that stopped reading
blocked routing and registration for everyone. Control transfers ran
inline in the protocol loop behind a 5s timeout. Also fixed: a use-after-
free where a discarded URB's memory could be collected while the kernel
still owned it, a reap loop that spun at 100% CPU on ioctl errors, a
missing attach timeout, a double close(done) panic, and Hash[:8] in the
relay's log line, which let a client with a short hash take the server
down.

Adds mode "both", so one client can offer and consume devices at once.
The tunnel and client-left callbacks became multicast for it: as plain
fields the second manager to register silently unhooked the first.

Tunnel traffic is now AES-256-GCM end to end, on the relay path as well
as directly. The key is derived from the three tokens, not from the group
hash — the relay is told the hash, so a key derived from it would protect
nothing from the one party in the middle. Group IDs are unchanged, so
existing setups keep working; only clients configured without the tokens
drop to unencrypted, relay-only operation.

Peers now try to connect directly, with the relay supplying the public
address neither side can determine for itself. Candidates are raced
because an unreachable address hangs until timeout rather than refusing.
Falling back to the relay is not an error.

Platform reach: cross-compiled targets for ARM, MIPS and RISC-V (the
Linux client needed no code changes — usbdevfs is not architecture
specific), multi-arch Docker images, an Android bridge that accepts
devices over SCM_RIGHTS because apps cannot open /dev/bus/usb, and macOS
builds via system_profiler enumeration.

Adds a Windows KMDF filter driver under driver/windows with its Go side.
UNTESTED: it has never been compiled or run, needs the WDK to build and
an EV certificate to distribute. Treat it as a starting point.

Adds "usb-client diag": says per machine whether sharing and using are
possible, what stands in the way, and what fixes it. Reports can be
uploaded to a relay to get them off machines that are awkward to copy
from.

96 tests, all green under -race. Builds for linux, windows and darwin on
amd64 and arm64.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-11 22:02:04 +02:00

214 lines
5.9 KiB
Go

package relay
import (
"encoding/json"
"log"
"net"
"net/http"
"strings"
"time"
"github.com/duffy/usb-server/internal/protocol"
"github.com/gorilla/websocket"
)
const (
// readTimeout is how long a client may stay silent before we drop it.
// It must exceed pingInterval so that keepalive pongs refresh it.
readTimeout = 60 * time.Second
// pingInterval is how often the relay pings each client.
pingInterval = 20 * time.Second
// writeTimeout bounds a single frame write. Without it, a peer that has
// stopped reading would pin its write pump forever.
writeTimeout = 20 * time.Second
// maxMessageSize caps an inbound frame. Tunnel frames are at most 64 KB
// of USB payload plus the tunnel header; 1 MB leaves ample headroom.
maxMessageSize = 1024 * 1024
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 64 * 1024,
WriteBufferSize: 64 * 1024,
CheckOrigin: func(r *http.Request) bool {
return true // relay accepts all origins
},
}
// Server is the WebSocket relay server
type Server struct {
hub *Hub
addr string
diag *diagStore
}
// NewServer creates a new relay server
func NewServer(addr string) *Server {
return &Server{
hub: NewHub(),
addr: addr,
diag: newDiagStore(),
}
}
// Run starts the relay server
func (s *Server) Run() error {
mux := http.NewServeMux()
mux.HandleFunc("/ws", s.handleWebSocket)
mux.HandleFunc("/health", s.handleHealth)
mux.HandleFunc("/diag/", s.handleDiag)
// Timeouts bound how long a stuck client can hold a connection. The
// WebSocket route needs no write timeout — those connections are
// long-lived by design — so it is left to the per-message deadlines the
// write pump sets.
server := &http.Server{
Addr: s.addr,
Handler: mux,
ReadHeaderTimeout: 15 * time.Second,
IdleTimeout: 120 * time.Second,
}
log.Printf("[relay] starting on %s", s.addr)
return server.ListenAndServe()
}
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}
func (s *Server) handleWebSocket(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("[relay] upgrade error: %v", err)
return
}
defer conn.Close()
// Set read limits and deadlines
conn.SetReadLimit(maxMessageSize)
conn.SetReadDeadline(time.Now().Add(readTimeout))
conn.SetPongHandler(func(string) error {
conn.SetReadDeadline(time.Now().Add(readTimeout))
return nil
})
// Wait for registration message
_, msgData, err := conn.ReadMessage()
if err != nil {
log.Printf("[relay] read error during registration: %v", err)
return
}
var reg protocol.Register
if err := json.Unmarshal(msgData, &reg); err != nil || reg.Type != protocol.MsgRegister {
log.Printf("[relay] invalid registration message")
conn.WriteJSON(&protocol.ErrorMsg{Type: protocol.MsgError, Message: "invalid registration"})
return
}
if reg.Hash == "" || reg.ClientID == "" || !protocol.ValidMode(reg.Mode) {
conn.WriteJSON(&protocol.ErrorMsg{Type: protocol.MsgError, Message: "missing or invalid registration fields"})
return
}
client := newClient(reg.ClientID, reg.Hash, reg.Mode, reg.Name, conn)
client.DirectPort = reg.DirectPort
client.PublicIP = clientIP(r)
s.hub.Register(client)
defer s.hub.Unregister(client)
// The write pump owns the socket's write side: every frame for this
// client, plus keepalive pings, goes through it. Nothing else may write,
// which is what keeps one unresponsive peer from blocking the hub.
go s.writePump(client)
// Read loop
for {
msgType, data, err := conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure) {
log.Printf("[relay] read error from %s: %v", protocol.ShortID(client.ID), err)
}
break
}
conn.SetReadDeadline(time.Now().Add(readTimeout))
switch msgType {
case websocket.TextMessage:
s.hub.HandleTextMessage(client, data)
case websocket.BinaryMessage:
s.hub.HandleBinaryMessage(client, data)
}
}
client.kill()
}
// clientIP determines the address a client connects from, which is passed on
// to its peers so they can reach it directly.
//
// X-Forwarded-For is honoured because relays are commonly deployed behind a
// reverse proxy, where RemoteAddr would otherwise be the proxy itself. Only
// the first entry is used: later ones are supplied by upstream hops and are
// not trustworthy. A wrong value here costs a failed direct attempt and a
// fallback to relaying, never a security property — the peer still has to
// prove group membership in the handshake.
func clientIP(r *http.Request) string {
if fwd := r.Header.Get("X-Forwarded-For"); fwd != "" {
first := strings.TrimSpace(strings.Split(fwd, ",")[0])
if ip := net.ParseIP(first); ip != nil {
return ip.String()
}
}
if real := strings.TrimSpace(r.Header.Get("X-Real-IP")); real != "" {
if ip := net.ParseIP(real); ip != nil {
return ip.String()
}
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return ""
}
if ip := net.ParseIP(host); ip != nil {
return ip.String()
}
return ""
}
// writePump serialises all writes to one client's socket.
func (s *Server) writePump(client *Client) {
ticker := time.NewTicker(pingInterval)
defer ticker.Stop()
defer client.Conn.Close() // unblocks the read loop when we give up
for {
select {
case msg := <-client.Send:
client.Conn.SetWriteDeadline(time.Now().Add(writeTimeout))
if err := client.Conn.WriteMessage(msg.typ, msg.data); err != nil {
log.Printf("[relay] write error to %s: %v", protocol.ShortID(client.ID), err)
client.kill()
return
}
case <-ticker.C:
client.Conn.SetWriteDeadline(time.Now().Add(writeTimeout))
if err := client.Conn.WriteMessage(websocket.PingMessage, nil); err != nil {
client.kill()
return
}
case <-client.dead:
return
}
}
}