Files
usb-server/internal/usb/device.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

87 lines
2.9 KiB
Go

package usb
// Device represents a USB device
type Device struct {
BusID string `json:"bus_id"` // e.g. "1-1.4"
BusNum uint32 `json:"bus_num"`
DevNum uint32 `json:"dev_num"`
Speed uint32 `json:"speed"`
VendorID uint16 `json:"vendor_id"`
ProductID uint16 `json:"product_id"`
BcdDevice uint16 `json:"bcd_device"`
DeviceClass uint8 `json:"device_class"`
DeviceSubClass uint8 `json:"device_sub_class"`
DeviceProtocol uint8 `json:"device_protocol"`
ConfigValue uint8 `json:"config_value"`
NumConfigs uint8 `json:"num_configs"`
Manufacturer string `json:"manufacturer"`
Product string `json:"product"`
Serial string `json:"serial"`
SysPath string `json:"sys_path"` // sysfs path
DevPath string `json:"dev_path"` // /dev/bus/usb path
// Interfaces holds one entry per interface number at alternate setting 0.
// These are the interfaces that get claimed when sharing the device.
Interfaces []Interface `json:"interfaces"`
// Endpoints holds every endpoint of the active configuration across all
// alternate settings, keyed by full bEndpointAddress (direction bit
// included). Endpoints that only exist in a non-zero alternate setting
// are included, so the transfer type stays correct after SET_INTERFACE.
Endpoints map[uint8]Endpoint `json:"endpoints"`
}
// Interface represents a USB interface at one alternate setting
type Interface struct {
Number uint8 `json:"number"`
AltSetting uint8 `json:"alt_setting"`
Class uint8 `json:"class"`
SubClass uint8 `json:"sub_class"`
Protocol uint8 `json:"protocol"`
Driver string `json:"driver"`
Endpoints []Endpoint `json:"endpoints"`
}
// Endpoint represents a USB endpoint
type Endpoint struct {
Address uint8 `json:"address"` // bEndpointAddress (bit 7=direction, bits 3:0=number)
TransferType uint8 `json:"transfer_type"` // 0=control, 1=iso, 2=bulk, 3=interrupt
MaxPacketSize uint16 `json:"max_packet_size"`
Interval uint8 `json:"interval"` // bInterval
}
// IsIn reports whether this is an IN (device-to-host) endpoint.
func (e Endpoint) IsIn() bool { return e.Address&0x80 != 0 }
// Number returns the endpoint number without the direction bit.
func (e Endpoint) Number() uint8 { return e.Address & 0x0F }
// USB transfer types (from bmAttributes)
const (
TransferTypeControl = 0
TransferTypeIsochronous = 1
TransferTypeBulk = 2
TransferTypeInterrupt = 3
)
// DevID returns the USB/IP device ID (busnum << 16 | devnum)
func (d *Device) DevID() uint32 {
return (d.BusNum << 16) | d.DevNum
}
// IsHub returns true if this is a USB hub
func (d *Device) IsHub() bool {
return d.DeviceClass == 9
}
// DisplayName returns a human-readable device name
func (d *Device) DisplayName() string {
if d.Product != "" {
if d.Manufacturer != "" {
return d.Manufacturer + " " + d.Product
}
return d.Product
}
return "Unknown USB Device"
}