-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpac.go
181 lines (171 loc) · 4.04 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
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
package kpx
import (
"fmt"
"math/big"
"net"
"regexp"
"strings"
"sync"
"time"
"github.com/dop251/goja"
"github.com/palantir/stacktrace"
)
type PacExecutor struct {
js string
program *goja.Program
pool *sync.Pool
}
func NewPac(pacJs string) (*PacExecutor, error) {
js := `
(function(url,host) {
%s
return FindProxyForURL(url,host);
})(url,host)
`
js = fmt.Sprintf(js, pacJs)
program, err := goja.Compile("", js, false)
if err != nil {
return nil, stacktrace.Propagate(err, "unable to compile regex")
}
return &PacExecutor{
js: js,
program: program,
pool: &sync.Pool{},
}, nil
}
func (p *PacExecutor) Run(url, host string) (string, error) {
// get runtime from pool or create a new one
var runtime *goja.Runtime
item := p.pool.Get()
if item == nil {
runtime = p.build()
} else {
runtime = item.(*goja.Runtime)
}
defer p.pool.Put(runtime)
// execute code
runtime.Set("url", url)
runtime.Set("host", host)
val, err := runtime.RunProgram(p.program)
if err != nil {
return "", err // no wrap
}
return val.String(), nil
}
func (p *PacExecutor) build() *goja.Runtime {
runtime := goja.New()
runtime.Set("isPlainHostName", isPlainHostName)
runtime.Set("dnsDomainIs", dnsDomainIs)
runtime.Set("localHostOrDomainIs", localHostOrDomainIs)
runtime.Set("isResolvable", isResolvable)
runtime.Set("isInNet", isInNet)
runtime.Set("dnsResolve", dnsResolve)
runtime.Set("convert_addr", convert_addr)
runtime.Set("myIpAddress", myIpAddress)
runtime.Set("dnsDomainLevels", dnsDomainLevels)
runtime.Set("shExpMatch", shExpMatch)
runtime.Set("weekdayRange", weekdayRange)
runtime.Set("dateRange", dateRange)
runtime.Set("timeRange", timeRange)
runtime.Set("alert", alert)
return runtime
}
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_(PAC)_file#isPlainHostName
func isPlainHostName(host string) bool {
return !strings.Contains(host, ".")
}
func dnsDomainIs(host, domain string) bool {
return strings.HasPrefix(domain, ".") && strings.HasSuffix(host, domain)
}
func localHostOrDomainIs(host, hostdom string) bool {
return host == hostdom || (!strings.Contains(host, ".") && strings.HasPrefix(hostdom, host))
}
func isResolvable(host string) bool {
_, err := net.LookupHost(host)
return err == nil
}
func isInNet(host, pattern, mask string) bool {
host = dnsResolve(host)
hostInt := convert_addr(host)
patternInt := convert_addr(pattern)
maskInt := convert_addr(mask)
return hostInt&maskInt == patternInt
}
func dnsResolve(host string) string {
ips, err := net.LookupHost(host)
if err != nil {
return ""
}
if len(ips) == 0 {
return ""
}
return ips[0]
}
func convert_addr(ipaddr string) int64 {
ip := net.ParseIP(ipaddr)
ipInt := big.NewInt(0)
ipInt.SetBytes(ip.To4())
return ipInt.Int64()
}
func myIpAddress() string {
ips, err := net.LookupHost("localhost")
if err != nil {
return "127.0.0.1"
}
if len(ips) == 0 {
return "127.0.0.1"
}
return ips[0]
}
func dnsDomainLevels(host string) int {
return len(strings.Split(host, ".")) - 1
}
func shExpMatch(str, shexp string) bool {
shexp = strings.ReplaceAll(shexp, ".", `\.`)
shexp = strings.ReplaceAll(shexp, "*", ".*")
shexp = strings.ReplaceAll(shexp, "?", ".")
shexp = "^" + shexp + "$"
regex, _ := regexp.Compile(shexp)
return regex.MatchString(str)
}
var days = [...]string{"SUN", "MON", "TUE", "WEN", "THU", "FRI", "SAT"}
func weekdayRange(start, end, tz string) bool {
startDay := -1
endDay := -1
for i, day := range days {
if start == day {
startDay = i
}
if end == day {
endDay = i
}
}
if end == "GMT" {
tz = "GMT"
endDay = startDay
}
today := time.Now()
if tz == "GMT" {
today = today.UTC()
}
weekDay := int(today.Weekday())
if startDay <= weekDay && weekDay <= endDay {
return true
}
weekDay += 7
if startDay <= weekDay && weekDay <= endDay {
return true
}
return false
}
func dateRange() bool {
// TODO implement PAC dateRange()
return true
}
func timeRange() bool {
// TODO implement PAC timeRange()
return true
}
func alert(message string) {
logInfo("%s", message)
}