Skip to content

Commit fb7f994

Browse files
committed
update. fix #32. add field Connect.HostKeyCallback
1 parent 2958c4b commit fb7f994

File tree

3 files changed

+96
-8
lines changed

3 files changed

+96
-8
lines changed

_example/example_http_dynamic_forward_shell.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by an MIT license
33
// that can be found in the LICENSE file.
44

5-
// Shell connection and dynamic forwarding Example file.
5+
// Shell connection and http dynamic forwarding Example file.
66
// Change the value of the variable and compile to make sure that you can actually connect.
77
//
88
// This file uses password authentication. Please replace as appropriate.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) 2020 Blacknon. All rights reserved.
2+
// Use of this source code is governed by an MIT license
3+
// that can be found in the LICENSE file.
4+
5+
// Shell connection and any HostKeyCallback use Example file.
6+
// Change the value of the variable and compile to make sure that you can actually connect.
7+
//
8+
// This file uses key authentication. Please replace as appropriate.
9+
10+
package main
11+
12+
import (
13+
"fmt"
14+
"os"
15+
16+
sshlib "github.com/blacknon/go-sshlib"
17+
"golang.org/x/crypto/ssh"
18+
)
19+
20+
var (
21+
host = "target.com"
22+
port = "22"
23+
user = "user"
24+
privkey = "~/.ssh/sshenc_key"
25+
keypass = "password"
26+
27+
termlog = "./test_termlog"
28+
)
29+
30+
func main() {
31+
// Create sshlib.Connect
32+
con := &sshlib.Connect{
33+
// If you use x11 forwarding, please set to true.
34+
ForwardX11: false,
35+
36+
// If you use ssh-agent forwarding, please set to true.
37+
// And after, run `con.ConnectSshAgent()`.
38+
ForwardAgent: false,
39+
}
40+
41+
// set any HostKeyCallback.
42+
con.HostKeyCallback = ssh.InsecureIgnoreHostKey()
43+
44+
// Create ssh.AuthMethods
45+
authMethod, err := sshlib.CreateAuthMethodPublicKey(privkey, keypass)
46+
if err != nil {
47+
fmt.Println(err)
48+
os.Exit(1)
49+
}
50+
51+
// If you use ssh-agent forwarding, uncomment it.
52+
// con.ConnectSshAgent()
53+
54+
// Connect ssh server
55+
err = con.CreateClient(host, port, user, []ssh.AuthMethod{authMethod})
56+
if err != nil {
57+
fmt.Println(err)
58+
os.Exit(1)
59+
}
60+
61+
// Set terminal log
62+
// con.SetLog(termlog, false)
63+
64+
// Create Session
65+
session, err := con.CreateSession()
66+
if err != nil {
67+
fmt.Println(err)
68+
os.Exit(1)
69+
}
70+
71+
// Start ssh shell
72+
con.Shell(session)
73+
}

connect.go

+22-7
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,22 @@ type Connect struct {
4141
SendKeepAliveInterval int
4242

4343
// Session use tty flag.
44+
// Set it before CraeteClient.
4445
TTY bool
4546

4647
// Forward ssh agent flag.
48+
// Set it before CraeteClient.
4749
ForwardAgent bool
4850

4951
// CheckKnownHosts if true, check knownhosts.
52+
// Set it before CraeteClient.
5053
CheckKnownHosts bool
5154

55+
// HostKeyCallback is ssh.HostKeyCallback.
56+
// This item takes precedence over `CheckKnownHosts`.
57+
// Set it before CraeteClient.
58+
HostKeyCallback ssh.HostKeyCallback
59+
5260
// OverwriteKnownHosts if true, if the knownhost is different, check whether to overwrite.
5361
OverwriteKnownHosts bool
5462

@@ -73,13 +81,16 @@ type Connect struct {
7381

7482
// ssh-agent interface.
7583
// agent.Agent or agent.ExtendedAgent
84+
// Set it before CraeteClient.
7685
Agent AgentInterface
7786

7887
// Forward x11 flag.
88+
// Set it before CraeteClient.
7989
ForwardX11 bool
8090

8191
// Forward X11 trusted flag.
8292
// This flag is ssh -Y option like flag.
93+
// Set it before CraeteClient.
8394
ForwardX11Trusted bool
8495

8596
// shell terminal log flag
@@ -111,14 +122,18 @@ func (c *Connect) CreateClient(host, port, user string, authMethods []ssh.AuthMe
111122
Timeout: time.Duration(timeout) * time.Second,
112123
}
113124

114-
if c.CheckKnownHosts {
115-
if len(c.KnownHostsFiles) == 0 {
116-
// append default files
117-
c.KnownHostsFiles = append(c.KnownHostsFiles, "~/.ssh/known_hosts")
118-
}
119-
config.HostKeyCallback = c.verifyAndAppendNew
125+
if c.HostKeyCallback != nil {
126+
config.HostKeyCallback = c.HostKeyCallback
120127
} else {
121-
config.HostKeyCallback = ssh.InsecureIgnoreHostKey()
128+
if c.CheckKnownHosts {
129+
if len(c.KnownHostsFiles) == 0 {
130+
// append default files
131+
c.KnownHostsFiles = append(c.KnownHostsFiles, "~/.ssh/known_hosts")
132+
}
133+
config.HostKeyCallback = c.verifyAndAppendNew
134+
} else {
135+
config.HostKeyCallback = ssh.InsecureIgnoreHostKey()
136+
}
122137
}
123138

124139
// check Dialer

0 commit comments

Comments
 (0)