-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
193 lines (166 loc) · 4.38 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
// Text-mode user interface (TUI) for remote device connections.
package main
import (
"bufio"
_ "embed"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"time"
)
//go:embed devices.json
var device_database string
func systemOk() bool {
var err error
if runtime.GOOS == "windows" {
if err = exec.Command("ssh", "-V").Run(); err != nil {
fmt.Println("Please install OpenSSH client: https://www.hawaii.edu/askus/1874")
return false
}
}
return true
}
func checkExitConditions(done *bool) {
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
cmd := exec.Command("sh", "-c", "lsof | grep sshfs/")
output, _ := cmd.Output()
if string(output) == "" {
*done = true
} else {
fmt.Printf("Some processes are holding sshfs references, please close them:\n%s\n", output)
}
} else if runtime.GOOS == "windows" {
// sshfs not supported on Windows, so nothing to check.
*done = true
}
}
func setLoopback(mode bool) {
if mode && runtime.GOOS == "windows" {
if !windowsIsAdmin() {
windowsShowMessage("Run as adminstrator to use loopback mode.")
return
}
}
config.UseLoopbackAddrs = mode
state := "disabled"
if config.UseLoopbackAddrs {
state = "enabled"
}
fmt.Printf("%s loopback addresses for forwards\n", state)
}
func help() {
fmt.Println("")
fmt.Println("Commands:")
fmt.Println("123 - connect to device with port offset 123")
fmt.Println("23080123T - connect to device with serial 23080123T")
fmt.Println("22123! - connect to device with tunnel port 22123 (for unlisted devices)")
fmt.Println("list - list devices")
fmt.Println("unlock-hidden - unhide prod and demo devices (speedbump)")
fmt.Println("lock-hidden - hide prod and demo devices (speedbump)")
fmt.Println("loopback - toggle use of loopback addresses for port forwards")
fmt.Println("help - this help")
fmt.Println("exit - exit program")
fmt.Println("exit!- exit program even if clean exit conditions aren't met (also ctrl-d or ctrl-z)")
}
func main() {
config = ConfigLoad()
// Pass certain args along to ssh commands.
optNext := ""
for _, arg := range os.Args {
if arg == "-v" {
config.Verbose = true
} else if len(arg) == 2 && arg[0:1] == "-" && strings.Index("iIo", arg[1:]) >= 0 {
optNext = arg
continue
} else if optNext != "" {
config.SshOptionList = append(config.SshOptionList, fmt.Sprintf("%s %s\n", optNext, arg))
}
optNext = ""
}
awsSetup()
checkForUpdates()
if !systemOk() {
fmt.Print("Press Enter to continue...")
fmt.Scanln()
return
}
allDevices := loadDevices()
allDevices.list()
help()
fmt.Print("> ")
command := make(chan string)
go func() {
// Command-line input
scanner := bufio.NewScanner(os.Stdin)
for {
if !scanner.Scan() {
// eof
fmt.Println("")
// We've lost stdin at this point, so we can't retry
// if there are problems
command <- "exit!"
break
}
input := strings.TrimSpace(scanner.Text())
command <- input
}
}()
handleCommand := func(input string, done *bool) {
ilen := len(input)
if ilen == 0 {
} else if input == "exit" {
checkExitConditions(done)
} else if input == "exit!" {
*done = true
} else if input == "list" {
allDevices.list()
} else if input == "unlock-hidden" {
allDevices.unlockHidden = true
} else if input == "lock-hidden" {
allDevices.unlockHidden = false
} else if input == "loopback" {
setLoopback(!config.UseLoopbackAddrs)
} else if input == "help" {
help()
} else if input[ilen-1:] == "~" {
if dev := allDevices.find(input[:ilen-1]); dev != nil {
dev.mount()
}
} else if dev := allDevices.find(input); dev != nil {
dev.connect()
}
if !*done {
fmt.Print("> ")
}
}
for {
// Main loop servicing command-line (and TBD http) requests.
// To avoid race conditions, limit state changes to synchronous
// function calls from this loop.
done := false
select {
case input := <-command:
handleCommand(input, &done)
case _ = <-time.After(1 * time.Second):
// fmt.Println("timeout")
case dev := <-allDevices.tunnelFinish:
// Explicitly close/kill all connections supported by tunnel.
// TBD for now, needs testing.
dev = dev
case con := <-allDevices.connectionFinish:
// Remove from connection set.
delete(allDevices.connections, con)
}
if done {
break
}
}
loopbackCleanup()
for _, dev := range allDevices.deviceList {
if dev.tunnelCmd != nil {
dev.tunnelCmd.Process.Kill()
}
}
}