-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
312 lines (243 loc) · 9.02 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
306
307
308
309
310
311
312
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/juju/loggo"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"github.com/godbus/dbus"
"github.com/gosexy/gettext"
"github.com/gotk3/gotk3/gtk"
"github.com/okelet/goutils"
"github.com/okelet/proxychanger/proxychangerlib"
"github.com/olekukonko/tablewriter"
)
var Version = "master"
func main() {
var err error
err = proxychangerlib.InitConstants()
if err != nil {
fmt.Printf("Error during initialization: %v\n", err)
goutils.ShowZenityError("Error", fmt.Sprintf("Error initializing application: %v.", err))
os.Exit(1)
}
gettext.BindTextdomain(proxychangerlib.GETTEXT_DOMAIN, proxychangerlib.LOCALE_DIR)
gettext.Textdomain(proxychangerlib.GETTEXT_DOMAIN)
gettext.SetLocale(gettext.LcAll, "")
sessionBus, err := dbus.SessionBus()
if err != nil {
fmt.Println(proxychangerlib.MyGettextv("Error getting D-Bus session connection: %v.", err))
goutils.ShowZenityError(proxychangerlib.MyGettextv("Error"), proxychangerlib.MyGettextv("Error getting D-Bus session connection: %v.", err))
os.Exit(1)
}
app := kingpin.New("proxychanger", proxychangerlib.MyGettextv("Changes proxy settings in multiple applications")).Version(Version)
app.HelpFlag.Short('h')
app.VersionFlag.Short('v')
logLevel := app.Flag("log-level", proxychangerlib.MyGettextv("Log level")).Short('l').String()
logErrorOutput := app.Flag("error-output", proxychangerlib.MyGettextv("Log to error output")).Bool()
configFile := app.Flag("config", proxychangerlib.MyGettextv("Configuration file")).Short('c').ExistingFile()
indicatorCommand := app.Command("indicator", proxychangerlib.MyGettextv("Start as an indicator"))
testMode := indicatorCommand.Flag("test", proxychangerlib.MyGettextv("Test mode")).Short('t').Bool()
indicatorCommand.Default()
listCommand := app.Command("list", proxychangerlib.MyGettextv("List proxies"))
// TODO: output format
includePasswords := listCommand.Flag("include-passwords", proxychangerlib.MyGettextv("Include passwords")).Bool()
applyActiveCommand := app.Command("apply", proxychangerlib.MyGettextv("Apply current current active proxy"))
getActiveCommand := app.Command("get", proxychangerlib.MyGettextv("Get current active proxy slug; returns empty if no active proxy"))
setActiveCommand := app.Command("set", proxychangerlib.MyGettextv("Set active proxy"))
setActiveCommandSlug := setActiveCommand.Arg("slug", proxychangerlib.MyGettextv("New active proxy slug; use 'none' to unset the proxy")).Required().String()
// TODO: add command
// TODO: edit command
// TODO: delete command
kingpin.MustParse(app.Parse(os.Args[1:]))
cmdLogLevelSet := false
if *logLevel != "" {
level, ok := loggo.ParseLevel(*logLevel)
if !ok {
fmt.Println(proxychangerlib.MyGettextv("Invalid LOG level: %v.", *logLevel))
goutils.ShowZenityError(proxychangerlib.MyGettextv("Error"), proxychangerlib.MyGettextv("Invalid LOG level: %v.", *logLevel))
os.Exit(1)
}
proxychangerlib.Log.SetLogLevel(level)
cmdLogLevelSet = true
}
if *logErrorOutput {
proxychangerlib.AddErrorOutputLogging()
}
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case indicatorCommand.FullCommand():
os.Exit(runIndicator(sessionBus, *configFile, cmdLogLevelSet, *testMode))
case listCommand.FullCommand():
listProxies(sessionBus, *configFile, cmdLogLevelSet, *includePasswords)
case applyActiveCommand.FullCommand():
applyActiveProxyBySlug(sessionBus, *configFile, cmdLogLevelSet)
case getActiveCommand.FullCommand():
getActiveProxyBySlug(sessionBus, *configFile, cmdLogLevelSet)
case setActiveCommand.FullCommand():
setActiveProxyBySlug(sessionBus, *setActiveCommandSlug, *configFile, cmdLogLevelSet)
}
}
func runIndicator(sessionBus *dbus.Conn, configFile string, cmdLogLevelSet bool, testMode bool) int {
config, err := proxychangerlib.NewConfig(configFile, false)
if err != nil {
fmt.Println(proxychangerlib.MyGettextv("Error loading configuration: %v.", err))
goutils.ShowZenityError(proxychangerlib.MyGettextv("Error"), proxychangerlib.MyGettextv("Error loading configuration: %v.", err))
return 1
}
err = config.Lock(sessionBus)
if err == proxychangerlib.ApplicationAlreadyRunningError {
fmt.Println(proxychangerlib.MyGettextv("Application already running."))
goutils.ShowZenityError(proxychangerlib.MyGettextv("Error"), proxychangerlib.MyGettextv("Application already running."))
return 1
} else if err != nil {
fmt.Println(proxychangerlib.MyGettextv("Error locking configuration: %v.", err))
goutils.ShowZenityError(proxychangerlib.MyGettextv("Error"), proxychangerlib.MyGettextv("Error locking configuration: %v.", err))
return 1
}
if !cmdLogLevelSet {
proxychangerlib.Log.SetLogLevel(config.LogLevel)
}
gtk.Init(nil)
i, err := proxychangerlib.NewIndicator(sessionBus, config, Version, cmdLogLevelSet, testMode)
if err != nil {
fmt.Println(proxychangerlib.MyGettextv("Error creating indicator: %v.", err))
goutils.ShowZenityError(proxychangerlib.MyGettextv("Error"), proxychangerlib.MyGettextv("Error creating indicator: %v.", err))
return 1
}
err = i.Run(true)
if err == nil {
gtk.Main()
return 0
} else {
fmt.Println("Error running indicator", err)
return 1
}
}
func getConfigService(dbusConnection *dbus.Conn, configFile string, cmdLogLevelSet bool) (proxychangerlib.ConfigService, error) {
// Get the current list of names to check if already running
var names []string
err := dbusConnection.BusObject().Call("org.freedesktop.DBus.ListNames", 0).Store(&names)
if err != nil {
return nil, err
}
if goutils.ListContainsString(names, proxychangerlib.DBUS_INTERFACE) {
// Already running, connect using dbus
return proxychangerlib.NewConfigDbus(dbusConnection)
} else {
// Not running, create a new configuration and lock it
config, err := proxychangerlib.NewConfig(configFile, false)
if err != nil {
return nil, err
}
err = config.Lock(dbusConnection)
if err != nil {
return nil, err
}
if !cmdLogLevelSet {
proxychangerlib.Log.SetLogLevel(config.LogLevel)
}
return config, nil
}
}
func listProxies(dbusConnection *dbus.Conn, configFile string, cmdLogLevelSet bool, includePasswords bool) int {
c, err := getConfigService(dbusConnection, configFile, cmdLogLevelSet)
if err != nil {
fmt.Println("Error", err)
return 1
}
responseData, err := c.ListProxies(includePasswords)
var response proxychangerlib.ListProxiesResponse
err = json.Unmarshal([]byte(responseData), &response)
if err != nil {
panic(err)
}
if response.Error != "" {
fmt.Println(proxychangerlib.MyGettextv("Error listing proxies: %v.", response.Error))
return 1
}
table := tablewriter.NewWriter(os.Stdout)
header := []string{
proxychangerlib.MyGettextv("Active"),
proxychangerlib.MyGettextv("Slug"),
proxychangerlib.MyGettextv("Name"),
proxychangerlib.MyGettextv("Protocol"),
proxychangerlib.MyGettextv("Address"),
proxychangerlib.MyGettextv("Username"),
}
if includePasswords {
header = append(header, proxychangerlib.MyGettextv("Password"))
}
table.SetHeader(header)
for _, v := range response.Proxies {
row := []string{
map[bool]string{true: proxychangerlib.MyGettextv("Yes"), false: proxychangerlib.MyGettextv("No")}[v.Active],
v.Slug,
v.Name,
v.Protocol,
v.Address,
v.Username,
}
if includePasswords {
row = append(row, v.Password)
}
table.Append(row)
}
table.Render()
return 0
}
func applyActiveProxyBySlug(dbusConnection *dbus.Conn, configFile string, cmdLogLevelSet bool) int {
c, err := getConfigService(dbusConnection, configFile, cmdLogLevelSet)
if err != nil {
fmt.Println("Error", err)
return 1
}
responseData, err := c.ApplyActiveProxy()
var response proxychangerlib.ApplyActiveProxyResponse
err = json.Unmarshal([]byte(responseData), &response)
if err != nil {
panic(err)
}
if response.Error != "" {
fmt.Println(proxychangerlib.MyGettextv("Error applying active proxy: %v.", response.Error))
return 1
}
return 0
}
func getActiveProxyBySlug(dbusConnection *dbus.Conn, configFile string, cmdLogLevelSet bool) int {
c, err := getConfigService(dbusConnection, configFile, cmdLogLevelSet)
if err != nil {
fmt.Println("Error", err)
return 1
}
responseData, err := c.GetActiveProxySlug()
var response proxychangerlib.GetActiveProxySlugResponse
err = json.Unmarshal([]byte(responseData), &response)
if err != nil {
panic(err)
}
if response.Error != "" {
fmt.Println(proxychangerlib.MyGettextv("Error setting active proxy: %v.", response.Error))
return 1
} else {
fmt.Println(response.Slug)
}
return 0
}
func setActiveProxyBySlug(dbusConnection *dbus.Conn, slug string, configFile string, cmdLogLevelSet bool) int {
c, err := getConfigService(dbusConnection, configFile, cmdLogLevelSet)
if err != nil {
fmt.Println("Error", err)
return 1
}
responseData, err := c.SetActiveProxyBySlug(slug)
var response proxychangerlib.SetActiveProxyBySlugResponse
err = json.Unmarshal([]byte(responseData), &response)
if err != nil {
panic(err)
}
if response.Error != "" {
fmt.Println(proxychangerlib.MyGettextv("Error setting active proxy: %v.", response.Error))
return 1
}
return 0
}