Skip to content

Commit

Permalink
增加扩展包方式
Browse files Browse the repository at this point in the history
  • Loading branch information
dtapps committed Jun 25, 2021
1 parent e32f9f8 commit 1f8256c
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 51 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
## v1.0.0 / 2021-06-25
## v1.0.2 / 2021-06-25

- Golang SSH 隧道
- Golang SSH 隧道
- 增加扩展包方式
112 changes: 63 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,76 +11,90 @@

[comment]: <> (go)
![golang version](https://img.shields.io/badge/golang-%3E%3D1.6-8892BF.svg?style=for-the-badge)
![godoc](https://pkg.go.dev/badge/github.com/dtapps/go-ssh-Tunnel?status.svg)
![godoc](https://pkg.go.dev/badge/github.com/dtapps/go-ssh-tunnel?status.svg)

[comment]: <> (goproxy.cn)
![goproxy](https://goproxy.cn/stats/github.com/dtapps/go-ssh-Tunnel/badges/download-count.svg)
![goproxy](https://goproxy.cn/stats/github.com/dtapps/go-ssh-tunnel/badges/download-count.svg)

[comment]: <> (goreportcard.com)
![go report card](https://goreportcard.com/badge/github.com/dtapps/go-ssh-Tunnel)
![go report card](https://goreportcard.com/badge/github.com/dtapps/go-ssh-tunnel)

[comment]: <> (badge.fury.io)
![go project version](https://badge.fury.io/go/github.com%2Fdtapps%2Fgo-ssh-Tunnel.svg)
![go project version](https://badge.fury.io/go/github.com%2Fdtapps%2Fgo-ssh-tunnel.svg)

[comment]: <> (github.com)
![latest release](https://badgen.net/github/release/dtapps/go-ssh-Tunnel)
![latest stable release](https://badgen.net/github/release/dtapps/go-ssh-Tunnel/stable)
![latest tag](https://badgen.net/github/tag/dtapps/go-ssh-Tunnel)
![watchers](https://badgen.net/github/watchers/dtapps/go-ssh-Tunnel)
![combined checks (master branch)](https://badgen.net/github/checks/dtapps/go-ssh-Tunnel)
![stars](https://badgen.net/github/stars/dtapps/go-ssh-Tunnel)
![forks](https://badgen.net/github/forks/dtapps/go-ssh-Tunnel)
![issues](https://badgen.net/github/issues/dtapps/go-ssh-Tunnel)
![branches](https://badgen.net/github/branches/dtapps/go-ssh-Tunnel)
![releases](https://badgen.net/github/releases/dtapps/go-ssh-Tunnel)
![tags](https://badgen.net/github/tags/dtapps/go-ssh-Tunnel)
![license](https://badgen.net/github/license/dtapps/go-ssh-Tunnel)
![contributors](https://badgen.net/github/contributors/dtapps/go-ssh-Tunnel)
![assets downloads for latest release](https://badgen.net/github/assets-dl/dtapps/go-ssh-Tunnel)
![assets downloads for a tag](https://badgen.net/github/assets-dl/dtapps/go-ssh-Tunnel/1.0.11)
![repository dependents](https://badgen.net/github/dependents-repo/dtapps/go-ssh-Tunnel)
![package dependents](https://badgen.net/github/dependents-pkg/dtapps/go-ssh-Tunnel)
![dependabot status](https://badgen.net/github/dependabot/dtapps/go-ssh-Tunnel)
![GitHub last commit](https://img.shields.io/github/last-commit/dtapps/go-ssh-Tunnel?style=for-the-badge)
![GitHub commit activity](https://img.shields.io/github/commit-activity/w/dtapps/go-ssh-Tunnel?style=for-the-badge)
![Github All Contributors](https://img.shields.io/github/all-contributors/dtapps/go-ssh-Tunnel/master?style=for-the-badge)
![latest release](https://badgen.net/github/release/dtapps/go-ssh-tunnel)
![latest stable release](https://badgen.net/github/release/dtapps/go-ssh-tunnel/stable)
![latest tag](https://badgen.net/github/tag/dtapps/go-ssh-tunnel)
![watchers](https://badgen.net/github/watchers/dtapps/go-ssh-tunnel)
![combined checks (master branch)](https://badgen.net/github/checks/dtapps/go-ssh-tunnel)
![stars](https://badgen.net/github/stars/dtapps/go-ssh-tunnel)
![forks](https://badgen.net/github/forks/dtapps/go-ssh-tunnel)
![issues](https://badgen.net/github/issues/dtapps/go-ssh-tunnel)
![branches](https://badgen.net/github/branches/dtapps/go-ssh-tunnel)
![releases](https://badgen.net/github/releases/dtapps/go-ssh-tunnel)
![tags](https://badgen.net/github/tags/dtapps/go-ssh-tunnel)
![license](https://badgen.net/github/license/dtapps/go-ssh-tunnel)
![contributors](https://badgen.net/github/contributors/dtapps/go-ssh-tunnel)
![assets downloads for latest release](https://badgen.net/github/assets-dl/dtapps/go-ssh-tunnel)
![assets downloads for a tag](https://badgen.net/github/assets-dl/dtapps/go-ssh-tunnel/1.0.11)
![repository dependents](https://badgen.net/github/dependents-repo/dtapps/go-ssh-tunnel)
![package dependents](https://badgen.net/github/dependents-pkg/dtapps/go-ssh-tunnel)
![dependabot status](https://badgen.net/github/dependabot/dtapps/go-ssh-tunnel)
![GitHub last commit](https://img.shields.io/github/last-commit/dtapps/go-ssh-tunnel?style=for-the-badge)
![GitHub commit activity](https://img.shields.io/github/commit-activity/w/dtapps/go-ssh-tunnel?style=for-the-badge)
![Github All Contributors](https://img.shields.io/github/all-contributors/dtapps/go-ssh-tunnel/master?style=for-the-badge)
![GitHub go.mod Go version (branch)](https://img.shields.io/github/go-mod/go-version/dtapps/go-version/master?style=for-the-badge)
![GitHub go.mod Go version (subdirectory of monorepo)](https://img.shields.io/github/go-mod/go-version/dtapps/go-ssh-Tunnel?style=for-the-badge)
![GitHub go.mod Go version (branch & subdirectory of monorepo)](https://img.shields.io/github/go-mod/go-version/dtapps/go-ssh-Tunnel/master?style=for-the-badge)
![GitHub release (latest by date)](https://img.shields.io/github/v/release/dtapps/go-ssh-Tunnel?style=for-the-badge)
![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/dtapps/go-ssh-Tunnel?style=for-the-badge)
![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/dtapps/go-ssh-Tunnel?style=for-the-badge)
![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/dtapps/go-ssh-Tunnel?style=for-the-badge)
![GitHub pull requests](https://img.shields.io/github/issues-pr/dtapps/go-ssh-Tunnel?style=for-the-badge)
![GitHub issues](https://img.shields.io/github/issues/dtapps/go-ssh-Tunnel?style=for-the-badge)
![GitHub go.mod Go version (subdirectory of monorepo)](https://img.shields.io/github/go-mod/go-version/dtapps/go-ssh-tunnel?style=for-the-badge)
![GitHub go.mod Go version (branch & subdirectory of monorepo)](https://img.shields.io/github/go-mod/go-version/dtapps/go-ssh-tunnel/master?style=for-the-badge)
![GitHub release (latest by date)](https://img.shields.io/github/v/release/dtapps/go-ssh-tunnel?style=for-the-badge)
![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/dtapps/go-ssh-tunnel?style=for-the-badge)
![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/dtapps/go-ssh-tunnel?style=for-the-badge)
![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/dtapps/go-ssh-tunnel?style=for-the-badge)
![GitHub pull requests](https://img.shields.io/github/issues-pr/dtapps/go-ssh-tunnel?style=for-the-badge)
![GitHub issues](https://img.shields.io/github/issues/dtapps/go-ssh-tunnel?style=for-the-badge)
![GitHub Sponsors](https://img.shields.io/github/sponsors/dtapps?style=for-the-badge)
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/dtapps/go-ssh-Tunnel?style=for-the-badge)
![GitHub language count](https://img.shields.io/github/languages/count/dtapps/go-ssh-Tunnel?style=for-the-badge)
![GitHub search hit counter](https://img.shields.io/github/search/dtapps/go-ssh-Tunnel/go?style=for-the-badge)
![GitHub top language](https://img.shields.io/github/languages/top/dtapps/go-ssh-Tunnel?style=for-the-badge)
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/dtapps/go-ssh-tunnel?style=for-the-badge)
![GitHub language count](https://img.shields.io/github/languages/count/dtapps/go-ssh-tunnel?style=for-the-badge)
![GitHub search hit counter](https://img.shields.io/github/search/dtapps/go-ssh-tunnel/go?style=for-the-badge)
![GitHub top language](https://img.shields.io/github/languages/top/dtapps/go-ssh-tunnel?style=for-the-badge)

[comment]: <> (sourcegraph.com)
![sourcegraph](https://sourcegraph.com/github.com/dtapps/go-ssh-Tunnel/-/badge.svg)
![sourcegraph](https://sourcegraph.com/github.com/dtapps/go-ssh-tunnel/-/badge.svg)

[comment]: <> (scrutinizer-ci.com)
![Scrutinizer build (GitHub/Bitbucket)](https://img.shields.io/scrutinizer/build/g/dtapps/go-ssh-Tunnel/master?style=for-the-badge)
![Scrutinizer coverage (GitHub/BitBucket)](https://img.shields.io/scrutinizer/coverage/g/dtapps/go-ssh-Tunnel/master?style=for-the-badge)
![Scrutinizer code quality (GitHub/Bitbucket)](https://img.shields.io/scrutinizer/quality/g/dtapps/go-ssh-Tunnel/master?style=for-the-badge)
![Scrutinizer build (GitHub/Bitbucket)](https://img.shields.io/scrutinizer/build/g/dtapps/go-ssh-tunnel/master?style=for-the-badge)
![Scrutinizer coverage (GitHub/BitBucket)](https://img.shields.io/scrutinizer/coverage/g/dtapps/go-ssh-tunnel/master?style=for-the-badge)
![Scrutinizer code quality (GitHub/Bitbucket)](https://img.shields.io/scrutinizer/quality/g/dtapps/go-ssh-tunnel/master?style=for-the-badge)

[comment]: <> (www.codetriage.com)
![open source helpers](https://www.codetriage.com/dtapps/go-ssh-Tunnel/badges/users.svg)
![open source helpers](https://www.codetriage.com/dtapps/go-ssh-tunnel/badges/users.svg)

[comment]: <> (www.travis-ci.com)
![Travis (.com) branch](https://img.shields.io/travis/com/dtapps/go-ssh-Tunnel/master?style=for-the-badge)
![Travis (.com) branch](https://img.shields.io/travis/com/dtapps/go-ssh-tunnel/master?style=for-the-badge)

[comment]: <> (app.codecov.io)
![Codecov branch](https://img.shields.io/codecov/c/github/dtapps/go-ssh-Tunnel/master?style=for-the-badge)
![Codecov branch](https://img.shields.io/codecov/c/github/dtapps/go-ssh-tunnel/master?style=for-the-badge)

[comment]: <> (twitter.com)
![Twitter Follow](https://img.shields.io/twitter/follow/GC19980202?style=for-the-badge)
## 下载使用

## 使用
- 把.example.config.yaml文件重命名为config.yaml
- 修改对应的配置,运行即可

- 把.example.config.yaml文件重命名为config.yaml
- 修改对应的配置,运行即可
## 扩展包使用

```go
go get -u github.com/dtapps/go-ssh-tunnel
```

```go
package main

import (
"github.com/dtapps/go-ssh-tunnel"
)

func main() {
Tunnel("root", "", ":22", ":3306", "localhost:13306")
}
```
66 changes: 66 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"fmt"
"golang.org/x/crypto/ssh"
"io"
"net"
"time"
)

// 转发
func sForward(serverAddr string, remoteAddr string, localConn net.Conn, config *ssh.ClientConfig) {
// 设置sshClientConn
sshClientConn, err := ssh.Dial("tcp", serverAddr, config)
if err != nil {
fmt.Printf("ssh.Dial failed: %s", err)
}

// 设置Connection
sshConn, err := sshClientConn.Dial("tcp", remoteAddr)

// 将localConn.Reader复制到sshConn.Writer
go func() {
_, err = io.Copy(sshConn, localConn)
if err != nil {
fmt.Printf("io.Copy failed: %v", err)
}
}()

// 将sshConn.Reader复制到localConn.Writer
go func() {
_, err = io.Copy(localConn, sshConn)
if err != nil {
fmt.Printf("io.Copy failed: %v", err)
}
}()
}
func Tunnel(username string, password string, serverAddr string, remoteAddr string, localAddr string) {
// 设置SSH配置
fmt.Printf("%s,服务器:%s;远程:%s;本地:%s\n", "设置SSH配置", serverAddr, remoteAddr, localAddr)
config := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
Timeout: 30 * time.Second,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}

// 设置本地监听器
localListener, err := net.Listen("tcp", localAddr)
if err != nil {
fmt.Printf("net.Listen failed: %v\n", err)
}

for {
// 设置本地
localConn, err := localListener.Accept()
if err != nil {
fmt.Printf("localListener.Accept failed: %v\n", err)
}
go sForward(serverAddr, remoteAddr, localConn, config)
}
}
7 changes: 7 additions & 0 deletions service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "testing"

func TestName(t *testing.T) {
Tunnel("root", "", ":22", ":3306", "localhost:13306")
}

0 comments on commit 1f8256c

Please sign in to comment.