15

I'm trying to create (and later close) a simple TCP port forward over SSH with Go. I'm new to Golang and statically typed languages. (Coming from Ruby.)

In a terminal I would simply run ssh -L 9000:localhost:9999 [email protected] and this accomplishes what I need. I want to do the same, programmatically with Go.

I have tried using this example as a starting point and this recent test to try to understand what to do, but now I have a pile of confusing jumbled code when it seems like this is actually a very simple thing to do.

Any help would be very much appreciated! :-)

4
  • 2
    What is the pile of jumbled code that you do have?
    – user289086
    Jan 28, 2014 at 1:37
  • Thanks MichaelT, the jumbled pile was successfully deleted and recreated in to the below answer. Thanks for your desire to help!
    – damick
    Feb 9, 2014 at 5:01
  • Do you think that (in your answer below) you could go through the steps of what it took to get to the final code, what the root cause was and how it was resolved?
    – user289086
    Feb 10, 2014 at 16:54
  • I hope those edits help. There was so much changed and changed again, it's hard to layout how I got there. :) Thanks again.
    – damick
    Feb 21, 2014 at 1:12

4 Answers 4

22

I finally figured out how to do this, I got hints from schmichael in an IRC channel. Thanks to all!

EDIT: A little explanation: A big part of the problem I was having was that I did not realize a local net.Listener (not just a local net.Conn) needed setup to receive a local request and create the net.Conn before forwarding the bytes. Also, there exist both port forwards and reverse port forwards and I hadn't previously thought in detail about the fact that a regular port forward also sends bytes back, so copying the remote reader to local writer was not something I had implemented, yet it's very much needed. Here is an attempt to relate the essence of what this code does:

  • Listen on local port 9000.
  • Upon attempted read from local port 9000: (listener.Accept()),
  • Accept connection and return a local io.Reader and io.Writer and,
  • Connect to remote server and,
  • Connect to remote port 9999 returning a io.Reader and io.Writer.
  • Continually copy local io.Reader bytes to remote io.Writer,
  • Continually copy remote io.Reader bytes to local io.Writer.

Here is the code:

package main

// Forward from local port 9000 to remote port 9999

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

var (
    username         = "root"
    password         = "password"
    serverAddrString = "192.168.1.100:22"
    localAddrString  = "localhost:9000"
    remoteAddrString = "localhost:9999"
)

func forward(localConn net.Conn, config *ssh.ClientConfig) {
    // Setup sshClientConn (type *ssh.ClientConn)
    sshClientConn, err := ssh.Dial("tcp", serverAddrString, config)
    if err != nil {
        log.Fatalf("ssh.Dial failed: %s", err)
    }

    // Setup sshConn (type net.Conn)
    sshConn, err := sshClientConn.Dial("tcp", remoteAddrString)

    // Copy localConn.Reader to sshConn.Writer
    go func() {
        _, err = io.Copy(sshConn, localConn)
        if err != nil {
            log.Fatalf("io.Copy failed: %v", err)
        }
    }()

    // Copy sshConn.Reader to localConn.Writer
    go func() {
        _, err = io.Copy(localConn, sshConn)
        if err != nil {
            log.Fatalf("io.Copy failed: %v", err)
        }
    }()
}

func main() {
    // Setup SSH config (type *ssh.ClientConfig)
    config := &ssh.ClientConfig{
        User: username,
        Auth: []ssh.AuthMethod{
            ssh.Password(password),
        },
    }

    // Setup localListener (type net.Listener)
    localListener, err := net.Listen("tcp", localAddrString)
    if err != nil {
        log.Fatalf("net.Listen failed: %v", err)
    }

    for {
        // Setup localConn (type net.Conn)
        localConn, err := localListener.Accept()
        if err != nil {
            log.Fatalf("listen.Accept failed: %v", err)
        }
        go forward(localConn, config)
    }
}
3
  • Can you please tell me how to make dynamic forwarding work? like ssh -D
    – BigSack
    Mar 12, 2014 at 3:21
  • I wish I could help you there friend, but I'm not familiar enough with SOCKS and all. This might help you though. Good luck, let me know if you get it!
    – damick
    Mar 25, 2014 at 22:19
  • 2
    Just a note - the go.grypto package is now hosted at golang.org/x/crypto/ssh, plus in order to get this excellent example to work (thank you @damick!), you need to look into swapping ssh.ClientAuth with ssh.AuthMethod, as per the changes to the package API. Apr 27, 2016 at 15:34
5

I have used your (damick) example code to build a tiny open source tool: SSHTunnel https://github.com/SommerEngineering/SSHTunnel

Therefore, the code is freely available at GitHub for anyone: Please feel free to use it for learning purposes or for anything else :) I have mentioned your nickname and also linked to this question.

Best regards, Thorsten.

2

I'v finished a simple SSH port forward tool called mallory. It provides HTTP proxy instead of SOCKS proxy, which is really similar to ssh -D.

The core code is similar to damick's answer.

  1. Create ClientConfig
  2. ssh.Dial to remote SSH server with the config and return Client
  3. Now you can use Client.Dial to forward anything you like.

    Dial initiates a connection to the addr from the remote host. The resulting connection has a zero LocalAddr() and RemoteAddr().

  4. If you want to serve a SOCKS proxy server, use Client.Dial to connect to the remote server.

3
  • I have successfully installed mallory, but when I run the command:mallory -engine=ssh -remote=ssh://root:[email protected]:22, I have encountered the below error: flag provided but not defined: -engine Usage of mallory: -config string config file (default "$HOME/.config/mallory.json") -reload send signal to reload config file -suffix string print pulbic suffix for the given domain
    – Thomas
    May 20, 2018 at 6:41
  • hope the help from you
    – Thomas
    May 20, 2018 at 6:53
  • Can i connect with user and password. Thank
    – mangovn
    Oct 11, 2019 at 3:00
0

I Wrote a tool,Called gosshtool,with this tool,you can easy to create a simple TCP port forward over SSH with Go.https://github.com/scottkiss/gosshtool

I use this tool implemented a port forward server example project: https://github.com/scottkiss/gooverssh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.