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

View File

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

View File

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

View File

@ -9,11 +9,9 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"path/filepath"
"sync"
"time"
"golang.org/x/crypto/ssh"
@ -67,12 +65,7 @@ func getKeyFile(keypath string) (ssh.Signer, error) {
return pubkey, nil
}
// returns *ssh.ClientConfig and io.Closer.
// 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
func getSSHConfig(config DefaultConfig) *ssh.ClientConfig {
// auths holds the detected ssh auth methods
auths := []ssh.AuthMethod{}
@ -80,62 +73,54 @@ func getSSHConfig(config DefaultConfig) (*ssh.ClientConfig, io.Closer) {
if 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 pubkey, err := getKeyFile(config.KeyPath); err != nil {
log.Printf("getKeyFile: %v\n", err)
} else {
if pubkey, err := getKeyFile(config.KeyPath); err == nil {
auths = append(auths, ssh.PublicKeys(pubkey))
}
}
if config.Key != "" {
if signer, err := ssh.ParsePrivateKey([]byte(config.Key)); err != nil {
log.Printf("ssh.ParsePrivateKey: %v\n", err)
} else {
if signer, err := ssh.ParsePrivateKey([]byte(config.Key)); err == nil {
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{
Timeout: config.Timeout,
User: config.User,
Auth: auths,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}, sshAgent
}
}
// Connect to remote server using MakeConfig struct and returns *ssh.Session
func (ssh_conf *MakeConfig) Connect() (*ssh.Session, error) {
// connect to remote server using MakeConfig struct and returns *ssh.Session
func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) {
var client *ssh.Client
var err error
targetConfig, closer := getSSHConfig(DefaultConfig{
targetConfig := getSSHConfig(DefaultConfig{
User: ssh_conf.User,
Key: ssh_conf.Key,
KeyPath: ssh_conf.KeyPath,
Password: ssh_conf.Password,
Timeout: ssh_conf.Timeout,
})
if closer != nil {
defer closer.Close()
}
// Enable proxy command
if ssh_conf.Proxy.Server != "" {
proxyConfig, closer := getSSHConfig(DefaultConfig{
proxyConfig := getSSHConfig(DefaultConfig{
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,
})
if closer != nil {
defer closer.Close()
}
proxyClient, err := ssh.Dial("tcp", net.JoinHostPort(ssh_conf.Proxy.Server, ssh_conf.Proxy.Port), proxyConfig)
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
// 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.
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
stdoutChan := 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)
// connect to remote host
session, err := ssh_conf.Connect()
session, err := ssh_conf.connect()
if err != nil {
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 session.Close()
timeoutChan := time.After(timeout * time.Second)
res := make(chan struct{}, 1)
var resWg sync.WaitGroup
resWg.Add(2)
timeoutChan := time.After(time.Duration(timeout) * time.Second)
res := make(chan bool, 1)
go func() {
for stdoutScanner.Scan() {
stdoutChan <- stdoutScanner.Text()
}
resWg.Done()
}()
go func() {
for stderrScanner.Scan() {
stderrChan <- stderrScanner.Text()
}
resWg.Done()
}()
go func() {
resWg.Wait()
// close all of our open resources
res <- struct{}{}
res <- true
}()
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
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)
if err != nil {
return outStr, errStr, isTimeout, err
@ -279,7 +253,7 @@ loop:
// Scp uploads sourceFile to remote machine like native scp console app.
func (ssh_conf *MakeConfig) Scp(sourceFile string, etargetFile string) error {
session, err := ssh_conf.Connect()
session, err := ssh_conf.connect()
if err != nil {
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",
"package": [
{
"checksumSHA1": "s2s4GT8UfsxkawhCzjkKWi/XWLI=",
"checksumSHA1": "EcF7T9tPEMMJfuRdPBB3NdRUg4c=",
"path": "github.com/appleboy/easyssh-proxy",
"revision": "9b6972862812dafd568ffb2cfc7b49510af7d502",
"revisionTime": "2018-10-17T14:39:14Z",
"version": "master",
"versionExact": "master"
"revision": "33d87eae3a018c3312e32cc4eb4578d5a563aabd",
"revisionTime": "2017-05-16T07:22:25Z",
"version": "1.1.6",
"versionExact": "1.1.6"
},
{
"checksumSHA1": "dvabztWVQX8f6oMLRyv4dLH+TGY=",