forked from mikesimons/pacyak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacyak.go
223 lines (187 loc) · 6.68 KB
/
pacyak.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"net"
"net/http"
"net/url"
"os/exec"
"time"
log "github.com/Sirupsen/logrus"
"github.com/mikesimons/earl"
"github.com/mikesimons/pacyak/pacsandbox"
"github.com/mikesimons/pacyak/proxyfactory"
"github.com/mikesimons/readly"
)
const DIRECT_SANDBOX = 0
const PROXY_SANDBOX = 1
// PacYakOpts holds runtime config options for PacYakApplication
type PacYakOpts struct {
PingCheckHost string
PacFile string
ListenAddr string
PacProxy string
LogLevelStr string
LogLevel log.Level
}
// pacInterpreter is a simple interface we use to provide a dummy implementation of pacsandbox for directPac
type pacInterpreter interface {
ProxyFor(string) (string, error)
Reset() // HACK
}
// directPac is a dummy implementation of pacsandbox to avoid invoking JS to return a constant static string ("DIRECT")
type directPac struct{}
func (p *directPac) ProxyFor(s string) (string, error) { return "DIRECT", nil }
func (p *directPac) Reset() {}
// PacYakApplication holds all application state
type PacYakApplication struct {
opts *PacYakOpts
pacFile *earl.URL
sandboxIndex int
sandboxes []pacInterpreter
factory *proxyfactory.ProxyFactory
listenAddr string
interfaceMap map[string]string
Reader *readly.Reader
}
// Run is the entry point for pacyak. It will initialize pacyak and start listening.
func Run(opts *PacYakOpts) {
log.SetLevel(opts.LogLevel)
reader := readly.New()
// We need to explicitly set HTTP client to prevent it trying to use ENV vars for proxy
// pacyak listen addr is expected to be set as HTTP_PROXY / HTTPS_PROXY but it isn't started yet!
// This level of control also means a lib like hashicorp/go-getter is not suitable :(
reader.Client = &http.Client{
Transport: &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
if opts.PacProxy != "" {
return url.Parse(opts.PacProxy)
}
return nil, nil
},
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 5 * time.Second,
}).DialContext,
IdleConnTimeout: 5 * time.Second,
},
}
app := &PacYakApplication{
opts: opts,
pacFile: earl.Parse(opts.PacFile),
factory: proxyfactory.New(),
sandboxIndex: DIRECT_SANDBOX,
sandboxes: []pacInterpreter{&directPac{}, &directPac{}},
listenAddr: opts.ListenAddr,
Reader: reader,
}
go app.monitorPingAvailability()
go app.monitorNetworkInterfaces()
// FIXME - graceful handler; server 502 on error and keep going
log.Fatal(http.ListenAndServe(app.opts.ListenAddr, app))
}
// ServeHTTP handles directing the request to the correct proxy
func (app *PacYakApplication) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{
"method": r.Method,
"url": r.URL.String(),
}).Debug("Processing HTTP request")
// FIXME: We want to be able to serve some stats / tools from here
//if !r.URL.IsAbs() {
// fmt.Fprintf(w, `function FindProxyForURL(url, host) { return "PROXY %s"; }`, app.listenAddr)
// return
//}
pacResponse, err := app.activeSandbox().ProxyFor(r.URL.String())
if err != nil {
log.WithFields(log.Fields{"response": pacResponse, "sandbox_error": err, "url": r.URL.String()}).Error("Sandbox error!")
} else {
log.WithFields(log.Fields{"response": pacResponse}).Debug("PAC result")
}
proxy := app.factory.FromPacResponse(pacResponse)
proxy.ServeHTTP(w, r)
}
// switchToDirect switches the pac sandbox to the dummy "DIRECT" implementation
// We do this when our ping check fails (indicating a proxy may no longer be required)
func (app *PacYakApplication) switchToDirect() {
if app.sandboxIndex != DIRECT_SANDBOX {
log.Info("PAC availability check failed; switching to direct")
app.setSandbox(DIRECT_SANDBOX)
}
}
// switchToPac switches the pac sandbox to the JS implementation (using the PAC file specified on the CLI)
// We do this when our ping check passes (indicating we're in an env that requires a proxy)
func (app *PacYakApplication) switchToPac() {
if app.sandboxIndex == DIRECT_SANDBOX {
pac, err := app.Reader.Read(app.pacFile.Input)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("PAC availability check passed but was unable to fetch PAC")
} else {
log.Info("PAC availability check passed; switching from direct")
app.sandboxes[PROXY_SANDBOX] = pacsandbox.New(pac)
app.setSandbox(PROXY_SANDBOX)
}
}
}
// handlePacAvailability updates the status of the ping check and switches sandbox if required
// This may be called from multiple go routines so we wrap it in a mutex to avoid racing
// Tried this with channels once but CPU usage blew up! Probably PEBKAC
func (app *PacYakApplication) handlePacAvailability() {
available := false
retries := 0
for ; retries < 2; retries++ {
available = exec.Command("ping", "-w", "1", app.opts.PingCheckHost).Run() == nil
log.WithFields(log.Fields{"available": available}).Info("PAC availability check")
if !available {
time.Sleep(time.Second * 5)
} else {
break
}
}
if !available {
app.switchToDirect()
} else {
app.switchToPac()
}
}
// monitorPingAvailability is a wrapper for handlePacAvailability invoking it every 30 seconds
func (app *PacYakApplication) monitorPingAvailability() {
app.handlePacAvailability()
for _ = range time.Tick(30 * time.Second) {
app.handlePacAvailability()
}
}
// checkNetworkInterfaces will trigger a ping check if network interfaces have changed since last check
func (app *PacYakApplication) checkNetworkInterfaces() {
interfaceMap := makeInterfaceMap()
lastInterfaceMap := app.interfaceMap
defer func() {
app.interfaceMap = interfaceMap
}()
newInterfaces := interfaceMapKeys(interfaceMap)
oldInterfaces := interfaceMapKeys(lastInterfaceMap)
if interfaceListChanged(newInterfaces, oldInterfaces) {
log.WithFields(log.Fields{"old": oldInterfaces, "new": newInterfaces}).Debug("Network interface list has changed")
app.handlePacAvailability()
return
}
for key, val := range interfaceMap {
if lastInterfaceMap[key] != val {
log.WithFields(log.Fields{"interface": key, "old": lastInterfaceMap[key], "new": val}).Debug("Network interface configuration has changed")
app.handlePacAvailability()
return
}
}
log.Debug("No network changes detected")
}
// monitorNetworkInterfaces is a wrapper for checkNetworkInterfaces invoking it every 5 seconds
func (app *PacYakApplication) monitorNetworkInterfaces() {
app.interfaceMap = makeInterfaceMap()
for _ = range time.Tick(5 * time.Second) {
app.checkNetworkInterfaces()
}
}
func (app *PacYakApplication) activeSandbox() pacInterpreter {
return app.sandboxes[app.sandboxIndex]
}
func (app *PacYakApplication) setSandbox(index int) {
app.sandboxes[index].Reset()
app.sandboxIndex = index
}