/
direct.go
70 lines (61 loc) · 1.58 KB
/
direct.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
package action
import (
"strings"
"github.com/chzyer/readline"
"github.com/sakoken/sshh/config"
"github.com/sakoken/sshh/connector"
"github.com/sakoken/sshh/encrypt"
"github.com/sakoken/sshh/interactive"
)
func Direct(requestHost string, pOption string, port string) error {
host := &connector.Connector{}
host.Host = requestHost
host.Port = "22"
if index := strings.Index(requestHost, "@"); index > 0 {
host.Host = requestHost[index+1:]
host.User = requestHost[:index]
}
if pOption == "-p" && port != "" {
host.Port = port
}
i, _ := interactive.NewEx(&readline.Config{
Prompt: "\033[36msshh»\033[0m ",
InterruptPrompt: "\n",
EOFPrompt: "exit",
FuncFilterInputRune: interactive.FilterInput,
})
defer func(rl *interactive.Interactive) {
if rl != nil {
err := rl.Close()
if err != nil {
println(err.Error())
}
}
}(i)
//すでに登録済みの場合はすぐにssh
if has, resistedHost := config.SshhData().Has(host); has {
key := i.PasswordQuestion("Enter secret phrase", true)
i.Close()
pw, err := encrypt.Decrypt(resistedHost.Password, key)
if err != nil {
return err
}
config.SshhData().ToTopPosition(resistedHost)
config.SshhData().Save()
resistedHost.SshConnection(string(pw))
return nil
}
key := ""
host.Password, key = i.ServerPassword(true)
i.Close()
config.SshhData().Add(host)
config.SshhData().Save()
pw, err := encrypt.Decrypt(host.Password, key)
if err != nil {
return err
}
config.SshhData().ToTopPosition(host)
config.SshhData().Save()
host.SshConnection(string(pw))
return nil
}