proxy

package module
v0.0.0-...-24fb71d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2024 License: MIT Imports: 6 Imported by: 0

README

tcp-proxy

A small TCP proxy written in Go, forked from https://github.com/jpillora/go-tcp-proxy

This project was intended for debugging text-based protocols. The next version will address binary protocols.

Install

Source

$ go get -v github.com/dtimm/go-tcp-proxy/cmd/tcp-proxy

Usage

$ tcp-proxy --help
Usage of tcp-proxy:
  -c    output ansi colors
  -d duration
        delay time as golang duration
  -h    output hex
  -l string
        local address (default ":9999")
  -match string
        match regex (in the form 'regex')
  -n    disable nagles algorithm
  -r string
        remote address (default "localhost:80")
  -replace string
        replace regex (in the form 'regex~replacer')
  -unwrap-tls
        remote connection with TLS exposed unencrypted locally
  -v    display server actions
  -vv
        display server actions and all tcp data

Note: Regex match and replace only works on text strings and does NOT work across packet boundaries

Simple Example

Since HTTP runs over TCP, we can also use tcp-proxy as a primitive HTTP proxy:

$ tcp-proxy -r echo.jpillora.com:80
Proxying from localhost:9999 to echo.jpillora.com:80

Then test with curl:

$ curl -H 'Host: echo.jpillora.com' localhost:9999/foo
{
  "method": "GET",
  "url": "/foo"
  ...
}
Delay Example
$ tcp-proxy -r echo.jpillora.com:80 -d 200ms
Proxying from localhost:9999 to echo.jpillora.com:80

Test with curl and it will add 200ms to each end of the request.

Match Example
$ tcp-proxy -r echo.jpillora.com:80 -match 'Host: (.+)'
Proxying from localhost:9999 to echo.jpillora.com:80
Matching Host: (.+)

#run curl again...

Connection #001 Match #1: Host: echo.jpillora.com
Replace Example
$ tcp-proxy -r echo.jpillora.com:80 -replace '"ip": "([^"]+)"~"ip": "REDACTED"'
Proxying from localhost:9999 to echo.jpillora.com:80
Replacing "ip": "([^"]+)" with "ip": "REDACTED"
#run curl again...
{
  "ip": "REDACTED",
  ...

Note: The -replace option is in the form regex~replacer. Where replacer may contain $N to substitute in group N.

Todo
  • Implement tcpproxy.Conn which provides accounting and hooks into the underlying net.Conn
  • Verify wire protocols by providing encoding.BinaryUnmarshaler to a tcpproxy.Conn
  • Modify wire protocols by also providing a map function
  • Implement SOCKS v5 to allow for user-decided remote addresses

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ColorLogger

type ColorLogger struct {
	VeryVerbose bool
	Verbose     bool
	Prefix      string
	Color       bool
}

ColorLogger - A Logger that logs to stdout in color

func (ColorLogger) Debug

func (l ColorLogger) Debug(f string, args ...interface{})

Debug - Log a debug message

func (ColorLogger) Info

func (l ColorLogger) Info(f string, args ...interface{})

Info - Log a general message

func (ColorLogger) Trace

func (l ColorLogger) Trace(f string, args ...interface{})

Trace - Log a very verbose trace message

func (ColorLogger) Warn

func (l ColorLogger) Warn(f string, args ...interface{})

Warn - Log a warning

type Logger

type Logger interface {
	Trace(f string, args ...interface{})
	Debug(f string, args ...interface{})
	Info(f string, args ...interface{})
	Warn(f string, args ...interface{})
}

Logger - Interface to pass into Proxy for it to log messages

type NullLogger

type NullLogger struct{}

NullLogger - An empty logger that ignores everything

func (NullLogger) Debug

func (l NullLogger) Debug(f string, args ...interface{})

Debug - no-op

func (NullLogger) Info

func (l NullLogger) Info(f string, args ...interface{})

Info - no-op

func (NullLogger) Trace

func (l NullLogger) Trace(f string, args ...interface{})

Trace - no-op

func (NullLogger) Warn

func (l NullLogger) Warn(f string, args ...interface{})

Warn - no-op

type Proxy

type Proxy struct {
	Matcher  func([]byte)
	Replacer func([]byte) []byte

	Delay time.Duration

	// Settings
	Nagles    bool
	Log       Logger
	OutputHex bool
	// contains filtered or unexported fields
}

Proxy - Manages a Proxy connection, piping data between local and remote.

func New

func New(lconn *net.TCPConn, laddr, raddr *net.TCPAddr) *Proxy

New - Create a new Proxy instance. Takes over local connection passed in, and closes it when finished.

func NewTLSUnwrapped

func NewTLSUnwrapped(lconn *net.TCPConn, laddr, raddr *net.TCPAddr, addr string) *Proxy

NewTLSUnwrapped - Create a new Proxy instance with a remote TLS server for which we want to unwrap the TLS to be able to connect without encryption locally

func (*Proxy) Start

func (p *Proxy) Start()

Start - open connection to remote and start proxying data.

Directories

Path Synopsis
cmd
tcp-proxy command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL