/
main.go
248 lines (227 loc) · 5.97 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
// 由res2go自动生成。
package main
import (
"fmt"
"github.com/ProtonMail/go-autostart"
"github.com/Unknwon/goconfig"
"github.com/getlantern/sysproxy"
"github.com/ying32/govcl/vcl"
"github.com/ying32/govcl/vcl/rtl"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strings"
)
var currentServerName string
var currentEditServerName string
var mainConfigName = "config.ini"
func main() {
vcl.Application.SetFormScaled(true)
vcl.Application.SetIconResId(3)
vcl.Application.Initialize()
vcl.Application.SetMainFormOnTaskBar(true)
vcl.Application.CreateForm(mainFormBytes, &MainForm)
vcl.Application.CreateForm(aboutFormBytes, &AboutForm)
vcl.Application.CreateForm(newProxyServerFormBytes, &NewProxyServerForm)
vcl.Application.CreateForm(settingFormBytes, &SettingForm)
initListView()
MainForm.SetCaption("ProxyGo-GUI")
if rtl.LcLLoaded() {
if runtime.GOOS != "windows" {
icon := vcl.NewIcon()
icon.LoadFromFile(rtl.ExtractFilePath(vcl.Application.ExeName()) + "/1.ico")
MainForm.TrayIcon1.SetIcon(icon)
icon.Free()
} else {
MainForm.TrayIcon1.SetIcon(vcl.Application.Icon())
}
}
MainForm.TrayIcon1.SetHint(MainForm.Caption())
MainForm.TrayIcon1.SetVisible(true)
// 捕捉最小化
vcl.Application.SetOnMinimize(func(sender vcl.IObject) {
if runtime.GOOS == "windows" {
MainForm.Hide() // 主窗口最小化掉
}
})
loadSysSetting()
vcl.Application.Run()
}
func initListView() {
b, err := PathExists(mainConfigName)
if err != nil {
panic("错误")
}
if !b {
WriteWithIoutil(mainConfigName, "[common]")
cfg, _ := goconfig.LoadConfigFile(mainConfigName)
cfg.SetValue("common", "version", "v1.0")
} else {
MainForm.ServerListView.Clear()
cfg, _ := goconfig.LoadConfigFile(mainConfigName)
list := cfg.GetSectionList()
MainForm.ServerListView.Items().BeginUpdate()
for idx := range list {
//fmt.Println(list[idx])
if list[idx] == "common" {
continue
}
lstitem := MainForm.ServerListView.Items().Add()
lstitem.SetCaption(list[idx])
lstitem.SubItems().Add(cfg.MustValue(list[idx], "name"))
lstitem.SubItems().Add(cfg.MustValue(list[idx], "SrcIp"))
lstitem.SubItems().Add(cfg.MustValue(list[idx], "SrcPort"))
lstitem.SubItems().Add(cfg.MustValue(list[idx], "SrcType"))
lstitem.SubItems().Add(cfg.MustValue(list[idx], "IsTls"))
lstitem.SubItems().Add(cfg.MustValue(list[idx], "LocalType"))
lstitem.SubItems().Add(cfg.MustValue(list[idx], "LocalPort"))
if cfg.MustBool(list[idx], "Status", false) {
lstitem.SubItems().Add("运行中")
} else {
lstitem.SubItems().Add("未启动")
}
}
MainForm.ServerListView.Items().EndUpdate()
}
if err != nil {
panic("错误")
}
}
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func WriteWithIoutil(name, content string) {
data := []byte(content)
if ioutil.WriteFile(name, data, 0644) == nil {
//fmt.Println("写入文件成功:",content)
}
}
func loadSysSetting() {
cfg, err := goconfig.LoadConfigFile(mainConfigName)
if err != nil {
panic("错误")
}
autoRun := cfg.MustBool("common", "autoRun", false)
sysProxy := cfg.MustBool("common", "sysProxy", false)
sysProxyAddr := cfg.MustValue("common", "sysProxyAddr", "")
fmt.Println(autoRun)
fmt.Println(sysProxy)
if autoRun {
setAutoRun()
} else {
unSetAutoRun()
}
if sysProxy {
setSysProxy(sysProxyAddr)
}
}
func setAutoRun() bool {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
return false
}
fmt.Println(dir)
split := strings.Split(os.Args[0], string(os.PathSeparator))
exeName := ""
if len(split) > 0 {
fmt.Println(split[len(split)-1])
exeName = split[len(split)-1]
} else {
exeName = os.Args[0]
}
fmt.Println(exeName)
var exec = dir + string(os.PathSeparator) + exeName
app := &autostart.App{
Name: "goproxy-gui",
DisplayName: "goproxy的图形界面",
Exec: []string{exec},
}
if app.IsEnabled() {
log.Println("App is already enabled, removing it...")
} else {
log.Println("Enabling app...")
if err := app.Enable(); err != nil {
log.Fatal(err)
return false
}
}
return true
}
func unSetAutoRun() bool {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
return false
}
fmt.Println(dir)
split := strings.Split(os.Args[0], string(os.PathSeparator))
exeName := ""
if len(split) > 0 {
fmt.Println(split[len(split)-1])
exeName = split[len(split)-1]
} else {
exeName = os.Args[0]
}
fmt.Println(exeName)
var exec = dir + string(os.PathSeparator) + exeName
app := &autostart.App{
Name: "goproxy-gui",
DisplayName: "goproxy的图形界面",
Exec: []string{exec},
}
if app.IsEnabled() {
log.Println("App is already enabled, removing it...")
if err := app.Disable(); err != nil {
log.Fatal(err)
return false
}
}
return true
}
func setSysProxy(addr string) bool {
helperFullPath := "sysproxy-cmd"
iconFullPath, _ := filepath.Abs("./icon.png")
log.Println("Using icon at %v", iconFullPath)
err := sysproxy.EnsureHelperToolPresent(helperFullPath, "Input your password and save the world!", iconFullPath)
if err != nil {
fmt.Printf("Error EnsureHelperToolPresent: %s\n", err)
return false
}
_, err1 := sysproxy.On(addr)
if err1 != nil {
fmt.Printf("Error set proxy: %s\n", err)
return false
}
return true
}
func unsetSysProxy(addr string) bool {
helperFullPath := "sysproxy-cmd"
iconFullPath, _ := filepath.Abs("./icon.png")
//log.Println("Using icon at %v", iconFullPath)
err := sysproxy.EnsureHelperToolPresent(helperFullPath, "Input your password and save the world!", iconFullPath)
if err != nil {
fmt.Printf("Error EnsureHelperToolPresent: %s\n", err)
return false
}
off, err1 := sysproxy.On(addr)
if err1 != nil {
fmt.Printf("Error set proxy: %s\n", err1)
return false
}
err2 := off()
if err2 != nil {
fmt.Printf("Error unset proxy: %s\n", err2)
return false
}
return true
}