文章目录:

  • 指标
    • golang.org/x/crypto
    • gopkg.in/yaml.v2
  • 留神

    • 本文讲的是客户端局部

文章应用到的软件:

  • Mac 12.0 Beta(macOS Monterey),处理器为:M1
  • Goland 2021.1.3
  • Golang 1.17beta1

指标

  • 通过Go在客户端实现ssh隧道性能并连贯到服务器的mysql

Go程序

  • Gitee 网址
  • Github 网址

在工作目录创立一个go应用程序,并配置SSH的信息....还是看正文吧! 阿巴阿巴阿巴

package mainfunc main() {    // 设置SSH配置    config := &ssh.ClientConfig{      // 服务器用户名        User: "",        Auth: []ssh.AuthMethod{        // 服务器明码            ssh.Password(""),        },        Timeout: 30 * time.Second,        HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {            return nil        },    }      // 设置本地监听器,格局:地址:端口  localListener, err := net.Listen("tcp", "localhost:13306")    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)    }}// 转发func forward(localConn net.Conn, config *ssh.ClientConfig) {    // 设置服务器地址,格局:地址:端口    sshClientConn, err := ssh.Dial("tcp", "", config)    if err != nil {        fmt.Printf("ssh.Dial failed: %s", err)    }    // 设置近程地址,格局:地址:端口(请在服务器通过 ifconfig 查看地址)    sshConn, err := sshClientConn.Dial("tcp", "")    // 将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)        }    }()}

疾速安顿

给不想写的敌人们安顿了一个扩大包 (早说嘛...)

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

扩大包应用

package mainimport (    "github.com/dtapps/dtapps/go-ssh-tunnel/dssh")func main() {    dssh.Tunnel("root", "", ":22", ":3306", "localhost:13306")}