From e9a3396024cd34c559fa8dd0c15094a3c1fae31b Mon Sep 17 00:00:00 2001 From: Hiddify <114227601+hiddify-com@users.noreply.github.com> Date: Fri, 2 Aug 2024 08:29:13 +0200 Subject: [PATCH] better tunnel management and cache dns of url test --- cmd/cmd_tunnel_service.go | 4 ++-- config/admin_service_commander.go | 8 +++++-- config/config.go | 39 ++++++++++++++++++++----------- v2/tunnel_service.go | 26 ++++++++++----------- 4 files changed, 46 insertions(+), 31 deletions(-) diff --git a/cmd/cmd_tunnel_service.go b/cmd/cmd_tunnel_service.go index c9a4413..90d54b6 100644 --- a/cmd/cmd_tunnel_service.go +++ b/cmd/cmd_tunnel_service.go @@ -11,7 +11,7 @@ import ( ) var commandService = &cobra.Command{ - Use: "tunnel run/start/stop/install/uninstall", + Use: "tunnel run/start/stop/install/uninstall/activate/deactivate/exit", Short: "Tunnel Service run/start/stop/install/uninstall/activate/deactivate/exit", ValidArgs: []string{"run", "start", "stop", "install", "uninstall", "activate", "deactivate", "exit"}, Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), @@ -22,7 +22,7 @@ var commandService = &cobra.Command{ config.ActivateTunnelService(config.ConfigOptions{ InboundOptions: config.InboundOptions{ EnableTunService: true, - MixedPort: 2334, + MixedPort: 12334, TUNStack: "gvisor", }, }) diff --git a/config/admin_service_commander.go b/config/admin_service_commander.go index b8732f7..cdeb9d2 100644 --- a/config/admin_service_commander.go +++ b/config/admin_service_commander.go @@ -51,6 +51,8 @@ func DeactivateTunnelService() (bool, error) { tunnelServiceRunning = false } return res, err + } else { + go stopTunnelRequest() } return true, nil @@ -89,11 +91,12 @@ func startTunnelRequest(opt ConfigOptions, installService bool) (bool, error) { c := pb.NewTunnelServiceClient(conn) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() + _, _ = c.Stop(ctx, &pb.Empty{}) res, err := c.Start(ctx, &pb.TunnelStartRequest{ Ipv6: opt.IPv6Mode == option.DomainStrategy(dns.DomainStrategyUseIPv4), ServerPort: int32(opt.InboundOptions.MixedPort), StrictRoute: opt.InboundOptions.StrictRoute, - EndpointIndependentNat: true, + EndpointIndependentNat: false, Stack: opt.InboundOptions.TUNStack, }) if err != nil { @@ -117,12 +120,13 @@ func stopTunnelRequest() (bool, error) { } defer conn.Close() c := pb.NewTunnelServiceClient(conn) - ctx, cancel := context.WithTimeout(context.Background(), time.Second*1) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) defer cancel() res, err := c.Stop(ctx, &pb.Empty{}) if err != nil { log.Printf("did not Stopped: %v %v", res, err) + _, _ = c.Stop(ctx, &pb.Empty{}) return false, err } diff --git a/config/config.go b/config/config.go index 1e2a9c3..4a284e5 100644 --- a/config/config.go +++ b/config/config.go @@ -266,7 +266,18 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro }, }, } - + if opt.EnableTun { + routeRules = append( + routeRules, + option.Rule{ + Type: C.RuleTypeDefault, + DefaultOptions: option.DefaultRule{ + ProcessName: []string{"Hiddify", "Hiddify.exe", "HiddifyCli", "HiddifyCli.exe"}, + Outbound: OutboundBypassTag, + }, + }, + ) + } if opt.BypassLAN { routeRules = append( routeRules, @@ -367,18 +378,21 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro options.DNS.Rules = []option.DNSRule{} } var dnsCPttl uint32 = 3000 - options.DNS.Rules = append( - options.DNS.Rules, - option.DNSRule{ - Type: C.RuleTypeDefault, - DefaultOptions: option.DefaultDNSRule{ - Domain: []string{"cp.cloudflare.com"}, - Server: DNSRemoteTag, - RewriteTTL: &dnsCPttl, - DisableCache: false, + parsedURL, err := url.Parse(opt.ConnectionTestUrl) + if err == nil { + options.DNS.Rules = append( + options.DNS.Rules, + option.DNSRule{ + Type: C.RuleTypeDefault, + DefaultOptions: option.DefaultDNSRule{ + Domain: []string{parsedURL.Host}, + Server: DNSRemoteTag, + RewriteTTL: &dnsCPttl, + DisableCache: false, + }, }, - }, - ) + ) + } options.Route = &option.RouteOptions{ Rules: routeRules, @@ -392,7 +406,6 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro // Path: opt.GeoSitePath, // }, } - fmt.Println("Region==========================", opt.Region) if opt.Region != "other" { options.DNS.Rules = append( options.DNS.Rules, diff --git a/v2/tunnel_service.go b/v2/tunnel_service.go index 79e7328..e0d834b 100644 --- a/v2/tunnel_service.go +++ b/v2/tunnel_service.go @@ -39,6 +39,9 @@ func makeTunnelConfig(Ipv6 bool, ServerPort int32, StrictRoute bool, EndpointInd ipv6 = "" } base := `{ + "log":{ + "level": "warn" + }, "inbounds": [ { "type": "tun", @@ -46,7 +49,7 @@ func makeTunnelConfig(Ipv6 bool, ServerPort int32, StrictRoute bool, EndpointInd "interface_name": "HiddifyTunnel", "inet4_address": "172.19.0.1/30", ` + ipv6 + ` - "mtu": 9000, + "mtu": 1492, "auto_route": true, "strict_route": ` + fmt.Sprintf("%t", StrictRoute) + `, "endpoint_independent_nat": ` + fmt.Sprintf("%t", EndpointIndependentNat) + `, @@ -67,21 +70,16 @@ func makeTunnelConfig(Ipv6 bool, ServerPort int32, StrictRoute bool, EndpointInd } ], "route": { + "auto_detect_interface": true, + "rules": [ { - "process_name":"Hiddify.exe", - "outbound": "direct-out" - }, - { - "process_name":"Hiddify", - "outbound": "direct-out" - }, - { - "process_name":"HiddifyCli", - "outbound": "direct-out" - }, - { - "process_name":"HiddifyCli.exe", + "process_name":[ + "Hiddify.exe", + "Hiddify", + "HiddifyCli", + "HiddifyCli.exe" + ], "outbound": "direct-out" } ]