-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocks.go
71 lines (62 loc) · 1.88 KB
/
socks.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
package socks
import (
"fmt"
"os/exec"
"sync"
"github.com/getlantern/byteexec"
"github.com/getlantern/golog"
)
var (
log = golog.LoggerFor("socks")
mu sync.Mutex
be *byteexec.Exec
)
// EnsureHelperToolPresent checks if helper tool exists and extracts it if not.
// On Mac OS, it also checks and set the file's owner to root:wheel and the setuid bit,
// it will request user to input password through a dialog to gain the rights to do so.
// path: absolute or relative path of the file to be checked and generated if
// not exists. Note - relative paths are resolved relative to the system-
// specific folder for aplication resources.
// prompt: the message to be shown on the dialog.
// iconPath: the full path of the icon to be shown on the dialog.
func EnsureHelperToolPresent(path string, prompt string, iconFullPath string) (err error) {
mu.Lock()
defer mu.Unlock()
pacBytes, err := Asset("socks")
if err != nil {
return fmt.Errorf("Unable to access socks asset: %v", err)
}
be, err = byteexec.New(pacBytes, path)
if err != nil {
return fmt.Errorf("Unable to extract helper tool: %v", err)
}
return ensureElevatedOnDarwin(be, prompt, iconFullPath)
}
/* On tells OS to configure proxy through `socks` */
func On(SocksIP, SocksPort string) (err error) {
mu.Lock()
defer mu.Unlock()
if be == nil {
return fmt.Errorf("call EnsureHelperToolPresent() first")
}
cmd := be.Command("on", SocksIP, SocksPort)
return run(cmd)
}
/* Off sets proxy mode back to direct/none */
func Off() (err error) {
mu.Lock()
defer mu.Unlock()
if be == nil {
return fmt.Errorf("call EnsureHelperToolPresent() first")
}
cmd := be.Command("off")
return run(cmd)
}
func run(cmd *exec.Cmd) error {
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Unable to execute %v: %s\n%s", cmd.Path, err, string(out))
}
log.Tracef("Command %v output %v", cmd.Path, string(out))
return nil
}