-
Notifications
You must be signed in to change notification settings - Fork 1
/
fyneGui.go
340 lines (319 loc) · 10.4 KB
/
fyneGui.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/gonutz/w32/v2"
"github.com/Rldeckard/aesGenerate256/authGen"
"github.com/Rldeckard/sshRunCMD/dialSSHClient"
"github.com/spf13/viper"
"github.com/zenthangplus/goccm"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)
// Windows doesn't allow console and Gui apps in one anymore.
// Dont specify the windowsgui flag and use this to maintain CLI functionality with Fyne
func hideConsole() {
console := w32.GetConsoleWindow()
if console == 0 {
return // no console attached
}
// If this application is the process that created the console window, then
// this program was not compiled with the -H=windowsgui flag and on start-up
// it created a console along with the main application window. In this case
// hide the console window.
// See
// http://stackoverflow.com/questions/9009333/how-to-check-if-the-program-is-run-from-a-console
_, consoleProcID := w32.GetWindowThreadProcessId(console)
if w32.GetCurrentProcessId() == consoleProcID {
w32.ShowWindowAsync(console, w32.SW_HIDE)
}
}
func (cred *CRED) guiApp() {
// Windows doesn't allow console and Gui apps in one anymore.
// Dont specify the windowsgui flag and use this to maintain CLI functionality with Fyne
hideConsole()
myApp := app.New()
myWindow = myApp.NewWindow("sshRunCMD")
myWindow.Resize(fyne.NewSize(950, 500))
// Main menu
formUser := widget.NewEntry()
formPass := widget.NewPasswordEntry()
formDrop := widget.NewSelect([]string{"Active Directory", "Local Account"}, func(value string) {
if value == "Active Directory" {
formUser.Text = cred.username
}
if value == "Local Account" {
formUser.Text = cred.fallbackUser
}
formUser.Refresh()
})
formDrop.SetSelectedIndex(0)
loginFormItems := []*widget.FormItem{
{Text: "", Widget: formDrop},
{Text: "Username", Widget: formUser},
{Text: "Password", Widget: formPass},
}
fileMenu := fyne.NewMenu("File",
fyne.NewMenuItem("Manage Credentials", func() {
formUser.Text = cred.username
formPopUp := dialog.NewForm("Manage", "Update", "Cancel", loginFormItems, func(ok bool) {
if ok {
if formDrop.Selected == "Active Directory" {
cred.username = strings.Trim(formUser.Text, " ")
cred.password = strings.Trim(formPass.Text, " ")
//TODO: Generate appCode and save to file if not present.
if appCode != "" {
viper.Set("helper.username", aes256.Encrypt(appCode, cred.username))
viper.Set("helper.password", aes256.Encrypt(appCode, cred.password))
}
} else {
cred.fallbackUser = strings.Trim(formUser.Text, " ")
cred.fallbackPass = strings.Trim(formPass.Text, " ")
if appCode != "" {
viper.Set("helper.fallbackUser", aes256.Encrypt(appCode, cred.fallbackUser))
viper.Set("helper.fallbackPass", aes256.Encrypt(appCode, cred.fallbackPass))
}
}
viper.WriteConfig()
formPass.Text = ""
}
}, myWindow)
formPopUp.Resize(fyne.NewSize(300, 0))
formPopUp.Show()
}),
fyne.NewMenuItem("Export Output", func() {
if outputCMD.Text == "" {
dialog.NewCustom("Oops", "Close", widget.NewLabel("No results to export. Please run query before exporting."), myWindow).Show()
} else {
timeRaw := time.Now()
os.UserConfigDir()
userHome, _ := os.UserHomeDir()
fileName := fmt.Sprintf("sshRunCMD-Export_%s.txt", timeRaw.Format("150405"))
fileLocation := fmt.Sprintf(`%s\Downloads\%s`, userHome, fileName)
f, err := os.OpenFile(
fileLocation,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644,
)
if err != nil {
dialog.NewError(err, myWindow)
}
defer f.Close()
//file output automatically adds a new line. No new line needed when converting outputArray to string
_, err = f.WriteString(outputCMD.Text)
if err != nil {
log.Println("Issue writing to file.")
}
fileOpen := widget.Hyperlink{
Text: "Open File",
OnTapped: func() {
cmd := exec.Command("cmd", "/C", "start "+fileLocation)
err := cmd.Run()
if err != nil {
dialog.NewCustom("Error", "Close", widget.NewLabel("Issue with file, retry:\n"+err.Error()), myWindow).Show()
}
},
}
linkText := container.NewVBox(
widget.NewLabel("File exported to Downloads folder"),
container.NewCenter(
container.NewHBox(&fileOpen),
),
)
dialog.NewCustom("Export Complete", "Ok", linkText, myWindow).Show()
}
}),
fyne.NewMenuItem("Quit", func() { myApp.Quit() }),
)
helpMenu := fyne.NewMenu("Help",
fyne.NewMenuItem("About", func() {
dialog.ShowCustom("About", "Close", container.NewVBox(
widget.NewLabel("Welcome to sshRunCMD, a simple CLI application for managing switches."),
widget.NewLabel("Version: v1.4.0"),
widget.NewLabel("Author: Ryan Deckard"),
), myWindow)
}))
pingCountEntry := widget.NewEntry()
pingTimeoutEntry := widget.NewEntry()
coreEntry := widget.NewEntry()
settingsFormItems := []*widget.FormItem{
{Text: "Core IP", Widget: coreEntry},
{Text: "Ping Count", Widget: pingCountEntry},
{Text: "Ping Timeout (ms)", Widget: pingTimeoutEntry},
}
editMenu := fyne.NewMenu("Edit",
fyne.NewMenuItem("Options", func() {
coreEntry.Text = cred.core
pingCountEntry.Text = fmt.Sprint(cred.pingCount)
pingTimeoutEntry.Text = fmt.Sprint(cred.pingTimeout)
settingsPop := dialog.NewForm("Manage", "Update", "Cancel", settingsFormItems, func(ok bool) {
if ok {
cred.core = strings.Trim(coreEntry.Text, " ")
cred.pingCount, _ = strconv.Atoi(strings.Trim(pingCountEntry.Text, " "))
cred.pingTimeout, _ = strconv.Atoi(strings.Trim(pingTimeoutEntry.Text, " "))
viper.Set("helper.core", cred.core)
viper.Set("blockTimer.pingCount", cred.pingCount)
viper.Set("blockTimer.pingTimeout", cred.pingTimeout)
viper.WriteConfig()
}
}, myWindow)
settingsPop.Resize(fyne.NewSize(300, 0))
settingsPop.Show()
}),
)
mainMenu := fyne.NewMainMenu(
fileMenu,
editMenu,
helpMenu,
)
myWindow.SetMainMenu(mainMenu)
outTitle := widget.NewLabel("Switch Output")
outTitle.TextStyle.Bold = true
outTitle.Alignment = fyne.TextAlignCenter
outputScroll := container.NewVScroll(outputCMD)
outputCMD.Wrapping = fyne.TextWrapWord //outputScroll.ScrollToBottom()
outputScroll.SetMinSize(fyne.NewSize(450, 0))
progBar = widget.NewProgressBar()
progBar.Hide()
deviceList := widget.NewMultiLineEntry()
userScript := widget.NewMultiLineEntry()
submitButton := widget.NewButton(
"Run CMD", func() { cred.runProgram(deviceList, userScript) },
)
submitButton.Importance = widget.HighImportance
testButton := widget.NewButton(
//premade test to run against network to verify applicaton functionality
"Test Run", func() {
surePopup := dialog.NewConfirm("Please confirm", "Run test script?", func(ok bool) {
if ok {
procDeviceList := viper.GetStringSlice("tester.devices")
deviceList.Text = strings.Join(procDeviceList, "\n")
procUserScript := viper.GetString("tester.commands")
userScript.Text = procUserScript
if len(procDeviceList) == 0 || len(procUserScript) == 0 {
dialog.NewCustom("Error", "Close", widget.NewLabel("Test not found. Please add test case to helper file and restart."), myWindow).Show()
deviceList.Text = ""
userScript.Text = ""
return
}
deviceList.Refresh()
userScript.Refresh()
cred.runProgram(deviceList, userScript)
}
}, myWindow)
surePopup.Show()
},
)
verifyCredsCheck := widget.NewCheck("Check Credentials", func(ok bool) {
*verifyCreds = ok
})
legacySSHCheck := widget.NewCheck("Legacy SSH", func(ok bool) {
legacySSH = ok
})
verifyCredsCheck.SetChecked(true)
buttonBox := container.New(
layout.NewHBoxLayout(),
testButton,
verifyCredsCheck,
legacySSHCheck,
)
ipBox := container.NewBorder(
widget.NewLabel("IP Addresses"),
buttonBox,
nil,
nil,
deviceList,
)
cmdBox := container.NewBorder(
widget.NewLabel("Commands"),
submitButton,
nil,
nil,
userScript,
)
left := container.New(
layout.NewGridLayout(1),
ipBox,
cmdBox,
)
right := container.NewBorder(
outTitle,
progBar,
nil,
nil,
outputScroll,
)
content := container.New(
layout.NewGridLayout(2),
left,
right,
)
// Display our content
myWindow.SetContent(content)
// Close the App when Escape key is pressed
myWindow.Canvas().SetOnTypedKey(func(keyEvent *fyne.KeyEvent) {
if keyEvent.Name == fyne.KeyEscape {
myApp.Quit()
}
})
// Show window and run app
myWindow.ShowAndRun()
}
func (cred *CRED) runProgram(deviceList *widget.Entry, userScript *widget.Entry) { // optional, handle form submission
//clear output for subsequent runs
if *verifyCreds {
if cred.username == "" || cred.password == "" {
dialog.NewCustom("Error", "Close", widget.NewLabel("Credentials not provided. \nPlease update helper.yml or manually add under File > Manage Credentials.\n"), myWindow).Show()
return
}
//checks credentials against a default device so you don't lock yourself out
connect.Init(cred.username, cred.password, "", legacySSH)
if cred.core == "" {
dialog.NewCustom("Error", "Close", widget.NewLabel("No core device specified in helper file. Please add to Edit > Options."), myWindow).Show()
return
} else {
_, err := connect.DialClient(cred.core)
if err != nil {
dialog.NewCustom("Error", "Close", widget.NewLabel("Can't connect to Core Device. Please check credentials.\n"+err.Error()), myWindow).Show()
//"Supplied Credentials not working."
return
}
}
}
outputCMD.Text = ""
waitGroup := goccm.New(40)
deviceSlice := strings.Split(deviceList.Text, "\n")
userScriptSlice := strings.Split(userScript.Text, "\n")
outputCMD.Text = "\nApplication Started....\n"
outputCMD.Refresh()
progress.step = 1 / float64(len(deviceSlice))
progBar.Show()
progBar.SetValue(progress.step)
for _, deviceIP := range deviceSlice {
waitGroup.Wait()
go func(host string) {
defer waitGroup.Done()
if strings.TrimSpace(host) != "" {
err := cred.SSHConnect(userScriptSlice, strings.TrimSpace(host))
if err != nil {
errOut := err.Error()
//no idea why this error means authentication failed.....but it does
if strings.Contains(errOut, "reason 2: Non-assigned port") {
errOut = "unable to connect: ssh: authentication not provided"
}
outputCMD.Text = host + ": issue with ssh: " + errOut + "\n" + outputCMD.Text
}
}
}(deviceIP)
}
waitGroup.WaitAllDone()
showResults()
}