Skip to content

Commit 5223f7b

Browse files
authored
Merge pull request #34 from blacknon/develop
Version 0.1.11
2 parents bc219a9 + 9be7ee4 commit 5223f7b

13 files changed

+411
-148
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919

2020
_TEST
2121
_test
22+
23+
test_termlog

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ env:
66
- GO111MODULE=on
77

88
go:
9-
- 1.12.x
9+
- 1.22.x
1010

1111
git:
1212
depth: 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2024 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 http dynamic forwarding Example file.
6+
// Change the value of the variable and compile to make sure that you can actually connect.
7+
//
8+
// This file uses password 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 = "proxy.com"
22+
port = "22"
23+
user = "user"
24+
password = "password"
25+
localAddr = "localhost:10080"
26+
remoteAddr = "localhost:10080"
27+
28+
termlog = "./test_termlog"
29+
)
30+
31+
func main() {
32+
// Create sshlib.Connect
33+
con := &sshlib.Connect{}
34+
35+
// Create ssh.AuthMethod
36+
authMethod := sshlib.CreateAuthMethodPassword(password)
37+
38+
// Connect ssh server
39+
err := con.CreateClient(host, port, user, []ssh.AuthMethod{authMethod})
40+
if err != nil {
41+
fmt.Println(err)
42+
os.Exit(1)
43+
}
44+
45+
// Dynamic PortForward
46+
go func() {
47+
err = con.HTTPDynamicForward("localhost", "10080")
48+
if err != nil {
49+
fmt.Println(err)
50+
}
51+
}()
52+
53+
// Set terminal log
54+
con.SetLog(termlog, false)
55+
56+
// Create session
57+
session, err := con.CreateSession()
58+
if err != nil {
59+
fmt.Println(err)
60+
os.Exit(1)
61+
}
62+
63+
// Start ssh shell
64+
con.Shell(session)
65+
}
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+
}

common.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"path/filepath"
1313
"strings"
1414

15-
"golang.org/x/crypto/ssh/terminal"
15+
terminal "golang.org/x/term"
1616
)
1717

1818
// getAbsPath return absolute path convert.

connect.go

+32-9
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ package sshlib
66

77
import (
88
"io"
9+
"log"
910
"net"
1011
"os"
1112
"os/signal"
1213
"syscall"
1314
"time"
1415

1516
"golang.org/x/crypto/ssh"
16-
"golang.org/x/crypto/ssh/terminal"
1717
"golang.org/x/net/proxy"
18+
terminal "golang.org/x/term"
1819
)
1920

2021
// Connect structure to store contents about ssh connection.
@@ -41,14 +42,23 @@ type Connect struct {
4142
SendKeepAliveInterval int
4243

4344
// Session use tty flag.
45+
// Set it before CraeteClient.
4446
TTY bool
4547

4648
// Forward ssh agent flag.
49+
// Set it before CraeteClient.
4750
ForwardAgent bool
4851

4952
// CheckKnownHosts if true, check knownhosts.
53+
// Ignored if HostKeyCallback is set.
54+
// Set it before CraeteClient.
5055
CheckKnownHosts bool
5156

57+
// HostKeyCallback is ssh.HostKeyCallback.
58+
// This item takes precedence over `CheckKnownHosts`.
59+
// Set it before CraeteClient.
60+
HostKeyCallback ssh.HostKeyCallback
61+
5262
// OverwriteKnownHosts if true, if the knownhost is different, check whether to overwrite.
5363
OverwriteKnownHosts bool
5464

@@ -73,11 +83,21 @@ type Connect struct {
7383

7484
// ssh-agent interface.
7585
// agent.Agent or agent.ExtendedAgent
86+
// Set it before CraeteClient.
7687
Agent AgentInterface
7788

7889
// Forward x11 flag.
90+
// Set it before CraeteClient.
7991
ForwardX11 bool
8092

93+
// Forward X11 trusted flag.
94+
// This flag is ssh -Y option like flag.
95+
// Set it before CraeteClient.
96+
ForwardX11Trusted bool
97+
98+
//
99+
DynamicForwardLogger *log.Logger
100+
81101
// shell terminal log flag
82102
logging bool
83103

@@ -107,14 +127,18 @@ func (c *Connect) CreateClient(host, port, user string, authMethods []ssh.AuthMe
107127
Timeout: time.Duration(timeout) * time.Second,
108128
}
109129

110-
if c.CheckKnownHosts {
111-
if len(c.KnownHostsFiles) == 0 {
112-
// append default files
113-
c.KnownHostsFiles = append(c.KnownHostsFiles, "~/.ssh/known_hosts")
114-
}
115-
config.HostKeyCallback = c.verifyAndAppendNew
130+
if c.HostKeyCallback != nil {
131+
config.HostKeyCallback = c.HostKeyCallback
116132
} else {
117-
config.HostKeyCallback = ssh.InsecureIgnoreHostKey()
133+
if c.CheckKnownHosts {
134+
if len(c.KnownHostsFiles) == 0 {
135+
// append default files
136+
c.KnownHostsFiles = append(c.KnownHostsFiles, "~/.ssh/known_hosts")
137+
}
138+
config.HostKeyCallback = c.verifyAndAppendNew
139+
} else {
140+
config.HostKeyCallback = ssh.InsecureIgnoreHostKey()
141+
}
118142
}
119143

120144
// check Dialer
@@ -197,7 +221,6 @@ func (c *Connect) CheckClientAlive() error {
197221

198222
// RequestTty requests the association of a pty with the session on the remote
199223
// host. Terminal size is obtained from the currently connected terminal
200-
//
201224
func RequestTty(session *ssh.Session) (err error) {
202225
modes := ssh.TerminalModes{
203226
ssh.ECHO: 1,

0 commit comments

Comments
 (0)