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)
    }
}
 
103
Kudos
 
103
Kudos

Now read this

Using cloud-init in oVirt

What is cloud-init? cloud-init provides the capability of early initialization of a virtual machine. This usually happens during the startup of the guest operating system on the virtual machine. What can I do with it in oVirt? oVirt... Continue →