-
Notifications
You must be signed in to change notification settings - Fork 0
/
pac.go
85 lines (78 loc) · 1.79 KB
/
pac.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
package sidecar
import (
"encoding/base64"
"net/http"
"time"
"github.com/pmezard/adblock/adblock"
)
type Pac struct {
Matcher *adblock.RuleMatcher
}
func NewPac(server RemoteServerInfo, gfwUrl string, customHosts []string) *Pac {
p := &Pac{
Matcher: nil,
}
if gfwUrl != "" {
p.getGfwList(server.Host, server.ComplexPath, server.CustomHeaders, gfwUrl)
}
if customHosts != nil {
p.ExpandHosts(customHosts)
}
return p
}
func (p *Pac) getGfwList(server string, subpath string, headers map[string]string, url string) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
Panic("Fetch GfwList failed.")
}
req_url := req.URL
raw_path := req_url.Path
req_url.Host = server
req_url.Path = "/" + subpath + "/" + req.Host + raw_path
req.Host = server
for k, v := range headers {
req.Header.Add(k, v)
}
resp, err := defaultTransport.RoundTrip(req)
if err != nil {
Panic("Fetch GfwList failed.")
}
Info("Fetch GfwList from ", url)
decoder := base64.NewDecoder(base64.StdEncoding, resp.Body)
if p.Matcher == nil {
p.Matcher = adblock.NewMatcher()
}
rules, _ := adblock.ParseRules(decoder)
for _, rule := range rules {
p.Matcher.AddRule(rule, 0)
}
}
func (p *Pac) ExpandHosts(list []string) {
if p.Matcher == nil {
p.Matcher = adblock.NewMatcher()
}
var rule *adblock.Rule
var err error
for _, host := range list {
rule, err = adblock.ParseRule("||" + host)
if err != nil {
Debug("Parse Pac Rule failed, host: ", host)
continue
}
p.Matcher.AddRule(rule, 0)
Debug("Parse Pac Rule host: ", host)
}
}
func (p *Pac) Compare(req *http.Request) bool {
url := req.URL
url.Scheme = "https"
matched, _, err := p.Matcher.Match(
&adblock.Request{
URL: url.String(),
Timeout: 10 * time.Millisecond,
})
if err != nil {
return false
}
return matched
}