@@ -10,6 +10,7 @@ import (
10
10
"net"
11
11
"os"
12
12
"os/signal"
13
+ "slices"
13
14
"strings"
14
15
"syscall"
15
16
"text/template"
@@ -36,19 +37,19 @@ type OverwriteInventory struct {
36
37
// If is no problem, error returns Nil.
37
38
//
38
39
// 【参考】: https://github.com/tatsushid/minssh/blob/57eae8c5bcf5d94639891f3267f05251f05face4/pkg/minssh/minssh.go#L190-L237
39
- func (c * Connect ) verifyAndAppendNew (hostname string , remote net.Addr , key ssh.PublicKey ) (err error ) {
40
+ func (c * Connect ) VerifyAndAppendNew (hostname string , remote net.Addr , key ssh.PublicKey ) (err error ) {
40
41
// set TextAskWriteKnownHosts default text
41
42
if len (c .TextAskWriteKnownHosts ) == 0 {
42
43
c .TextAskWriteKnownHosts += "The authenticity of host '{{.Address}} ({{.RemoteAddr}})' can't be established.\n "
43
44
c .TextAskWriteKnownHosts += "RSA key fingerprint is {{.Fingerprint}}\n "
44
- c .TextAskWriteKnownHosts += "Are you sure you want to continue connecting (yes/no)? "
45
+ c .TextAskWriteKnownHosts += "Are you sure you want to continue connecting (( yes|y)/(no|n))? "
45
46
}
46
47
47
48
// set TextAskOverwriteKnownHosts default text
48
49
if len (c .TextAskOverwriteKnownHosts ) == 0 {
49
50
c .TextAskOverwriteKnownHosts += "The authenticity of host '{{.Address}} ({{.RemoteAddr}})' can't be established.\n "
50
51
c .TextAskOverwriteKnownHosts += "Old key: {{.OldKeyText}}\n "
51
- c .TextAskOverwriteKnownHosts += "Are you sure you want to overwrite {{.Fingerprint}}, continue connecting (yes/no)? "
52
+ c .TextAskOverwriteKnownHosts += "Are you sure you want to overwrite {{.Fingerprint}}, continue connecting (( yes|y)/(no|n))? "
52
53
}
53
54
54
55
// check count KnownHostsFiles
@@ -79,6 +80,12 @@ func (c *Connect) verifyAndAppendNew(hostname string, remote net.Addr, key ssh.P
79
80
filepath := knownHostsFiles [0 ]
80
81
var line int
81
82
83
+ // check mutex
84
+ if c .StdoutMutex != nil {
85
+ c .StdoutMutex .Lock ()
86
+ defer c .StdoutMutex .Unlock ()
87
+ }
88
+
82
89
// check error
83
90
keyErr , ok := err .(* knownhosts.KeyError )
84
91
if ! ok || len (keyErr .Want ) > 0 {
@@ -107,7 +114,7 @@ func (c *Connect) verifyAndAppendNew(hostname string, remote net.Addr, key ssh.P
107
114
108
115
err = writeKnownHostsKey (filepath , line , hostname , remote , key )
109
116
110
- return nil
117
+ return err
111
118
}
112
119
113
120
// askAddingUnknownHostKey
@@ -150,9 +157,9 @@ func askAddingUnknownHostKey(text string, address string, remote net.Addr, key s
150
157
return false , fmt .Errorf ("failed to read answer: %s" , err )
151
158
}
152
159
answer = string (strings .ToLower (strings .TrimSpace (answer )))
153
- if answer == "yes" {
160
+ if slices . Contains ([] string { "yes" , "y" }, answer ) {
154
161
return true , nil
155
- } else if answer == "no" {
162
+ } else if slices . Contains ([] string { "no" , "n" }, answer ) {
156
163
return false , nil
157
164
}
158
165
fmt .Print ("Please type 'yes' or 'no': " )
@@ -204,7 +211,7 @@ func askOverwriteKnownHostKey(text string, address string, remote net.Addr, key
204
211
} else if answer == "no" {
205
212
return false , nil
206
213
}
207
- fmt .Print ("Please type 'yes' or 'no': " )
214
+ fmt .Print ("Please type 'yes|y ' or 'no|n ': " )
208
215
}
209
216
}
210
217
0 commit comments