diff --git a/.gitignore b/.gitignore index 5a964b24e..d2b74d08c 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,9 @@ /*.jar /*.aar /*.xcframework/ +/experimental/libbox/*.aar +/experimental/libbox/*.xcframework/ +/experimental/libbox/*.nupkg .DS_Store /config.d/ /venv/ diff --git a/Makefile b/Makefile index b94af10c0..88eb89c60 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,8 @@ PARAMS = -v -trimpath -ldflags "-X 'github.com/sagernet/sing-box/constant.Versio MAIN_PARAMS = $(PARAMS) -tags "$(TAGS)" MAIN = ./cmd/sing-box PREFIX ?= $(shell go env GOPATH) +SING_FFI ?= sing-ffi +LIBBOX_FFI_CONFIG ?= ./experimental/libbox/ffi.json .PHONY: test release docs build @@ -237,11 +239,14 @@ lib_android: lib_apple: go run ./cmd/internal/build_libbox -target apple +lib_windows: + $(SING_FFI) generate --config $(LIBBOX_FFI_CONFIG) --platform-type csharp + lib_android_new: - go run ./cmd/internal/build_libbox_newffi -target android + $(SING_FFI) generate --config $(LIBBOX_FFI_CONFIG) --platform-type android lib_apple_new: - go run ./cmd/internal/build_libbox_newffi -target apple + $(SING_FFI) generate --config $(LIBBOX_FFI_CONFIG) --platform-type apple lib_install: go install -v github.com/sagernet/gomobile/cmd/gomobile@v0.1.11 diff --git a/cmd/internal/build_libbox_newffi/main.go b/cmd/internal/build_libbox_newffi/main.go deleted file mode 100644 index 4df1d4659..000000000 --- a/cmd/internal/build_libbox_newffi/main.go +++ /dev/null @@ -1,93 +0,0 @@ -package main - -import ( - "flag" - "os" - "os/exec" - "path/filepath" - - "github.com/sagernet/sing-box/log" - "github.com/sagernet/sing/common/rw" -) - -var target string - -func init() { - flag.StringVar(&target, "target", "android", "target platform (android or apple)") -} - -func main() { - flag.Parse() - - args := []string{ - "generate", - "-v", - "--config", "experimental/libbox/ffi.json", - "--platform-type", target, - } - command := exec.Command("sing-ffi", args...) - command.Stdout = os.Stdout - command.Stderr = os.Stderr - err := command.Run() - if err != nil { - log.Fatal(err) - } - - copyArtifacts(target) -} - -func copyArtifacts(target string) { - switch target { - case "android": - copyPath := filepath.Join("..", "sing-box-for-android", "app", "libs") - if rw.IsDir(copyPath) { - copyPath, _ = filepath.Abs(copyPath) - for _, name := range []string{"libbox.aar", "libbox-legacy.aar"} { - artifactPath, found := findArtifactPath(name) - if !found { - continue - } - targetPath := filepath.Join(target, artifactPath) - os.RemoveAll(targetPath) - err := os.Rename(artifactPath, targetPath) - if err != nil { - log.Fatal(err) - } - log.Info("copied ", name, " to ", copyPath) - } - } - case "apple": - copyPath := filepath.Join("..", "sing-box-for-apple") - if rw.IsDir(copyPath) { - sourceDir, found := findArtifactPath("Libbox.xcframework") - if !found { - log.Fatal("Libbox.xcframework not found in current directory or experimental/libbox") - } - - targetDir := filepath.Join(copyPath, "Libbox.xcframework") - targetDir, _ = filepath.Abs(targetDir) - err := os.RemoveAll(targetDir) - if err != nil { - log.Fatal(err) - } - err = os.Rename(sourceDir, targetDir) - if err != nil { - log.Fatal(err) - } - log.Info("copied ", sourceDir, " to ", targetDir) - } - } -} - -func findArtifactPath(name string) (string, bool) { - candidates := []string{ - name, - filepath.Join("experimental", "libbox", name), - } - for _, candidate := range candidates { - if rw.IsFile(candidate) || rw.IsDir(candidate) { - return candidate, true - } - } - return "", false -} diff --git a/experimental/libbox/command_client.go b/experimental/libbox/command_client.go index 84fe95951..a5077bea9 100644 --- a/experimental/libbox/command_client.go +++ b/experimental/libbox/command_client.go @@ -119,7 +119,11 @@ func dialTarget() (string, func(context.Context, string) (net.Conn, error)) { } } if sCommandServerListenPort == 0 { - return "unix://" + filepath.Join(sBasePath, "command.sock"), nil + socketPath := filepath.Join(sBasePath, "command.sock") + return "passthrough:///command-socket", func(ctx context.Context, _ string) (net.Conn, error) { + var networkDialer net.Dialer + return networkDialer.DialContext(ctx, "unix", socketPath) + } } return net.JoinHostPort("127.0.0.1", strconv.Itoa(int(sCommandServerListenPort))), nil } diff --git a/experimental/libbox/ffi.json b/experimental/libbox/ffi.json index 5c5c72d5e..283338712 100644 --- a/experimental/libbox/ffi.json +++ b/experimental/libbox/ffi.json @@ -1,10 +1,19 @@ { "version": 1, + "variables": { + "VERSION": "$(go run github.com/sagernet/sing-box/cmd/internal/read_tag@latest)", + "WORKSPACE_ROOT": "../../..", + "DEPLOY_ANDROID": "${WORKSPACE_ROOT}/sing-box-for-android/app/libs", + "DEPLOY_APPLE": "${WORKSPACE_ROOT}/sing-box-for-apple", + "DEPLOY_WINDOWS": "${WORKSPACE_ROOT}/sing-box-for-windows/local-packages" + }, "packages": [ { "id": "libbox", "path": ".", "java_package": "io.nekohasekai.libbox", + "csharp_namespace": "SagerNet", + "csharp_entrypoint": "Libbox", "apple_prefix": "Libbox" } ], @@ -36,7 +45,7 @@ "ts_omit_synology", "ts_omit_bird" ], - "ldflags": "-X github.com/sagernet/sing-box/constant.Version=$(CGO_ENABLED=0 go run github.com/sagernet/sing-box/cmd/internal/read_tag@latest) -X internal/godebug.defaultGODEBUG=multipathtcp=0 -s -w -buildid= -checklinkname=0", + "ldflags": "-X github.com/sagernet/sing-box/constant.Version=${VERSION} -X internal/godebug.defaultGODEBUG=multipathtcp=0 -s -w -buildid= -checklinkname=0", "trimpath": true } }, @@ -66,7 +75,7 @@ "ts_omit_synology", "ts_omit_bird" ], - "ldflags": "-X github.com/sagernet/sing-box/constant.Version=$(CGO_ENABLED=0 go run github.com/sagernet/sing-box/cmd/internal/read_tag@latest) -X internal/godebug.defaultGODEBUG=multipathtcp=0 -s -w -buildid= -checklinkname=0", + "ldflags": "-X github.com/sagernet/sing-box/constant.Version=${VERSION} -X internal/godebug.defaultGODEBUG=multipathtcp=0 -s -w -buildid= -checklinkname=0", "trimpath": true } }, @@ -99,7 +108,7 @@ "ts_omit_synology", "ts_omit_bird" ], - "ldflags": "-X github.com/sagernet/sing-box/constant.Version=$(CGO_ENABLED=0 go run github.com/sagernet/sing-box/cmd/internal/read_tag@latest) -X internal/godebug.defaultGODEBUG=multipathtcp=0 -s -w -buildid= -checklinkname=0", + "ldflags": "-X github.com/sagernet/sing-box/constant.Version=${VERSION} -X internal/godebug.defaultGODEBUG=multipathtcp=0 -s -w -buildid= -checklinkname=0", "trimpath": true }, "overrides": [ @@ -112,6 +121,38 @@ "tags_append": ["with_low_memory"] } ] + }, + { + "id": "windows", + "packages": ["libbox"], + "default": { + "tags": [ + "with_gvisor", + "with_quic", + "with_wireguard", + "with_utls", + "with_naive_outbound", + "with_purego", + "with_clash_api", + "with_conntrack", + "badlinkname", + "tfogo_checklinkname0", + "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" + ], + "ldflags": "-X github.com/sagernet/sing-box/constant.Version=${VERSION} -X internal/godebug.defaultGODEBUG=multipathtcp=0 -s -w -buildid= -checklinkname=0", + "trimpath": true + } } ], "platforms": [ @@ -119,12 +160,19 @@ "type": "android", "build": "android-main", "min_sdk": 23, + "ndk_version": "28.0.13004108", "lib_name": "box", "languages": [{ "type": "java" }], "artifacts": [ { "type": "aar", - "output_path": "libbox.aar" + "output_path": "libbox.aar", + "execute_after": [ + "if [ -d \"${DEPLOY_ANDROID}\" ]; then", + " rm -f \"${DEPLOY_ANDROID}/$$(basename \"${OUTPUT_PATH}\")\"", + " mv \"${OUTPUT_PATH}\" \"${DEPLOY_ANDROID}/\"", + "fi" + ] } ] }, @@ -132,12 +180,19 @@ "type": "android", "build": "android-legacy", "min_sdk": 21, + "ndk_version": "28.0.13004108", "lib_name": "box", "languages": [{ "type": "java" }], "artifacts": [ { "type": "aar", - "output_path": "libbox-legacy.aar" + "output_path": "libbox-legacy.aar", + "execute_after": [ + "if [ -d \"${DEPLOY_ANDROID}\" ]; then", + " rm -f \"${DEPLOY_ANDROID}/$$(basename \"${OUTPUT_PATH}\")\"", + " mv \"${OUTPUT_PATH}\" \"${DEPLOY_ANDROID}/\"", + "fi" + ] } ] }, @@ -159,7 +214,46 @@ { "type": "xcframework", "module_name": "Libbox", - "output_path": "Libbox.xcframework" + "execute_after": [ + "if [ -d \"${DEPLOY_APPLE}\" ]; then", + " rm -rf \"${DEPLOY_APPLE}/${MODULE_NAME}.xcframework\"", + " mv \"${OUTPUT_PATH}\" \"${DEPLOY_APPLE}/\"", + "fi" + ] + } + ] + }, + { + "type": "csharp", + "build": "windows", + "targets": [ + "windows/amd64" + ], + "languages": [{ "type": "csharp" }], + "artifacts": [ + { + "type": "nuget", + "package_id": "SagerNet.Libbox", + "package_version": "0.0.0-local", + "execute_after": { + "windows": [ + "$$deployPath = '${DEPLOY_WINDOWS}'", + "if (Test-Path $$deployPath) {", + " Remove-Item \"$$deployPath\\${PACKAGE_ID}.*.nupkg\" -ErrorAction SilentlyContinue", + " Move-Item -Force '${OUTPUT_PATH}' \"$$deployPath\\\"", + " $$cachePath = if ($$env:NUGET_PACKAGES) { $$env:NUGET_PACKAGES } else { \"$$env:USERPROFILE\\.nuget\\packages\" }", + " Remove-Item -Recurse -Force \"$$cachePath\\sagernet.libbox\\${PACKAGE_VERSION}\" -ErrorAction SilentlyContinue", + "}" + ], + "default": [ + "if [ -d \"${DEPLOY_WINDOWS}\" ]; then", + " rm -f \"${DEPLOY_WINDOWS}/${PACKAGE_ID}.*.nupkg\"", + " mv \"${OUTPUT_PATH}\" \"${DEPLOY_WINDOWS}/\"", + " cache_path=\"$${NUGET_PACKAGES:-$${HOME}/.nuget/packages}\"", + " rm -rf \"$${cache_path}/sagernet.libbox/${PACKAGE_VERSION}\"", + "fi" + ] + } } ] }