commit ab7282118a23acd4a577161f7a3289235e55c257 Author: 李光春 Date: Thu May 19 12:51:58 2022 +0800 - init diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..511f0fc --- /dev/null +++ b/.drone.yml @@ -0,0 +1,11 @@ +kind: pipeline +type: docker +name: clone + +steps: + - name: test-golang + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -v ./... \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..442794a --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.env +.git +.svn +.idea +.vscode +*.log +gomod.sh +/vendor/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..353b88d --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +

+Golang Ssh +

+ +📦 Golang Ssh + +[comment]: <> (go) +[![godoc](https://pkg.go.dev/badge/go.dtapp.net/gossh?status.svg)](https://pkg.go.dev/go.dtapp.net/gossh) +[![goproxy.cn](https://goproxy.cn/stats/go.dtapp.net/gossh/badges/download-count.svg)](https://goproxy.cn/stats/go.dtapp.net/gossh) +[![goreportcard.com](https://goreportcard.com/badge/go.dtapp.net/gossh )](https://goreportcard.com/report/go.dtapp.net/gossh) +[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/go.dtapp.net/gossh) + +#### 安装使用 + +```go +go get -v -u go.dtapp.net/gossh +``` + +#### 导入 + +```go +import ( + "go.dtapp.net/gossh" +) +``` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4e9d26e --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module go.dtapp.net/gossh + +go 1.18 + +require golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 + +require golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..474095b --- /dev/null +++ b/go.sum @@ -0,0 +1,5 @@ +golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 h1:SLP7Q4Di66FONjDJbCYrCRrh97focO6sLogHO7/g8F0= +golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= diff --git a/gossh.go b/gossh.go new file mode 100644 index 0000000..ffb2bf8 --- /dev/null +++ b/gossh.go @@ -0,0 +1,94 @@ +package gossh + +import ( + "golang.org/x/crypto/ssh" + "io" + "log" + "net" + "time" +) + +// SshConfig 配置 +type SshConfig struct { + Username string + Password string + ServerAddr string + RemoteAddr string + LocalAddr string +} + +type Ssh struct { + SshConfig +} + +// NewSsh 实例化 +func NewSsh(config *SshConfig) *Ssh { + app := &Ssh{} + app.Username = config.Username + app.Password = config.Password + app.ServerAddr = config.ServerAddr + app.RemoteAddr = config.RemoteAddr + app.LocalAddr = config.LocalAddr + return app +} + +func (app *Ssh) Tunnel() { + + // 设置log配置 + log.Printf("%s,服务器:%s;远程:%s;本地:%s\n", "设置SSH配置", app.ServerAddr, app.RemoteAddr, app.LocalAddr) + + config := &ssh.ClientConfig{ + User: app.Username, + Auth: []ssh.AuthMethod{ + ssh.Password(app.Password), + }, + Timeout: 30 * time.Second, + HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { + return nil + }, + } + + // 设置本地监听器 + localListener, err := net.Listen("tcp", app.LocalAddr) + if err != nil { + log.Printf("设置本地监听器失败: %v\n", err) + } + + for { + // 设置本地 + localConn, err := localListener.Accept() + if err != nil { + log.Printf("设置本地失败: %v\n", err) + } + go sForward(app.ServerAddr, app.RemoteAddr, localConn, config) + } +} + +// 转发 +func sForward(serverAddr string, remoteAddr string, localConn net.Conn, config *ssh.ClientConfig) { + + // 设置sshClientConn + sshClientConn, err := ssh.Dial("tcp", serverAddr, config) + if err != nil { + log.Printf("连接失败: %s", err) + } + + // 设置Connection + sshConn, err := sshClientConn.Dial("tcp", remoteAddr) + + // 将localConn.Reader复制到sshConn.Writer + go func() { + _, err = io.Copy(sshConn, localConn) + if err != nil { + log.Printf("复制失败: %v", err) + } + }() + + // 将sshConn.Reader复制到localConn.Writer + go func() { + _, err = io.Copy(localConn, sshConn) + if err != nil { + log.Printf("复制失败: %v", err) + } + }() +} diff --git a/gossh_test.go b/gossh_test.go new file mode 100644 index 0000000..fc1731a --- /dev/null +++ b/gossh_test.go @@ -0,0 +1,9 @@ +package gossh + +import ( + "testing" +) + +func TestClient(t *testing.T) { + //Tunnel("root", "", ":22", ":3306", "localhost:13306") +} diff --git a/version.go b/version.go new file mode 100644 index 0000000..d1778e4 --- /dev/null +++ b/version.go @@ -0,0 +1,3 @@ +package gossh + +const Version = "1.0.0"