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>
278 lines
6.8 KiB
Go
278 lines
6.8 KiB
Go
//go:build linux
|
|
|
|
package usb
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
const sysfsUSBDevices = "/sys/bus/usb/devices"
|
|
|
|
// Enumerate lists all USB devices by reading sysfs, plus any device that was
|
|
// registered from outside the process (see RegisterExternalDevice).
|
|
func Enumerate() ([]Device, error) {
|
|
entries, err := os.ReadDir(sysfsUSBDevices)
|
|
if err != nil {
|
|
// On Android sysfs is not readable by an app, but devices handed in
|
|
// through the bridge still work. Only report a failure when there is
|
|
// nothing at all to go on.
|
|
if external := ExternalDevices(); len(external) > 0 {
|
|
return external, nil
|
|
}
|
|
return nil, fmt.Errorf("reading sysfs: %w", err)
|
|
}
|
|
|
|
var devices []Device
|
|
for _, entry := range entries {
|
|
name := entry.Name()
|
|
|
|
// Skip interfaces (contain ":") and "usb*" root hubs
|
|
if strings.Contains(name, ":") || strings.HasPrefix(name, "usb") {
|
|
continue
|
|
}
|
|
|
|
// Must be a device path like "1-1", "1-1.4", "2-3", etc.
|
|
if !isDevicePath(name) {
|
|
continue
|
|
}
|
|
|
|
dev, err := readDevice(name)
|
|
if err != nil {
|
|
continue // skip devices we can't read
|
|
}
|
|
|
|
// Skip hubs
|
|
if dev.IsHub() {
|
|
continue
|
|
}
|
|
|
|
devices = append(devices, *dev)
|
|
}
|
|
|
|
return mergeExternal(devices), nil
|
|
}
|
|
|
|
func isDevicePath(name string) bool {
|
|
// Device paths look like "1-1", "1-1.4", "2-3.1.2"
|
|
// First char is a digit (bus number)
|
|
if len(name) < 3 {
|
|
return false
|
|
}
|
|
if name[0] < '1' || name[0] > '9' {
|
|
return false
|
|
}
|
|
if name[1] != '-' {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func readDevice(busID string) (*Device, error) {
|
|
sysPath := filepath.Join(sysfsUSBDevices, busID)
|
|
|
|
dev := &Device{
|
|
BusID: busID,
|
|
SysPath: sysPath,
|
|
}
|
|
|
|
// Read basic attributes
|
|
dev.BusNum = readUint32(sysPath, "busnum")
|
|
dev.DevNum = readUint32(sysPath, "devnum")
|
|
dev.Speed = parseSpeed(readString(sysPath, "speed"))
|
|
dev.VendorID = readHex16(sysPath, "idVendor")
|
|
dev.ProductID = readHex16(sysPath, "idProduct")
|
|
dev.BcdDevice = readHex16(sysPath, "bcdDevice")
|
|
dev.DeviceClass = readHex8(sysPath, "bDeviceClass")
|
|
dev.DeviceSubClass = readHex8(sysPath, "bDeviceSubClass")
|
|
dev.DeviceProtocol = readHex8(sysPath, "bDeviceProtocol")
|
|
dev.ConfigValue = uint8(readUint32(sysPath, "bConfigurationValue"))
|
|
dev.NumConfigs = uint8(readUint32(sysPath, "bNumConfigurations"))
|
|
|
|
// Read string descriptors
|
|
dev.Manufacturer = readString(sysPath, "manufacturer")
|
|
dev.Product = readString(sysPath, "product")
|
|
dev.Serial = readString(sysPath, "serial")
|
|
|
|
// Compute dev path
|
|
dev.DevPath = fmt.Sprintf("/dev/bus/usb/%03d/%03d", dev.BusNum, dev.DevNum)
|
|
|
|
// Read interfaces from sysfs. This gives us the bound kernel driver per
|
|
// interface, which the raw descriptors don't contain.
|
|
dev.Interfaces = readInterfaces(sysPath, busID)
|
|
|
|
// Overlay the raw descriptors from the usbdevfs file. Only these expose
|
|
// interface alternate settings and correct endpoint attributes; sysfs
|
|
// shows just the active alternate setting. Without the non-zero alternate
|
|
// settings the endpoint type map is wrong for webcams and audio devices.
|
|
applyRawDescriptors(dev)
|
|
|
|
return dev, nil
|
|
}
|
|
|
|
// applyRawDescriptors reads the device's descriptor blob from its usbdevfs
|
|
// file and fills in Endpoints plus any interface data sysfs did not provide.
|
|
// Failure is not fatal: reading /dev/bus/usb requires permissions we may not
|
|
// have when merely listing devices, and the sysfs data alone is enough for
|
|
// that. Sharing a device opens the same file anyway and would fail earlier.
|
|
func applyRawDescriptors(dev *Device) {
|
|
data, err := os.ReadFile(dev.DevPath)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
pd, err := ParseDescriptors(data)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
cfg := pd.FindConfig(dev.ConfigValue)
|
|
if cfg == nil {
|
|
// The device is unconfigured, or sysfs and the descriptors disagree.
|
|
// Fall back to the first configuration.
|
|
if len(pd.Configs) == 0 {
|
|
return
|
|
}
|
|
cfg = &pd.Configs[0]
|
|
}
|
|
|
|
dev.Endpoints = cfg.AllEndpoints()
|
|
|
|
// Merge: keep the driver names from sysfs, take everything else from the
|
|
// descriptors (which are authoritative and include endpoint intervals).
|
|
drivers := make(map[uint8]string, len(dev.Interfaces))
|
|
for _, iface := range dev.Interfaces {
|
|
drivers[iface.Number] = iface.Driver
|
|
}
|
|
|
|
ifaces := cfg.ActiveInterfaces()
|
|
for i := range ifaces {
|
|
ifaces[i].Driver = drivers[ifaces[i].Number]
|
|
}
|
|
if len(ifaces) > 0 {
|
|
dev.Interfaces = ifaces
|
|
}
|
|
}
|
|
|
|
func readInterfaces(sysPath, busID string) []Interface {
|
|
entries, err := os.ReadDir(sysPath)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
var ifaces []Interface
|
|
for _, entry := range entries {
|
|
name := entry.Name()
|
|
// Interface directories look like "1-1.4:1.0"
|
|
if !strings.HasPrefix(name, busID+":") {
|
|
continue
|
|
}
|
|
|
|
ifacePath := filepath.Join(sysPath, name)
|
|
iface := Interface{
|
|
Number: readHex8(ifacePath, "bInterfaceNumber"),
|
|
Class: readHex8(ifacePath, "bInterfaceClass"),
|
|
SubClass: readHex8(ifacePath, "bInterfaceSubClass"),
|
|
Protocol: readHex8(ifacePath, "bInterfaceProtocol"),
|
|
}
|
|
|
|
// Read driver
|
|
driverLink, err := os.Readlink(filepath.Join(ifacePath, "driver"))
|
|
if err == nil {
|
|
iface.Driver = filepath.Base(driverLink)
|
|
}
|
|
|
|
// Read endpoints
|
|
iface.Endpoints = readEndpoints(ifacePath)
|
|
|
|
ifaces = append(ifaces, iface)
|
|
}
|
|
|
|
return ifaces
|
|
}
|
|
|
|
func readEndpoints(ifacePath string) []Endpoint {
|
|
entries, err := os.ReadDir(ifacePath)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
var eps []Endpoint
|
|
for _, entry := range entries {
|
|
name := entry.Name()
|
|
if !strings.HasPrefix(name, "ep_") || name == "ep_00" {
|
|
continue
|
|
}
|
|
|
|
epPath := filepath.Join(ifacePath, name)
|
|
|
|
// Every numeric endpoint attribute in sysfs is hex, without a 0x
|
|
// prefix — wMaxPacketSize "0040" means 64, not 40.
|
|
eps = append(eps, Endpoint{
|
|
Address: readHex8(epPath, "bEndpointAddress"),
|
|
TransferType: readHex8(epPath, "bmAttributes") & 0x03,
|
|
MaxPacketSize: readHex16(epPath, "wMaxPacketSize"),
|
|
Interval: readHex8(epPath, "bInterval"),
|
|
})
|
|
}
|
|
|
|
return eps
|
|
}
|
|
|
|
func readString(dir, attr string) string {
|
|
data, err := os.ReadFile(filepath.Join(dir, attr))
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(string(data))
|
|
}
|
|
|
|
func readUint32(dir, attr string) uint32 {
|
|
s := readString(dir, attr)
|
|
if s == "" {
|
|
return 0
|
|
}
|
|
v, _ := strconv.ParseUint(s, 10, 32)
|
|
return uint32(v)
|
|
}
|
|
|
|
func readHex16(dir, attr string) uint16 {
|
|
s := readString(dir, attr)
|
|
if s == "" {
|
|
return 0
|
|
}
|
|
v, _ := strconv.ParseUint(s, 16, 16)
|
|
return uint16(v)
|
|
}
|
|
|
|
func readHex8(dir, attr string) uint8 {
|
|
s := readString(dir, attr)
|
|
if s == "" {
|
|
return 0
|
|
}
|
|
v, _ := strconv.ParseUint(s, 16, 8)
|
|
return uint8(v)
|
|
}
|
|
|
|
func parseSpeed(s string) uint32 {
|
|
switch s {
|
|
case "1.5":
|
|
return 1 // Low
|
|
case "12":
|
|
return 2 // Full
|
|
case "480":
|
|
return 3 // High
|
|
case "5000":
|
|
return 5 // Super
|
|
case "10000":
|
|
return 6 // Super+
|
|
case "20000":
|
|
return 6 // Super+ (USB 3.2 2x1)
|
|
default:
|
|
return 0 // Unknown
|
|
}
|
|
}
|