-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyberghostvpn-gui.go
204 lines (176 loc) · 5.39 KB
/
cyberghostvpn-gui.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
package main
import (
"fmt"
"image/color"
"log"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"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"
"github.com/carlos-el/cyberghostvpn-gui/commander"
"github.com/carlos-el/cyberghostvpn-gui/components"
"github.com/carlos-el/cyberghostvpn-gui/debounce"
"github.com/carlos-el/cyberghostvpn-gui/models"
)
func filterCountries(countries *[]models.Country, filter string) []models.Country {
var result = []models.Country{}
for _, country := range *countries {
if strings.Contains(strings.ToLower(country.String()), strings.ToLower(filter)) {
result = append(result, country)
}
}
return result
}
func applyFilterToServerList(
inputList *[]models.Country,
outputList *[]models.Country,
list *widget.List,
filter string,
) {
*outputList = filterCountries(inputList, filter)
list.Refresh()
}
func disconnect(loader *dialog.CustomDialog) error {
loader.Show()
defer loader.Hide()
err := commander.Disconnect()
if err != nil {
return fmt.Errorf("could not disconnect: %w", err)
}
return nil
}
func connect(
status *components.ConnectionStatus,
loader *dialog.CustomDialog,
c *models.Country,
vpnOptionFunc func() commander.VpnProtocol,
transOption func() commander.TransmissionProtocol,
srvOption func() commander.ServiceType,
) error {
loader.Show()
defer loader.Hide()
server, err := commander.Connect(c, vpnOptionFunc(), transOption(), srvOption())
if err != nil {
return fmt.Errorf("could not connect: %w", err)
}
status.SetConnected(c, server)
return nil
}
func GetServerList(serviceType commander.ServiceType, errorDialog *components.ErrorDialog, loader *dialog.CustomDialog) []models.Country {
loader.Show()
defer loader.Hide()
var countries, err = commander.GetCountryList(serviceType)
if err != nil {
errorDialog.Show(err)
}
return countries
}
func main() {
a := app.New()
myWindow := a.NewWindow("CyberGhost GUI")
myWindow.Resize(fyne.NewSize(400, 600))
// Error dialog
errorDialog := components.NewErrorDialog(&a, &myWindow)
// Loader dialog to block interaction
loader := dialog.NewCustomWithoutButtons("", widget.NewLabel("Loading..."), myWindow)
// Status component
connectionStatusComponent := components.NewConnectionStatus(
func() { disconnect(loader) },
)
// Connection options selectors
connectionOptionsComponent := components.NewConnectionOptions()
// Service type selector
serviceTypeOptions := []string{
commander.Traffic.String(),
commander.Torrent.String(),
commander.Streaming.String(),
}
inputServiceType := widget.NewSelect(serviceTypeOptions, nil)
inputServiceType.SetSelected(serviceTypeOptions[0])
// Search selector
inputSearch := components.NewInputSearch()
// Server list array
fullServerList := GetServerList(commander.ServiceType(inputServiceType.SelectedIndex()+1), errorDialog, loader)
filteredServerList := fullServerList
// Service list component
list := widget.NewList(
func() int {
return len(filteredServerList)
},
func() fyne.CanvasObject {
b := widget.NewButtonWithIcon("", theme.LoginIcon(), nil)
return container.NewHBox(widget.NewLabel("template"), layout.NewSpacer(), b)
},
func(i widget.ListItemID, o fyne.CanvasObject) {
country := filteredServerList[i]
o.(*fyne.Container).Objects[0].(*widget.Label).SetText(country.String())
o.(*fyne.Container).Objects[2].(*widget.Button).OnTapped = func() {
errCon := connect(
connectionStatusComponent,
loader,
&country,
connectionOptionsComponent.GetVpnOption,
connectionOptionsComponent.GetTransmissionOption,
func() commander.ServiceType {
return commander.ServiceType(inputServiceType.SelectedIndex() + 1)
},
)
if errCon != nil {
errorDialog.Show(errCon)
}
}
},
)
// Update service list on service type change
inputServiceType.OnChanged = func(s string) {
log.Print("Changed selected service type: ", s)
fullServerList = GetServerList(commander.ServiceType(inputServiceType.SelectedIndex()+1), errorDialog, loader)
applyFilterToServerList(&fullServerList, &filteredServerList, list, inputSearch.GetInputText())
list.Refresh()
}
// Update service list on search input
debounced, _ := debounce.NewDebounce(300*time.Millisecond, func() {
applyFilterToServerList(&fullServerList, &filteredServerList, list, inputSearch.GetInputText())
})
inputSearch.SetOnChanged(func(s string) {
debounced()
})
// List component
listTitle := widget.NewRichTextFromMarkdown("### Select a server to connect to: ")
serviceListComponent := container.NewBorder(
container.NewVBox(listTitle, inputServiceType, inputSearch.Container), // Top
nil, // Bottom
nil, // Left
nil, // Right
list, // Rest
)
// Check if connection to server already in place
isConnected, err := commander.CheckConnection()
if err != nil {
errorDialog.Show(err)
}
if isConnected {
connectionStatusComponent.SetConnected(nil, "Unknown")
}
// Main component
mainComp := container.NewBorder(
container.NewVBox(
connectionStatusComponent.Container,
canvas.NewText("", color.White),
connectionOptionsComponent.Container,
canvas.NewText("", color.White),
), // Top
nil, // Bottom
nil, // Left
nil, // Right
serviceListComponent, // Rest
)
myWindow.SetContent(mainComp)
myWindow.ShowAndRun()
}