From 70157d1ac6f25ff58e9d6cb17d9cc87e122e8780 Mon Sep 17 00:00:00 2001 From: Hiddify Date: Wed, 21 Feb 2024 19:51:49 +0100 Subject: [PATCH] fix: warp configs, add warp delay, fix network switch --- cmd/cmd_config.go | 9 +++++ config/config.go | 92 +++++++++++++++++++++++++++++++++++------------ config/option.go | 19 +++++----- custom/custom.go | 7 ++++ global/global.go | 7 ++++ go.mod | 2 +- go.sum | 4 +-- mobile/mobile.go | 6 ++++ 8 files changed, 111 insertions(+), 35 deletions(-) diff --git a/cmd/cmd_config.go b/cmd/cmd_config.go index 221934c..7332c9d 100644 --- a/cmd/cmd_config.go +++ b/cmd/cmd_config.go @@ -116,8 +116,17 @@ func readConfigOptionsAt(path string) (*config.ConfigOptions, error) { } var options config.ConfigOptions err = json.Unmarshal(content, &options) + if err != nil { return nil, err } + if options.Warp.WireguardConfigStr != "" { + err := json.Unmarshal([]byte(options.Warp.WireguardConfigStr), &options.Warp.WireguardConfig) + if err != nil { + p + return nil, err + } + } + return &options, nil } diff --git a/config/config.go b/config/config.go index ad06e17..01da195 100644 --- a/config/config.go +++ b/config/config.go @@ -36,7 +36,10 @@ const ( InboundDNSTag = "dns-in" ) +var OutboundMainProxyTag = OutboundSelectTag + func BuildConfigJson(configOpt ConfigOptions, input option.Options) (string, error) { + options, err := BuildConfig(configOpt, input) if err != nil { return "", err @@ -246,7 +249,7 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro Type: C.RuleTypeDefault, DefaultOptions: option.DefaultRule{ ClashMode: "Global", - Outbound: OutboundSelectTag, + Outbound: OutboundMainProxyTag, }, }, } @@ -388,16 +391,29 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro var outbounds []option.Outbound var tags []string - if opt.Warp.EnableWarp && (opt.Warp.Mode == "proxy_over_warp" || opt.Warp.Mode == "warp_over_proxy") { + OutboundMainProxyTag = OutboundSelectTag + //inbound==warp over proxies + //outbound==proxies over warp + if opt.Warp.EnableWarp && (opt.Warp.Mode == "inbound" || opt.Warp.Mode == "outbound") { + fmt.Println("===========================") + fmt.Printf("%+v\n\n", opt.Warp) + fmt.Printf("%+v\n\n", opt.Warp.WireguardConfig) + fmt.Printf("%+v\n\n", opt.Warp.Account) + out, err := generateWarpSingbox(opt.Warp.WireguardConfig.ToWireguardConfig(), opt.Warp.CleanIP, opt.Warp.CleanPort, opt.Warp.FakePackets, opt.Warp.FakePacketSize, opt.Warp.FakePacketDelay) if err != nil { return nil, fmt.Errorf("failed to generate warp config: %v", err) } - out.Tag = "Hiddify Warp Config" - if opt.Warp.Mode == "warp_over_proxy" { - out.WireGuardOptions.Detour = "select" + out.Tag = "Hiddify Warp ✅" + if opt.Warp.Mode == "inbound" { + out.WireGuardOptions.Detour = OutboundURLTestTag + OutboundMainProxyTag = out.Tag + } else { + out.WireGuardOptions.Detour = OutboundDirectTag } + patchWarp(out) outbounds = append(outbounds, *out) + // tags = append(tags, out.Tag) } for _, out := range input.Outbounds { outbound, serverDomain, err := patchOutbound(out, opt) @@ -439,7 +455,7 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro selector := option.Outbound{ Type: C.TypeSelector, - Tag: "select", + Tag: OutboundSelectTag, SelectorOptions: option.SelectorOutboundOptions{ Outbounds: append([]string{urlTest.Tag}, tags...), Default: urlTest.Tag, @@ -510,27 +526,57 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro dnsRule.Server = DNSDirectTag options.DNS.Rules = append([]option.DNSRule{{Type: C.RuleTypeDefault, DefaultOptions: dnsRule}}, options.DNS.Rules...) } - + options.Route.Final = OutboundMainProxyTag return &options, nil } func patchHiddifyWarpFromConfig(out option.Outbound, opt ConfigOptions) option.Outbound { - if opt.Warp.EnableWarp && opt.Warp.Mode == "proxy_over_warp" { - out.DirectOptions.Detour = "Hiddify Warp Config" - out.HTTPOptions.Detour = "Hiddify Warp Config" - out.Hysteria2Options.Detour = "Hiddify Warp Config" - out.HysteriaOptions.Detour = "Hiddify Warp Config" - out.SSHOptions.Detour = "Hiddify Warp Config" - out.ShadowTLSOptions.Detour = "Hiddify Warp Config" - out.ShadowsocksOptions.Detour = "Hiddify Warp Config" - out.ShadowsocksROptions.Detour = "Hiddify Warp Config" - out.SocksOptions.Detour = "Hiddify Warp Config" - out.TUICOptions.Detour = "Hiddify Warp Config" - out.TorOptions.Detour = "Hiddify Warp Config" - out.TrojanOptions.Detour = "Hiddify Warp Config" - out.VLESSOptions.Detour = "Hiddify Warp Config" - out.VMessOptions.Detour = "Hiddify Warp Config" - out.WireGuardOptions.Detour = "Hiddify Warp Config" + if opt.Warp.EnableWarp && opt.Warp.Mode == "outbound" { + if out.DirectOptions.Detour == "" { + out.DirectOptions.Detour = "Hiddify Warp ✅" + } + if out.HTTPOptions.Detour == "" { + out.HTTPOptions.Detour = "Hiddify Warp ✅" + } + if out.Hysteria2Options.Detour == "" { + out.Hysteria2Options.Detour = "Hiddify Warp ✅" + } + if out.HysteriaOptions.Detour == "" { + out.HysteriaOptions.Detour = "Hiddify Warp ✅" + } + if out.SSHOptions.Detour == "" { + out.SSHOptions.Detour = "Hiddify Warp ✅" + } + if out.ShadowTLSOptions.Detour == "" { + out.ShadowTLSOptions.Detour = "Hiddify Warp ✅" + } + if out.ShadowsocksOptions.Detour == "" { + out.ShadowsocksOptions.Detour = "Hiddify Warp ✅" + } + if out.ShadowsocksROptions.Detour == "" { + out.ShadowsocksROptions.Detour = "Hiddify Warp ✅" + } + if out.SocksOptions.Detour == "" { + out.SocksOptions.Detour = "Hiddify Warp ✅" + } + if out.TUICOptions.Detour == "" { + out.TUICOptions.Detour = "Hiddify Warp ✅" + } + if out.TorOptions.Detour == "" { + out.TorOptions.Detour = "Hiddify Warp ✅" + } + if out.TrojanOptions.Detour == "" { + out.TrojanOptions.Detour = "Hiddify Warp ✅" + } + if out.VLESSOptions.Detour == "" { + out.VLESSOptions.Detour = "Hiddify Warp ✅" + } + if out.VMessOptions.Detour == "" { + out.VMessOptions.Detour = "Hiddify Warp ✅" + } + if out.WireGuardOptions.Detour == "" { + out.WireGuardOptions.Detour = "Hiddify Warp ✅" + } } return out } diff --git a/config/option.go b/config/option.go index a6332c6..5a8f4d5 100644 --- a/config/option.go +++ b/config/option.go @@ -72,15 +72,16 @@ type MuxOptions struct { } type WarpOptions struct { - EnableWarp bool `json:"enable"` - Mode string `json:"mode"` - WireguardConfig WarpWireguardConfig `json:"wireguard-config"` - FakePackets string `json:"fake-packets"` - FakePacketSize string `json:"fake-packet-size"` - FakePacketDelay string `json:"fake-packet-delay"` - CleanIP string `json:"clean-ip"` - CleanPort uint16 `json:"clean-port"` - Account WarpAccount + EnableWarp bool `json:"enable"` + Mode string `json:"mode"` + WireguardConfigStr string `json:"wireguardConfig"` + WireguardConfig WarpWireguardConfig `json:"wireguard-config"` + FakePackets string `json:"warpNoise"` + FakePacketSize string `json:"fake-packet-size"` + FakePacketDelay string `json:"warpNoiseDelay"` + CleanIP string `json:"cleanIp"` + CleanPort uint16 `json:"cleanPort"` + Account WarpAccount } func DefaultConfigOptions() *ConfigOptions { diff --git a/custom/custom.go b/custom/custom.go index d0a6f76..903d752 100644 --- a/custom/custom.go +++ b/custom/custom.go @@ -85,6 +85,13 @@ func changeConfigOptions(configOptionsJson *C.char) (CErr *C.char) { if err != nil { return C.CString(err.Error()) } + if configOptions.Warp.WireguardConfigStr != "" { + err := json.Unmarshal([]byte(configOptions.Warp.WireguardConfigStr), &configOptions.Warp.WireguardConfig) + if err != nil { + return C.CString(err.Error()) + } + } + return C.CString("") } diff --git a/global/global.go b/global/global.go index 49e2f48..6e767a5 100644 --- a/global/global.go +++ b/global/global.go @@ -62,6 +62,13 @@ func changeConfigOptions(configOptionsJson string) error { if err != nil { return err } + if configOptions.Warp.WireguardConfigStr != "" { + err := json.Unmarshal([]byte(configOptions.Warp.WireguardConfigStr), &configOptions.Warp.WireguardConfig) + if err != nil { + return err + } + } + return nil } diff --git a/go.mod b/go.mod index 6989648..b0c759c 100644 --- a/go.mod +++ b/go.mod @@ -105,7 +105,7 @@ require ( lukechampine.com/blake3 v1.2.1 // indirect ) -replace github.com/sagernet/sing-box => github.com/hiddify/hiddify-sing-box v1.8.6-0.20240221131435-1f6e9d56ebd8 +replace github.com/sagernet/sing-box => github.com/hiddify/hiddify-sing-box v1.8.6-0.20240221142438-1b8e9df9d3f6 replace github.com/sagernet/wireguard-go => github.com/hiddify/wireguard-go v0.0.0-20240220184421-1831c84ddbf9 diff --git a/go.sum b/go.sum index 7e524ee..862ac67 100644 --- a/go.sum +++ b/go.sum @@ -53,8 +53,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-com/wireguard-go v0.0.2-alpha.0.20240220074702-29dc5459810f h1:h9Vsu3eALcx5il3kYVTTZ/la6znEXLrBMJtkRWuUZ+M= github.com/hiddify-com/wireguard-go v0.0.2-alpha.0.20240220074702-29dc5459810f/go.mod h1:E1qZQpw2IrbRtFpTloTHGfJ2bJc4ZpZRNS497Eq5jCo= -github.com/hiddify/hiddify-sing-box v1.8.6-0.20240221131435-1f6e9d56ebd8 h1:6E2QU3kukvQBDg8TXKb1Qkw4xGH1MeejFa4UfOokwho= -github.com/hiddify/hiddify-sing-box v1.8.6-0.20240221131435-1f6e9d56ebd8/go.mod h1:/Pfh8KgxbWEg5s5rmXjRc6NWW0W4zcIDLDSpnC43snc= +github.com/hiddify/hiddify-sing-box v1.8.6-0.20240221142438-1b8e9df9d3f6 h1:WYDFYkj04TaW/vgs90eu8IXGgfFOPrjXZ3HpQwTBjxI= +github.com/hiddify/hiddify-sing-box v1.8.6-0.20240221142438-1b8e9df9d3f6/go.mod h1:/Pfh8KgxbWEg5s5rmXjRc6NWW0W4zcIDLDSpnC43snc= 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-20240220184421-1831c84ddbf9 h1:pdb/3VPsfJPBEXaY046ISWYS4yevzzZ4rSjyOsCKy8I= diff --git a/mobile/mobile.go b/mobile/mobile.go index 29b34bf..571647c 100644 --- a/mobile/mobile.go +++ b/mobile/mobile.go @@ -34,6 +34,12 @@ func BuildConfig(path string, configOptionsJson string) (string, error) { if err != nil { return "", nil } + if configOptions.Warp.WireguardConfigStr != "" { + err := json.Unmarshal([]byte(configOptions.Warp.WireguardConfigStr), &configOptions.Warp.WireguardConfig) + if err != nil { + return "", err + } + } return config.BuildConfigJson(*configOptions, options) }