From cf1edf2d31d7cf0d92c2da831515f7d5b488be33 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Wed, 12 Dec 2018 14:56:53 -0800 Subject: [PATCH] Add simple proxy. --- cmd/sprox/main.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 cmd/sprox/main.go diff --git a/cmd/sprox/main.go b/cmd/sprox/main.go new file mode 100644 index 0000000..b600890 --- /dev/null +++ b/cmd/sprox/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "flag" + "io" + "log" + "net" + + "github.com/kisom/goutils/die" +) + +func proxy(conn net.Conn, inside string) error { + proxyConn, err := net.Dial("tcp", inside) + if err != nil { + return err + } + + defer proxyConn.Close() + defer conn.Close() + + go func() { + io.Copy(conn, proxyConn) + }() + _, err = io.Copy(proxyConn, conn) + return err +} + +func main() { + var outside, inside string + flag.StringVar(&outside, "f", "8080", "outside port") + flag.StringVar(&inside, "p", "4000", "inside port") + flag.Parse() + + l, err := net.Listen("tcp", "0.0.0.0:"+outside) + die.If(err) + + for { + conn, err := l.Accept() + if err != nil { + log.Println(err) + continue + } + + go proxy(conn, "127.0.0.1:"+inside) + } +}