forked from shawn1m/overture
/
dispatcher.go
157 lines (126 loc) · 4.66 KB
/
dispatcher.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
150
151
152
153
154
155
156
157
package outbound
import (
"net"
"regexp"
log "github.com/sirupsen/logrus"
"github.com/miekg/dns"
"github.com/shadowsocks/overture/core/cache"
"github.com/shadowsocks/overture/core/common"
"github.com/shadowsocks/overture/core/hosts"
)
type Dispatcher struct {
QuestionMessage *dns.Msg
PrimaryDNS []*common.DNSUpstream
AlternativeDNS []*common.DNSUpstream
OnlyPrimaryDNS bool
PrimaryClientBundle *ClientBundle
AlternativeClientBundle *ClientBundle
ActiveClientBundle *ClientBundle
IPNetworkPrimaryList []*net.IPNet
IPNetworkAlternativeList []*net.IPNet
DomainPrimaryList []string
DomainAlternativeList []string
RedirectIPv6Record bool
InboundIP string
MinimumTTL int
Hosts *hosts.Hosts
Cache *cache.Cache
}
func (d *Dispatcher) Exchange() {
d.PrimaryClientBundle = NewClientBundle(d.QuestionMessage, d.PrimaryDNS, d.InboundIP, d.MinimumTTL, d.Hosts, d.Cache)
d.AlternativeClientBundle = NewClientBundle(d.QuestionMessage, d.AlternativeDNS, d.InboundIP, d.MinimumTTL, d.Hosts, d.Cache)
for _, cb := range [2]*ClientBundle{d.PrimaryClientBundle, d.AlternativeClientBundle} {
if ok := cb.ExchangeFromLocal(); ok {
d.ActiveClientBundle = cb
return
}
}
if d.OnlyPrimaryDNS || d.ExchangeForPrimaryDomain() {
d.ActiveClientBundle = d.PrimaryClientBundle
d.ActiveClientBundle.ExchangeFromRemote(true, true)
return
}
if ok := d.ExchangeForIPv6() || d.ExchangeForAlternativeDomain(); ok {
d.ActiveClientBundle = d.AlternativeClientBundle
d.ActiveClientBundle.ExchangeFromRemote(true, true)
return
}
d.ChooseActiveClientBundle()
if d.ActiveClientBundle == d.AlternativeClientBundle {
d.ActiveClientBundle.ExchangeFromRemote(false, true)
}
d.ActiveClientBundle.CacheResult()
}
func (d *Dispatcher) ExchangeForIPv6() bool {
if (d.PrimaryClientBundle.QuestionMessage.Question[0].Qtype == dns.TypeAAAA) && d.RedirectIPv6Record {
d.ActiveClientBundle = d.AlternativeClientBundle
log.Debug("Finally use alternative DNS")
return true
}
return false
}
func (d *Dispatcher) ExchangeForAlternativeDomain() bool {
qn := d.PrimaryClientBundle.QuestionMessage.Question[0].Name[:len(d.PrimaryClientBundle.QuestionMessage.Question[0].Name)-1]
for _, re := range d.DomainAlternativeList {
matched, err := regexp.MatchString(re, qn)
if err == nil && matched {
log.Debug("Matched: Domain alternative " + qn + " with " + re)
d.ActiveClientBundle = d.AlternativeClientBundle
log.Debug("Finally use alternative DNS")
return true
}
}
log.Debug("Domain alternative match fail")
return false
}
func (d *Dispatcher) ExchangeForPrimaryDomain() bool {
qn := d.PrimaryClientBundle.QuestionMessage.Question[0].Name[:len(d.PrimaryClientBundle.QuestionMessage.Question[0].Name)-1]
for _, re := range d.DomainPrimaryList {
matched, err := regexp.MatchString(re, qn)
if err == nil && matched {
log.Debug("Matched: Domain primary " + qn + " with " + re)
d.ActiveClientBundle = d.PrimaryClientBundle
log.Debug("Finally use primary DNS")
return true
}
}
log.Debug("Domain primary match fail")
return false
}
func (d *Dispatcher) ChooseActiveClientBundle() {
d.ActiveClientBundle = d.PrimaryClientBundle
d.PrimaryClientBundle.ExchangeFromRemote(false, true)
if d.PrimaryClientBundle.ResponseMessage == nil || !common.HasAnswer(d.PrimaryClientBundle.ResponseMessage) {
log.Debug("Primary DNS answer is empty, finally use alternative DNS")
d.ActiveClientBundle = d.AlternativeClientBundle
return
}
for _, a := range d.PrimaryClientBundle.ResponseMessage.Answer {
log.Debug("Try to match response ip address with IP network")
if a.Header().Rrtype == dns.TypeA {
if common.IsIPMatchList(net.ParseIP(a.(*dns.A).A.String()), d.IPNetworkPrimaryList, true, "primary") {
d.ActiveClientBundle = d.PrimaryClientBundle
log.Debug("Finally use primary DNS")
return
}
if common.IsIPMatchList(net.ParseIP(a.(*dns.A).A.String()), d.IPNetworkAlternativeList, true, "alternative") {
d.ActiveClientBundle = d.AlternativeClientBundle
log.Debug("Finally use alternative DNS")
return
}
} else if a.Header().Rrtype == dns.TypeAAAA {
if common.IsIPMatchList(net.ParseIP(a.(*dns.AAAA).AAAA.String()), d.IPNetworkPrimaryList, true, "primary") {
d.ActiveClientBundle = d.PrimaryClientBundle
log.Debug("Finally use primary DNS")
return
}
if common.IsIPMatchList(net.ParseIP(a.(*dns.AAAA).AAAA.String()), d.IPNetworkAlternativeList, true, "alternative") {
d.ActiveClientBundle = d.AlternativeClientBundle
log.Debug("Finally use alternative DNS")
return
}
}
log.Debug("IP network match failed, finally use alternative DNS")
d.ActiveClientBundle = d.AlternativeClientBundle
}
}