-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
118 lines (109 loc) · 3.21 KB
/
client.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
package dns
import (
"context"
"fmt"
cResolver "github.com/Dreamacro/clash/component/resolver"
"github.com/igoogolx/itun2socks/internal/cfg/distribution"
"github.com/igoogolx/itun2socks/internal/constants"
"github.com/igoogolx/itun2socks/internal/matcher"
"github.com/igoogolx/itun2socks/pkg/log"
"github.com/igoogolx/itun2socks/pkg/pool"
D "github.com/miekg/dns"
"net"
"strings"
"sync"
"time"
)
var dnsMap = map[constants.DnsType]cResolver.Resolver{}
var mux sync.Mutex
func UpdateDnsMap(local, remote, boost cResolver.Resolver) {
mux.Lock()
defer mux.Unlock()
dnsMap[constants.LocalDns] = local
dnsMap[constants.RemoteDns] = remote
dnsMap[constants.BoostDns] = boost
}
type Conn interface {
ReadFrom([]byte) (int, net.Addr, error)
WriteTo([]byte, net.Addr) (int, error)
}
func HandleDnsConn(conn Conn) error {
var err error
data := pool.NewBytes(pool.BufSize)
defer pool.FreeBytes(data)
_, addr, err := conn.ReadFrom(data)
if err != nil {
return fmt.Errorf("fail to read dns message: err: %v", err)
}
dnsMessage := new(D.Msg)
err = dnsMessage.Unpack(data)
if err != nil {
return fmt.Errorf("fail to unpack dns message: err: %v", err)
}
res, err := handle(dnsMessage)
if err != nil {
return fmt.Errorf("fail to hanlde dns message: err: %v", err)
}
resData, err := res.Pack()
if err != nil {
return fmt.Errorf("fail to pack dns message: err: %v", err)
}
_, err = conn.WriteTo(resData, addr)
return err
}
func getDnsQuestion(msg *D.Msg) (string, error) {
if len(msg.Question) == 0 {
return "", fmt.Errorf("no dns question")
}
name := msg.Question[0].Name
if strings.HasSuffix(name, ".") {
name = name[:len(name)-1]
}
return name, nil
}
func getResponseIp(msg *D.Msg) []net.IP {
var ips []net.IP
for _, a := range msg.Answer {
if a.Header().Rrtype == D.TypeA {
ip := net.ParseIP(a.(*D.A).A.String())
ips = append(ips, ip)
} else if a.Header().Rrtype == D.TypeAAAA {
ip := net.ParseIP(a.(*D.AAAA).AAAA.String())
ips = append(ips, ip)
} else {
continue
}
}
return ips
}
func handle(dnsMessage *D.Msg) (*D.Msg, error) {
mux.Lock()
defer mux.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
start := time.Now()
question, err := getDnsQuestion(dnsMessage)
if err != nil {
return nil, fmt.Errorf("invalid dns question, err: %v", err)
}
dnsType, err := matcher.GetDnsMatcher().GetDnsType(question)
if err != nil {
return nil, fmt.Errorf("fail to get dns type, err: %v, question: %v", err, question)
}
res, err := dnsMap[dnsType].ExchangeContext(ctx, dnsMessage)
if err != nil {
return nil, fmt.Errorf("fail to exchange dns message, err: %v, question: %v", err, question)
} else if res == nil {
return nil, fmt.Errorf("fail to exchange dns message, err: msg is nil, question: %v", question)
}
resIps := getResponseIp(res)
for _, resIp := range resIps {
if resIp != nil {
log.Debugln(log.FormatLog(log.DnsPrefix, "add cache, resIp:%v, question: %v, rule: %v"), resIp, question, dnsType)
distribution.AddCachedDnsItem(resIp.String(), question, dnsType)
}
}
elapsed := time.Since(start).Milliseconds()
log.Infoln(log.FormatLog(log.DnsPrefix, "target: %v, time: %v ms, result: %v"), question, elapsed, resIps)
return res, err
}