-
Notifications
You must be signed in to change notification settings - Fork 33
/
MainFormImpl.go
465 lines (396 loc) · 11.3 KB
/
MainFormImpl.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// +build gui
package main
import (
"fmt"
"math/rand"
"runtime"
"strings"
"time"
"errors"
_ "github.com/ying32/govcl/pkgs/winappres"
"github.com/ying32/govcl/vcl"
"github.com/ying32/govcl/vcl/rtl"
"github.com/ying32/govcl/vcl/types"
"github.com/ying32/govcl/vcl/types/colors"
"github.com/ying32/rproxy/librp"
)
const (
randStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
SERVER = 1
CLIENT = 0
)
var CheckConfigError = errors.New("检查配置错误")
//::private::
type TMainFormFields struct {
rpObj librp.IRPObject
rpConfigFileName string
running bool
autoReboot bool
appCfg *vcl.TIniFile
modeIndex int32
//isDarwin bool
rpConfigLoaded bool
}
func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
rand.Seed(time.Now().Unix())
//f.isDarwin = runtime.GOOS == "darwin"
// 都是坑
if runtime.GOOS == "linux" {
f.SpinMaxLogLine.SetAutoSize(true)
f.SpinCliHTTPPort.SetAutoSize(true)
f.SpinSvrHTTPPort.SetAutoSize(true)
f.SpinTCPPort.SetAutoSize(true)
}
f.ScreenCenter()
f.PageControl1.SetActivePageIndex(0)
f.PageControl2.SetActivePageIndex(0)
f.running = false
f.appCfg = vcl.NewIniFile(rtl.ExtractFilePath(vcl.Application.ExeName()) + "app.conf")
if runtime.GOOS != "windows" {
loadMainIconFromStream(f.TrayIcon1.Icon())
}
librp.IsGUI = true
librp.LogGUICallback = f.logCallback
f.loadAppConfig()
}
func (f *TMainForm) OnFormCloseQuery(sender vcl.IObject, canClose *bool) {
if f.running {
vcl.MessageDlg("当前服务正在运行中,请停止后再退出。", types.MtWarning, types.MbOK)
*canClose = false
}
}
func loadMainIconFromStream(outIcon *vcl.TIcon) {
if outIcon.IsValid() {
mem := vcl.NewMemoryStreamFromBytes(mainIconBytes)
defer mem.Free()
mem.SetPosition(0)
outIcon.LoadFromStream(mem)
}
}
func (f *TMainForm) OnFormDestroy(sender vcl.IObject) {
if f.appCfg != nil {
f.appCfg.Free()
}
if f.rpObj != nil {
f.rpObj.Close()
}
}
func (f *TMainForm) loadAppConfig() {
f.rpConfigFileName = f.appCfg.ReadString("System", "RPConfigFileName", "")
cfg := new(librp.TRProxyConfig)
err := librp.LoadConfig(f.rpConfigFileName, cfg)
if err == nil {
librp.SetConfig(cfg)
f.updateUIConfig(cfg)
f.rpConfigLoaded = true
librp.Log.I("已加载RP配置:" + f.rpConfigFileName)
}
f.autoReboot = f.appCfg.ReadBool("System", "AutoReboot", true)
f.ChkAutoReconnect.SetChecked(f.autoReboot)
f.modeIndex = f.appCfg.ReadInteger("System", "ModeIndex", 0)
f.setRPMode(f.modeIndex)
f.SpinMaxLogLine.SetValue(f.appCfg.ReadInteger("System", "MaxLogLine", 5000))
f.updateCaption()
}
func (f *TMainForm) logCallback(msg string) {
vcl.ThreadSync(func() {
if f.LstLogs.Count() > f.SpinMaxLogLine.Value() {
f.LstLogs.Clear()
}
f.LstLogs.Items().Add(msg)
})
}
// 下面两个函数用于解决TRadioGroup在MacOS下bug问题
func (f *TMainForm) getRPMode() int32 {
return f.RGMode.ItemIndex()
}
func (f *TMainForm) setRPMode(idx int32) {
f.RGMode.SetItemIndex(idx)
}
func (f *TMainForm) setTrayHint(text string) {
//if f.isDarwin {
// // darwin下出bug
// return
//}
f.TrayIcon1.SetHint(text)
}
func (f *TMainForm) OnBtnRandKeyClick(sender vcl.IObject) {
var randKey []byte
for i := 0; i < 16; i++ {
randKey = append(randKey, randStr[rand.Intn(len(randStr))])
}
f.EditVerifyKey.SetText(string(randKey))
}
func (f *TMainForm) OnBtnCAOpenClick(sender vcl.IObject) {
if f.DlgOpen.Execute() {
f.EditTLSCAFile.SetText(f.DlgOpen.FileName())
}
}
func (f *TMainForm) OnBtnSaveCfgClick(sender vcl.IObject) {
f.saveUIConfig()
}
func (f *TMainForm) updateUIConfig(cfg *librp.TRProxyConfig) {
f.SpinTCPPort.SetValue(int32(cfg.TCPPort))
f.EditVerifyKey.SetText(cfg.VerifyKey)
f.ChkIsZip.SetChecked(cfg.IsZIP)
f.ChkIsHttps.SetChecked(cfg.IsHTTPS)
f.EditTLSCAFile.SetText(cfg.TLSCAFile)
// client
f.SpinCliHTTPPort.SetValue(int32(cfg.Client.HTTPPort))
f.EditSvrAddr.SetText(cfg.Client.SvrAddr)
f.EditTLSCliCertFile.SetText(cfg.Client.TLSCertFile)
f.EditTLSCliKeyFile.SetText(cfg.Client.TLSKeyFile)
// server
f.SpinSvrHTTPPort.SetValue(int32(cfg.Server.HTTPPort))
f.EditTLSSvrCertFile.SetText(cfg.Server.TLSCertFile)
f.EditTLSSvrKeyFile.SetText(cfg.Server.TLSKeyFile)
}
func (f *TMainForm) checkConfig() error {
if f.EditVerifyKey.Text() == "" {
f.EditVerifyKey.SetFocus()
vcl.ShowMessage("必须输入一个验证的key")
return CheckConfigError
}
if f.EditSvrAddr.Text() == "" {
f.EditSvrAddr.SetFocus()
vcl.ShowMessage("要连接的服务器地址不能为空")
return CheckConfigError
}
if f.ChkIsHttps.Checked() {
if f.EditTLSCliCertFile.Text() == "" || f.EditTLSCliKeyFile.Text() == "" {
vcl.ShowMessage("当为HTTPS时,客户端TLS证书不能为空。")
return CheckConfigError
}
if f.EditTLSSvrCertFile.Text() == "" || f.EditTLSSvrKeyFile.Text() == "" {
vcl.ShowMessage("当为HTTPS时,服务端TLS证书不能为空。")
return CheckConfigError
}
}
return nil
}
func (f *TMainForm) saveUIConfig() {
if f.checkConfig() != nil {
return
}
cfg := new(librp.TRProxyConfig)
cfg.TCPPort = int(f.SpinTCPPort.Value())
cfg.VerifyKey = f.EditVerifyKey.Text()
cfg.IsZIP = f.ChkIsZip.Checked()
cfg.IsHTTPS = f.ChkIsHttps.Checked()
cfg.TLSCAFile = f.EditTLSCAFile.Text()
// client
cfg.Client.HTTPPort = int(f.SpinCliHTTPPort.Value())
cfg.Client.SvrAddr = f.EditSvrAddr.Text()
cfg.Client.TLSCertFile = f.EditTLSCliCertFile.Text()
cfg.Client.TLSKeyFile = f.EditTLSCliKeyFile.Text()
// server
cfg.Server.HTTPPort = int(f.SpinSvrHTTPPort.Value())
cfg.Server.TLSCertFile = f.EditTLSSvrCertFile.Text()
cfg.Server.TLSKeyFile = f.EditTLSSvrKeyFile.Text()
if !rtl.FileExists(f.rpConfigFileName) {
if f.DlgSaveCfg.Execute() {
f.rpConfigFileName = f.DlgSaveCfg.FileName()
} else {
librp.Log.I("取消保存RP配置")
return
}
}
librp.SetConfig(cfg)
err := librp.SaveConfig(f.rpConfigFileName, cfg)
if err != nil {
librp.Log.I("P配置保存失败")
return
}
f.rpConfigLoaded = true
librp.Log.I("RP配置已保存:" + f.rpConfigFileName)
}
func (f *TMainForm) OnBtnLoadCfgClick(sender vcl.IObject) {
if f.DlgOpen.Execute() {
cfg := new(librp.TRProxyConfig)
err := librp.LoadConfig(f.DlgOpen.FileName(), cfg)
if err != nil {
vcl.ShowMessage("载入RP配置失败:" + err.Error())
} else {
librp.SetConfig(cfg)
f.rpConfigFileName = f.DlgOpen.FileName()
f.updateUIConfig(cfg)
// 载入即保存下当前的文件名
f.appCfg.WriteString("System", "RPConfigFileName", f.rpConfigFileName)
f.rpConfigLoaded = true
librp.Log.I("已加载RP配置:" + f.rpConfigFileName)
}
}
}
func (f *TMainForm) OnChkAutoReconnectClick(sender vcl.IObject) {
f.autoReboot = f.ChkAutoReconnect.Checked()
f.appCfg.WriteBool("System", "AutoReboot", f.autoReboot)
}
func (f *TMainForm) runSvr() {
switch f.modeIndex {
case CLIENT:
librp.Log.I("连接服务端...")
for f.running {
err := f.rpObj.Start()
if err != nil {
librp.Log.E("连接服务器错误:", err)
librp.Log.I("5秒后重新连接...")
for i := 0; i < 5; i++ {
if !f.running {
break
}
time.Sleep(time.Second * 1)
}
if !f.autoReboot {
break
}
}
}
case SERVER:
if f.running {
err := f.rpObj.Start()
if err != nil {
librp.Log.E("启动服务端错误:", err)
}
}
}
librp.Log.I("已停止")
vcl.ThreadSync(func() {
f.BtnStop.Click()
})
}
func (f *TMainForm) OnActStartExecute(sender vcl.IObject) {
switch f.modeIndex {
case CLIENT:
f.rpObj = librp.NewRPClient()
case SERVER:
f.rpObj = librp.NewRPServer()
}
if f.rpObj == nil {
vcl.ShowMessage("rproxy对象创建失败。")
return
}
f.running = true
f.LstLogs.Clear()
str := f.Caption() + "\r\n"
switch f.modeIndex {
case CLIENT:
s := fmt.Sprintln("客户端启动,连接服务器:", f.EditSvrAddr.Text(), ", 端口:", f.SpinTCPPort.Value())
str += s
librp.Log.I(s)
if f.ChkIsHttps.Checked() {
s = "转发至HTTP服务为HTTPS"
str += s + "\r\n"
librp.Log.I(s)
}
s = fmt.Sprintln("转发至本地HTTP(S)端口:", f.SpinCliHTTPPort.Value())
str += s
librp.Log.I(s)
case SERVER:
s := fmt.Sprintln("TCP服务端已启动,端口:", f.SpinTCPPort.Value())
str += s
librp.Log.I(s)
if f.ChkIsHttps.Checked() {
s = "当前HTTP服务为HTTPS"
str += s + "\r\n"
librp.Log.I(s)
}
s = fmt.Sprintln("HTTP(S)服务端已开启,端口:", f.SpinSvrHTTPPort.Value())
str += s
librp.Log.I(s)
}
f.setTrayHint(str)
go f.runSvr()
f.setControlState(false)
}
func (f *TMainForm) setControlState(state bool) {
f.ChkIsHttps.SetEnabled(state)
f.RGMode.SetEnabled(state)
var i int32
for i = 0; i < f.GBBase.ControlCount(); i++ {
f.GBBase.Controls(i).SetEnabled(state)
}
for i = 0; i < f.GBTLS.ControlCount(); i++ {
f.GBTLS.Controls(i).SetEnabled(state)
}
for i = 0; i < f.GBAppSettings.ControlCount(); i++ {
f.GBAppSettings.Controls(i).SetEnabled(state)
}
}
func (f *TMainForm) OnActStartUpdate(sender vcl.IObject) {
vcl.AsAction(sender).SetEnabled(!f.running && f.rpConfigLoaded)
}
func (f *TMainForm) OnActStopExecute(sender vcl.IObject) {
f.running = false
f.setControlState(true)
if f.rpObj != nil {
f.rpObj.Close()
}
f.rpObj = nil
f.updateCaption()
}
func (f *TMainForm) OnActStopUpdate(sender vcl.IObject) {
vcl.AsAction(sender).SetEnabled(f.running && f.rpConfigLoaded)
}
func (f *TMainForm) OnBtnNewCfgClick(sender vcl.IObject) {
librp.Log.I("新建配置")
f.rpConfigFileName = ""
f.rpConfigLoaded = false
}
func (f *TMainForm) updateCaption() {
//if f.isDarwin {
// // 唉macOS下liblcl.dylib有内部bug
// m := []string{"客户端", "服务端"}
// f.SetCaption("rproxy[" + m[f.modeIndex] + "]")
//} else {
f.SetCaption("rproxy[" + f.RGMode.Items().S(f.modeIndex) + "]")
//}
f.setTrayHint(f.Caption())
}
func (f *TMainForm) OnRGModeClick(sender vcl.IObject) {
f.modeIndex = f.getRPMode()
f.appCfg.WriteInteger("System", "ModeIndex", f.modeIndex)
f.updateCaption()
}
func (f *TMainForm) OnLstLogsDrawItem(control vcl.IWinControl, index int32, aRect types.TRect, state types.TOwnerDrawState) {
canvas := f.LstLogs.Canvas()
canvas.Font().SetColor(colors.ClBlack)
s := f.LstLogs.Items().S(index)
if strings.HasPrefix(s, "[ERROR]") {
canvas.Font().SetColor(colors.ClRed)
} else if strings.HasPrefix(s, "[DEBUG]") {
canvas.Font().SetColor(colors.ClGreen)
} else if strings.HasPrefix(s, "[WARNING]") {
canvas.Font().SetColor(colors.ClYellow)
}
canvas.Brush().SetColor(colors.ClWhite)
canvas.FillRect(aRect)
if state.In(types.OdSelected) {
canvas.Font().SetColor(colors.ClBlue)
}
canvas.TextOut(aRect.Left, aRect.Top, s)
}
func (f *TMainForm) OnBtnCliCertOpenClick(sender vcl.IObject) {
if f.DlgOpen.Execute() {
f.EditTLSCliCertFile.SetText(f.DlgOpen.FileName())
}
}
func (f *TMainForm) OnBtnCliKeyOpenClick(sender vcl.IObject) {
if f.DlgOpen.Execute() {
f.EditTLSCliKeyFile.SetText(f.DlgOpen.FileName())
}
}
func (f *TMainForm) OnBtnSvrCertOpenClick(sender vcl.IObject) {
if f.DlgOpen.Execute() {
f.EditTLSSvrCertFile.SetText(f.DlgOpen.FileName())
}
}
func (f *TMainForm) OnBtnSvrKeyOpenClick(sender vcl.IObject) {
if f.DlgOpen.Execute() {
f.EditTLSSvrKeyFile.SetText(f.DlgOpen.FileName())
}
}
func (f *TMainForm) OnSpinMaxLogLineChange(sender vcl.IObject) {
f.appCfg.WriteInteger("System", "MaxLogLine", f.SpinMaxLogLine.Value())
}
func (f *TMainForm) OnPageControl1Change(sender vcl.IObject) {
}