feat: Support proxy command. (#55)

This commit is contained in:
Bo-Yi Wu
2017-03-04 18:31:49 +08:00
committed by GitHub
parent 7e4e0224ee
commit 530df8d98b
5 changed files with 165 additions and 25 deletions

View File

@@ -25,23 +25,44 @@ import (
// Port is SSH server port on remote machine.
// Note: easyssh looking for private key in user's home directory (ex. /home/john + Key).
// Then ensure your Key begins from '/' (ex. /.ssh/id_rsa)
type MakeConfig struct {
User string
Server string
Key string
KeyPath string
Port string
Password string
Timeout time.Duration
}
type (
defaultConfig struct {
User string
Server string
Key string
KeyPath string
Port string
Password string
Timeout time.Duration
}
type sshConfig struct {
User string
Key string
KeyPath string
Password string
Timeout time.Duration
}
MakeConfig struct {
User string
Server string
Key string
KeyPath string
Port string
Password string
Timeout time.Duration
Proxy struct {
User string
Server string
Key string
KeyPath string
Port string
Password string
Timeout time.Duration
}
}
sshConfig struct {
User string
Key string
KeyPath string
Password string
Timeout time.Duration
}
)
// returns ssh.Signer from user you running app home path + cutted key path.
// (ex. pubkey,err := getKeyFile("/.ssh/id_rsa") )
@@ -93,7 +114,10 @@ func getSSHConfig(config sshConfig) *ssh.ClientConfig {
// connect to remote server using MakeConfig struct and returns *ssh.Session
func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) {
config := getSSHConfig(sshConfig{
var client *ssh.Client
var err error
targetConfig := getSSHConfig(sshConfig{
User: ssh_conf.User,
Key: ssh_conf.Key,
KeyPath: ssh_conf.KeyPath,
@@ -101,9 +125,37 @@ func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) {
Timeout: ssh_conf.Timeout,
})
client, err := ssh.Dial("tcp", net.JoinHostPort(ssh_conf.Server, ssh_conf.Port), config)
if err != nil {
return nil, err
// Enable proxy command
if ssh_conf.Proxy.Server != "" {
proxyConfig := getSSHConfig(sshConfig{
User: ssh_conf.Proxy.User,
Key: ssh_conf.Proxy.Key,
KeyPath: ssh_conf.Proxy.KeyPath,
Password: ssh_conf.Proxy.Password,
Timeout: ssh_conf.Proxy.Timeout,
})
proxyClient, err := ssh.Dial("tcp", net.JoinHostPort(ssh_conf.Proxy.Server, ssh_conf.Proxy.Port), proxyConfig)
if err != nil {
return nil, err
}
conn, err := proxyClient.Dial("tcp", net.JoinHostPort(ssh_conf.Server, ssh_conf.Port))
if err != nil {
return nil, err
}
ncc, chans, reqs, err := ssh.NewClientConn(conn, net.JoinHostPort(ssh_conf.Server, ssh_conf.Port), targetConfig)
if err != nil {
return nil, err
}
client = ssh.NewClient(ncc, chans, reqs)
} else {
client, err = ssh.Dial("tcp", net.JoinHostPort(ssh_conf.Server, ssh_conf.Port), targetConfig)
if err != nil {
return nil, err
}
}
session, err := client.NewSession()

10
vendor/vendor.json vendored
View File

@@ -3,12 +3,12 @@
"ignore": "test",
"package": [
{
"checksumSHA1": "NCdmzR+clcl2/1jUPn0vFeqjwjk=",
"checksumSHA1": "knoaYH97GvBuW65HyXhR0CQwEJ8=",
"path": "github.com/appleboy/easyssh-proxy",
"revision": "89c61a4555c1578454f75ae406f4e3cdded275d2",
"revisionTime": "2017-03-04T06:27:13Z",
"version": "=1.0.0",
"versionExact": "1.0.0"
"revision": "438121ffb50f6f6de791bceb7046e74c1d818c3d",
"revisionTime": "2017-03-04T08:24:35Z",
"version": "=1.1.0",
"versionExact": "1.1.0"
},
{
"checksumSHA1": "dvabztWVQX8f6oMLRyv4dLH+TGY=",