-
Notifications
You must be signed in to change notification settings - Fork 35
/
pacwrapper.go
87 lines (77 loc) · 2.43 KB
/
pacwrapper.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
// Copyright 2019 The Alpaca Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"log"
"net/http"
"text/template"
)
// PACData contains program configuration to be made available to the pacWrapTmpl.
type PACData struct {
Port int
}
type pacData struct {
PACData
UpstreamPAC string
}
type PACWrapper struct {
data pacData
tmpl *template.Template
alpacaPAC string
}
// PACWrapper template for serving a PAC file to point at alpaca or DIRECT. If we have a valid
// PAC file, we wrap that PAC file with a wrapper function that only returns "DIRECT" or
// "localhost:port". If we do not have a PAC file, the PAC function we serve only returns "DIRECT",
// which should prevent all requests reaching us.
var pacWrapTmpl = `// Wrapped for and by alpaca
function FindProxyForURL(url, host) {
{{ if .UpstreamPAC }}
return FindProxyForURL(url, host) === "DIRECT" ? "DIRECT" : "PROXY localhost:{{.Port}}";
{{.UpstreamPAC}}
{{ else }}
return "DIRECT";
{{ end }}
}
`
func NewPACWrapper(data PACData) *PACWrapper {
t := template.Must(template.New("alpaca").Parse(pacWrapTmpl))
return &PACWrapper{pacData{data, ""}, t, ""}
}
func (pw *PACWrapper) Wrap(pacjs []byte) {
pac := string(pacjs)
if pac == pw.data.UpstreamPAC && pw.alpacaPAC != "" {
return
}
pw.data.UpstreamPAC = pac
b := &bytes.Buffer{}
if err := pw.tmpl.Execute(b, pw.data); err != nil {
log.Printf("error executing PAC wrap template: %v", err)
return
}
pw.alpacaPAC = b.String()
}
func (pw *PACWrapper) SetupHandlers(mux *http.ServeMux) {
mux.HandleFunc("/alpaca.pac", pw.handlePAC)
}
func (pw *PACWrapper) handlePAC(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig")
if _, err := w.Write([]byte(pw.alpacaPAC)); err != nil {
log.Printf("Error writing PAC to response: %v", err)
}
}