initial commit

This commit is contained in:
Hendrik Schlehlein 2019-06-18 03:05:06 +02:00
commit 98ba2c9212
4 changed files with 101 additions and 0 deletions

13
.gitignore vendored Normal file
View file

@ -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

10
Dockerfile Normal file
View file

@ -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"]

15
main.go Normal file
View file

@ -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())
}

63
proxy.go Normal file
View file

@ -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)
}