commit 98ba2c9212365923cf949f22587a21cf49ee083a Author: Hendrik Schlehlein Date: Tue Jun 18 03:05:06 2019 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4d9a5c2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib +*.bat + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..25b7900 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM golang:latest + +ENV SOURCE_ADDR=127.0.0.1:80 +ENV DESTINATION_ADDR=127.0.0.1:8071 + +RUN mkdir /app +ADD . /app/ +WORKDIR /app +RUN go build -o main . +CMD ["/app/main"] \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..05bb4a7 --- /dev/null +++ b/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "log" + "os" +) + +func main() { + p := Server{ + Addr: os.Getenv("SOURCE_ADDR"), + Target: os.Getenv("DESTINATION_ADDR"), + } + + log.Panicln(p.ListenAndServe()) +} diff --git a/proxy.go b/proxy.go new file mode 100644 index 0000000..6ad1f36 --- /dev/null +++ b/proxy.go @@ -0,0 +1,63 @@ +package main + +import ( + "log" + "net" +) + +type Server struct { + Addr string + Target string +} + +func (s *Server) ListenAndServe() error { + listener, err := net.Listen("tcp", s.Addr) + if err != nil { + return err + } + return s.serve(listener) +} + +func (s *Server) serve(ln net.Listener) error { + for { + conn, err := ln.Accept() + if err != nil { + log.Println(err) + continue + } + go s.handleConn(conn) + } +} + +func (s *Server) handleConn(conn net.Conn) { + rconn, err := net.Dial("tcp", s.Target) + if err != nil { + return + } + + var pipe = func(src, dst net.Conn) { + defer func() { + conn.Close() + rconn.Close() + }() + + buff := make([]byte, 65535) + for { + n, err := src.Read(buff) + if err != nil { + log.Println(err) + return + } + b := buff[:n] + + _, err = dst.Write(b) + if err != nil { + log.Println(err) + return + } + } + } + + go pipe(conn, rconn) + go pipe(rconn, conn) +}