diff --git a/config/outbound.go b/config/outbound.go index cead912..0320bc8 100644 --- a/config/outbound.go +++ b/config/outbound.go @@ -166,6 +166,15 @@ func patchOutbound(base option.Outbound, configOpt ConfigOptions) (*option.Outbo } func patchWarp(base *option.Outbound) error { + if base.Type == C.TypeWireGuard { + host := base.WireGuardOptions.Server + if host == "default" || host == "random" || host == "auto" { + base.WireGuardOptions.Server = getRandomIP() + } + if base.WireGuardOptions.ServerPort == 0 { + base.WireGuardOptions.ServerPort = generateRandomPort() + } + } if base.Type == C.TypeCustom { if warp, ok := base.CustomOptions["warp"].(map[string]interface{}); ok { key, _ := warp["key"].(string) @@ -186,6 +195,7 @@ func patchWarp(base *option.Outbound) error { } } + return nil } diff --git a/config/parser.go b/config/parser.go index 16f9b9e..946fac9 100644 --- a/config/parser.go +++ b/config/parser.go @@ -10,6 +10,7 @@ import ( "github.com/hiddify/ray2sing/ray2sing" "github.com/sagernet/sing-box/experimental/libbox" + "github.com/sagernet/sing-box/option" SJ "github.com/sagernet/sing/common/json" "github.com/xmdhs/clash2singbox/convert" "github.com/xmdhs/clash2singbox/model/clash" @@ -38,12 +39,13 @@ func ParseConfig(path string, debug bool) ([]byte, error) { } newContent, _ := json.MarshalIndent(jsonObj, "", " ") - return validateResult(newContent, "SingboxParser") + + return patchConfig([]byte(newContent), "SingboxParser") } fmt.Printf("Convert using v2ray\n") v2rayStr, err := ray2sing.Ray2Singbox(string(content)) if err == nil { - return validateResult([]byte(v2rayStr), "V2rayParser") + return patchConfig([]byte(v2rayStr), "V2rayParser") } fmt.Printf("Convert using clash\n") clashObj := clash.Clash{} @@ -60,13 +62,35 @@ func ParseConfig(path string, debug bool) ([]byte, error) { if err != nil { return nil, fmt.Errorf("[ClashParser] patching clash config error: %w", err) } - return validateResult(output, "ClashParser") + return patchConfig(output, "ClashParser") } return nil, fmt.Errorf("unable to determine config format") } +func patchConfig(content []byte, name string) ([]byte, error) { + options := option.Options{} + err := json.Unmarshal(content, &options) + if err != nil { + return nil, fmt.Errorf("[SingboxParser] unmarshal error: %w", err) + } + + for i, base := range options.Outbounds { + err := patchWarp(&base) + if err != nil { + return nil, fmt.Errorf("[Warp] patch warp error: %w", err) + } + options.Outbounds[i] = base + + } + + content, _ = json.MarshalIndent(options, "", " ") + fmt.Printf("%s\n", content) + return validateResult(content, name) +} + func validateResult(content []byte, name string) ([]byte, error) { + err := libbox.CheckConfig(string(content)) if err != nil { return nil, fmt.Errorf("[%s] invalid sing-box config: %w", name, err) diff --git a/config/warp.go b/config/warp.go index 80e5713..4f61ac5 100644 --- a/config/warp.go +++ b/config/warp.go @@ -162,11 +162,8 @@ func generateWarp(license string, host string, port uint16, fakePackets string) if host == "auto" && fakePackets == "" { fakePackets = "5-10" } - if host == "default" || host == "random" || host == "auto" { - host = getRandomIP() - } - if port == 0 { - port = generateRandomPort() + if _, err := os.Stat("./wgcf-identity.json"); err == nil { + os.Remove("./wgcf-identity.json") } if !warp.CheckProfileExists(license) { diff --git a/go.mod b/go.mod index f5301ff..59d91d8 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/hiddify/libcore go 1.21.5 require ( - github.com/hiddify/ray2sing v0.0.0-20240127015415-b0b85dcbf102 + github.com/hiddify/ray2sing v0.0.0-20240130180912-f90754dd8f44 github.com/kardianos/service v1.2.2 github.com/sagernet/gomobile v0.1.1 github.com/sagernet/sing v0.3.0 diff --git a/go.sum b/go.sum index 44d3f62..9667617 100644 --- a/go.sum +++ b/go.sum @@ -51,8 +51,8 @@ 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/hiddify-sing-box v1.7.9-0.20240130115748-5838de40349e h1:FhrU990kkhxRoFAMvEZwUyM05s1AWBn7lzwijl9ucq0= github.com/hiddify/hiddify-sing-box v1.7.9-0.20240130115748-5838de40349e/go.mod h1:B74zKdMcH3ZEmCi2OUqJTvEXCNtNQjivUEQ20y/5XQM= -github.com/hiddify/ray2sing v0.0.0-20240127015415-b0b85dcbf102 h1:4vKmPE8AyvsBYuZmjGkPnsju8ZzVxEjC9I96uqxX5+o= -github.com/hiddify/ray2sing v0.0.0-20240127015415-b0b85dcbf102/go.mod h1:zYKnf7EoPqrk7JOMO9BApTXxfH0sva8AKfoFywN7uuA= +github.com/hiddify/ray2sing v0.0.0-20240130180912-f90754dd8f44 h1:A/gbpY8/5jhcoKPCLYpw+LO5MqlBZAsXm6nflWMDbjA= +github.com/hiddify/ray2sing v0.0.0-20240130180912-f90754dd8f44/go.mod h1:zYKnf7EoPqrk7JOMO9BApTXxfH0sva8AKfoFywN7uuA= github.com/hiddify/wireguard-go v0.0.0-20240125143346-481d18d77fe1 h1:neOb+wzHbWLNZ2sHFEV4+GTuqORO7/MndQLFW8FjUY8= github.com/hiddify/wireguard-go v0.0.0-20240125143346-481d18d77fe1/go.mod h1:K4J7/npM+VAMUeUmTa2JaA02JmyheP0GpRBOUvn3ecc= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=