first commit

This commit is contained in:
Garionion 2023-01-28 19:30:56 +01:00
commit 3afae91502
Signed by: garionion
SSH key fingerprint: SHA256:6uQQGh4dHIdYnrR+qeLdgx5SDmbttGp2HusA73563QA
3 changed files with 119 additions and 0 deletions

11
go.mod Normal file
View file

@ -0,0 +1,11 @@
module tcp-proxy
go 1.19
require (
github.com/integrii/flaggy v1.5.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/rs/zerolog v1.29.0 // indirect
golang.org/x/sys v0.4.0 // indirect
)

22
go.sum Normal file
View file

@ -0,0 +1,22 @@
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/integrii/flaggy v1.5.2 h1:bWV20MQEngo4hWhno3i5Z9ISPxLPKj9NOGNwTWb/8IQ=
github.com/integrii/flaggy v1.5.2/go.mod h1:dO13u7SYuhk910nayCJ+s1DeAAGC1THCMj1uSFmwtQ8=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

86
main.go Normal file
View file

@ -0,0 +1,86 @@
package main
import (
"io"
"net"
"os"
"sync"
"github.com/integrii/flaggy"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
var (
listenAddr = ":8000"
forwardAddr string
)
flaggy.String(&listenAddr, "l", "listen", "Address to listen on")
flaggy.String(&forwardAddr, "f", "forward", "Address to forward to")
flaggy.Parse()
if forwardAddr == "" {
log.Fatal().Msg("Forward address is required")
}
ln, err := net.Listen("tcp", listenAddr)
if err != nil {
log.Panic().Err(err).Msg("Failed to listen")
}
defer ln.Close()
log.Info().Str("Listen Address", listenAddr).Msg("Listening")
for {
conn, err := ln.Accept()
if err != nil {
log.Err(err).Msg("Failed to accept connection")
continue
}
log.Info().Str("Incoming Address", conn.RemoteAddr().String()).Msg("Connection accepted")
go handleConnection(conn, forwardAddr)
}
}
func handleConnection(conn net.Conn, forwardAddr string) {
defer conn.Close()
fwrd, err := net.Dial("tcp", forwardAddr)
if err != nil {
log.Err(err).Msg("Failed to connect to forward address")
return
}
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer fwrd.Close()
defer wg.Done()
_, err := io.Copy(fwrd, conn)
if err != nil {
log.Err(err).Msg("Failed to copy from connection to forward address")
}
}()
go func() {
defer conn.Close()
defer wg.Done()
_, err := io.Copy(conn, fwrd)
if err != nil {
log.Err(err).Msg("Failed to copy from forward address to connection")
}
}()
wg.Wait()
log.Info().Str("Incoming Address", conn.RemoteAddr().String()).Msg("Connection closed")
}