refactor: upgrade easyssh to support DefaultConfig (#61)

This commit is contained in:
Bo-Yi Wu 2017-04-01 13:57:53 +08:00 committed by GitHub
parent d447bbd595
commit 05b1a61165
7 changed files with 155 additions and 52 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"os"
"github.com/appleboy/easyssh-proxy"
"github.com/joho/godotenv"
_ "github.com/joho/godotenv/autoload"
"github.com/urfave/cli"
@ -168,7 +169,7 @@ func run(c *cli.Context) error {
Timeout: c.Duration("timeout"),
CommandTimeout: c.Int("command.timeout"),
Script: c.StringSlice("script"),
Proxy: defaultConfig{
Proxy: easyssh.DefaultConfig{
Key: c.String("proxy.ssh-key"),
KeyPath: c.String("proxy.key-path"),
User: c.String("proxy.username"),

View File

@ -20,16 +20,6 @@ const (
)
type (
defaultConfig struct {
User string
Server string
Key string
KeyPath string
Port string
Password string
Timeout time.Duration
}
// Config for the plugin.
Config struct {
Key string
@ -41,7 +31,7 @@ type (
Timeout time.Duration
CommandTimeout int
Script []string
Proxy defaultConfig
Proxy easyssh.DefaultConfig
}
// Plugin structure
@ -78,7 +68,7 @@ func (p Plugin) Exec() error {
Key: p.Config.Key,
KeyPath: p.Config.KeyPath,
Timeout: p.Config.Timeout,
Proxy: defaultConfig{
Proxy: easyssh.DefaultConfig{
Server: p.Config.Proxy.Server,
User: p.Config.Proxy.User,
Password: p.Config.Proxy.Password,

View File

@ -3,6 +3,7 @@ package main
import (
"testing"
"github.com/appleboy/easyssh-proxy"
"github.com/stretchr/testify/assert"
)
@ -129,7 +130,7 @@ func TestProxyCommand(t *testing.T) {
KeyPath: "./tests/.ssh/id_rsa",
Script: []string{"whoami"},
CommandTimeout: 1,
Proxy: defaultConfig{
Proxy: easyssh.DefaultConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",

View File

@ -1,4 +1,4 @@
.PHONY: test drone-ssh build fmt vet errcheck lint install update release-dirs release-build release-copy release-check release coverage
.PHONY: test drone-ssh build fmt vet errcheck lint install update release-dirs release-build release-copy release-check release coverage embedmd
PACKAGES ?= $(shell go list ./... | grep -v /vendor/)
@ -28,6 +28,12 @@ unconvert:
fi
for PKG in $(PACKAGES); do unconvert -v $$PKG || exit 1; done;
embedmd:
@hash embedmd > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
go get -u github.com/campoy/embedmd; \
fi
embedmd -d *.md
test:
for PKG in $(PACKAGES); do go test -v -cover -coverprofile $$GOPATH/src/$$PKG/coverage.txt $$PKG || exit 1; done;

View File

@ -1,5 +1,125 @@
# easyssh-proxy
[![GoDoc](https://godoc.org/github.com/appleboy/easyssh-proxy?status.svg)](https://godoc.org/github.com/appleboy/easyssh-proxy) [![Build Status](http://drone.wu-boy.com/api/badges/appleboy/easyssh-proxy/status.svg)](http://drone.wu-boy.com/appleboy/easyssh-proxy) [![codecov](https://codecov.io/gh/appleboy/easyssh-proxy/branch/master/graph/badge.svg)](https://codecov.io/gh/appleboy/easyssh-proxy) [![Go Report Card](https://goreportcard.com/badge/github.com/appleboy/easyssh-proxy)](https://goreportcard.com/report/github.com/appleboy/easyssh-proxy)
[![GoDoc](https://godoc.org/github.com/appleboy/easyssh-proxy?status.svg)](https://godoc.org/github.com/appleboy/easyssh-proxy) [![Build Status](http://drone.wu-boy.com/api/badges/appleboy/easyssh-proxy/status.svg)](http://drone.wu-boy.com/appleboy/easyssh-proxy) [![codecov](https://codecov.io/gh/appleboy/easyssh-proxy/branch/master/graph/badge.svg)](https://codecov.io/gh/appleboy/easyssh-proxy) [![Go Report Card](https://goreportcard.com/badge/github.com/appleboy/easyssh-proxy)](https://goreportcard.com/report/github.com/appleboy/easyssh-proxy) [![Sourcegraph](https://sourcegraph.com/github.com/appleboy/easyssh-proxy/-/badge.svg)](https://sourcegraph.com/github.com/appleboy/easyssh-proxy?badge)
easyssh-proxy provides a simple implementation of some SSH protocol features in Go
easyssh-proxy provides a simple implementation of some SSH protocol features in Go.
## Feature
This project is forked from [easyssh](https://github.com/hypersleep/easyssh) but add some features as the following.
* [x] Support plain text of user private key.
* [x] Support key path of user private key.
* [x] Support Timeout for the TCP connection to establish.
* [x] Support SSH ProxyCommand.
```
+--------+ +----------+ +-----------+
| Laptop | <--> | Jumphost | <--> | FooServer |
+--------+ +----------+ +-----------+
OR
+--------+ +----------+ +-----------+
| Laptop | <--> | Firewall | <--> | FooServer |
+--------+ +----------+ +-----------+
192.168.1.5 121.1.2.3 10.10.29.68
```
## Usage:
You can see `ssh`, `scp`, `ProxyCommand` on `examples` folder.
### ssh
See [example/ssh/ssh.go](./example/ssh/ssh.go)
[embedmd]:# (example/ssh/ssh.go go)
```go
package main
import (
"fmt"
"github.com/appleboy/easyssh-proxy"
)
func main() {
// Create MakeConfig instance with remote username, server address and path to private key.
ssh := &easyssh.MakeConfig{
User: "appleboy",
Server: "example.com",
// Optional key or Password without either we try to contact your agent SOCKET
//Password: "password",
Key: "/.ssh/id_rsa",
Port: "22",
Timeout: 60,
}
// Call Run method with command you want to run on remote server.
stdout, stderr, done, err := ssh.Run("ls -al", 60)
// Handle errors
if err != nil {
panic("Can't run remote command: " + err.Error())
} else {
fmt.Println("don is :", done, "stdout is :", stdout, "; stderr is :", stderr)
}
}
```
### scp
See [example/scp/scp.go](./example/scp/scp.go)
[embedmd]:# (example/scp/scp.go go)
```go
package main
import (
"fmt"
"github.com/appleboy/easyssh-proxy"
)
func main() {
// Create MakeConfig instance with remote username, server address and path to private key.
ssh := &easyssh.MakeConfig{
User: "appleboy",
Server: "example.com",
Password: "123qwe",
Port: "22",
}
// Call Scp method with file you want to upload to remote server.
// Please make sure the `tmp` floder exists.
err := ssh.Scp("/root/source.csv", "/tmp/target.csv")
// Handle errors
if err != nil {
panic("Can't run remote command: " + err.Error())
} else {
fmt.Println("success")
}
}
```
### SSH ProxyCommand
See [example/proxy/proxy.go](./example/proxy/proxy.go)
[embedmd]:# (example/proxy/proxy.go go /\tssh :=/ /\t}$/)
```go
ssh := &easyssh.MakeConfig{
User: "drone-scp",
Server: "localhost",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
Proxy: easyssh.DefaultConfig{
User: "drone-scp",
Server: "localhost",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
},
}
```

View File

@ -18,24 +18,14 @@ import (
"golang.org/x/crypto/ssh/agent"
)
// MakeConfig Contains main authority information.
// User field should be a name of user on remote server (ex. john in ssh john@example.com).
// Server field should be a remote machine address (ex. example.com in ssh john@example.com)
// Key is a path to private key on your local machine.
// 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 (
defaultConfig struct {
User string
Server string
Key string
KeyPath string
Port string
Password string
Timeout time.Duration
}
// MakeConfig Contains main authority information.
// User field should be a name of user on remote server (ex. john in ssh john@example.com).
// Server field should be a remote machine address (ex. example.com in ssh john@example.com)
// Key is a path to private key on your local machine.
// 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)
MakeConfig struct {
User string
Server string
@ -44,21 +34,16 @@ type (
Port string
Password string
Timeout time.Duration
Proxy struct {
User string
Server string
Key string
KeyPath string
Port string
Password string
Timeout time.Duration
}
Proxy DefaultConfig
}
sshConfig struct {
// DefaultConfig for ssh proxy config
DefaultConfig struct {
User string
Server string
Key string
KeyPath string
Port string
Password string
Timeout time.Duration
}
@ -80,7 +65,7 @@ func getKeyFile(keypath string) (ssh.Signer, error) {
return pubkey, nil
}
func getSSHConfig(config sshConfig) *ssh.ClientConfig {
func getSSHConfig(config DefaultConfig) *ssh.ClientConfig {
// auths holds the detected ssh auth methods
auths := []ssh.AuthMethod{}
@ -117,7 +102,7 @@ func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) {
var client *ssh.Client
var err error
targetConfig := getSSHConfig(sshConfig{
targetConfig := getSSHConfig(DefaultConfig{
User: ssh_conf.User,
Key: ssh_conf.Key,
KeyPath: ssh_conf.KeyPath,
@ -127,7 +112,7 @@ func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) {
// Enable proxy command
if ssh_conf.Proxy.Server != "" {
proxyConfig := getSSHConfig(sshConfig{
proxyConfig := getSSHConfig(DefaultConfig{
User: ssh_conf.Proxy.User,
Key: ssh_conf.Proxy.Key,
KeyPath: ssh_conf.Proxy.KeyPath,

10
vendor/vendor.json vendored
View File

@ -3,12 +3,12 @@
"ignore": "test",
"package": [
{
"checksumSHA1": "knoaYH97GvBuW65HyXhR0CQwEJ8=",
"checksumSHA1": "KzIglkvAb68+jcmwajNkQLwa1R0=",
"path": "github.com/appleboy/easyssh-proxy",
"revision": "438121ffb50f6f6de791bceb7046e74c1d818c3d",
"revisionTime": "2017-03-04T08:24:35Z",
"version": "=1.1.0",
"versionExact": "1.1.0"
"revision": "b5f58761a17ac54f2b0008fb8dcc883c73bf289d",
"revisionTime": "2017-03-14T07:47:33Z",
"version": "1.1.1",
"versionExact": "1.1.1"
},
{
"checksumSHA1": "dvabztWVQX8f6oMLRyv4dLH+TGY=",