Skip to content

Commit be15d93

Browse files
committed
优化默认DNS查询
1 parent 2a32292 commit be15d93

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

core/custom.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,36 @@ package core
22

33
import (
44
"encoding/json"
5+
"net"
56

67
panel "github.com/wyx2685/v2node/api/v2board"
78
"github.com/xtls/xray-core/app/dns"
89
"github.com/xtls/xray-core/app/router"
9-
"github.com/xtls/xray-core/common/net"
10+
xnet "github.com/xtls/xray-core/common/net"
1011
"github.com/xtls/xray-core/core"
1112
coreConf "github.com/xtls/xray-core/infra/conf"
1213
)
1314

15+
// hasPublicIPv6 checks if the machine has a public IPv6 address
16+
func hasPublicIPv6() bool {
17+
addrs, err := net.InterfaceAddrs()
18+
if err != nil {
19+
return false
20+
}
21+
for _, addr := range addrs {
22+
ipNet, ok := addr.(*net.IPNet)
23+
if !ok {
24+
continue
25+
}
26+
ip := ipNet.IP
27+
// Check if it's IPv6, not loopback, not link-local, not private/ULA
28+
if ip.To4() == nil && !ip.IsLoopback() && !ip.IsLinkLocalUnicast() && !ip.IsPrivate() {
29+
return true
30+
}
31+
}
32+
return false
33+
}
34+
1435
func hasOutboundWithTag(list []*core.OutboundHandlerConfig, tag string) bool {
1536
for _, o := range list {
1637
if o != nil && o.Tag == tag {
@@ -22,25 +43,37 @@ func hasOutboundWithTag(list []*core.OutboundHandlerConfig, tag string) bool {
2243

2344
func GetCustomConfig(infos []*panel.NodeInfo) (*dns.Config, []*core.OutboundHandlerConfig, *router.Config, error) {
2445
//dns
46+
queryStrategy := "UseIPv4v6"
47+
if !hasPublicIPv6() {
48+
queryStrategy = "UseIPv4"
49+
}
2550
coreDnsConfig := &coreConf.DNSConfig{
2651
Servers: []*coreConf.NameServerConfig{
2752
{
2853
Address: &coreConf.Address{
29-
Address: net.ParseAddress("localhost"),
54+
Address: xnet.ParseAddress("localhost"),
3055
},
3156
},
3257
},
58+
QueryStrategy: queryStrategy,
3359
}
3460
//outbound
3561
defaultoutbound, _ := buildDefaultOutbound()
3662
coreOutboundConfig := append([]*core.OutboundHandlerConfig{}, defaultoutbound)
3763
block, _ := buildBlockOutbound()
3864
coreOutboundConfig = append(coreOutboundConfig, block)
65+
dns, _ := buildDnsOutbound()
66+
coreOutboundConfig = append(coreOutboundConfig, dns)
3967

4068
//route
4169
domainStrategy := "AsIs"
70+
dnsRule, _ := json.Marshal(map[string]interface{}{
71+
"port": "53",
72+
"network": "udp",
73+
"outboundTag": "dns_out",
74+
})
4275
coreRouterConfig := &coreConf.RouterConfig{
43-
RuleList: []json.RawMessage{},
76+
RuleList: []json.RawMessage{dnsRule},
4477
DomainStrategy: &domainStrategy,
4578
}
4679

@@ -56,7 +89,7 @@ func GetCustomConfig(infos []*panel.NodeInfo) (*dns.Config, []*core.OutboundHand
5689
}
5790
server := &coreConf.NameServerConfig{
5891
Address: &coreConf.Address{
59-
Address: net.ParseAddress(*route.ActionValue),
92+
Address: xnet.ParseAddress(*route.ActionValue),
6093
},
6194
Domains: route.Match,
6295
}
@@ -152,7 +185,7 @@ func GetCustomConfig(infos []*panel.NodeInfo) (*dns.Config, []*core.OutboundHand
152185
}
153186
coreOutboundConfig = append(coreOutboundConfig, custom_outbound)
154187
rule := map[string]interface{}{
155-
"network": []string{"tcp", "udp"},
188+
"network": "tcp,udp",
156189
"outboundTag": "default_out",
157190
}
158191
rawRule, err := json.Marshal(rule)

core/outbound.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ func buildBlockOutbound() (*core.OutboundHandlerConfig, error) {
3939
return outboundDetourConfig.Build()
4040
}
4141

42+
// build dns outbound
43+
func buildDnsOutbound() (*core.OutboundHandlerConfig, error) {
44+
outboundDetourConfig := &conf.OutboundDetourConfig{}
45+
outboundDetourConfig.Protocol = "dns"
46+
outboundDetourConfig.Tag = "dns_out"
47+
return outboundDetourConfig.Build()
48+
}
49+
4250
func (v *V2Core) addOutbound(config *core.OutboundHandlerConfig) error {
4351
rawHandler, err := core.CreateObject(v.Server, config)
4452
if err != nil {

0 commit comments

Comments
 (0)