Skip to content

Commit dd506a4

Browse files
committed
better dns checker
1 parent b4bf110 commit dd506a4

File tree

4 files changed

+125
-96
lines changed

4 files changed

+125
-96
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ require (
257257
gopkg.in/yaml.v3 v3.0.1
258258
)
259259

260-
replace github.com/sagernet/sing-box => github.com/hiddify/hiddify-sing-box v1.8.9-0.20260206122724-dbbfefeeff07
260+
replace github.com/sagernet/sing-box => github.com/hiddify/hiddify-sing-box v1.8.9-0.20260206153433-7beb59f0b1dd
261261

262262
// replace github.com/sagernet/sing-box => ../../h-sing-box
263263

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ github.com/hdevalence/ed25519consensus v0.2.0 h1:37ICyZqdyj0lAZ8P4D1d1id3HqbbG1N
358358
github.com/hdevalence/ed25519consensus v0.2.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo=
359359
github.com/hectane/go-acl v0.0.0-20230122075934-ca0b05cb1adb h1:PGufWXXDq9yaev6xX1YQauaO1MV90e6Mpoq1I7Lz/VM=
360360
github.com/hectane/go-acl v0.0.0-20230122075934-ca0b05cb1adb/go.mod h1:QiyDdbZLaJ/mZP4Zwc9g2QsfaEA4o7XvvgZegSci5/E=
361-
github.com/hiddify/hiddify-sing-box v1.8.9-0.20260206122724-dbbfefeeff07 h1:eE4RYzTmdGHy0U0UILu0HDckrY/QOmc8fEtvc4Vcm08=
362-
github.com/hiddify/hiddify-sing-box v1.8.9-0.20260206122724-dbbfefeeff07/go.mod h1:TnQH2qSPS9PUS5DaLgiw/5611WWGr+t2H/MjHk+3CJE=
361+
github.com/hiddify/hiddify-sing-box v1.8.9-0.20260206153433-7beb59f0b1dd h1:VuHy+F3EvtbUU91cwHUA+oIhQOktZbGKOLzS4y2TUIE=
362+
github.com/hiddify/hiddify-sing-box v1.8.9-0.20260206153433-7beb59f0b1dd/go.mod h1:TnQH2qSPS9PUS5DaLgiw/5611WWGr+t2H/MjHk+3CJE=
363363
github.com/hiddify/psiphon-quic-go v0.0.0-20260205155832-2164d1653824 h1:a5HGC6a/s4SmRvfqsYWBGd6ump3iploFoddqKpIyO7M=
364364
github.com/hiddify/psiphon-quic-go v0.0.0-20260205155832-2164d1653824/go.mod h1:rONdWgPMbFjyyBai7gB1IBF4pT9r4l0GyiDst5XR1SY=
365365
github.com/hiddify/psiphon-tls v0.0.0-20260205181946-4af85c2fb9f2 h1:42YhXZ/qMbWOlH+AE6KZKpv96G60FXMaeQ+TYFUFwNU=

v2/config/builder.go

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const (
2929
// DNSBlockTag = "dns-block"
3030
DNSFakeTag = "dns-fake"
3131
DNSTricksDirectTag = "dns-trick-direct"
32+
DNSMultiDirectTag = "dns-multi-direct"
33+
DNSMultiRemoteTag = "dns-multi-remote"
3234

3335
OutboundDirectTag = "direct §hide§"
3436
OutboundBypassTag = "bypass §hide§"
@@ -105,8 +107,8 @@ func getHostnameIfNotIP(inp string) (string, error) {
105107
}
106108
if net.ParseIP(strings.Trim(inp, "[]")) == nil {
107109
u, err := url.Parse(inp)
108-
if err != nil {
109-
return inp, err
110+
if err != nil || u.Host == "" {
111+
return inp, nil
110112
}
111113
return u.Host, nil
112114
}
@@ -520,17 +522,17 @@ func setRoutingOptions(options *option.Options, hopt *HiddifyOptions) error {
520522
// // )
521523
// }
522524

523-
dnsRules = append(dnsRules, option.DefaultDNSRule{
524-
RawDefaultDNSRule: option.RawDefaultDNSRule{},
525-
DNSRuleAction: option.DNSRuleAction{
526-
Action: C.RuleActionTypeRoute,
527-
RouteOptions: option.DNSRouteActionOptions{
528-
Server: DNSStaticTag,
529-
BypassIfFailed: true,
530-
},
531-
},
532-
},
533-
)
525+
// dnsRules = append(dnsRules, option.DefaultDNSRule{
526+
// RawDefaultDNSRule: option.RawDefaultDNSRule{},
527+
// DNSRuleAction: option.DNSRuleAction{
528+
// Action: C.RuleActionTypeRoute,
529+
// RouteOptions: option.DNSRouteActionOptions{
530+
// Server: DNSStaticTag,
531+
// BypassIfFailed: true,
532+
// },
533+
// },
534+
// },
535+
// )
534536
forceDirectRules, err := addForceDirect(options, hopt)
535537
if err != nil {
536538
return err
@@ -675,7 +677,7 @@ func setRoutingOptions(options *option.Options, hopt *HiddifyOptions) error {
675677
DNSRuleAction: option.DNSRuleAction{
676678
Action: C.RuleActionTypeRoute,
677679
RouteOptions: option.DNSRouteActionOptions{
678-
Server: DNSDirectTag,
680+
Server: DNSMultiDirectTag,
679681
RewriteTTL: &dnsCPttl,
680682
DisableCache: false,
681683
BypassIfFailed: true,
@@ -799,7 +801,7 @@ func setRoutingOptions(options *option.Options, hopt *HiddifyOptions) error {
799801
DNSRuleAction: option.DNSRuleAction{
800802
Action: C.RuleActionTypeRoute,
801803
RouteOptions: option.DNSRouteActionOptions{
802-
Server: DNSDirectTag,
804+
Server: DNSMultiDirectTag,
803805
BypassIfFailed: true,
804806
},
805807
},
@@ -829,7 +831,7 @@ func setRoutingOptions(options *option.Options, hopt *HiddifyOptions) error {
829831
DNSRuleAction: option.DNSRuleAction{
830832
Action: C.RuleActionTypeRoute,
831833
RouteOptions: option.DNSRouteActionOptions{
832-
Server: DNSDirectTag,
834+
Server: DNSMultiDirectTag,
833835
BypassIfFailed: true,
834836
},
835837
},
@@ -890,7 +892,7 @@ func setRoutingOptions(options *option.Options, hopt *HiddifyOptions) error {
890892
Final: OutboundMainDetour,
891893
AutoDetectInterface: (!C.IsAndroid && !C.IsIos) && (hopt.EnableTun || hopt.EnableTunService),
892894
DefaultDomainResolver: &option.DomainResolveOptions{
893-
Server: DNSDirectTag,
895+
Server: DNSMultiDirectTag,
894896
Strategy: hopt.DirectDnsDomainStrategy,
895897
},
896898
// OverrideAndroidVPN: hopt.EnableTun && C.IsAndroid,
@@ -928,23 +930,13 @@ func setRoutingOptions(options *option.Options, hopt *HiddifyOptions) error {
928930
DNSRuleAction: option.DNSRuleAction{
929931
Action: C.RuleActionTypeRoute,
930932
RouteOptions: option.DNSRouteActionOptions{
931-
Server: DNSRemoteTag,
932-
BypassIfFailed: true,
933-
},
934-
},
935-
},
936-
)
937-
dnsRules = append(dnsRules, option.DefaultDNSRule{
938-
RawDefaultDNSRule: option.RawDefaultDNSRule{},
939-
DNSRuleAction: option.DNSRuleAction{
940-
Action: C.RuleActionTypeRoute,
941-
RouteOptions: option.DNSRouteActionOptions{
942-
Server: DNSRemoteTagFallback,
933+
Server: DNSMultiRemoteTag,
943934
BypassIfFailed: false,
944935
},
945936
},
946937
},
947938
)
939+
948940
// dnsRules = append(dnsRules, option.DefaultDNSRule{
949941

950942
// RawDefaultDNSRule: option.RawDefaultDNSRule{},

v2/config/dns.go

Lines changed: 101 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ import (
1616
M "github.com/sagernet/sing/common/metadata"
1717
)
1818

19+
var DnsDirectTags = []string{
20+
DNSStaticTag,
21+
DNSDirectTag,
22+
DNSLocalTag,
23+
DNSTricksDirectTag,
24+
}
25+
var DnsRemoteTags = []string{
26+
DNSRemoteTag,
27+
DNSRemoteTagFallback,
28+
}
29+
1930
func setDns(options *option.Options, opt *HiddifyOptions, staticIps *map[string][]string) error {
2031
remote_dns, err := getDNSServerOptions(DNSRemoteTag, opt.RemoteDnsAddress, DNSDirectTag, OutboundMainDetour)
2132
if err != nil {
@@ -58,12 +69,22 @@ func setDns(options *option.Options, opt *HiddifyOptions, staticIps *map[string]
5869
// return err
5970
// }
6071

72+
multi_dns_direct, err := getMultiDnsServerOptions(DNSMultiDirectTag, DnsDirectTags, false)
73+
if err != nil {
74+
return err
75+
}
76+
77+
multi_dns_remote, err := getMultiDnsServerOptions(DNSMultiRemoteTag, DnsRemoteTags, true)
78+
if err != nil {
79+
return err
80+
}
81+
6182
dnsOptions := option.DNSOptions{
6283
RawDNSOptions: option.RawDNSOptions{
6384
DNSClientOptions: option.DNSClientOptions{
6485
IndependentCache: opt.IndependentDNSCache && !C.IsIos,
6586
},
66-
Final: DNSRemoteTag,
87+
Final: DNSMultiRemoteTag,
6788

6889
Servers: []option.DNSServerOptions{
6990
*static_dns,
@@ -73,6 +94,8 @@ func setDns(options *option.Options, opt *HiddifyOptions, staticIps *map[string]
7394
*direct_dns,
7495
*local_dns,
7596
*remote_no_warp_dns,
97+
*multi_dns_direct,
98+
*multi_dns_remote,
7699
// *block_dns,
77100
},
78101
Rules: []option.DNSRule{},
@@ -116,75 +139,74 @@ func getAllOutboundsOptions(options *option.Options) []any {
116139
}
117140
func addForceDirect(options *option.Options, hopt *HiddifyOptions) ([]option.DefaultDNSRule, error) {
118141
dnsMap := make(map[string]string)
119-
outbounds := getAllOutboundsOptions(options)
142+
// outbounds := getAllOutboundsOptions(options)
120143

121-
for _, outbound := range outbounds {
122-
123-
if server, ok := outbound.(option.ServerOptionsWrapper); ok {
124-
serverDomain := server.TakeServerOptions().Server
125-
detour := OutboundDirectTag
126-
if dialer, ok := outbound.(option.DialerOptionsWrapper); ok {
127-
if server_detour := dialer.TakeDialerOptions().Detour; server_detour != "" {
128-
detour = server_detour
129-
}
130-
}
144+
// for _, outbound := range outbounds {
145+
// // fmt.Println("out", outbound)
146+
// if server, ok := outbound.(option.ServerOptionsWrapper); ok {
147+
// serverDomain := server.TakeServerOptions().Server
148+
// detour := OutboundDirectTag
149+
// if dialer, ok := outbound.(option.DialerOptionsWrapper); ok {
150+
// if server_detour := dialer.TakeDialerOptions().Detour; server_detour != "" {
151+
// detour = server_detour
152+
// }
153+
// }
154+
// fmt.Println("serverDomain", serverDomain, "detour", detour)
131155

132-
if host, err := getHostnameIfNotIP(serverDomain); err == nil {
133-
if _, ok := dnsMap[host]; !ok || detour == OutboundDirectTag {
134-
dnsMap[host] = detour
135-
}
136-
}
137-
}
138-
}
156+
// if host, err := getHostnameIfNotIP(serverDomain); err == nil && host != "" {
157+
// fmt.Println("serverDomain", serverDomain, "host", host, "detour", detour)
158+
// if _, ok := dnsMap[host]; !ok || detour == OutboundDirectTag {
159+
// dnsMap[host] = detour
160+
// }
161+
// }
162+
// }
163+
// }
139164

140-
// dnsMap[]
165+
// // dnsMap[]
141166
forceDirectRules := []option.DefaultDNSRule{}
142-
if len(dnsMap) > 0 {
143-
unique_dns_detours := make(map[string]bool)
144-
for _, detour := range dnsMap {
145-
unique_dns_detours[detour] = true
146-
}
167+
// if len(dnsMap) > 0 {
168+
// unique_dns_detours := make(map[string]bool)
169+
// for _, detour := range dnsMap {
170+
// unique_dns_detours[detour] = true
171+
// }
147172

148-
for detour := range unique_dns_detours {
149-
domains := []string{}
150-
for domain, d := range dnsMap {
151-
if d == detour {
152-
domains = append(domains, domain)
153-
}
154-
}
155-
if len(domains) == 0 {
156-
continue
157-
}
158-
dns_detour := DNSDirectTag
159-
if detour != OutboundDirectTag {
160-
dns_detour = "dns-" + detour
161-
remote_dns, err := getDNSServerOptions(dns_detour, hopt.RemoteDnsAddress, DNSDirectTag, detour)
162-
if err != nil {
163-
return nil, err
164-
}
165-
options.DNS.Servers = append(options.DNS.Servers, *remote_dns)
166-
if err != nil {
167-
return nil, err
168-
}
173+
// for detour := range unique_dns_detours {
174+
// domains := []string{}
175+
// for domain, d := range dnsMap {
176+
// if d == detour {
177+
// domains = append(domains, domain)
178+
// }
179+
// }
180+
// if len(domains) == 0 {
181+
// continue
182+
// }
183+
// dns_detour := DNSMultiDirectTag
184+
// if detour != OutboundDirectTag {
185+
// dns_detour = "dns-" + detour
186+
// remote_dns, err := getDNSServerOptions(dns_detour, hopt.RemoteDnsAddress, DNSDirectTag, detour)
187+
// if err != nil {
188+
// return nil, err
189+
// }
190+
// options.DNS.Servers = append(options.DNS.Servers, *remote_dns)
169191

170-
}
192+
// }
171193

172-
forceDirectRules = append(forceDirectRules,
173-
option.DefaultDNSRule{
174-
RawDefaultDNSRule: option.RawDefaultDNSRule{
175-
Domain: domains,
176-
},
177-
DNSRuleAction: option.DNSRuleAction{
178-
Action: C.RuleActionTypeRoute,
179-
RouteOptions: option.DNSRouteActionOptions{
180-
Server: dns_detour,
181-
BypassIfFailed: true,
182-
},
183-
},
184-
},
185-
)
186-
}
187-
}
194+
// forceDirectRules = append(forceDirectRules,
195+
// option.DefaultDNSRule{
196+
// RawDefaultDNSRule: option.RawDefaultDNSRule{
197+
// Domain: domains,
198+
// },
199+
// DNSRuleAction: option.DNSRuleAction{
200+
// Action: C.RuleActionTypeRoute,
201+
// RouteOptions: option.DNSRouteActionOptions{
202+
// Server: dns_detour,
203+
// BypassIfFailed: true,
204+
// },
205+
// },
206+
// },
207+
// )
208+
// }
209+
// }
188210
domains := []string{}
189211

190212
forceDirectRules = append(forceDirectRules,
@@ -226,7 +248,7 @@ func addForceDirect(options *option.Options, hopt *HiddifyOptions) ([]option.Def
226248
DNSRuleAction: option.DNSRuleAction{
227249
Action: C.RuleActionTypeRoute,
228250
RouteOptions: option.DNSRouteActionOptions{
229-
Server: DNSDirectTag,
251+
Server: DNSMultiDirectTag,
230252
BypassIfFailed: true,
231253
},
232254
},
@@ -444,3 +466,18 @@ func getStaticDNSServerOptions(tag string, staticIps *map[string][]string) (*opt
444466
}
445467
return &o, nil
446468
}
469+
func getMultiDnsServerOptions(tag string, servers []string, parallel bool) (*option.DNSServerOptions, error) {
470+
o := option.DNSServerOptions{
471+
Tag: tag,
472+
Type: C.DNSTypeMulti,
473+
Options: &option.MultiDNSServerOptions{
474+
Servers: servers,
475+
Parallel: parallel,
476+
IgnoreRanges: []badoption.Prefix{
477+
badoption.Prefix(netip.MustParsePrefix("10.10.34.0/24")),
478+
badoption.Prefix(netip.MustParsePrefix("001:4188:2:600::/64")),
479+
},
480+
},
481+
}
482+
return &o, nil
483+
}

0 commit comments

Comments
 (0)