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

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