forked from MatsuriDayo/Matsuri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv2ray_dns.go
149 lines (130 loc) · 3.78 KB
/
v2ray_dns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package libcore
import (
"context"
"fmt"
"libcore/doh"
"libcore/protect"
"net"
"strconv"
"strings"
"sync/atomic"
"time"
"unsafe"
dns_feature "github.com/v2fly/v2ray-core/v5/features/dns"
"github.com/v2fly/v2ray-core/v5/features/dns/localdns"
"github.com/v2fly/v2ray-core/v5/nekoutils"
"github.com/v2fly/v2ray-core/v5/transport/internet"
)
// DNS & Protect
var staticHosts = make(map[string][]net.IP)
var tryDomains = make([]string, 0) // server's domain, set when enhanced domain mode
var systemResolver = &net.Resolver{PreferGo: false} // Using System API, lookup from current network.
var underlyingResolver = &simpleSekaiWrapper{systemResolver: systemResolver} // Using System API, lookup from non-VPN network.
var v2rayDNSClient unsafe.Pointer
var underlyingDialer = &protect.ProtectedDialer{
Resolver: func(domain string) ([]net.IP, error) {
return underlyingResolver.LookupIP("ip", domain)
},
}
// sekaiResolver
type LocalResolver interface {
LookupIP(network string, domain string) (string, error)
}
type simpleSekaiWrapper struct {
systemResolver *net.Resolver
sekaiResolver LocalResolver // Android: passed from java (only when VPNService)
}
func (p *simpleSekaiWrapper) LookupIP(network, host string) (ret []net.IP, err error) {
// NOTE only Android
isSekai := p.sekaiResolver != nil
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
ok := make(chan interface{})
defer cancel()
go func() {
defer func() {
select {
case <-ctx.Done():
default:
ok <- nil
}
close(ok)
}()
if isSekai {
var str string
str, err = p.sekaiResolver.LookupIP(network, host)
// java -> go
if err != nil {
rcode, err2 := strconv.Atoi(err.Error())
if err2 == nil {
err = dns_feature.RCodeError(rcode)
}
return
} else if str == "" {
err = dns_feature.ErrEmptyResponse
return
}
ret = make([]net.IP, 0)
for _, ip := range strings.Split(str, ",") {
ret = append(ret, net.ParseIP(ip))
}
} else {
ret, err = p.systemResolver.LookupIP(context.Background(), network, host)
}
}()
select {
case <-ctx.Done():
return nil, newError(fmt.Sprintf("underlyingResolver: context cancelled! (sekai=%t)", isSekai))
case <-ok:
return
}
}
func setupResolvers() {
// golang lookup -> System
net.DefaultResolver = systemResolver
// dnsClient lookup -> Underlying
internet.UseAlternativeSystemDNSDialer(underlyingDialer)
// "localhost" localDns lookup -> Underlying
localdns.SetLookupFunc(underlyingResolver.LookupIP)
// doh package
doh.SetDialContext(underlyingDialer.DialContext)
// All lookup except dnsClient -> dc.LookupIP()
// and also set protectedDialer for outbound connections
internet.UseAlternativeSystemDialer(&protect.ProtectedDialer{
Resolver: func(domain string) ([]net.IP, error) {
if ips, ok := staticHosts[domain]; ok && ips != nil {
return ips, nil
}
if nekoutils.In(tryDomains, domain) {
// first try A
_ips, err := doh.LookupManyDoH(domain, 1)
if err != nil {
// then try AAAA
_ips, err = doh.LookupManyDoH(domain, 28)
if err != nil {
return nil, err
}
}
ips := _ips.([]net.IP)
staticHosts[domain] = ips
return ips, nil
}
// Have running instance?
ptr := (*dns_feature.Client)(atomic.LoadPointer(&v2rayDNSClient))
if ptr != nil && *ptr != nil {
return (*ptr).LookupIP(&dns_feature.MatsuriDomainStringEx{
Domain: domain,
OptNetwork: "ip",
})
} else {
return systemResolver.LookupIP(context.Background(), "ip", domain)
}
},
})
// UDP ListenPacket
internet.RegisterListenerController(func(network, address string, fd uintptr) error {
if protect.FdProtector != nil {
protect.FdProtector.Protect(int32(fd))
}
return nil
})
}