proxy

package module
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2023 License: MIT Imports: 20 Imported by: 0

README

Proxy

Project Logo

Code Coverage Last Updated

This project provides a proxy server that can proxy multiple websites with a custom http.Transport.

Install

go get github.com/FrauElster/proxy

Why?

I have a automated browser which crawles websites, and I wanted it to use a VPN without the system its running on needing to connect to one. Since I dont have control over the http transport the browser uses, I had to come up with this proxy.

What exactly can it do?

It is basically a man in the middle between your requesting client and the actual website. Lets say you want to GET www.google.com/search?q=hello+world, you would setup the proxy and send you request to GET localhost:8080/google/search?q=hello+world. The proxy would request the website over a custom http.Transport (e.g. using SOCKS5), swap all URL that resolve to www.google.com with localhost:8080/google and forwards the response to the client.

How to use?

import (
  goproxy "golang.org/x/net/proxy"
)

func main() {
  // build a custom transport, this can be any http.RoundTripper,
  socksAddr := os.Getenv("SOCKS5_PROXY")
	user := os.Getenv("SOCKS5_USER")
	pass := os.Getenv("SOCKS5_PASS")
	transport := proxy.NewStealthTransport(
    proxy.WithSocks5(socksAddr, &goproxy.Auth{User: user, Password: pass}), 
    proxy.WithUserAgents(proxy.CommonUserAgents...)
  )

  // define website to forward to
  targets := []proxy.Target({BaseUrl: "https://www.github.com", Prefix:  "/github/"})

  // build proxy
  p, err := proxy.NewProxy(targets, 
    proxy.WithTransport(transport), 
    proxy.WithPort(8080)
  )
  if err != nil {
    panic(err)
  }

  // start the server
  err := proxy.ListenAndServe()
	if err != nil && err != http.ErrServerClosed {
		panic(err)
	}
}

Overwatch it

The package comes with a separate stats server, that can be used like that:

func main() {
  // define targets
	targetOne := Target{BaseUrl: "https://example.com", Prefix: "/example/"}
  targetTwo := Target{BaseUrl: "https://github.com", Prefix: "/github/"}
	
  // start stats server
  stats := stats.NewStatServer(stats.WithPort(8081))
  stats.RegisterTarget(targetOne)
  stats.RegisterTarget(targetTwo)
	go func() {
		err := stats.ListenAndServe()
		if err != nil && err != http.ErrServerClosed {
			panic(err)
		}
	}()

  // start proxy
  p, err := proxy.NewProxy([]proxy.Target{targetOne, targetTwo}, proxy.WithPort(8080))
  if err != nil {
    panic(err)
  }
  go func() {
		err := p.ListenAndServe()
		if err != nil && err != http.ErrServerClosed {
			panic(err)
		}
	}()

  // wait for ctrl+c
  sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
	<-sigChan
}

CORS

Some Browser wont allow the forwarding from a secure (https) website over a unsecure connection (http). Therefore, this package comes with a function to generate a self-signed cert, which can be applied to the proxy:

func main() {
  // define targets
	targetOne := Target{BaseUrl: "https://example.com", Prefix: "/example/"}
  targetTwo := Target{BaseUrl: "https://github.com", Prefix: "/github/"}

  // generate cert
  sslCert, err := proxy.GenerateSslCerts("Hans Maier GmbH")
	if err != nil {
    panic(err)
  }

  // start proxy
	proxy, err := proxy.NewProxy(
		[]proxy.Target{targetOne, targetTwo}
		proxy.WithSsl(sslCert),
	)
  // ListenAndServe will automatically use ServeTLS
  err := p.ListenAndServe()
	if err != nil && err != http.ErrServerClosed {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateSslCerts

func GenerateSslCerts(caOrganisation string) (tls.Certificate, error)

Types

type Proxy added in v0.1.5

type Proxy struct {
	// contains filtered or unexported fields
}

func NewProxy

func NewProxy(opts ...ProxyOption) (*Proxy, error)

func (*Proxy) AddTarget added in v0.1.6

func (p *Proxy) AddTarget(target Target) error

func (*Proxy) Addr added in v0.1.5

func (p *Proxy) Addr() string

func (*Proxy) ListenAndServe added in v0.1.5

func (p *Proxy) ListenAndServe() (err error)

ListenAndServe starts the proxy server It blocks until the server is shut down If the proxy server was started with WithSsl, it will use http.ListenAndServeTLS instead of http.ListenAndServe

func (*Proxy) Shutdown added in v0.1.5

func (p *Proxy) Shutdown(ctx context.Context) error

type ProxyOption

type ProxyOption func(*Proxy)

func WithPort added in v0.1.5

func WithPort(port int) ProxyOption

func WithSsl

func WithSsl(cert tls.Certificate) ProxyOption

WithSsl enables SSL for the proxy server ListenAndServe will use http.ListenAndServeTLS instead of http.ListenAndServe

func WithTransport

func WithTransport(transport http.RoundTripper) ProxyOption

WithTransport sets the transport used by the proxy server

type Target

type Target struct {
	BaseUrl string
	Prefix  string
	// PreRequest can be used to manipulate the http.Request
	PreRequest func(*http.Request) *http.Request
	// PostRequest can be used to manipulate the http.Response
	// if the request failed, *http.Response will be nil and the returned value will be ignored
	PostRequest func(*http.Response) *http.Response
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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