better tunnel management and cache dns of url test

This commit is contained in:
Hiddify
2024-08-02 08:29:13 +02:00
parent b7e82e7f5c
commit e9a3396024
4 changed files with 46 additions and 31 deletions

View File

@@ -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",
},
})

View File

@@ -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
}

View File

@@ -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,

View File

@@ -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"
}
]