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