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

223 lines
6.2 KiB
Go

//go:build windows
package usb
import (
"fmt"
"log"
"strings"
"unsafe"
"golang.org/x/sys/windows"
)
var (
modsetupapi = windows.NewLazySystemDLL("setupapi.dll")
procSetupDiGetClassDevsW = modsetupapi.NewProc("SetupDiGetClassDevsW")
procSetupDiEnumDeviceInterfaces = modsetupapi.NewProc("SetupDiEnumDeviceInterfaces")
procSetupDiGetDeviceInterfaceDetailW = modsetupapi.NewProc("SetupDiGetDeviceInterfaceDetailW")
procSetupDiDestroyDeviceInfoList = modsetupapi.NewProc("SetupDiDestroyDeviceInfoList")
)
const (
digcfPresent = 0x00000002
digcfDeviceInterface = 0x00000010
)
type spDeviceInterfaceData struct {
CbSize uint32
InterfaceClassGuid windows.GUID
Flags uint32
Reserved uintptr
}
// Enumerate lists USB devices reachable through the usbshare filter driver,
// plus any device registered from outside this process.
//
// Only devices with the filter attached appear: Windows has no equivalent of
// walking /sys/bus/usb, and without the filter there is no way to drive a
// device from user mode anyway, so listing the others would only offer
// devices that cannot actually be shared.
func Enumerate() ([]Device, error) {
devices, err := enumerateFiltered()
if err != nil {
if external := ExternalDevices(); len(external) > 0 {
return external, nil
}
return nil, err
}
return mergeExternal(devices), nil
}
func enumerateFiltered() ([]Device, error) {
handle, _, _ := procSetupDiGetClassDevsW.Call(
uintptr(unsafe.Pointer(&guidDevInterfaceUsbShare)),
0, 0,
uintptr(digcfPresent|digcfDeviceInterface),
)
if handle == uintptr(windows.InvalidHandle) {
return nil, fmt.Errorf("no USB devices with the usbshare filter found " +
"(install driver/windows/usbshare.inf and attach it to the devices you want to share)")
}
defer procSetupDiDestroyDeviceInfoList.Call(handle)
var devices []Device
for index := uint32(0); ; index++ {
var ifaceData spDeviceInterfaceData
ifaceData.CbSize = uint32(unsafe.Sizeof(ifaceData))
ret, _, _ := procSetupDiEnumDeviceInterfaces.Call(
handle, 0,
uintptr(unsafe.Pointer(&guidDevInterfaceUsbShare)),
uintptr(index),
uintptr(unsafe.Pointer(&ifaceData)),
)
if ret == 0 {
break // no more interfaces
}
devicePath, err := interfaceDetailPath(handle, &ifaceData)
if err != nil {
continue
}
dev, err := describeFilteredDevice(devicePath)
if err != nil {
log.Printf("[usb] skipping %s: %v", devicePath, err)
continue
}
devices = append(devices, *dev)
}
return devices, nil
}
// interfaceDetailPath resolves an interface to the device path used to open it.
func interfaceDetailPath(handle uintptr, ifaceData *spDeviceInterfaceData) (string, error) {
// First call determines the size.
var required uint32
procSetupDiGetDeviceInterfaceDetailW.Call(
handle,
uintptr(unsafe.Pointer(ifaceData)),
0, 0,
uintptr(unsafe.Pointer(&required)),
0,
)
if required == 0 {
return "", fmt.Errorf("could not determine the interface detail size")
}
buf := make([]byte, required)
// SP_DEVICE_INTERFACE_DETAIL_DATA_W starts with cbSize, which must be set
// to the size of the fixed part — 8 on 64-bit, counting the alignment of
// the WCHAR array that follows — not the size of the whole buffer.
*(*uint32)(unsafe.Pointer(&buf[0])) = 8
ret, _, err := procSetupDiGetDeviceInterfaceDetailW.Call(
handle,
uintptr(unsafe.Pointer(ifaceData)),
uintptr(unsafe.Pointer(&buf[0])),
uintptr(required),
uintptr(unsafe.Pointer(&required)),
0,
)
if ret == 0 {
return "", fmt.Errorf("reading interface detail: %w", err)
}
// The path is a null-terminated WCHAR string starting after cbSize.
pathPtr := (*uint16)(unsafe.Pointer(&buf[4]))
return windows.UTF16PtrToString(pathPtr), nil
}
// describeFilteredDevice opens a device briefly to read its descriptors.
//
// Claiming it here means the class driver stops seeing it for the duration.
// Enumeration therefore releases immediately: holding the claim would make
// merely listing devices disrupt whatever is using them.
func describeFilteredDevice(devicePath string) (*Device, error) {
handle, err := OpenDriverDevice(devicePath)
if err != nil {
return nil, err
}
defer handle.Close()
descriptors, err := handle.Descriptors()
if err != nil {
return nil, fmt.Errorf("reading descriptors: %w", err)
}
parsed, err := ParseDescriptors(descriptors)
if err != nil {
return nil, fmt.Errorf("parsing descriptors: %w", err)
}
info := handle.Info()
cfg := parsed.FindConfig(info.ConfigurationValue)
if cfg == nil {
cfg = &parsed.Configs[0]
}
dev := &Device{
BusID: busIDFromPath(devicePath),
BusNum: 0,
DevNum: uint32(info.PortNumber),
Speed: translateWindowsSpeed(info.Speed),
VendorID: parsed.VendorID,
ProductID: parsed.ProductID,
BcdDevice: parsed.BcdDevice,
DeviceClass: parsed.DeviceClass,
DeviceSubClass: parsed.DeviceSubClass,
DeviceProtocol: parsed.DeviceProtocol,
ConfigValue: cfg.Value,
NumConfigs: parsed.NumConfigs,
DevPath: devicePath,
Interfaces: cfg.ActiveInterfaces(),
Endpoints: cfg.AllEndpoints(),
}
return dev, nil
}
// busIDFromPath derives a stable identifier from a Windows device path.
//
// Paths look like \\?\usb#vid_046d&pid_c52b#5&1a2b3c4d&0&2#{guid}. The
// instance part is stable for as long as the device stays in the same port,
// which is what peers need: they request devices by this ID.
func busIDFromPath(devicePath string) string {
trimmed := strings.TrimPrefix(devicePath, `\\?\`)
if idx := strings.LastIndex(trimmed, "#{"); idx > 0 {
trimmed = trimmed[:idx]
}
// '#' separates the parts; '&' appears inside them. Neither is a problem
// for transport, but a shorter, more readable ID helps in the UI.
parts := strings.Split(trimmed, "#")
if len(parts) >= 3 {
return strings.ReplaceAll(parts[2], "&", "-")
}
return strings.ReplaceAll(trimmed, "#", "-")
}
// translateWindowsSpeed maps USB_DEVICE_SPEED onto the USB/IP speed codes.
func translateWindowsSpeed(speed uint32) uint32 {
switch speed {
case 0: // UsbLowSpeed
return 1
case 1: // UsbFullSpeed
return 2
case 2: // UsbHighSpeed
return 3
case 3: // UsbSuperSpeed
return 5
default:
return 0
}
}