upgrade easyssh-proxy (#69)

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu
2017-05-14 18:28:13 +08:00
committed by GitHub
parent 2ee4b54624
commit a363fa22bf
3 changed files with 40 additions and 29 deletions
+6 -6
View File
@@ -1,8 +1,7 @@
.PHONY: test drone-ssh fmt vet errcheck lint install update coverage embedmd
GOFMT ?= gofmt "-s"
GOFILES := find . -name "*.go" -type f -not -path "./vendor/*"
GOFILES := $(shell find . -name "*.go" -type f -not -path "./vendor/*")
PACKAGES ?= $(shell go list ./... | grep -v /vendor/)
all: install lint
@@ -14,16 +13,17 @@ install:
govendor sync
fmt:
$(GOFILES) | xargs $(GOFMT) -w
$(GOFMT) -w $(GOFILES)
.PHONY: fmt-check
fmt-check:
# get all go files and run go fmt on them
@files=$$($(GOFILES) | xargs $(GOFMT) -l); if [ -n "$$files" ]; then \
@diff=$$($(GOFMT) -d $(GOFILES)); \
if [ -n "$$diff" ]; then \
echo "Please run 'make fmt' and commit the result:"; \
echo "$${files}"; \
echo "$${diff}"; \
exit 1; \
fi;
fi;
vet:
go vet $(PACKAGES)
+29 -18
View File
@@ -86,8 +86,9 @@ func getSSHConfig(config DefaultConfig) *ssh.ClientConfig {
}
if config.Key != "" {
signer, _ := ssh.ParsePrivateKey([]byte(config.Key))
auths = append(auths, ssh.PublicKeys(signer))
if signer, err := ssh.ParsePrivateKey([]byte(config.Key)); err == nil {
auths = append(auths, ssh.PublicKeys(signer))
}
}
return &ssh.ClientConfig{
@@ -155,36 +156,44 @@ 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 int) (stdout chan string, stderr chan string, done chan bool, err error) {
func (ssh_conf *MakeConfig) Stream(command string, timeout int) (stdout chan string, stderr chan string, done chan bool, errChan chan error, err error) {
// connect to remote host
session, err := ssh_conf.connect()
if err != nil {
return stdout, stderr, done, err
return stdout, stderr, done, errChan, err
}
// defer session.Close()
// connect to both outputs (they are of type io.Reader)
outReader, err := session.StdoutPipe()
if err != nil {
return stdout, stderr, done, err
return stdout, stderr, done, errChan, err
}
errReader, err := session.StderrPipe()
if err != nil {
return stdout, stderr, done, err
return stdout, stderr, done, errChan, err
}
err = session.Start(command)
if err != nil {
return stdout, stderr, done, errChan, err
}
// combine outputs, create a line-by-line scanner
stdoutReader := io.MultiReader(outReader)
stderrReader := io.MultiReader(errReader)
err = session.Start(command)
stdoutScanner := bufio.NewScanner(stdoutReader)
stderrScanner := bufio.NewScanner(stderrReader)
// continuously send the command's output over the channel
stdoutChan := make(chan string)
stderrChan := make(chan string)
done = make(chan bool)
errChan = make(chan error)
go func(stdoutScanner, stderrScanner *bufio.Scanner, stdoutChan, stderrChan chan string, done chan bool) {
go func(stdoutScanner, stderrScanner *bufio.Scanner, stdoutChan, stderrChan chan string, done chan bool, errChan chan error) {
defer close(stdoutChan)
defer close(stderrChan)
defer close(done)
defer close(errChan)
defer session.Close()
timeoutChan := time.After(time.Duration(timeout) * time.Second)
res := make(chan bool, 1)
@@ -202,23 +211,21 @@ func (ssh_conf *MakeConfig) Stream(command string, timeout int) (stdout chan str
select {
case <-res:
stdoutChan <- ""
stderrChan <- ""
errChan <- session.Wait()
done <- true
case <-timeoutChan:
stdoutChan <- ""
stderrChan <- "Run Command Timeout!"
errChan <- nil
done <- false
}
}(stdoutScanner, stderrScanner, stdoutChan, stderrChan, done, errChan)
session.Close()
}(stdoutScanner, stderrScanner, stdoutChan, stderrChan, done)
return stdoutChan, stderrChan, done, err
return stdoutChan, stderrChan, done, errChan, err
}
// Run command on remote machine and returns its stdout as a string
func (ssh_conf *MakeConfig) Run(command string, timeout int) (outStr string, errStr string, isTimeout bool, err error) {
stdoutChan, stderrChan, doneChan, err := ssh_conf.Stream(command, timeout)
stdoutChan, stderrChan, doneChan, errChan, err := ssh_conf.Stream(command, timeout)
if err != nil {
return outStr, errStr, isTimeout, err
}
@@ -236,6 +243,7 @@ func (ssh_conf *MakeConfig) Run(command string, timeout int) (outStr string, err
if errline != "" {
errStr += errline + "\n"
}
case err = <-errChan:
}
}
// return the concatenation of all signals from the output channel
@@ -266,17 +274,20 @@ func (ssh_conf *MakeConfig) Scp(sourceFile string, etargetFile string) error {
}
go func() {
w, _ := session.StdinPipe()
w, err := session.StdinPipe()
if err != nil {
return
}
defer w.Close()
fmt.Fprintln(w, "C0644", srcStat.Size(), targetFile)
if srcStat.Size() > 0 {
io.Copy(w, src)
fmt.Fprint(w, "\x00")
w.Close()
} else {
fmt.Fprint(w, "\x00")
w.Close()
}
}()
+5 -5
View File
@@ -11,12 +11,12 @@
"versionExact": "master"
},
{
"checksumSHA1": "L3PugNJJOEpRmRbD+27LgTZC2E4=",
"checksumSHA1": "YgrgNVNBf7Ro0f3KuiHewOCHrwo=",
"path": "github.com/appleboy/easyssh-proxy",
"revision": "a13ed86767b8e8a24d8147a4909a702e7cf6b465",
"revisionTime": "2017-04-14T13:46:38Z",
"version": "1.1.2",
"versionExact": "1.1.2"
"revision": "14882d1d04ac6a85700586997695fcd936470e86",
"revisionTime": "2017-05-11T07:07:30Z",
"version": "1.1.5",
"versionExact": "1.1.5"
},
{
"checksumSHA1": "dvabztWVQX8f6oMLRyv4dLH+TGY=",