/
sysproxy.go
61 lines (55 loc) · 1.36 KB
/
sysproxy.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
package gosshtun
import (
"github.com/fimreal/goutils/ezap"
"github.com/wzshiming/sysproxy"
)
// ref. https://github.com/wzshiming/sysproxy/blob/master/cmd/sysproxy/main.go
func (st *SSHTun) EnableSystemProxy() (ok bool) {
proxy, err := proxyAddr(st.ListenAddr)
if err != nil {
ezap.Fatalf("listen address[%s] is not vaild: %s", st.ListenAddr, err)
}
// set noproxy
err = sysproxy.OnNoProxy([]string{"127.0.0.1", "localhost"})
if err != nil {
ezap.Errorf("error set system noproxy: %s", err)
return
}
// set http proxy
err = sysproxy.OnHTTP(proxy)
if err != nil {
ezap.Errorf("error set system http proxy: %s", err)
return
}
out, err := sysproxy.GetHTTP()
if err != nil {
ezap.Errorf("error get system http proxy: %s", err)
return
}
ezap.Info("set system http proxy: ", out)
// set https proxy
err = sysproxy.OnHTTPS(proxy)
if err != nil {
ezap.Errorf("error set system https proxy: %s", err)
return
}
out, err = sysproxy.GetHTTPS()
if err != nil {
ezap.Errorf("error get system https proxy: %s", err)
return
}
ezap.Info("set system https proxy: ", out)
return true
}
func (st *SSHTun) DisableSystemProxy() {
ezap.Info("unset system http proxy")
err := sysproxy.OffHTTP()
if err != nil {
ezap.Debug(err.Error())
}
ezap.Info("unset system https proxy")
err = sysproxy.OffHTTPS()
if err != nil {
ezap.Debug(err.Error())
}
}