-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
305 lines (280 loc) · 8.74 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"fmt"
"net/url"
"runtime"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
resource "github.com/genshen/wssocks-plugin-ustb/client-ui/resources"
"github.com/genshen/wssocks-plugin-ustb/extra"
"github.com/genshen/wssocks-plugin-ustb/plugins/vpn"
pluginversion "github.com/genshen/wssocks-plugin-ustb/wssocks-ustb/version"
"github.com/genshen/wssocks/client"
"github.com/genshen/wssocks/version"
)
const (
AppName = "wssocks Client"
AppId = "wssocks-ustb.genshen.github.com"
CoreGithubRepoUrl = "https://github.com/genshen/wssocks"
GithubRepoUrl = "https://github.com/genshen/wssocks-plugin-ustb"
DocumentUrl = "https://genshen.github.io/wssocks-plugin-ustb/"
)
const (
btnStopped = iota
btnStarting
btnRunning
btnStopping
)
const (
ProxyCommandGit = iota
ProxyCommandHttp
ProxyCommandSsh
)
func newEntryWithText(text string) *widget.Entry {
entry := widget.NewEntry()
entry.SetText(text)
return entry
}
func newCheckbox(text string, checked bool, onChanged func(bool)) *widget.Check {
checkbox := widget.NewCheck(text, onChanged)
checkbox.SetChecked(checked)
return checkbox
}
func main() {
wssApp := app.NewWithID(AppId)
wssApp.Settings().SetTheme(&myTheme{})
w := wssApp.NewWindow(AppName)
//w.SetFixedSize(true)
//w.Resize(fyne.NewSize(100, 100))
// basic input
uiLocalAddr := &widget.Entry{PlaceHolder: "socks5 listen address", Text: "127.0.0.1:1080"}
uiRemoteAddr := &widget.Entry{PlaceHolder: "wssocks server address"}
uiAuthToken := &widget.Entry{PlaceHolder: "the token for proxy authentication"}
uiHttpEnable := newCheckbox("", false, nil)
uiHttpLocalAddr := &widget.Entry{PlaceHolder: "http listen address", Text: "127.0.0.1:1086"}
uiSkipTSLVerify := newCheckbox("", false, nil)
loadBasicPreference(wssApp.Preferences(), uiLocalAddr, uiRemoteAddr, uiHttpLocalAddr, uiHttpEnable, uiSkipTSLVerify)
uiHttpEnable.OnChanged = func(checked bool) {
if checked {
uiHttpLocalAddr.Enable()
} else {
uiHttpLocalAddr.Disable()
}
}
// vpn input
uiVpnEnable := newCheckbox("enable ustb vpn", true, nil)
uiVpnForceLogout := newCheckbox("", true, nil)
uiVpnHostEncrypt := newCheckbox("", true, nil)
uiVpnHostInput := &widget.Entry{PlaceHolder: "vpn hostname", Text: "n.ustb.edu.cn"}
uiVpnUsername := &widget.Entry{PlaceHolder: "vpn username", Text: ""}
uiVpnPassword := &widget.Entry{PlaceHolder: "vpn password", Text: "", Password: true}
loadVPNPreference(wssApp.Preferences(), uiVpnEnable, uiVpnForceLogout,
uiVpnHostEncrypt, uiVpnHostInput, uiVpnUsername, uiVpnPassword)
uiVpnEnable.OnChanged = func(checked bool) {
if checked {
uiVpnForceLogout.Enable()
uiVpnHostEncrypt.Enable()
uiVpnHostInput.Enable()
uiVpnUsername.Enable()
uiVpnPassword.Enable()
} else {
uiVpnForceLogout.Disable()
uiVpnHostEncrypt.Disable()
uiVpnHostInput.Disable()
uiVpnUsername.Disable()
uiVpnPassword.Disable()
}
}
btnStart := widget.NewButtonWithIcon("Start", theme.MailSendIcon(), nil)
btnStart.Importance = widget.HighImportance
btnStatus := btnStopped
var handles extra.TaskHandles
var ignoreWaitErr = true
btnStart.OnTapped = func() {
if btnStatus == btnRunning { // running can stop
btnStatus = btnStopping
ignoreWaitErr = true
btnStart.SetText("Stopping")
handles.NotifyCloseWrapper()
btnStart.SetText("Start")
btnStatus = btnStopped
} else if btnStatus == btnStopped { // stopped can run
options := extra.Options{
Options: client.Options{
LocalSocks5Addr: uiLocalAddr.Text,
HttpEnabled: uiHttpEnable.Checked,
LocalHttpAddr: uiHttpLocalAddr.Text,
SkipTLSVerify: uiSkipTSLVerify.Checked,
},
UstbVpn: vpn.UstbVpn{
Enable: uiVpnEnable.Checked,
ForceLogout: uiVpnForceLogout.Checked,
HostEncrypt: uiVpnHostEncrypt.Checked,
TargetVpn: uiVpnHostInput.Text,
Username: uiVpnUsername.Text,
Password: uiVpnPassword.Text,
},
RemoteAddr: uiRemoteAddr.Text,
}
btnStatus = btnStarting
btnStart.SetText("Loading")
if err := handles.StartWssocks(options); err != nil {
// log error
dialog.ShowError(err, w)
btnStart.SetText("Start")
btnStatus = btnStopped
return
}
btnStart.SetText("Stop")
btnStatus = btnRunning
go func() {
// the `ignoreWaitErr` the same as swiftui.
ignoreWaitErr = false
// wait error and stop the client
if err := handles.Wait(); err != nil && !ignoreWaitErr {
dialog.ShowError(err, w)
}
btnStart.SetText("Start")
btnStatus = btnStopped
ignoreWaitErr = true
}()
}
}
docUrl, err := url.Parse(DocumentUrl)
if err != nil {
return
}
repoUrl, err := url.Parse(GithubRepoUrl)
if err != nil {
return
}
coreRepoUrl, err := url.Parse(CoreGithubRepoUrl)
if err != nil {
return
}
tabs := container.NewAppTabs(
container.NewTabItemWithIcon(
"Basic",
theme.SettingsIcon(),
&widget.Form{Items: []*widget.FormItem{
{Text: "socks5 address", Widget: uiLocalAddr},
{Text: "remote address", Widget: uiRemoteAddr},
{Text: "auth token", Widget: uiAuthToken},
{Text: "http(s) proxy", Widget: uiHttpEnable},
{Text: "http(s) address", Widget: uiHttpLocalAddr},
{Text: "skip TSL verify", Widget: uiSkipTSLVerify},
}},
),
container.NewTabItemWithIcon(
"USTB VPN",
theme.AccountIcon(),
&widget.Form{Items: []*widget.FormItem{
{Text: "enable", Widget: uiVpnEnable},
{Text: "force logout", Widget: uiVpnForceLogout},
{Text: "host encrypt", Widget: uiVpnHostEncrypt},
{Text: "vpn host", Widget: uiVpnHostInput},
{Text: "username", Widget: uiVpnUsername},
{Text: "password", Widget: uiVpnPassword},
}},
),
)
tabs.SetTabLocation(container.TabLocationTop)
selectCopyProxyCommand := container.NewBorder(nil, nil, nil, nil,
NewWSelectWithCopyProxyCommand([]string{"git", "http/https", "ssh/sftp/scp"},
func(sel *widget.Select, value string) {
if value != "" {
sel.ClearSelected()
switch value {
case "git":
copyToClipboard(ProxyCommandGit, uiLocalAddr.Text, uiHttpLocalAddr.Text, w)
case "http/https":
copyToClipboard(ProxyCommandHttp, uiLocalAddr.Text, uiHttpLocalAddr.Text, w)
case "ssh/sftp/scp":
copyToClipboard(ProxyCommandSsh, uiLocalAddr.Text, uiHttpLocalAddr.Text, w)
}
}
},
),
)
w.SetContent(container.NewVBox(
widget.NewCard("Settings", "", tabs),
btnStart,
selectCopyProxyCommand,
&widget.Separator{},
container.NewGridWithColumns(2,
container.NewHBox(
NewHyperlinkIcon(resource.GithubIcon(), coreRepoUrl),
widget.NewHyperlink("wssocks core: ", coreRepoUrl),
),
widget.NewLabel("v"+version.VERSION),
),
container.NewGridWithColumns(2,
container.NewHBox(
NewHyperlinkIcon(resource.GithubIcon(), repoUrl),
widget.NewHyperlink("USTB vpn plugin: ", repoUrl),
),
container.NewGridWithColumns(2,
widget.NewLabel("v"+pluginversion.VERSION),
container.NewHBox(
layout.NewSpacer(),
widget.NewToolbar(
widget.NewToolbarAction(theme.HelpIcon(), func() {
if err := fyne.CurrentApp().OpenURL(docUrl); err != nil {
dialog.ShowError(fmt.Errorf("open link %s failed", docUrl), w)
}
}),
),
),
),
),
))
w.SetOnClosed(func() {
// todo close all and stop if network lost
if btnStatus == btnRunning { // running can stop
btnStatus = btnStopping
btnStart.SetText("Stopping")
handles.NotifyCloseWrapper()
}
saveBasicPreference(wssApp.Preferences(), uiLocalAddr, uiRemoteAddr, uiHttpLocalAddr, uiHttpEnable, uiSkipTSLVerify)
saveVPNPreference(wssApp.Preferences(), uiVpnEnable, uiVpnForceLogout,
uiVpnHostEncrypt, uiVpnHostInput, uiVpnUsername, uiVpnPassword)
})
//w.SetOnClosed() todo
w.ShowAndRun()
}
// NewWSelectWithCopyProxyCommand is copied from widget.NewSelect.
func NewWSelectWithCopyProxyCommand(options []string, changed func(sel *widget.Select, val string)) *widget.Select {
s := &widget.Select{
Options: options,
PlaceHolder: "(copy proxy command)",
}
s.OnChanged = func(val string) {
changed(s, val)
}
s.ExtendBaseWidget(s)
return s
}
func copyToClipboard(category int, socksAddr string, httpAddr string, win fyne.Window) {
var text = ""
var nc = "nc -x" // darwin or linux
if runtime.GOOS == "windows" {
nc = "connect -S"
}
switch category {
case ProxyCommandGit:
text = fmt.Sprintf("export GIT_SSH_COMMAND=\"ssh -o ProxyCommand='%s %s %%h %%p' \"", nc, socksAddr)
break
case ProxyCommandHttp:
text = fmt.Sprintf("export https_proxy=http://%s http_proxy=http://%s", socksAddr, httpAddr)
break
case ProxyCommandSsh:
text = fmt.Sprintf("ssh -o ProxyCommand='%s %s %%h %%p'", nc, socksAddr)
break
}
win.Clipboard().SetContent(text)
}