Created
October 28, 2013 03:43
-
-
Save tungd/7191142 to your computer and use it in GitHub Desktop.
Redir replacement written in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net" | |
"flag" | |
"fmt" | |
"io" | |
"log" | |
) | |
func main() { | |
laddr := flag.String("laddr", "127.0.0.1", "address of interface to listen on") | |
lport := flag.Int("lport", 8080, "port to listen on") | |
caddr := flag.String("caddr", "127.0.0.1", "remote host to connect to") | |
cport := flag.Int("cport", 80, "port to connect to") | |
flag.Parse() | |
fmt.Printf("rediring %s:%d -> %s:%d\n", *laddr, *lport, *caddr, *cport) | |
local, err := net.Listen("tcp", fmt.Sprintf("%s:%d", *laddr, *lport)) | |
if local == nil { | |
log.Fatalf("Error listen: %v", err) | |
} | |
remote := fmt.Sprintf("%s:%d", *caddr, *cport) | |
for { | |
conn, err := local.Accept() | |
if conn == nil { | |
log.Printf("Error accept: %v", err) | |
} | |
go redir(conn, remote) | |
} | |
} | |
func redir(local net.Conn, addr string) { | |
remote, err := net.Dial("tcp", addr) | |
if remote == nil { | |
log.Printf("Error dial: %v\n", err) | |
return | |
} | |
go io.Copy(local, remote) | |
go io.Copy(remote, local) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was trying to setup Vagrant LXC on Archlinux; however the
redir
package on the AUR did not work, so I write a simple replacement in Go with compatible command line args.