-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpac.go
54 lines (49 loc) · 897 Bytes
/
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
package main
import (
"html/template"
"io"
"strings"
)
// PAC 参数
type PacParam struct {
SiteList []string
ProxyHost string
ProxyPort int
}
// PAC 文件模板
var PacTemplate = `function FindProxyForURL(url, host) {
{{range .SiteList}}
if (shExpMatch(url,"*{{.}}/*")) {
return "PROXY {{$.ProxyHost}}:{{$.ProxyPort}}";
}
{{- end}}
return "DIRECT";
}`
func pac(wr io.Writer, param PacParam) error {
pac, err := template.New("").Parse(PacTemplate)
if err != nil {
return err
}
err = pac.Execute(wr, param)
if err != nil {
return err
}
return nil
}
func inpac(list []string, host string) bool {
var n1 = strings.Split(host, ".")
OUT:
for i := range list {
var n2 = strings.Split(list[i], ".")
if len(n1) != len(n2) {
continue
}
for j := range n1 {
if n1[j] != n2[j] && n2[j] != "*" {
continue OUT
}
}
return true
}
return false
}