proxy

package module
v0.0.0-...-b61099e Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2023 License: MIT Imports: 11 Imported by: 0

README

tcp-proxy

A small TCP proxy written in Go

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

Install

Binaries

Download the latest release, or

Install latest release now with curl https://i.jpillora.com/go-tcp-proxy! | bash

Source

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

Usage

$ tcp-proxy --help
Usage of tcp-proxy:
  -c: output ansi colors
  -h: output hex
  -l="localhost:9999": local address
  -n: disable nagles algorithm
  -r="localhost:80": remote address
  -match="": match regex (in the form 'regex')
  -replace="": replace regex (in the form 'regex~replacer')
  -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"
  ...
}
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

	// Settings
	Nagles         bool
	Log            Logger
	OutputHex      bool
	OutputRawBytes bool
	H2             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) SetInboundFile

func (p *Proxy) SetInboundFile(f *os.File)

func (*Proxy) SetOutboundFile

func (p *Proxy) SetOutboundFile(f *os.File)

func (*Proxy) Start

func (p *Proxy) Start()

Start - open connection to remote and start proxying data.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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