Skip to content

Commit f45c22c

Browse files
committed
Package usbip drivers with the daemon
1 parent a1f7780 commit f45c22c

3 files changed

Lines changed: 67 additions & 4 deletions

File tree

cmd/internal/build_boxdd/main.go

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import (
99
"os/exec"
1010
"path/filepath"
1111
"runtime"
12+
"slices"
1213
"strings"
1314

1415
"github.com/sagernet/sing-box/cmd/internal/build_shared"
1516
"github.com/sagernet/sing-box/common/windivert"
1617
"github.com/sagernet/sing-box/log"
18+
"github.com/sagernet/sing-usbip/driverassets"
1719
E "github.com/sagernet/sing/common/exceptions"
1820
)
1921

@@ -102,6 +104,67 @@ func build() error {
102104
if err != nil {
103105
return err
104106
}
107+
err = stageUSBIPDrivers(architecture, filepath.Dir(absoluteOutputPath))
108+
if err != nil {
109+
return err
110+
}
111+
}
112+
return nil
113+
}
114+
115+
func stageUSBIPDrivers(architecture string, outputDirectory string) error {
116+
driverPackages := []struct {
117+
assets map[string]driverassets.Package
118+
assetDir string
119+
}{
120+
{driverassets.VBoxUSB, filepath.Join("internal", "vboxusb", "assets")},
121+
{driverassets.VHCI, filepath.Join("internal", "usbipvhci", "assets")},
122+
}
123+
var moduleDirectory string
124+
for _, driverPackage := range driverPackages {
125+
staged := driverPackage.assets[architecture]
126+
for _, architecturePackage := range driverPackage.assets {
127+
for _, file := range architecturePackage.Files {
128+
if slices.ContainsFunc(staged.Files, func(stagedFile driverassets.File) bool {
129+
return stagedFile.Name == file.Name
130+
}) {
131+
continue
132+
}
133+
err := os.Remove(filepath.Join(outputDirectory, file.Name))
134+
if err != nil && !os.IsNotExist(err) {
135+
return E.Cause(err, "remove stale ", file.Name)
136+
}
137+
}
138+
}
139+
if len(staged.Files) == 0 {
140+
continue
141+
}
142+
if moduleDirectory == "" {
143+
listOutput, err := exec.Command("go", "list", "-m", "-f", "{{.Dir}}", "github.com/sagernet/sing-usbip").Output()
144+
if err != nil {
145+
return E.Cause(err, "locate sing-usbip module directory")
146+
}
147+
moduleDirectory = strings.TrimSpace(string(listOutput))
148+
}
149+
for _, file := range staged.Files {
150+
content, err := os.ReadFile(filepath.Join(moduleDirectory, driverPackage.assetDir, architecture, file.Name))
151+
if err != nil {
152+
return E.Cause(err, "read ", file.Name)
153+
}
154+
checksum := sha256.Sum256(content)
155+
if hex.EncodeToString(checksum[:]) != file.SHA256 {
156+
return E.New(file.Name, " does not match the digest declared in sing-usbip/driverassets")
157+
}
158+
targetPath := filepath.Join(outputDirectory, file.Name)
159+
stagedContent, err := os.ReadFile(targetPath)
160+
if err == nil && bytes.Equal(stagedContent, content) {
161+
continue
162+
}
163+
err = os.WriteFile(targetPath, content, 0o644)
164+
if err != nil {
165+
return E.Cause(err, "write ", file.Name)
166+
}
167+
}
105168
}
106169
return nil
107170
}
@@ -164,7 +227,7 @@ func buildTags(operatingSystem string, architecture string, cgoEnabled bool) ([]
164227
}
165228
tags := strings.Split(strings.TrimSpace(string(content)), ",")
166229
if operatingSystem == "windows" {
167-
tags = append(tags, "with_external_windivert")
230+
tags = append(tags, "with_external_windivert", "with_external_usbip_drivers")
168231
}
169232
if debugEnabled {
170233
tags = append(tags, "debug")

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ require (
5656
github.com/sagernet/sing-shadowtls v0.2.1
5757
github.com/sagernet/sing-snell v0.0.0-20260727093646-7cb813e07b73
5858
github.com/sagernet/sing-tun v0.8.12-0.20260810140529-d67734281390
59-
github.com/sagernet/sing-usbip v0.0.0-20260813125128-908a3a2fa917
59+
github.com/sagernet/sing-usbip v0.0.0-20260817040617-28bd42667eca
6060
github.com/sagernet/sing-vmess v0.2.8-0.20250909125414-3aed155119a1
6161
github.com/sagernet/smux v1.5.50-sing-box-mod.1
6262
github.com/sagernet/tailscale v1.102.1-sing-box-1.14-mod.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ github.com/sagernet/sing-snell v0.0.0-20260727093646-7cb813e07b73 h1:Iyhoka9XutV
342342
github.com/sagernet/sing-snell v0.0.0-20260727093646-7cb813e07b73/go.mod h1:et8Lws4f5QbOrY65DmjevHGup3mijJkhswkto6cwciM=
343343
github.com/sagernet/sing-tun v0.8.12-0.20260810140529-d67734281390 h1:hEBG77TGYDLDtrcfexL+OuwMGNGw18nyhdEAmag2Ia8=
344344
github.com/sagernet/sing-tun v0.8.12-0.20260810140529-d67734281390/go.mod h1:3EgPst7agntRO7D6GOsiZ1l9FoqdLeuWmKT5TnWkmf0=
345-
github.com/sagernet/sing-usbip v0.0.0-20260813125128-908a3a2fa917 h1:aTUWExMVkwFCQFTo+uFD7JCO5m7J0xGqymr94/t3eJs=
346-
github.com/sagernet/sing-usbip v0.0.0-20260813125128-908a3a2fa917/go.mod h1:D4CnJX3MNAAANhbQUxfIRgBdnvlTEaV7h6ojedcs+pw=
345+
github.com/sagernet/sing-usbip v0.0.0-20260817040617-28bd42667eca h1:5wA+IE0Fq1CGVLOgSpm0gKKZ03HzugcJ2JyhmDqeX6A=
346+
github.com/sagernet/sing-usbip v0.0.0-20260817040617-28bd42667eca/go.mod h1:ADAZZU85MnM91XNhd2TdZRTaIbA7RjTZuQ1KCHcUbNg=
347347
github.com/sagernet/sing-vmess v0.2.8-0.20250909125414-3aed155119a1 h1:aSwUNYUkVyVvdmBSufR8/nRFonwJeKSIROxHcm5br9o=
348348
github.com/sagernet/sing-vmess v0.2.8-0.20250909125414-3aed155119a1/go.mod h1:P11scgTxMxVVQ8dlM27yNm3Cro40mD0+gHbnqrNGDuY=
349349
github.com/sagernet/smux v1.5.50-sing-box-mod.1 h1:XkJcivBC9V4wBjiGXIXZ229aZCU1hzcbp6kSkkyQ478=

0 commit comments

Comments
 (0)