-
Notifications
You must be signed in to change notification settings - Fork 1
/
pac.go
63 lines (50 loc) · 1.33 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
package proxy
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/alphabetY/goflyway1/pkg/lookup"
)
func (proxy *ProxyClient) PACFile(w http.ResponseWriter, r *http.Request) {
if lookup.ChinaList == nil || len(lookup.ChinaList) == 0 {
w.Write([]byte("the list is empty"))
return
}
table, _ := json.Marshal(lookup.ChinaList)
t := "SOCKS5"
if proxy.Policy.IsSet(PolicyManInTheMiddle) {
t = "PROXY"
}
pac := &bytes.Buffer{}
pac.WriteString("var table = ")
pac.Write(table)
pac.WriteString(fmt.Sprintf(`;
function isInTable(host) {
var cands = host.split('.');
if (cands.length <= 1)
return false;
var _table = table;
for (var i = cands.length - 1; i >= 0; i--) {
var cand = cands[i];
if (!(cand in _table))
return false;
if (_table[cand] === 0)
return true;
_table = _table[cand];
}
return true;
}
function FindProxyForURL(url, host) {
if (isPlainHostName(host) ||
shExpMatch(host, "*.local") ||
isInNet(dnsResolve(host), "10.0.0.0", "255.0.0.0") ||
isInNet(dnsResolve(host), "172.16.0.0", "255.240.0.0") ||
isInNet(dnsResolve(host), "192.168.0.0", "255.255.0.0") ||
isInNet(dnsResolve(host), "127.0.0.0", "255.255.255.0") ||
isInTable(host))
return "DIRECT";
return "%s %s";
}`, t, r.Host))
w.Write(pac.Bytes())
}