Skip to content

Commit

Permalink
feat(utils/3rdparty/go_tcp_proxy): Add refactored code from github.co…
Browse files Browse the repository at this point in the history
…m/jpillora/go-tcp-proxy
  • Loading branch information
ondrejsika committed Aug 21, 2021
1 parent 8a192ff commit f60eceb
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
72 changes: 72 additions & 0 deletions utils/3rdparty/go_tcp_proxy/go_tcp_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package go_tcp_proxy

import (
"fmt"
"net"
"os"

proxy "github.com/jpillora/go-tcp-proxy"
)

func RunProxy(localAddr string, remoteAddr string) {
logger := proxy.ColorLogger{
Verbose: *verbose,
Color: *colors,
}

logger.Info("go-tcp-proxy (%s) proxing from %v to %v ", version, localAddr, remoteAddr)

laddr, err := net.ResolveTCPAddr("tcp", localAddr)
if err != nil {
logger.Warn("Failed to resolve local address: %s", err)
os.Exit(1)
}
raddr, err := net.ResolveTCPAddr("tcp", remoteAddr)
if err != nil {
logger.Warn("Failed to resolve remote address: %s", err)
os.Exit(1)
}
listener, err := net.ListenTCP("tcp", laddr)
if err != nil {
logger.Warn("Failed to open local port to listen: %s", err)
os.Exit(1)
}

matcher := createMatcher(*match)
replacer := createReplacer(*replace)

if *veryverbose {
*verbose = true
}

for {
conn, err := listener.AcceptTCP()
if err != nil {
logger.Warn("Failed to accept connection '%s'", err)
continue
}
connid++

var p *proxy.Proxy
if *unwrapTLS {
logger.Info("Unwrapping TLS")
p = proxy.NewTLSUnwrapped(conn, laddr, raddr, remoteAddr)
} else {
p = proxy.New(conn, laddr, raddr)
}

p.Matcher = matcher
p.Replacer = replacer

p.Nagles = *nagles
p.OutputHex = *hex
p.Log = proxy.ColorLogger{
Verbose: *verbose,
VeryVerbose: *veryverbose,
Prefix: fmt.Sprintf("Connection #%03d ", connid),
Color: *colors,
}

go p.Start()
}
}
72 changes: 72 additions & 0 deletions utils/3rdparty/go_tcp_proxy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package go_tcp_proxy

// From: https://github.com/jpillora/go-tcp-proxy/blob/master/cmd/tcp-proxy/main.go

import (
"flag"
"regexp"
"strings"

proxy "github.com/jpillora/go-tcp-proxy"
)

var (
version = "0.0.0-src"
matchid = uint64(0)
connid = uint64(0)
logger proxy.ColorLogger

verbose = flag.Bool("v", false, "display server actions")
veryverbose = flag.Bool("vv", false, "display server actions and all tcp data")
nagles = flag.Bool("n", false, "disable nagles algorithm")
hex = flag.Bool("h", false, "output hex")
colors = flag.Bool("c", false, "output ansi colors")
unwrapTLS = flag.Bool("unwrap-tls", false, "remote connection with TLS exposed unencrypted locally")
match = flag.String("match", "", "match regex (in the form 'regex')")
replace = flag.String("replace", "", "replace regex (in the form 'regex~replacer')")
)

func createMatcher(match string) func([]byte) {
if match == "" {
return nil
}
re, err := regexp.Compile(match)
if err != nil {
logger.Warn("Invalid match regex: %s", err)
return nil
}

logger.Info("Matching %s", re.String())
return func(input []byte) {
ms := re.FindAll(input, -1)
for _, m := range ms {
matchid++
logger.Info("Match #%d: %s", matchid, string(m))
}
}
}

func createReplacer(replace string) func([]byte) []byte {
if replace == "" {
return nil
}
//split by / (TODO: allow slash escapes)
parts := strings.Split(replace, "~")
if len(parts) != 2 {
logger.Warn("Invalid replace option")
return nil
}

re, err := regexp.Compile(string(parts[0]))
if err != nil {
logger.Warn("Invalid replace regex: %s", err)
return nil
}

repl := []byte(parts[1])

logger.Info("Replacing %s with %s", re.String(), repl)
return func(input []byte) []byte {
return re.ReplaceAll(input, repl)
}
}

0 comments on commit f60eceb

Please sign in to comment.