Simple connection fowarder in golang

Just a quicky. I love the simplicity how you can solve issues in golang.
I was trying to port forward something but had to have it on a public IP.
ssh did not do what I want so I just wrote a quick and dirty version in golang:

package main

import (
    "io"
    "log"
    "net"
    "os"
)

func forward(conn net.Conn) {
    client, err := net.Dial("tcp", os.Args[2])
    if err != nil {
        log.Fatalf("Dial failed: %v", err)
    }
    log.Printf("Connected to localhost %v\n", conn)
    go func() {
        defer client.Close()
        defer conn.Close()
        io.Copy(client, conn)
    }()
    go func() {
        defer client.Close()
        defer conn.Close()
        io.Copy(conn, client)
    }()
}

func main() {
    if len(os.Args) != 3 {
        log.Fatalf("Usage %s listen:port forward:port\n", os.Args[0]);
        return
    }    

    listener, err := net.Listen("tcp", os.Args[1])
    if err != nil {
        log.Fatalf("Failed to setup listener: %v", err)
    }

    for {
        conn, err := listener.Accept()
        if err != nil {
            log.Fatalf("ERROR: failed to accept listener: %v", err)
        }
        log.Printf("Accepted connection %v\n", conn)
        go forward(conn)
    }
}
 
191
Kudos
 
191
Kudos

Now read this

Meet pypa, libpypa

For quite a while, I have been playing with the thought of writing a parser for a programming language. I have never written one before and therefore I somehow had to scratch an itch in that regards. That itch however was never that much... Continue →