Revert "feat: upgrade easyssh-proxy and update flag of timeout field (#127)" (#128)

This reverts commit eb33537e3f.
This commit is contained in:
Bo-Yi Wu 2018-10-23 15:54:40 +08:00 committed by GitHub
parent eb33537e3f
commit 7ac526845d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 63 deletions

View File

@ -81,7 +81,7 @@ func main() {
Usage: "connection timeout", Usage: "connection timeout",
EnvVar: "PLUGIN_TIMEOUT,SSH_TIMEOUT", EnvVar: "PLUGIN_TIMEOUT,SSH_TIMEOUT",
}, },
cli.DurationFlag{ cli.IntFlag{
Name: "command.timeout,T", Name: "command.timeout,T",
Usage: "command timeout", Usage: "command timeout",
EnvVar: "PLUGIN_COMMAND_TIMEOUT,SSH_COMMAND_TIMEOUT", EnvVar: "PLUGIN_COMMAND_TIMEOUT,SSH_COMMAND_TIMEOUT",
@ -200,7 +200,7 @@ func run(c *cli.Context) error {
Host: c.StringSlice("host"), Host: c.StringSlice("host"),
Port: c.Int("port"), Port: c.Int("port"),
Timeout: c.Duration("timeout"), Timeout: c.Duration("timeout"),
CommandTimeout: c.Duration("command.timeout"), CommandTimeout: c.Int("command.timeout"),
Script: c.StringSlice("script"), Script: c.StringSlice("script"),
ScriptStop: c.Bool("script.stop"), ScriptStop: c.Bool("script.stop"),
Secrets: c.StringSlice("secrets"), Secrets: c.StringSlice("secrets"),

View File

@ -29,7 +29,7 @@ type (
Host []string Host []string
Port int Port int
Timeout time.Duration Timeout time.Duration
CommandTimeout time.Duration CommandTimeout int
Script []string Script []string
ScriptStop bool ScriptStop bool
Secrets []string Secrets []string

View File

@ -57,13 +57,7 @@ func main() {
Server: "example.com", Server: "example.com",
// Optional key or Password without either we try to contact your agent SOCKET // Optional key or Password without either we try to contact your agent SOCKET
//Password: "password", //Password: "password",
// Paste your source content of private key Key: "/.ssh/id_rsa",
// Key: `-----BEGIN RSA PRIVATE KEY-----
// MIIEpAIBAAKCAQEA4e2D/qPN08pzTac+a8ZmlP1ziJOXk45CynMPtva0rtK/RB26
// 7XC9wlRna4b3Ln8ew3q1ZcBjXwD4ppbTlmwAfQIaZTGJUgQbdsO9YA==
// -----END RSA PRIVATE KEY-----
// `,
KeyPath: "/Users/username/.ssh/id_rsa",
Port: "22", Port: "22",
Timeout: 60 * time.Second, Timeout: 60 * time.Second,
} }

View File

@ -9,11 +9,9 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
"sync"
"time" "time"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
@ -67,12 +65,7 @@ func getKeyFile(keypath string) (ssh.Signer, error) {
return pubkey, nil return pubkey, nil
} }
// returns *ssh.ClientConfig and io.Closer. func getSSHConfig(config DefaultConfig) *ssh.ClientConfig {
// if io.Closer is not nil, io.Closer.Close() should be called when
// *ssh.ClientConfig is no longer used.
func getSSHConfig(config DefaultConfig) (*ssh.ClientConfig, io.Closer) {
var sshAgent io.Closer
// auths holds the detected ssh auth methods // auths holds the detected ssh auth methods
auths := []ssh.AuthMethod{} auths := []ssh.AuthMethod{}
@ -80,62 +73,54 @@ func getSSHConfig(config DefaultConfig) (*ssh.ClientConfig, io.Closer) {
if config.Password != "" { if config.Password != "" {
auths = append(auths, ssh.Password(config.Password)) auths = append(auths, ssh.Password(config.Password))
} }
if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers))
defer sshAgent.Close()
}
if config.KeyPath != "" { if config.KeyPath != "" {
if pubkey, err := getKeyFile(config.KeyPath); err != nil { if pubkey, err := getKeyFile(config.KeyPath); err == nil {
log.Printf("getKeyFile: %v\n", err)
} else {
auths = append(auths, ssh.PublicKeys(pubkey)) auths = append(auths, ssh.PublicKeys(pubkey))
} }
} }
if config.Key != "" { if config.Key != "" {
if signer, err := ssh.ParsePrivateKey([]byte(config.Key)); err != nil { if signer, err := ssh.ParsePrivateKey([]byte(config.Key)); err == nil {
log.Printf("ssh.ParsePrivateKey: %v\n", err)
} else {
auths = append(auths, ssh.PublicKeys(signer)) auths = append(auths, ssh.PublicKeys(signer))
} }
} }
if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers))
}
return &ssh.ClientConfig{ return &ssh.ClientConfig{
Timeout: config.Timeout, Timeout: config.Timeout,
User: config.User, User: config.User,
Auth: auths, Auth: auths,
HostKeyCallback: ssh.InsecureIgnoreHostKey(), HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}, sshAgent }
} }
// Connect to remote server using MakeConfig struct and returns *ssh.Session // connect to remote server using MakeConfig struct and returns *ssh.Session
func (ssh_conf *MakeConfig) Connect() (*ssh.Session, error) { func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) {
var client *ssh.Client var client *ssh.Client
var err error var err error
targetConfig, closer := getSSHConfig(DefaultConfig{ targetConfig := getSSHConfig(DefaultConfig{
User: ssh_conf.User, User: ssh_conf.User,
Key: ssh_conf.Key, Key: ssh_conf.Key,
KeyPath: ssh_conf.KeyPath, KeyPath: ssh_conf.KeyPath,
Password: ssh_conf.Password, Password: ssh_conf.Password,
Timeout: ssh_conf.Timeout, Timeout: ssh_conf.Timeout,
}) })
if closer != nil {
defer closer.Close()
}
// Enable proxy command // Enable proxy command
if ssh_conf.Proxy.Server != "" { if ssh_conf.Proxy.Server != "" {
proxyConfig, closer := getSSHConfig(DefaultConfig{ proxyConfig := getSSHConfig(DefaultConfig{
User: ssh_conf.Proxy.User, User: ssh_conf.Proxy.User,
Key: ssh_conf.Proxy.Key, Key: ssh_conf.Proxy.Key,
KeyPath: ssh_conf.Proxy.KeyPath, KeyPath: ssh_conf.Proxy.KeyPath,
Password: ssh_conf.Proxy.Password, Password: ssh_conf.Proxy.Password,
Timeout: ssh_conf.Proxy.Timeout, Timeout: ssh_conf.Proxy.Timeout,
}) })
if closer != nil {
defer closer.Close()
}
proxyClient, err := ssh.Dial("tcp", net.JoinHostPort(ssh_conf.Proxy.Server, ssh_conf.Proxy.Port), proxyConfig) proxyClient, err := ssh.Dial("tcp", net.JoinHostPort(ssh_conf.Proxy.Server, ssh_conf.Proxy.Port), proxyConfig)
if err != nil { if err != nil {
@ -171,7 +156,7 @@ func (ssh_conf *MakeConfig) Connect() (*ssh.Session, error) {
// Stream returns one channel that combines the stdout and stderr of the command // Stream returns one channel that combines the stdout and stderr of the command
// as it is run on the remote machine, and another that sends true when the // as it is run on the remote machine, and another that sends true when the
// command is done. The sessions and channels will then be closed. // command is done. The sessions and channels will then be closed.
func (ssh_conf *MakeConfig) Stream(command string, timeout time.Duration) (<-chan string, <-chan string, <-chan bool, <-chan error, error) { func (ssh_conf *MakeConfig) Stream(command string, timeout int) (<-chan string, <-chan string, <-chan bool, <-chan error, error) {
// continuously send the command's output over the channel // continuously send the command's output over the channel
stdoutChan := make(chan string) stdoutChan := make(chan string)
stderrChan := make(chan string) stderrChan := make(chan string)
@ -179,7 +164,7 @@ func (ssh_conf *MakeConfig) Stream(command string, timeout time.Duration) (<-cha
errChan := make(chan error) errChan := make(chan error)
// connect to remote host // connect to remote host
session, err := ssh_conf.Connect() session, err := ssh_conf.connect()
if err != nil { if err != nil {
return stdoutChan, stderrChan, doneChan, errChan, err return stdoutChan, stderrChan, doneChan, errChan, err
} }
@ -211,29 +196,18 @@ func (ssh_conf *MakeConfig) Stream(command string, timeout time.Duration) (<-cha
defer close(errChan) defer close(errChan)
defer session.Close() defer session.Close()
timeoutChan := time.After(timeout * time.Second) timeoutChan := time.After(time.Duration(timeout) * time.Second)
res := make(chan struct{}, 1) res := make(chan bool, 1)
var resWg sync.WaitGroup
resWg.Add(2)
go func() { go func() {
for stdoutScanner.Scan() { for stdoutScanner.Scan() {
stdoutChan <- stdoutScanner.Text() stdoutChan <- stdoutScanner.Text()
} }
resWg.Done()
}()
go func() {
for stderrScanner.Scan() { for stderrScanner.Scan() {
stderrChan <- stderrScanner.Text() stderrChan <- stderrScanner.Text()
} }
resWg.Done()
}()
go func() {
resWg.Wait()
// close all of our open resources // close all of our open resources
res <- struct{}{} res <- true
}() }()
select { select {
@ -251,7 +225,7 @@ func (ssh_conf *MakeConfig) Stream(command string, timeout time.Duration) (<-cha
} }
// Run command on remote machine and returns its stdout as a string // Run command on remote machine and returns its stdout as a string
func (ssh_conf *MakeConfig) Run(command string, timeout time.Duration) (outStr string, errStr string, isTimeout bool, err error) { func (ssh_conf *MakeConfig) Run(command string, timeout int) (outStr string, errStr string, isTimeout bool, err error) {
stdoutChan, stderrChan, doneChan, errChan, err := ssh_conf.Stream(command, timeout) stdoutChan, stderrChan, doneChan, errChan, err := ssh_conf.Stream(command, timeout)
if err != nil { if err != nil {
return outStr, errStr, isTimeout, err return outStr, errStr, isTimeout, err
@ -279,7 +253,7 @@ loop:
// Scp uploads sourceFile to remote machine like native scp console app. // Scp uploads sourceFile to remote machine like native scp console app.
func (ssh_conf *MakeConfig) Scp(sourceFile string, etargetFile string) error { func (ssh_conf *MakeConfig) Scp(sourceFile string, etargetFile string) error {
session, err := ssh_conf.Connect() session, err := ssh_conf.connect()
if err != nil { if err != nil {
return err return err
@ -318,5 +292,9 @@ func (ssh_conf *MakeConfig) Scp(sourceFile string, etargetFile string) error {
} }
}() }()
return session.Run(fmt.Sprintf("scp -tr %s", etargetFile)) if err := session.Run(fmt.Sprintf("scp -tr %s", etargetFile)); err != nil {
return err
}
return nil
} }

10
vendor/vendor.json vendored
View File

@ -3,12 +3,12 @@
"ignore": "test", "ignore": "test",
"package": [ "package": [
{ {
"checksumSHA1": "s2s4GT8UfsxkawhCzjkKWi/XWLI=", "checksumSHA1": "EcF7T9tPEMMJfuRdPBB3NdRUg4c=",
"path": "github.com/appleboy/easyssh-proxy", "path": "github.com/appleboy/easyssh-proxy",
"revision": "9b6972862812dafd568ffb2cfc7b49510af7d502", "revision": "33d87eae3a018c3312e32cc4eb4578d5a563aabd",
"revisionTime": "2018-10-17T14:39:14Z", "revisionTime": "2017-05-16T07:22:25Z",
"version": "master", "version": "1.1.6",
"versionExact": "master" "versionExact": "1.1.6"
}, },
{ {
"checksumSHA1": "dvabztWVQX8f6oMLRyv4dLH+TGY=", "checksumSHA1": "dvabztWVQX8f6oMLRyv4dLH+TGY=",