| 8 | ) |
| 9 | |
| 10 | func (proxy *ProxyClient) servePACFile(w http.ResponseWriter, r *http.Request) { |
| 11 | table, _ := json.Marshal(proxy.ACL.White.DomainFastMatch) |
| 12 | table2 := proxy.ACL.White.DomainSlowMatch |
| 13 | t := "SOCKS5" |
| 14 | if proxy.Policy.IsSet(PolicyMITM) { |
| 15 | t = "PROXY" |
| 16 | } |
| 17 | |
| 18 | pac := &bytes.Buffer{} |
| 19 | pac.WriteString("function slowMatch(host) { ") |
| 20 | if len(table2) > 0 { |
| 21 | pac.WriteString("return ") |
| 22 | for _, r := range table2 { |
| 23 | pac.WriteString("host.match(/" + r.String() + "/)||") |
| 24 | } |
| 25 | pac.Truncate(pac.Len() - 2) |
| 26 | } else { |
| 27 | pac.WriteString("return false") |
| 28 | } |
| 29 | pac.WriteString(` } |
| 30 | var table = `) |
| 31 | pac.Write(table) |
| 32 | pac.WriteString(fmt.Sprintf(`; |
| 33 | function isInTable(host) { |
| 34 | var cands = host.split('.'); |
| 35 | if (cands.length <= 1) |
| 36 | return slowMatch(host); |
| 37 | |
| 38 | var _table = table; |
| 39 | for (var i = cands.length - 1; i >= 0; i--) { |
| 40 | var cand = cands[i]; |
| 41 | if (!(cand in _table)) |
| 42 | return slowMatch(host); |
| 43 | |
| 44 | if (_table[cand] === 0) |
| 45 | return true; |
| 46 | |
| 47 | _table = _table[cand]; |
| 48 | } |
| 49 | |
| 50 | return slowMatch(host); |
| 51 | } |
| 52 | |
| 53 | function FindProxyForURL(url, host) { |
| 54 | if (isPlainHostName(host) || |
| 55 | shExpMatch(host, "*.local") || |
| 56 | isInNet(dnsResolve(host), "10.0.0.0", "255.0.0.0") || |
| 57 | isInNet(dnsResolve(host), "172.16.0.0", "255.240.0.0") || |
| 58 | isInNet(dnsResolve(host), "192.168.0.0", "255.255.0.0") || |
| 59 | isInNet(dnsResolve(host), "127.0.0.0", "255.255.255.0") || |
| 60 | isInTable(host)) |
| 61 | return "DIRECT"; |
| 62 | |
| 63 | return "%s %s"; |
| 64 | }`, t, r.Host)) |
| 65 | |
| 66 | w.Write(pac.Bytes()) |
| 67 | } |