/
entry.go
192 lines (174 loc) · 4.57 KB
/
entry.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
package entry
import (
"bufio"
"fmt"
"os"
"os/user"
"path/filepath"
"time"
"golang.org/x/crypto/ssh"
"github.com/vimiix/ssx/internal/lg"
"github.com/vimiix/ssx/internal/terminal"
"github.com/vimiix/ssx/internal/utils"
"github.com/vimiix/ssx/ssx/env"
)
const (
SourceSSHConfig = "ssh_config"
SourceSSXStore = "ssx_store"
)
const defaultIdentityFile = "~/.ssh/id_rsa"
// Entry represent a target server
type Entry struct {
ID uint64 `json:"id"`
Host string `json:"host"`
User string `json:"user"`
Port string `json:"port"`
VisitCount int `json:"visit_count"` // Perhaps I will support sorting by VisitCount in the future
KeyPath string `json:"key_path"`
Passphrase string `json:"passphrase"`
Password string `json:"password"`
Tags []string `json:"tags"`
Source string `json:"source"` // Data source, used to distinguish that it is from ssx stored or local ssh configuration
CreateAt time.Time `json:"create_at"`
UpdateAt time.Time `json:"update_at"`
// TODO support jump server
}
func (e *Entry) String() string {
return fmt.Sprintf("%s@%s:%s", e.User, e.Host, e.Port)
}
func getConnectTimeout() time.Duration {
var defaultTimeout = time.Second * 10
val := os.Getenv(env.SSXConnectTimeout)
if len(val) <= 0 {
return defaultTimeout
}
d, err := time.ParseDuration(val)
if err != nil {
lg.Debug("invalid %q value: %q", env.SSXConnectTimeout, val)
d = defaultTimeout
}
return d
}
func (e *Entry) GenSSHConfig() *ssh.ClientConfig {
cfg := &ssh.ClientConfig{
User: e.User,
Auth: e.AuthMethods(),
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: getConnectTimeout(),
}
cfg.SetDefaults()
return cfg
}
func (e *Entry) Tidy() error {
if len(e.User) <= 0 {
curUser, err := user.Current()
if err != nil {
return err
}
e.User = curUser.Username
}
if len(e.Port) <= 0 {
e.Port = "22"
}
if e.KeyPath == "" {
e.KeyPath = defaultIdentityFile
}
return nil
}
// AuthMethods all possible auth methods
func (e *Entry) AuthMethods() []ssh.AuthMethod {
var authMethods []ssh.AuthMethod
// password auth
if e.Password != "" {
authMethods = append(authMethods, ssh.Password(e.Password))
}
// key file auth methods
keyfileAuths := e.privateKeyAuthMethods()
if len(keyfileAuths) > 0 {
authMethods = append(authMethods, keyfileAuths...)
}
authMethods = append(authMethods, e.interactAuth())
return authMethods
}
func (e *Entry) interactAuth() ssh.AuthMethod {
return ssh.KeyboardInteractive(func(name, instruction string, questions []string, echos []bool) (answers []string, err error) {
answers = make([]string, 0, len(questions))
for i, q := range questions {
fmt.Print(q)
if echos[i] {
scan := bufio.NewScanner(os.Stdin)
if scan.Scan() {
answers = append(answers, scan.Text())
}
if err := scan.Err(); err != nil {
return nil, err
}
} else {
b, err := terminal.ReadPassword()
if err != nil {
return nil, err
}
fmt.Println()
answers = append(answers, string(b))
}
}
return answers, nil
})
}
func (e *Entry) privateKeyAuthMethods() []ssh.AuthMethod {
keyfiles := e.collectKeyfiles()
if len(keyfiles) == 0 {
return nil
}
var methods []ssh.AuthMethod
for _, f := range keyfiles {
auth := e.keyfileAuth(f)
if auth != nil {
methods = append(methods, auth)
}
}
return methods
}
func (e *Entry) keyfileAuth(keypath string) ssh.AuthMethod {
pemBytes, err := os.ReadFile(keypath)
if err != nil {
lg.Debug("generate rsaAuth: failed to read file %q: %s", keypath, err)
return nil
}
var signer ssh.Signer
if e.Passphrase == "" {
signer, err = ssh.ParsePrivateKey(pemBytes)
} else {
signer, err = ssh.ParsePrivateKeyWithPassphrase(pemBytes, []byte(e.Passphrase))
}
if err != nil {
lg.Debug("generate rsaAuth: %s", err)
return nil
}
return ssh.PublicKeys(signer)
}
// defaultRSAKeyFiles List of possible key files
// The order of the list represents the priority
var defaultRSAKeyFiles = []string{
"id_rsa", "id_ecdsa", "id_ecdsa_sk",
"id_ed25519", "id_ed25519_sk", "id_rsa",
}
func (e *Entry) collectKeyfiles() []string {
var keypaths []string
if e.KeyPath != "" && utils.FileExists(e.KeyPath) {
keypaths = append(keypaths, e.KeyPath)
}
u, err := user.Current()
if err != nil {
lg.Debug("failed to get current user, ignore default rsa keys")
return keypaths
}
for _, fn := range defaultRSAKeyFiles {
fp := filepath.Join(u.HomeDir, ".ssh", fn)
if fp == e.KeyPath || !utils.FileExists(fp) {
continue
}
keypaths = append(keypaths, fp)
}
return keypaths
}