-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (128 loc) · 3.35 KB
/
main.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
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
package main
import (
"net"
"os"
"github.com/zserge/webview"
"github.com/rakyll/statik/fs"
"net/http"
_ "./statik"
"strconv"
"./proxysettings"
"encoding/json"
"sync"
"SocksOverWS/proxy"
"SocksOverWS/updater"
"SocksOverWS/proxyconfig"
"log"
"os/exec"
"html"
)
var server string
var checksum []byte
var view webview.WebView
func runGUIServer() string {
static, err := fs.New()
if err != nil {
os.Exit(1)
}
listener, err := net.Listen("tcp", "0.0.0.0:0")
if err != nil {
os.Exit(1)
}
http.Handle("/", http.FileServer(static))
go (func() {
http.Serve(listener, nil)
})()
return "http://127.0.0.1:" + strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)
}
func handleRPC(view webview.WebView, data string) {
var res map[string]interface{}
json.Unmarshal([]byte(data), &res)
cmd := res["action"].(string)
if cmd == "READY" {
available, sum, err := updater.Check()
checksum = sum
if err != nil {
view.Eval(`alert("Error while checking for updates: ` + err.Error() + `")`)
}
if available {
view.Eval(`window.receiveRPC({cmd: 'showUpdatePrompt'})`)
}
} else if cmd == "CONNECT" {
if res["pac"].(string) == "GFW" {
proxysettings.Set(server + "/gfw.pac")
} else {
proxysettings.Set(server + "/normal.pac")
}
err := proxy.Run(proxyconfig.ProxyConfig{
Addr: res["server"].(string),
ValidateCert: res["validateCertificate"].(string) == "true",
EncryptionType: res["encryptionType"].(string),
BypassType: res["bypassType"].(string),
})
if err != nil {
view.Eval("alert('Connection failed," + html.EscapeString(err.Error()) + "')")
return
}
go (func() {
log.Println("Testing connection")
err = proxy.TestConnection()
if err != nil {
view.Dispatch(func() {
view.Eval("alert('Failed to test connection," + html.EscapeString(err.Error()) + "')")
view.Eval("window.receiveRPC({cmd: 'setConnectionStatus', status: false})")
})
return
}
view.Dispatch(func() {
view.Eval("window.receiveRPC({cmd: 'setConnectionStatus', status: true})")
})
})()
} else if cmd == "DISCONNECT" {
proxysettings.Clear()
proxy.Stop()
view.Eval("window.receiveRPC({cmd: 'setConnectionStatus', status: false})")
} else if cmd == "EXIT" {
proxysettings.Clear()
os.Exit(0)
} else if cmd == "UPDATE" {
view.Eval(`window.receiveRPC({cmd: 'showUpdateScreen'})`)
go func() {
err := updater.Update(checksum)
if err != nil {
view.Eval(`alert("Updater has failed, please download the latest version. \nError: ` + err.Error() + `")`)
os.Exit(1)
} else {
view.Eval(`alert("Successfully updated, please start the proxy client again.")`)
os.Exit(0)
}
}()
} else if cmd == "SHOW_LOG" {
exec.Command("cmd","/C", "start", "powershell", "-Command", "Get-Content", "logs.log", "-Wait", "-Tail", "30").Run()
}
}
func main() {
var wait sync.WaitGroup
wait.Add(1)
server = runGUIServer()
go (func() {
view = webview.New(webview.Settings{
Title: "Proxy Settings",
URL: server,
Width: 290,
Height: 200,
ExternalInvokeCallback: handleRPC,
})
view.Run()
proxysettings.Clear()
wait.Done()
})()
os.Remove("logs.log")
logFile, err := os.OpenFile("logs.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
defer logFile.Close()
log.SetOutput(logFile)
wait.Wait()
}