/
start_app.go
232 lines (188 loc) · 5.65 KB
/
start_app.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
package main
import (
"context"
"encoding/base64"
"fmt"
"sort"
"fyne.io/fyne"
"fyne.io/fyne/app"
"fyne.io/fyne/container"
"fyne.io/fyne/widget"
"github.com/inverse-inc/packetfence/go/sharedutils"
"github.com/inverse-inc/wireguard-go/binutils"
"github.com/inverse-inc/wireguard-go/util"
"github.com/inverse-inc/wireguard-go/wgrpc"
"github.com/inverse-inc/wireguard-go/ztn"
)
var spacePlaceholder = " "
var statusLabel *widget.Label
var peersTableContainer *widget.Card
var peersTable = NewTable()
var restartBtn *widget.Button
var w fyne.Window
func Refresh() {
w.Content().Refresh()
}
func SetupAPIClientGUI(callback func(bool)) {
a := app.New()
icon, err := fyne.LoadResourceFromPath("logo.png")
if err != nil {
fmt.Println("Unable to find the app icon")
} else {
a.SetIcon(icon)
}
w = a.NewWindow(util.AppName)
Start(w, callback)
w.ShowAndRun()
}
func Start(w fyne.Window, callback func(bool)) {
var err error
tab1 := container.NewTabItem("Connection", widget.NewHBox())
tab2 := container.NewTabItem("Settings", widget.NewHBox())
tabs := container.NewAppTabs(tab1)
restartBtn = widget.NewButton("Restart", func() {
_, err := rpc.Stop(context.Background(), &wgrpc.StopRequest{})
if err != nil {
fmt.Println("Failed to stop the tunnel", err)
}
Start(w, callback)
})
restartBtn.Hide()
_, err = rpc.GetStatus(context.Background(), &wgrpc.StatusRequest{})
if err != nil {
tabs.Append(tab2)
PromptCredentials(tabs, callback)
} else {
PostConnect(tabs)
go checkTunnelStatus()
}
w.SetContent(tabs)
Refresh()
}
func PromptCredentials(tabs *container.AppTabs, callback func(bool)) {
connectionTab := tabs.Items[0]
settingsTab := tabs.Items[1]
serverEntry := widget.NewEntry()
serverEntry.PlaceHolder = "ztn.example.com"
serverEntry.Text = sharedutils.EnvOrDefault("WG_SERVER", "")
serverPortEntry := widget.NewEntry()
serverPortEntry.Text = sharedutils.EnvOrDefault("WG_SERVER_PORT", "9999")
verifyServerEntry := widget.NewCheck("Verify server identity", func(bool) {})
verifyServerEntry.Checked = (sharedutils.EnvOrDefault("WG_SERVER_VERIFY_TLS", "true") == "true")
usernameEntry := widget.NewEntry()
usernameEntry.PlaceHolder = spacePlaceholder
usernameEntry.Text = sharedutils.EnvOrDefault("WG_USERNAME", "")
formError := widget.NewLabelWithStyle("", fyne.TextAlignCenter, fyne.TextStyle{Bold: true})
formError.Hide()
passwordEntry := NewPasswordField()
passwordEntry.PlaceHolder = spacePlaceholder
installRoutesFromServerEntry := widget.NewCheck("Install routes from server", func(bool) {})
installRoutesFromServerEntry.Checked = true
preferedBindTechniqueEntry := widget.NewSelect([]string{
string(ztn.BindUPNPGID),
string(ztn.BindSTUN),
string(ztn.BindNATPMP),
string(ztn.BindThroughPeer),
}, func(string) {})
preferedBindTechniqueEntry.PlaceHolder = string(ztn.BindAutomatic)
connect := func() {
showFormError := func(msg string) {
formError.SetText(msg)
formError.Show()
}
if usernameEntry.Text == "" {
showFormError("You must enter a username")
return
}
if passwordEntry.Text == "" {
showFormError("You must enter a password")
return
}
if serverEntry.Text == "" {
showFormError("You must enter a server to connect to")
return
}
binutils.Setenv("WG_USERNAME", usernameEntry.Text)
binutils.Setenv("WG_PASSWORD", base64.StdEncoding.EncodeToString([]byte(passwordEntry.Text)))
binutils.Setenv("WG_SERVER", serverEntry.Text)
binutils.Setenv("WG_SERVER_PORT", serverPortEntry.Text)
verifySslStr := "y"
if !verifyServerEntry.Checked {
verifySslStr = "n"
}
binutils.Setenv("WG_SERVER_VERIFY_TLS", verifySslStr)
honorRoutesStr := "true"
if !installRoutesFromServerEntry.Checked {
honorRoutesStr = "false"
}
binutils.Setenv("WG_HONOR_ROUTES", honorRoutesStr)
if preferedBindTechniqueEntry.Selected != string(ztn.BindAutomatic) {
binutils.Setenv("WG_BIND_TECHNIQUE", preferedBindTechniqueEntry.Selected)
}
PostConnect(tabs)
callback(true)
}
passwordEntry.onEnter = connect
connectionTab.Content = widget.NewVBox(
formError,
widget.NewHBox(
widget.NewLabel("Server"),
serverEntry,
),
widget.NewHBox(
widget.NewLabel("Server port"),
serverPortEntry,
),
widget.NewHBox(
verifyServerEntry,
),
widget.NewHBox(
widget.NewLabel("Username"),
usernameEntry,
),
widget.NewHBox(
widget.NewLabel("Password"),
passwordEntry,
),
widget.NewButton("Connect", connect),
)
settingsTab.Content = widget.NewVBox(
widget.NewHBox(
installRoutesFromServerEntry,
),
widget.NewHBox(
widget.NewLabel("Bind technique"),
preferedBindTechniqueEntry,
),
)
Refresh()
}
func PostConnect(tabs *container.AppTabs) {
statusLabel = widget.NewLabel("Opening tunnel process")
statusLabel.Wrapping = fyne.TextWrapWord
peersTableContainer = widget.NewCard("Peers", "", widget.NewVBox())
tabs.Items[0].Content = widget.NewVBox(statusLabel, restartBtn, peersTableContainer)
if len(tabs.Items) > 1 {
tabs.RemoveIndex(1)
}
Refresh()
}
func UpdatePeers(ctx context.Context, rpc wgrpc.WGServiceClient) {
peers, err := rpc.GetPeers(ctx, &wgrpc.PeersRequest{})
if err != nil {
peersTableContainer.SetContent(widget.NewLabel("Failed to obtain peers from local wireguard server: " + err.Error()))
return
}
sort.Slice(peers.Peers, func(i, j int) bool {
return peers.Peers[i].Hostname < peers.Peers[j].Hostname
})
peersInfos := [][]string{}
for _, peer := range peers.Peers {
peersInfos = append(peersInfos, []string{peer.Hostname, peer.IpAddress, peer.Status})
}
peersTable.Update(
[]string{"Host", "IP address", "State"},
peersInfos,
)
peersTableContainer.SetContent(peersTable.GetContainer())
}