-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
330 lines (294 loc) · 7.34 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
curses "github.com/gbin/goncurses"
)
type Server struct {
Name string
User string
Password string
Host string
Server string
}
func pretty_exit(message string) {
fmt.Printf("%s\n", message)
os.Exit(0)
}
func shell_mode(args []string) {
var exit = func() {
fmt.Println("Proper use:\n$ executable user:password host_mount:server")
os.Exit(0)
}
var user string
var password string
var host string
var server string
if len(args) == 2 {
if len(strings.Split(args[0], ":")) == 2 {
user, password = strings.Split(args[0], ":")[0], strings.Split(args[0], ":")[1]
} else {
exit()
}
if len(strings.Split(args[1], ":")) == 2 {
host, server = strings.Split(args[1], ":")[0], strings.Split(args[1], ":")[1]
} else {
exit()
}
if !is_mounted(host) {
mount_samba(user, password, host, server, false)
}
} else if len(args) == 1 {
host = args[0]
dismount_samba(host, false)
} else {
exit()
}
}
func exec_path() string {
exePath, err := os.Executable()
if err != nil {
pretty_exit("Can't find exec path")
}
exeDir := filepath.Dir(exePath)
return exeDir
}
func print_banner(stdscr *curses.Window) int {
read_banner_file := func() string {
content, err := os.ReadFile(fmt.Sprintf("%s/banner.txt", exec_path()))
if err != nil {
log.Fatal(err)
}
return string(content)
}
banner := strings.Split(read_banner_file(), "\n")
y_position := 0
actual_banner := 6
for i, line := range banner {
if i < actual_banner {
stdscr.AttrOn(curses.ColorPair(1))
}
stdscr.MovePrint(i+1, 2, line)
y_position = i + 1
if i < actual_banner {
stdscr.AttrOff(curses.ColorPair(1))
}
}
return y_position + 1
}
func print_menu(stdscr *curses.Window, highlight int, servers []Server) int {
y_position := print_banner(stdscr)
y_position += 1
for i, s := range servers {
if i == highlight {
stdscr.AttrOn(curses.ColorPair(2))
}
stdscr.MovePrint(y_position, 5, fmt.Sprintf(" %s ", s.Name))
if i == highlight {
stdscr.AttrOff(curses.ColorPair(2))
}
y_position += 1
}
return y_position
}
func tui_mode() {
exeDir := exec_path()
content, err := os.ReadFile(fmt.Sprintf("%s/servers.json", exeDir))
if err != nil {
pretty_exit("Can't find/open servers.json")
}
var servers []Server
err = json.Unmarshal(content, &servers)
if err != nil {
pretty_exit("Can't parse json file")
}
// Here Ncurses start
stdscr, err := curses.Init()
if err != nil {
pretty_exit("Error regarding ncurses")
}
defer curses.End()
curses.StartColor()
curses.InitPair(1, curses.C_RED, curses.C_BLACK)
curses.InitPair(2, curses.C_BLACK, curses.C_WHITE)
curses.InitPair(3, curses.C_WHITE, curses.C_GREEN)
curses.InitPair(4, curses.C_WHITE, curses.C_RED)
height, width := stdscr.MaxYX()
curses.CBreak(true)
stdscr.Keypad(true)
if width < 60 || height < 24 {
pretty_exit("Your terminal is too small")
}
highlight := 0
for {
print_menu(stdscr, highlight, servers)
stdscr.MovePrint(0, 0, " ")
c := stdscr.GetChar()
if c == 'q' {
break
} else if c == curses.KEY_DOWN {
if highlight < len(servers)-1 {
highlight += 1
}
} else if c == curses.KEY_UP {
if highlight > 0 {
highlight -= 1
}
}
stdscr.Clear()
stdscr.Refresh()
if c == 'd' {
success := dismount_samba(servers[highlight].Host, true)
if success {
stdscr.AttrOn(curses.ColorPair(3))
stdscr.MovePrint(0, 2, "Successful dismount")
stdscr.AttrOff(curses.ColorPair(3))
} else {
stdscr.AttrOn(curses.ColorPair(4))
stdscr.MovePrint(0, 2, "Failed to dismount")
stdscr.AttrOff(curses.ColorPair(4))
}
}
if c == 'm' {
if servers[highlight].User != "" && servers[highlight].Password != "" {
success := mount_samba(servers[highlight].User, servers[highlight].Password, servers[highlight].Host, servers[highlight].Server, true)
if success {
stdscr.AttrOn(curses.ColorPair(3))
stdscr.MovePrint(0, 2, "Successful mount")
stdscr.AttrOff(curses.ColorPair(3))
} else {
stdscr.AttrOn(curses.ColorPair(4))
stdscr.MovePrint(0, 2, "Failed to mount")
stdscr.AttrOff(curses.ColorPair(4))
}
} else {
user := servers[highlight].User
password := servers[highlight].Password
if user == "" {
msg := "User: "
for {
y_position := print_menu(stdscr, highlight, servers)
y_position += 1
stdscr.MovePrint(y_position, 6, msg)
stdscr.MovePrint(y_position, 6+len(msg), user)
ch := stdscr.GetChar()
if ch == '\n' {
stdscr.Clear()
stdscr.Refresh()
break
} else if ch == curses.KEY_BACKSPACE {
if len(user) > 0 {
user = user[:len(user)-1]
}
} else {
user = fmt.Sprintf("%s%c", user, ch)
}
stdscr.Clear()
stdscr.Refresh()
}
}
if password == "" {
msg := "Password: "
for {
y_position := print_menu(stdscr, highlight, servers)
y_position += 1
stdscr.MovePrint(y_position, 6, msg)
// stdscr.MovePrint(y_position, 6+len(msg), password) // Don't display your password
ch := stdscr.GetChar()
if ch == '\n' {
stdscr.Clear()
stdscr.Refresh()
break
} else if ch == curses.KEY_BACKSPACE {
if len(password) > 0 {
password = password[:len(password)-1]
}
} else {
password = fmt.Sprintf("%s%c", password, ch)
}
stdscr.Clear()
stdscr.Refresh()
}
}
success := mount_samba(user, password, servers[highlight].Host, servers[highlight].Server, true)
if success {
stdscr.AttrOn(curses.ColorPair(3))
stdscr.MovePrint(0, 2, "Successful mount")
stdscr.AttrOff(curses.ColorPair(3))
} else {
stdscr.AttrOn(curses.ColorPair(4))
stdscr.MovePrint(0, 2, "Failed to mount")
stdscr.AttrOff(curses.ColorPair(4))
}
}
}
}
}
func mount_samba(user string, password string, host string, server string, tui_mode bool) bool {
shell := exec.Command("mount", "-t", "cifs", server, host, "-o", fmt.Sprintf("username=%s,password=%s,uid=1000,gid=1000,file_mode=0740,dir_mode=0700", user, password))
err := shell.Run()
if err != nil {
if !tui_mode {
pretty_exit("Can't mount directory, already mounted/no such directory/error with connection")
}
return false
}
if !tui_mode {
fmt.Println("Succesful mount :)")
}
return true
}
func dismount_samba(host string, tui_mode bool) bool {
shell := exec.Command("umount", host)
err := shell.Run()
if err != nil {
if !tui_mode {
pretty_exit("Can't dismount directory, wrong directory/not mounted")
}
return false
}
if !tui_mode {
fmt.Println("Succesful dismount :)")
}
return true
}
func is_mounted(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
pretty_exit("Can't find directory")
}
if !fileInfo.IsDir() {
pretty_exit("Mount location is not a directory")
}
stuff, err := os.ReadDir(path)
if err != nil {
pretty_exit("Directory can't be found")
}
return len(stuff) != 0
}
func is_root() bool {
user, err := user.Current()
if err != nil {
pretty_exit("Run this command as sudo/root")
return false
}
return user.Username == "root"
}
func main() {
if !is_root() {
fmt.Println("You need to run this app as a root user !!")
os.Exit(0)
}
if len(os.Args) > 1 {
args := os.Args[1:]
shell_mode(args)
} else {
tui_mode()
}
}