Repository navigation
Expand file tree
/
Copy pathpac.go
More file actions
170 lines (157 loc) · 4.03 KB
/
Copy pathpac.go
File metadata and controls
170 lines (157 loc) · 4.03 KB
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
package main
import (
"fmt"
"net"
"net/http"
"path/filepath"
"runtime"
"strings"
"sync"
"sync/atomic"
"github.com/getlantern/detour"
"github.com/getlantern/filepersist"
"github.com/getlantern/pac"
"github.com/getlantern/flashlight/ui"
)
var (
isPacOn = int32(0)
proxyAddr string
pacURL string
muPACFile sync.RWMutex
pacFile []byte
directHosts = make(map[string]bool)
proxyAll = int32(0)
)
func ServeProxyAllPacFile(b bool) {
if b {
atomic.StoreInt32(&proxyAll, 1)
} else {
atomic.StoreInt32(&proxyAll, 0)
}
genPACFile()
}
func setUpPacTool() error {
var iconFile string
if runtime.GOOS == "darwin" {
// We have to use a short filepath here because Cocoa won't display the
// icon if the path is too long.
iconFile := filepath.Join("/tmp", "escalatelantern.ico")
icon, err := Asset("icons/32on.ico")
if err != nil {
return fmt.Errorf("Unable to load escalation prompt icon: %v", err)
} else {
err := filepersist.Save(iconFile, icon, 0644)
if err != nil {
return fmt.Errorf("Unable to persist icon to disk: %v", err)
} else {
log.Debugf("Saved icon file to: %v", iconFile)
}
}
}
err := pac.EnsureHelperToolPresent("pac-cmd", "Lantern would like to be your system proxy", iconFile)
if err != nil {
return fmt.Errorf("Unable to set up pac setting tool: %v", err)
}
return nil
}
func setProxyAddr(addr string) {
proxyAddr = addr
}
func genPACFile() {
hostsString := "[]"
// only bypass sites if proxy all option is unset
if atomic.LoadInt32(&proxyAll) == 0 {
var hosts []string
for k, v := range directHosts {
if v {
hosts = append(hosts, k)
}
}
hostsString = "['" + strings.Join(hosts, "', '") + "']"
}
formatter :=
`var bypassDomains = %s;
function FindProxyForURL(url, host) {
if (isPlainHostName(host) // including localhost
|| shExpMatch(host, "*.local")) {
return "DIRECT";
}
// only checks plain IP addresses to avoid leaking domain name
if (/^[0-9.]+$/.test(host)) {
if (isInNet(host, "10.0.0.0", "255.0.0.0") ||
isInNet(host, "172.16.0.0", "255.240.0.0") ||
isInNet(host, "192.168.0.0", "255.255.0.0") ||
isInNet(host, "127.0.0.0", "255.255.255.0")) {
return "DIRECT";
}
}
for (var d in bypassDomains) {
if (host == bypassDomains[d]) {
return "DIRECT";
}
}
return "PROXY %s; DIRECT";
}`
muPACFile.Lock()
pacFile = []byte(fmt.Sprintf(formatter, hostsString, proxyAddr))
muPACFile.Unlock()
}
// watchDirectAddrs adds any site that has accessed directly without error to PAC file
func watchDirectAddrs() {
go func() {
for {
addr := <-detour.DirectAddrCh
// prevents Lantern from accidently leave pac on after exits
if atomic.LoadInt32(&isPacOn) == 0 {
return
}
host, _, err := net.SplitHostPort(addr)
if err != nil {
panic("watchDirectAddrs() got malformated host:port pair")
}
if !directHosts[host] {
directHosts[host] = true
genPACFile()
// reapply so browser will fetch the PAC URL again
doPACOff(pacURL)
doPACOn(pacURL)
}
}
}()
}
func pacOn() {
log.Debug("Setting lantern as system proxy")
handler := func(resp http.ResponseWriter, req *http.Request) {
resp.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig")
resp.WriteHeader(http.StatusOK)
muPACFile.RLock()
if _, err := resp.Write(pacFile); err != nil {
log.Debugf("Error writing response: %v", err)
}
muPACFile.RUnlock()
}
genPACFile()
pacURL = ui.Handle("/proxy_on.pac", http.HandlerFunc(handler))
log.Debugf("Serving PAC file at %v", pacURL)
doPACOn(pacURL)
atomic.StoreInt32(&isPacOn, 1)
}
func pacOff() {
if atomic.CompareAndSwapInt32(&isPacOn, 1, 0) {
log.Debug("Unsetting lantern as system proxy")
doPACOff(pacURL)
log.Debug("Unset lantern as system proxy")
}
}
func doPACOn(pacURL string) {
err := pac.On(pacURL)
if err != nil {
log.Errorf("Unable to set lantern as system proxy: %v", err)
}
}
func doPACOff(pacURL string) {
err := pac.Off(pacURL)
if err != nil {
log.Errorf("Unable to unset lantern as system proxy: %v", err)
}
}