Skip to content

Commit 87c0eac

Browse files
authored
Merge pull request #46 from blacknon/develop
Develop
2 parents e953f3f + 8a43d35 commit 87c0eac

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

connect.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net"
1212
"os"
1313
"os/signal"
14+
"sync"
1415
"syscall"
1516
"time"
1617

@@ -53,6 +54,9 @@ type Connect struct {
5354
// Set the TTY to be used as the input and output for the Session/Cmd.
5455
PtyRelayTty *os.File
5556

57+
// StdoutMutex is a mutex for use Stdout.
58+
StdoutMutex *sync.Mutex
59+
5660
// CheckKnownHosts if true, check knownhosts.
5761
// Ignored if HostKeyCallback is set.
5862
// Set it before CraeteClient.
@@ -139,7 +143,7 @@ func (c *Connect) CreateClient(host, port, user string, authMethods []ssh.AuthMe
139143
// append default files
140144
c.KnownHostsFiles = append(c.KnownHostsFiles, "~/.ssh/known_hosts")
141145
}
142-
config.HostKeyCallback = c.verifyAndAppendNew
146+
config.HostKeyCallback = c.VerifyAndAppendNew
143147
} else {
144148
config.HostKeyCallback = ssh.InsecureIgnoreHostKey()
145149
}

knownhosts.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net"
1111
"os"
1212
"os/signal"
13+
"slices"
1314
"strings"
1415
"syscall"
1516
"text/template"
@@ -36,19 +37,19 @@ type OverwriteInventory struct {
3637
// If is no problem, error returns Nil.
3738
//
3839
// 【参考】: 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) {
4041
// set TextAskWriteKnownHosts default text
4142
if len(c.TextAskWriteKnownHosts) == 0 {
4243
c.TextAskWriteKnownHosts += "The authenticity of host '{{.Address}} ({{.RemoteAddr}})' can't be established.\n"
4344
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))? "
4546
}
4647

4748
// set TextAskOverwriteKnownHosts default text
4849
if len(c.TextAskOverwriteKnownHosts) == 0 {
4950
c.TextAskOverwriteKnownHosts += "The authenticity of host '{{.Address}} ({{.RemoteAddr}})' can't be established.\n"
5051
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))? "
5253
}
5354

5455
// check count KnownHostsFiles
@@ -79,6 +80,12 @@ func (c *Connect) verifyAndAppendNew(hostname string, remote net.Addr, key ssh.P
7980
filepath := knownHostsFiles[0]
8081
var line int
8182

83+
// check mutex
84+
if c.StdoutMutex != nil {
85+
c.StdoutMutex.Lock()
86+
defer c.StdoutMutex.Unlock()
87+
}
88+
8289
// check error
8390
keyErr, ok := err.(*knownhosts.KeyError)
8491
if !ok || len(keyErr.Want) > 0 {
@@ -107,7 +114,7 @@ func (c *Connect) verifyAndAppendNew(hostname string, remote net.Addr, key ssh.P
107114

108115
err = writeKnownHostsKey(filepath, line, hostname, remote, key)
109116

110-
return nil
117+
return err
111118
}
112119

113120
// askAddingUnknownHostKey
@@ -150,9 +157,9 @@ func askAddingUnknownHostKey(text string, address string, remote net.Addr, key s
150157
return false, fmt.Errorf("failed to read answer: %s", err)
151158
}
152159
answer = string(strings.ToLower(strings.TrimSpace(answer)))
153-
if answer == "yes" {
160+
if slices.Contains([]string{"yes", "y"}, answer) {
154161
return true, nil
155-
} else if answer == "no" {
162+
} else if slices.Contains([]string{"no", "n"}, answer) {
156163
return false, nil
157164
}
158165
fmt.Print("Please type 'yes' or 'no': ")
@@ -204,7 +211,7 @@ func askOverwriteKnownHostKey(text string, address string, remote net.Addr, key
204211
} else if answer == "no" {
205212
return false, nil
206213
}
207-
fmt.Print("Please type 'yes' or 'no': ")
214+
fmt.Print("Please type 'yes|y' or 'no|n': ")
208215
}
209216
}
210217

0 commit comments

Comments
 (0)