-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.go
77 lines (69 loc) · 2.08 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
package main
import (
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/BurntSushi/toml"
)
type config struct {
Host string `toml:"host"`
Port uint `toml:"port"`
User string `toml:"user"`
Password string `toml:"password"`
IdentityFile string `toml:"identity_file"`
}
func main() {
var (
host string
port uint
user string
password string
identityFile string
configPath string
)
hostUsage := "the target host (required if no config file)"
portUsage := "the port to connect"
portDefualt := uint(22)
userUsage := "the login user (required if no config file)"
passwordUsage := "the login password"
identityFileUsage := "the identity file"
configPathUsage := "the path of config file (ignore other args if a config file exists)"
configPathDefualt := "./config.toml"
flag.StringVar(&host, "t", "", hostUsage)
flag.UintVar(&port, "p", portDefualt, portUsage)
flag.StringVar(&user, "u", "", userUsage)
flag.StringVar(&password, "s", "", passwordUsage)
flag.StringVar(&identityFile, "i", "", identityFileUsage)
flag.StringVar(&configPath, "c", configPathDefualt, configPathUsage)
flag.Parse()
var cfg config
var handler *sshHandler
if _, err := toml.DecodeFile(configPath, &cfg); errors.Is(err, os.ErrNotExist) {
if host == "" {
log.Fatal("host can not be empty")
}
if user == "" {
log.Fatal("user can not be empty")
}
if password == "" && identityFile == "" {
log.Fatal("password can not be empty")
}
addr := fmt.Sprintf("%s:%d", host, port)
handler = &sshHandler{addr: addr, user: user, secret: password}
} else if err != nil {
log.Fatal("could not parse config file: ", err)
} else {
addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
if password != "" {
handler = &sshHandler{addr: addr, user: cfg.User, secret: cfg.Password}
} else {
handler = &sshHandler{addr: addr, user: cfg.User, keyfile: cfg.IdentityFile}
}
}
http.Handle("/", http.FileServer(http.Dir("./front/")))
http.HandleFunc("/web-socket/ssh", handler.webSocket)
log.Fatal(http.ListenAndServe(":8080", nil))
}