提交 6d2bdfca 编写于 作者: 李光春's avatar 李光春

init

上级
# 服务器
username: "root" #服务器用户名
password: "" #服务器密码
serverAddr: "" #服务器地址
serverPort: "22" #服务器端口
remoteAddr: "" #远程地址
remotePort: "3306" #远程端口
# 本地
localAddr: "localhost" #地址
localPort: "13306" #端口
.env
.git
.svn
.idea
.vscode
*.log
git.sh
gittag.sh
gitinit.sh
config.yaml
\ No newline at end of file
## v1.0.0 / 2021-06-25
- Golang SSH 隧道
\ No newline at end of file
MIT License
Copyright (c) 2021 李光春
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
<img align="right" width="100" src="https://kodo-cdn.dtapp.net/04/999e9f2f06d396968eacc10ce9bc8a.png" alt="www.dtapp.net"/>
<h1 align="left"><a href="https://www.dtapp.net/">Golang SSH Tunnel</a></h1>
📦 Golang SSH 隧道
通过Go实现从本地连接到远程服务器的数据库
[comment]: <> (dtapps)
![GitHub Org's stars](https://img.shields.io/github/stars/dtapps?style=for-the-badge)
[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)
[comment]: <> (goproxy.cn)
![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)
[comment]: <> (badge.fury.io)
![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)
![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 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)
[comment]: <> (sourcegraph.com)
![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)
[comment]: <> (www.codetriage.com)
![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)
[comment]: <> (app.codecov.io)
![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
- 修改对应的配置,运行即可
module github.com/dtapps/go-ssh-Tunnel
go 1.16
require golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e
require gopkg.in/yaml.v2 v2.4.0
package main
import (
"fmt"
"golang.org/x/crypto/ssh"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"log"
"net"
"time"
)
// GlobConfig 定义一个 config结构体变量
var GlobConfig Config
// Config 声明 配置结构体
type Config struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
ServerAddr string `yaml:"serverAddr"`
ServerPort string `yaml:"serverPort"`
RemoteAddr string `yaml:"remoteAddr"`
RemotePort string `yaml:"remotePort"`
LocalAddr string `yaml:"localAddr"`
LocalPort string `yaml:"localPort"`
}
// 读取配置文件
func (c *Config) getConfig() *Config {
yamlFile, err := ioutil.ReadFile("config.yaml")
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
return c
}
// 初始化读取配置
func init() {
// 直接赋值给结构体
GlobConfig.getConfig()
}
// 转发
func forward(localConn net.Conn, config *ssh.ClientConfig) {
// 设置sshClientConn
sshClientConn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", GlobConfig.ServerAddr, GlobConfig.ServerPort), config)
if err != nil {
fmt.Printf("ssh.Dial failed: %s", err)
}
// 设置Connection
sshConn, err := sshClientConn.Dial("tcp", fmt.Sprintf("%s:%s", GlobConfig.RemoteAddr, GlobConfig.RemotePort))
// 将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 main() {
// 设置SSH配置
fmt.Printf("%s,服务器:%s:%s;远程:%s:%s;本地:%s:%s\n", "设置SSH配置", GlobConfig.ServerAddr, GlobConfig.ServerPort, GlobConfig.RemoteAddr, GlobConfig.RemotePort, GlobConfig.LocalAddr, GlobConfig.LocalPort)
config := &ssh.ClientConfig{
User: GlobConfig.Username,
Auth: []ssh.AuthMethod{
ssh.Password(GlobConfig.Password),
},
Timeout: 30 * time.Second,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}
// 设置本地监听器
localListener, err := net.Listen("tcp", fmt.Sprintf("%s:%s", GlobConfig.LocalAddr, GlobConfig.LocalPort))
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 forward(localConn, config)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册