Repository navigation
Expand file tree
/
Copy pathpac.go
More file actions
145 lines (133 loc) · 3.29 KB
/
Copy pathpac.go
File metadata and controls
145 lines (133 loc) · 3.29 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
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)
)
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() {
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 (host == "localhost" || host == "127.0.0.1") {
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() {
detour.DirectAddrCh = make(chan string)
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()
doPACOn()
}
}
}()
}
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()
resp.Write(pacFile)
muPACFile.RUnlock()
}
genPACFile()
pacURL = ui.Handle("/proxy_on.pac", http.HandlerFunc(handler))
log.Debugf("Serving PAC file at %v", pacURL)
doPACOn()
atomic.StoreInt32(&isPacOn, 1)
}
func pacOff() {
if atomic.CompareAndSwapInt32(&isPacOn, 1, 0) {
log.Debug("Unsetting lantern as system proxy")
doPACOff()
log.Debug("Unset lantern as system proxy")
}
}
func doPACOn() {
err := pac.On(pacURL)
if err != nil {
log.Errorf("Unable to set lantern as system proxy: %v", err)
}
}
func doPACOff() {
err := pac.Off()
if err != nil {
log.Errorf("Unable to unset lantern as system proxy: %v", err)
}
}