/
config.go
307 lines (277 loc) · 6.74 KB
/
config.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package local
import (
"net"
"net/http"
"path/filepath"
"strings"
"github.com/yinqiwen/gsnova/common/channel"
"github.com/yinqiwen/gsnova/common/dns"
"github.com/yinqiwen/gsnova/common/helper"
"github.com/yinqiwen/gsnova/common/hosts"
"github.com/yinqiwen/gsnova/common/logger"
)
var GConf LocalConfig
const (
BlockedByGFWRule = "BlockedByGFW"
InHostsRule = "InHosts"
IsCNIPRule = "IsCNIP"
IsPrivateIPRule = "IsPrivateIP"
)
func matchHostnames(pattern, host string) bool {
host = strings.TrimSuffix(host, ".")
pattern = strings.TrimSuffix(pattern, ".")
if len(pattern) == 0 || len(host) == 0 {
return false
}
patternParts := strings.Split(pattern, ".")
hostParts := strings.Split(host, ".")
if len(patternParts) != len(hostParts) {
return false
}
for i, patternPart := range patternParts {
if i == 0 && patternPart == "*" {
continue
}
if patternPart != hostParts[i] {
return false
}
}
return true
}
type PACConfig struct {
Method []string
Host []string
URL []string
Rule []string
Protocol []string
Remote string
}
func (pac *PACConfig) ruleInHosts(req *http.Request) bool {
return hosts.InHosts(req.Host)
}
func (pac *PACConfig) matchProtocol(protocol string) bool {
if len(pac.Protocol) == 0 {
return true
}
for _, p := range pac.Protocol {
if p == "*" || strings.EqualFold(p, protocol) {
return true
}
}
return false
}
func (pac *PACConfig) matchRules(ip string, req *http.Request) bool {
if len(pac.Rule) == 0 {
return true
}
ok := true
for _, rule := range pac.Rule {
not := false
if strings.HasPrefix(rule, "!") {
not = true
rule = rule[1:]
}
if strings.EqualFold(rule, InHostsRule) {
if nil == req {
ok = false
} else {
ok = pac.ruleInHosts(req)
}
} else if strings.EqualFold(rule, BlockedByGFWRule) {
gfwList := getGFWList()
if nil != gfwList && nil != req {
ok = gfwList.IsBlockedByGFW(req)
if !ok {
logger.Debug("#### %s is NOT BlockedByGFW", req.Host)
}
} else {
ok = true
logger.Debug("NIL GFWList object or request")
}
} else if strings.EqualFold(rule, IsCNIPRule) {
if len(ip) == 0 || nil == dns.CNIPSet {
logger.Debug("NIL CNIP content or IP/Domain")
ok = false
} else {
var err error
if net.ParseIP(ip) == nil {
ip, err = dns.DnsGetDoaminIP(ip)
}
if nil == err {
ok = dns.CNIPSet.IsInCountry(net.ParseIP(ip), "CN")
}
logger.Debug("ip:%s is CNIP:%v", ip, ok)
}
} else if strings.EqualFold(rule, IsPrivateIPRule) {
if len(ip) == 0 {
ok = false
} else {
ok = helper.IsPrivateIP(ip)
}
} else {
logger.Error("###Invalid rule:%s", rule)
}
if not {
ok = ok != true
}
if !ok {
break
}
}
return ok
}
func MatchPatterns(str string, rules []string) bool {
if len(rules) == 0 {
return true
}
str = strings.ToLower(str)
for _, pattern := range rules {
matched, err := filepath.Match(pattern, str)
if nil != err {
logger.Error("Invalid pattern:%s with reason:%v", pattern, err)
continue
}
if matched {
return true
}
}
return false
}
func (pac *PACConfig) Match(protocol string, ip string, req *http.Request) bool {
ret := pac.matchProtocol(protocol)
if !ret {
return false
}
ret = pac.matchRules(ip, req)
if !ret {
return false
}
if nil == req {
if len(pac.Host) > 0 || len(pac.Method) > 0 || len(pac.URL) > 0 {
return false
}
return true
}
host := req.Host
if len(pac.Host) > 0 && strings.Contains(host, ":") {
host, _, _ = net.SplitHostPort(host)
}
return MatchPatterns(host, pac.Host) && MatchPatterns(req.Method, pac.Method) && MatchPatterns(req.URL.String(), pac.URL)
}
type HTTPDumpConfig struct {
Dump string
Domain []string
ExcludeBody []string
IncludeBody []string
}
func (dump *HTTPDumpConfig) MatchDomain(host string) bool {
for _, dm := range dump.Domain {
matched, _ := filepath.Match(dm, host)
if matched {
return true
}
}
return false
}
type ProxyConfig struct {
Local string
Forward string
MITM bool //Man-in-the-middle
Transparent bool
HTTPDump HTTPDumpConfig
PAC []PACConfig
}
func (cfg *ProxyConfig) getProxyChannelByHost(proto string, host string) string {
creq, _ := http.NewRequest("Connect", "https://"+host, nil)
return cfg.findProxyChannelByRequest(proto, host, creq)
}
func (cfg *ProxyConfig) findProxyChannelByRequest(proto string, ip string, req *http.Request) string {
var channelName string
// if len(ip) > 0 && helper.IsPrivateIP(ip) {
// //channel = "direct"
// return channel.DirectChannelName
// }
for _, pac := range cfg.PAC {
if pac.Match(proto, ip, req) {
channelName = pac.Remote
break
}
}
if len(channelName) == 0 {
logger.Error("No proxy channel found.")
}
return channelName
}
type AdminConfig struct {
Listen string
BroadcastAddr string
ConfigDir string
}
type UDPGWConfig struct {
Addr string
}
type SNIConfig struct {
Redirect map[string]string
}
func (sni *SNIConfig) redirect(domain string) (string, bool) {
for k, v := range sni.Redirect {
matched, err := filepath.Match(k, domain)
if nil != err {
logger.Error("Invalid pattern:%s with reason:%v", k, err)
continue
}
if matched {
return v, true
}
}
return "", false
}
type GFWListConfig struct {
URL string
UserRule []string
Proxy string
RefershPeriodMiniutes int
}
type LocalConfig struct {
Log []string
Cipher channel.CipherConfig
Mux channel.MuxConfig
ProxyLimit channel.ProxyLimitConfig
UserAgent string
UPNPExposePort int
LocalDNS dns.LocalDNSConfig
UDPGW UDPGWConfig
SNI SNIConfig
Admin AdminConfig
GFWList GFWListConfig
TransparentMark int
Proxy []ProxyConfig
Channel []channel.ProxyChannelConfig
}
func (cfg *LocalConfig) init() error {
haveDirect := false
for i := range GConf.Channel {
if GConf.Channel[i].Name == channel.DirectChannelName && GConf.Channel[i].Enable {
haveDirect = true
GConf.Channel[i].ServerList = []string{"direct://0.0.0.0:0"}
GConf.Channel[i].ConnsPerServer = 1
}
if len(GConf.Channel[i].Cipher.Key) == 0 {
GConf.Channel[i].Cipher = GConf.Cipher
}
if len(GConf.Channel[i].HTTP.UserAgent) == 0 {
GConf.Channel[i].HTTP.UserAgent = GConf.UserAgent
}
GConf.Channel[i].Adjust()
}
if !haveDirect {
directProxyChannel := make([]channel.ProxyChannelConfig, 1)
directProxyChannel[0].Name = channel.DirectChannelName
directProxyChannel[0].Enable = true
directProxyChannel[0].ConnsPerServer = 1
directProxyChannel[0].LocalDialMSTimeout = 5000
directProxyChannel[0].ServerList = []string{"direct://0.0.0.0:0"}
GConf.Channel = append(directProxyChannel, GConf.Channel...)
}
return nil
}