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>
84 lines
2.2 KiB
Go
84 lines
2.2 KiB
Go
//go:build linux
|
|
|
|
package usb
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// Externally supplied file descriptors, keyed by bus ID.
|
|
//
|
|
// Android is the reason this exists. Apps there cannot open /dev/bus/usb:
|
|
// access goes through the framework, which shows a permission dialog and
|
|
// returns an already-open descriptor. A small Java shim obtains it and passes
|
|
// it to this process, which then drives the device through the same usbdevfs
|
|
// ioctls as anywhere else — the kernel interface is identical, only the way
|
|
// the descriptor is obtained differs.
|
|
var (
|
|
adoptedMu sync.Mutex
|
|
adoptedFDs = make(map[string]int)
|
|
)
|
|
|
|
// AdoptDeviceFD registers an already-open usbdevfs file descriptor for a bus
|
|
// ID. The next OpenDevice for that bus ID takes it instead of opening a path.
|
|
//
|
|
// Ownership transfers: the descriptor is closed when the resulting handle is
|
|
// closed, or by ReleaseAdoptedFDs if it is never claimed.
|
|
func AdoptDeviceFD(busID string, fd int) error {
|
|
if busID == "" {
|
|
return fmt.Errorf("bus ID is required")
|
|
}
|
|
if fd < 0 {
|
|
return fmt.Errorf("invalid file descriptor %d", fd)
|
|
}
|
|
|
|
// Reject a descriptor that is not actually usable, so the failure is
|
|
// reported here rather than as a confusing ioctl error much later.
|
|
if _, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0); err != nil {
|
|
return fmt.Errorf("file descriptor %d is not open: %w", fd, err)
|
|
}
|
|
|
|
adoptedMu.Lock()
|
|
defer adoptedMu.Unlock()
|
|
|
|
if old, exists := adoptedFDs[busID]; exists && old != fd {
|
|
unix.Close(old)
|
|
}
|
|
adoptedFDs[busID] = fd
|
|
return nil
|
|
}
|
|
|
|
// takeAdoptedFD removes and returns a registered descriptor, if any.
|
|
func takeAdoptedFD(busID string) (int, bool) {
|
|
adoptedMu.Lock()
|
|
defer adoptedMu.Unlock()
|
|
|
|
fd, ok := adoptedFDs[busID]
|
|
if ok {
|
|
delete(adoptedFDs, busID)
|
|
}
|
|
return fd, ok
|
|
}
|
|
|
|
// HasAdoptedFD reports whether a descriptor is registered for a bus ID.
|
|
func HasAdoptedFD(busID string) bool {
|
|
adoptedMu.Lock()
|
|
defer adoptedMu.Unlock()
|
|
_, ok := adoptedFDs[busID]
|
|
return ok
|
|
}
|
|
|
|
// ReleaseAdoptedFDs closes every registered descriptor that was never claimed.
|
|
func ReleaseAdoptedFDs() {
|
|
adoptedMu.Lock()
|
|
defer adoptedMu.Unlock()
|
|
|
|
for busID, fd := range adoptedFDs {
|
|
unix.Close(fd)
|
|
delete(adoptedFDs, busID)
|
|
}
|
|
}
|