Skip to content

Commit a9fe3f3

Browse files
committed
Add USB/IP support for macOS
1 parent 1933c19 commit a9fe3f3

9 files changed

Lines changed: 645 additions & 23 deletions

File tree

adapter/usbip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
type USBIPDynamicServer interface {
12-
AddDevice(info usbip.DynamicDeviceInfo, transport usbip.DeviceTransport) (string, error)
12+
AddDevice(info usbip.ProvidedDeviceInfo, transport usbip.DeviceTransport) (string, error)
1313
RemoveDevice(busID string)
1414
SubscribeDevices(ctx context.Context, listener func([]usbip.ControlDeviceInfo))
1515
}

cmd/internal/build_libbox/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func init() {
6363
sharedFlags = append(sharedFlags, "-ldflags", "-X github.com/sagernet/sing-box/constant.Version="+currentTag+" -X internal/godebug.defaultGODEBUG=multipathtcp=0 -s -w -buildid= -checklinkname=0")
6464
debugFlags = append(debugFlags, "-ldflags", "-X github.com/sagernet/sing-box/constant.Version="+currentTag+" -X internal/godebug.defaultGODEBUG=multipathtcp=0 -checklinkname=0")
6565

66-
sharedTags = append(sharedTags, "with_gvisor", "with_quic", "with_wireguard", "with_utls", "with_naive_outbound", "with_clash_api", "badlinkname", "tfogo_checklinkname0")
66+
sharedTags = append(sharedTags, "with_gvisor", "with_quic", "with_wireguard", "with_utls", "with_naive_outbound", "with_clash_api", "with_usbip", "badlinkname", "tfogo_checklinkname0")
6767
darwinTags = append(darwinTags, "with_dhcp", "grpcnotrace")
6868
// memcTags = append(memcTags, "with_tailscale")
6969
sharedTags = append(sharedTags, "with_tailscale", "ts_omit_logtail", "ts_omit_ssh", "ts_omit_drive", "ts_omit_taildrop", "ts_omit_webclient", "ts_omit_doctor", "ts_omit_capture", "ts_omit_kube", "ts_omit_aws", "ts_omit_synology", "ts_omit_bird")

daemon/started_service_usbip.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -287,30 +287,41 @@ func addUSBDevice(ctx context.Context, serviceManager adapter.ServiceManager, se
287287
ctx: ctx,
288288
pending: make(map[uint64]chan *USBURBResponse),
289289
}
290-
busID, err := provider.AddDevice(usbip.DynamicDeviceInfo{
291-
BusID: descriptor.GetDeviceId(),
292-
BusNum: descriptor.GetBusNum(),
293-
DevNum: descriptor.GetDevNum(),
294-
Speed: descriptor.GetSpeed(),
295-
VendorID: uint16(descriptor.GetVendorId()),
296-
ProductID: uint16(descriptor.GetProductId()),
297-
BCDDevice: uint16(descriptor.GetBcdDevice()),
298-
DeviceClass: uint8(descriptor.GetDeviceClass()),
299-
DeviceSubClass: uint8(descriptor.GetDeviceSubClass()),
300-
DeviceProtocol: uint8(descriptor.GetDeviceProtocol()),
301-
ConfigurationValue: uint8(descriptor.GetConfigurationValue()),
302-
NumConfigurations: uint8(descriptor.GetNumConfigurations()),
303-
Interfaces: usbInterfacesFromProto(descriptor.GetInterfaces()),
304-
Serial: descriptor.GetSerial(),
305-
Product: descriptor.GetProduct(),
306-
}, device)
290+
entry := usbDeviceEntryFromDescriptor(descriptor)
291+
busID, err := provider.AddDevice(usbip.ProvidedDeviceInfo{Entry: entry}, device)
307292
if err != nil {
308293
return nil, err
309294
}
310295
device.busID = busID
311296
return device, nil
312297
}
313298

299+
func usbDeviceEntryFromDescriptor(descriptor *USBDeviceDescriptor) usbip.DeviceEntry {
300+
deviceID := descriptor.GetDeviceId()
301+
interfaces := usbInterfacesFromProto(descriptor.GetInterfaces())
302+
info := usbip.DeviceInfoTruncated{
303+
BusNum: descriptor.GetBusNum(),
304+
DevNum: descriptor.GetDevNum(),
305+
Speed: descriptor.GetSpeed(),
306+
IDVendor: uint16(descriptor.GetVendorId()),
307+
IDProduct: uint16(descriptor.GetProductId()),
308+
BCDDevice: uint16(descriptor.GetBcdDevice()),
309+
BDeviceClass: uint8(descriptor.GetDeviceClass()),
310+
BDeviceSubClass: uint8(descriptor.GetDeviceSubClass()),
311+
BDeviceProtocol: uint8(descriptor.GetDeviceProtocol()),
312+
BConfigurationValue: uint8(descriptor.GetConfigurationValue()),
313+
BNumConfigurations: uint8(descriptor.GetNumConfigurations()),
314+
BNumInterfaces: uint8(len(interfaces)),
315+
}
316+
copy(info.BusID[:], deviceID)
317+
return usbip.DeviceEntry{
318+
Info: info,
319+
Interfaces: interfaces,
320+
Serial: descriptor.GetSerial(),
321+
Product: descriptor.GetProduct(),
322+
}
323+
}
324+
314325
// sing-usbip calls Submit concurrently across endpoints for a single device.
315326
type usbProvidedDevice struct {
316327
deviceID string
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package libbox
2+
3+
type USBLocalDeviceInfo struct {
4+
StableID string
5+
BusID string
6+
Backend int32
7+
BusNum int32
8+
DevNum int32
9+
Speed int32
10+
VendorID int32
11+
ProductID int32
12+
BCDDevice int32
13+
DeviceClass int32
14+
DeviceSubClass int32
15+
DeviceProtocol int32
16+
ConfigurationValue int32
17+
NumConfigurations int32
18+
Serial string
19+
Product string
20+
21+
interfaces []*USBSharedDeviceInterface
22+
}
23+
24+
func (d *USBLocalDeviceInfo) Interfaces() USBSharedDeviceInterfaceIterator {
25+
return newIterator(d.interfaces)
26+
}
27+
28+
type USBLocalDeviceInfoIterator interface {
29+
Next() *USBLocalDeviceInfo
30+
HasNext() bool
31+
}
32+
33+
type USBLocalProvidedDevice struct {
34+
ServerTag string
35+
DeviceID string
36+
LocalDeviceID string
37+
Label string
38+
VendorID int32
39+
ProductID int32
40+
}
41+
42+
type USBLocalProviderHandler interface {
43+
OnDeviceError(serverTag string, deviceID string, message string)
44+
OnSessionError(serverTag string, message string)
45+
OnLocalDevicesChanged()
46+
}

0 commit comments

Comments
 (0)