Skip to content

Instantly share code, notes, and snippets.

@ubogdan
Forked from corny/sshtunnel.go
Created August 27, 2021 22:54
Show Gist options
  • Save ubogdan/7ea92d308b953863f8f6c8598cf11b40 to your computer and use it in GitHub Desktop.
Save ubogdan/7ea92d308b953863f8f6c8598cf11b40 to your computer and use it in GitHub Desktop.
SSH tunnelling in Golang
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"golang.org/x/crypto/ssh"
)
type SSHtunnel struct {
Local string
Server string
Remote string
Config *ssh.ClientConfig
}
func (tunnel *SSHtunnel) Start() error {
listener, err := net.Listen("tcp", tunnel.Local)
if err != nil {
return err
}
defer listener.Close()
sshClient, err := ssh.Dial("tcp", tunnel.Server, tunnel.Config)
if err != nil {
return err
}
for {
conn, err := listener.Accept()
if err != nil {
return err
}
go tunnel.forward(sshClient, conn)
}
}
func (tunnel *SSHtunnel) forward(sshClient *ssh.Client, localConn net.Conn) {
remoteConn, err := sshClient.Dial("tcp", tunnel.Remote)
if err != nil {
fmt.Printf("Remote dial error: %s\n", err)
return
}
copyConn := func(writer, reader net.Conn) {
_, err := io.Copy(writer, reader)
if err != nil {
fmt.Printf("io.Copy error: %s", err)
}
log.Println("closing channel", remoteConn.Close())
}
go copyConn(localConn, remoteConn)
go copyConn(remoteConn, localConn)
}
// Liest einen SSH-Key ein
func getKeyFile(path string) (ssh.Signer, error) {
buf, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return ssh.ParsePrivateKey(buf)
}
func main() {
key, err := getKeyFile(os.Getenv("HOME") + "/.ssh/id_rsa")
if err != nil {
panic(err)
}
sshConfig := &ssh.ClientConfig{
User: "root",
Auth: []ssh.AuthMethod{ssh.PublicKeys(key)},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}
tunnel := &SSHtunnel{
Config: sshConfig,
Server: "example.com:22",
Local: "localhost:9100",
Remote: "localhost:9100",
}
tunnel.Start()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment