proxystrip/main.go

52 lines
852 B
Go

package main
import (
"flag"
"io"
"log"
"net"
"github.com/armon/go-proxyproto"
"github.com/facebookgo/flagenv"
)
var (
target = flag.String("target-host", "127.0.0.1:22", "target TCP host:port")
port = flag.String("port", "2222", "TCP port to listen on")
)
func main() {
flagenv.Parse()
flag.Parse()
list, err := net.Listen("tcp", ":"+*port)
if err != nil {
panic(err)
}
log.Printf("now listening on port %s, forwarding traffic to %s", *port, *target)
proxyList := &proxyproto.Listener{Listener: list}
for {
conn, err := proxyList.Accept()
if err != nil {
log.Println(err)
continue
}
go dataTo(conn)
}
}
func dataTo(conn net.Conn) {
defer conn.Close()
peer, err := net.Dial("tcp", *target)
if err != nil {
log.Println(*target, err)
}
defer peer.Close()
go io.Copy(conn, peer)
io.Copy(peer, conn)
}