From 313b9a9adb83f8d0cd1ae5ea40a13e6904d3daa2 Mon Sep 17 00:00:00 2001 From: Hiddify Date: Tue, 13 Feb 2024 16:52:58 +0100 Subject: [PATCH] new: configurable warp noise parameters, fix stopping issues --- CONTRIBUTING.md | 2 +- config/outbound.go | 4 +++- config/warp.go | 11 ++++++++++- custom/custom.go | 13 +++++++------ custom/status.go | 12 ++++++++++-- go.mod | 6 +++--- go.sum | 12 ++++++------ 7 files changed, 40 insertions(+), 20 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ac44961..65b5865 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -12,7 +12,7 @@ go version go1.21.1 darwin/arm64 > if you're not interested in building/contributing to the Go code, you can skip this section -The Go code for Hiddify Next can be found in the `libcore` folder, as a [git submodule](https://git-scm.com/book/en/v2/Git-Tools-Submodules) and in [core repository](https://github.com/hiddify/hiddify-next-core). The entrypoints for the desktop version are available in the [`libcore/custom`](https://github.com/hiddify/hiddify-next-core/tree/main/custom) folder and for the mobile version they can be found in the [`libcore/mobile`](https://github.com/hiddify/hiddify-next-core/tree/main/mobile) folder. +The Go code for Hiddify can be found in the `libcore` folder, as a [git submodule](https://git-scm.com/book/en/v2/Git-Tools-Submodules) and in [core repository](https://github.com/hiddify/hiddify-next-core). The entrypoints for the desktop version are available in the [`libcore/custom`](https://github.com/hiddify/hiddify-next-core/tree/main/custom) folder and for the mobile version they can be found in the [`libcore/mobile`](https://github.com/hiddify/hiddify-next-core/tree/main/mobile) folder. For the desktop version, we have to compile the Go code into a C shared library. We are providing a Makefile to generate the C shared libraries for all operating systems. The following Make commands will build libcore and copy the resulting output in [`libcore/bin`](https://github.com/hiddify/hiddify-next-core/tree/main/bin): diff --git a/config/outbound.go b/config/outbound.go index 6ad4d9e..cfbb442 100644 --- a/config/outbound.go +++ b/config/outbound.go @@ -182,7 +182,9 @@ func patchWarp(base *option.Outbound) error { port, _ := warp["port"].(float64) detour, _ := warp["detour"].(string) fakePackets, _ := warp["fake_packets"].(string) - warpConfig, err := generateWarp(key, host, uint16(port), fakePackets) + fakePacketsSize, _ := warp["fake_packets_size"].(string) + fakePacketsDelay, _ := warp["fake_packets_delay"].(string) + warpConfig, err := generateWarp(key, host, uint16(port), fakePackets, fakePacketsSize, fakePacketsDelay) if err != nil { fmt.Printf("Error generating warp config: %v", err) return err diff --git a/config/warp.go b/config/warp.go index 567789a..1af2369 100644 --- a/config/warp.go +++ b/config/warp.go @@ -155,7 +155,7 @@ func generateRandomPort() uint16 { return warpPorts[rand.Intn(len(warpPorts))] } -func generateWarp(license string, host string, port uint16, fakePackets string) (*T.Outbound, error) { +func generateWarp(license string, host string, port uint16, fakePackets string, fakePacketsSize string, fakePacketsDelay string) (*T.Outbound, error) { if host == "" || isBlockedDomain(host) { host = "auto" } @@ -163,6 +163,13 @@ func generateWarp(license string, host string, port uint16, fakePackets string) if host == "auto" && fakePackets == "" { fakePackets = "8-15" } + if fakePackets != "" && fakePacketsSize == "" { + fakePacketsSize = "40-100" + } + if fakePackets != "" && fakePacketsDelay == "" { + fakePacketsDelay = "200-500" + } + // warp.UpdatePath("./secondary") if _, err := os.Stat("./wgcf-identity.json"); err == nil { os.Remove("./wgcf-identity.json") @@ -185,6 +192,8 @@ func generateWarp(license string, host string, port uint16, fakePackets string) singboxConfig, err := wireGuardToSingbox(wgConfig, host, port) singboxConfig.WireGuardOptions.FakePackets = fakePackets + singboxConfig.WireGuardOptions.FakePacketsSize = fakePacketsSize + singboxConfig.WireGuardOptions.FakePacketsDelay = fakePacketsDelay singboxJSON, err := json.MarshalIndent(singboxConfig, "", " ") if err != nil { fmt.Println("Error marshaling Singbox configuration:", err) diff --git a/custom/custom.go b/custom/custom.go index 6163a09..0b06a83 100644 --- a/custom/custom.go +++ b/custom/custom.go @@ -121,8 +121,8 @@ func generateConfigFromFile(path string, configOpt config.ConfigOptions) (string //export start func start(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) { defer config.DeferPanicToError("start", func(err error) { - CErr = C.CString(err.Error()) stopAndAlert("Unexpected Error!", err) + CErr = C.CString(err.Error()) }) if status != Stopped { @@ -187,40 +187,41 @@ func startService(delayStart bool) error { //export stop func stop() (CErr *C.char) { defer config.DeferPanicToError("stop", func(err error) { + stopAndAlert("Unexpected Error in Stop!", err) CErr = C.CString(err.Error()) }) config.DeactivateTunnelService() - if status != Started { + stopAndAlert("Already Stopped", nil) return C.CString("") } if box == nil { return C.CString("instance not found") } propagateStatus(Stopping) - commandServer.SetService(nil) + err := box.Close() if err != nil { + stopAndAlert("Unexpected Error in Close!", err) return C.CString(err.Error()) } box = nil - err = commandServer.Close() if err != nil { + stopAndAlert("Unexpected Error in Stop CommandServer/!", err) return C.CString(err.Error()) } commandServer = nil propagateStatus(Stopped) - return C.CString("") } //export restart func restart(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) { defer config.DeferPanicToError("restart", func(err error) { - CErr = C.CString(err.Error()) stopAndAlert("Unexpected Error!", err) + CErr = C.CString(err.Error()) }) log.Debug("[Service] Restarting") diff --git a/custom/status.go b/custom/status.go index edb915b..9909b63 100644 --- a/custom/status.go +++ b/custom/status.go @@ -35,7 +35,15 @@ func stopAndAlert(alert string, err error) (resultErr error) { fmt.Printf("Error: %s: %v\n", alert, err) propagateStatus(Stopped) config.DeactivateTunnelService() - commandServer.SetService(nil) - err = box.Close() + if commandServer != nil { + commandServer.SetService(nil) + } + if box != nil { + box.Close() + box = nil + } + if commandServer != nil { + commandServer.Close() + } return nil } diff --git a/go.mod b/go.mod index c3d0ec7..8dd2384 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.21.5 require ( github.com/bepass-org/wireguard-go v0.0.16-alpha - github.com/hiddify/ray2sing v0.0.0-20240130210008-8975ff77e3cd + github.com/hiddify/ray2sing v0.0.0-20240213091709-ba1d827e4f4a github.com/kardianos/service v1.2.2 github.com/sagernet/gomobile v0.1.1 github.com/sagernet/sing v0.3.0 @@ -105,8 +105,8 @@ require ( lukechampine.com/blake3 v1.2.1 // indirect ) -replace github.com/sagernet/sing-box => github.com/hiddify/hiddify-sing-box v1.8.6-0.20240213024345-1fb57d3690bd +replace github.com/sagernet/sing-box => github.com/hiddify/hiddify-sing-box v1.8.6-0.20240213125057-2ddd095a1429 -replace github.com/sagernet/wireguard-go => github.com/hiddify/wireguard-go v0.0.0-20240213020321-89576b161250 +replace github.com/sagernet/wireguard-go => github.com/hiddify/wireguard-go v0.0.0-20240213142816-f6785b165244 replace github.com/bepass-org/wireguard-go => github.com/hiddify-com/wireguard-go v0.0.2-alpha.0.20240212065415-62301f758cb7 diff --git a/go.sum b/go.sum index caa23da..4d90cb7 100644 --- a/go.sum +++ b/go.sum @@ -53,12 +53,12 @@ github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= github.com/hiddify-com/wireguard-go v0.0.2-alpha.0.20240212065415-62301f758cb7 h1:RTlGYawrP2Ir2ADQmLS7+OmRShmk1qPm69ufArXvsqI= github.com/hiddify-com/wireguard-go v0.0.2-alpha.0.20240212065415-62301f758cb7/go.mod h1:/ny1FvyrV7/QPClexDyYCEFQfTQn7dD/+1DfedBSwuY= -github.com/hiddify/hiddify-sing-box v1.8.6-0.20240213024345-1fb57d3690bd h1:F87K1GtyOOqX6Lsru+5CkUVwEuaOehN+ls4GC4RcL+k= -github.com/hiddify/hiddify-sing-box v1.8.6-0.20240213024345-1fb57d3690bd/go.mod h1:OQ0aCM3q+9mBaVr3yQx2cWvPR1pKZjaBcI24Y82Papw= -github.com/hiddify/ray2sing v0.0.0-20240130210008-8975ff77e3cd h1:3fKPvAWXTMGY0Qsk6wjCamIeC4xbrOUShMY95G5AZN4= -github.com/hiddify/ray2sing v0.0.0-20240130210008-8975ff77e3cd/go.mod h1:zYKnf7EoPqrk7JOMO9BApTXxfH0sva8AKfoFywN7uuA= -github.com/hiddify/wireguard-go v0.0.0-20240213020321-89576b161250 h1:mgIWgJSmGFfxYwcHPBcHel11zuBc5nQqTD4Zg1GBbH4= -github.com/hiddify/wireguard-go v0.0.0-20240213020321-89576b161250/go.mod h1:K4J7/npM+VAMUeUmTa2JaA02JmyheP0GpRBOUvn3ecc= +github.com/hiddify/hiddify-sing-box v1.8.6-0.20240213125057-2ddd095a1429 h1:qv+xj+Fd41r3WZdqNzdd95rIsv2Pf2Z7LNSWWqRLEqk= +github.com/hiddify/hiddify-sing-box v1.8.6-0.20240213125057-2ddd095a1429/go.mod h1:AnP54TD0EMNONIlc3uTWYT7K3PapAwalglF9TuKRiWE= +github.com/hiddify/ray2sing v0.0.0-20240213091709-ba1d827e4f4a h1:ObxmZ8AyhIxtH2Vu+vmDAsxsbsTPGq9pJzcY0V+3BCU= +github.com/hiddify/ray2sing v0.0.0-20240213091709-ba1d827e4f4a/go.mod h1:zYKnf7EoPqrk7JOMO9BApTXxfH0sva8AKfoFywN7uuA= +github.com/hiddify/wireguard-go v0.0.0-20240213142816-f6785b165244 h1:R/IVg5a0FOGT/85HFj81Fn3oGXPFA5RfMHyHngkLDGk= +github.com/hiddify/wireguard-go v0.0.0-20240213142816-f6785b165244/go.mod h1:K4J7/npM+VAMUeUmTa2JaA02JmyheP0GpRBOUvn3ecc= github.com/imkira/go-observer/v2 v2.0.0-20230629064422-8e0b61f11f1b h1:1+115FqGoS8p6Iry9AYmrcWDvSveH0F7P2nX1LU00qg= github.com/imkira/go-observer/v2 v2.0.0-20230629064422-8e0b61f11f1b/go.mod h1:XCscqBi1KKh7GcVDDAdkT/Cf6WDjnDAA1XM3nwmA0Ag= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=