-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathconfig.go
146 lines (130 loc) Β· 2.72 KB
/
config.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
package sshw
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"strconv"
"time"
"github.com/atrox/homedir"
"github.com/kevinburke/ssh_config"
"golang.org/x/crypto/ssh"
"gopkg.in/yaml.v2"
)
type Node struct {
Name string `yaml:"name"`
Alias string `yaml:"alias"`
Host string `yaml:"host"`
User string `yaml:"user"`
Port int `yaml:"port"`
KeyPath string `yaml:"keypath"`
Passphrase string `yaml:"passphrase"`
Password string `yaml:"password"`
CallbackShells []*CallbackShell `yaml:"callback-shells"`
Children []*Node `yaml:"children"`
Jump []*Node `yaml:"jump"`
}
type CallbackShell struct {
Cmd string `yaml:"cmd"`
Delay time.Duration `yaml:"delay"`
}
func (n *Node) String() string {
return n.Name
}
func (n *Node) user() string {
if n.User == "" {
return "root"
}
return n.User
}
func (n *Node) port() int {
if n.Port <= 0 {
return 22
}
return n.Port
}
func (n *Node) password() ssh.AuthMethod {
if n.Password == "" {
return nil
}
return ssh.Password(n.Password)
}
func (n *Node) alias() string {
return n.Alias
}
var (
config []*Node
)
func GetConfig() []*Node {
return config
}
func LoadConfig() error {
b, err := LoadConfigBytes(".sshw", ".sshw.yml", ".sshw.yaml")
if err != nil {
return err
}
var c []*Node
err = yaml.Unmarshal(b, &c)
if err != nil {
return err
}
config = c
return nil
}
func LoadSshConfig() error {
u, err := user.Current()
if err != nil {
l.Error(err)
return nil
}
f, _ := os.Open(path.Join(u.HomeDir, ".ssh/config"))
cfg, _ := ssh_config.Decode(f)
var nc []*Node
for _, host := range cfg.Hosts {
alias := fmt.Sprintf("%s", host.Patterns[0])
hostName, err := cfg.Get(alias, "HostName")
if err != nil {
return err
}
if hostName != "" {
port, _ := cfg.Get(alias, "Port")
if port == "" {
port = "22"
}
var c = new(Node)
c.Name = alias
c.Alias = alias
c.Host = hostName
c.User, _ = cfg.Get(alias, "User")
c.Port, _ = strconv.Atoi(port)
keyPath, _ := cfg.Get(alias, "IdentityFile")
c.KeyPath, _ = homedir.Expand(keyPath)
nc = append(nc, c)
// fmt.Println(c.Alias, c.Host, c.User, c.Port, c.KeyPath)
}
}
config = nc
return nil
}
func LoadConfigBytes(names ...string) ([]byte, error) {
u, err := user.Current()
if err != nil {
return nil, err
}
// homedir
for i := range names {
sshw, err := ioutil.ReadFile(path.Join(u.HomeDir, names[i]))
if err == nil {
return sshw, nil
}
}
// relative
for i := range names {
sshw, err := ioutil.ReadFile(names[i])
if err == nil {
return sshw, nil
}
}
return nil, err
}