groxy

package module
v0.0.0-...-6253a34 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2017 License: MIT Imports: 8 Imported by: 0

README

groxy

Build Status GoDoc

groxy is a library that provides programmable HTTP/HTTPS proxy. Using groxy, you can intercept http requests and responses. groxy also provides HTTPS hijacking.

Example

See _example/ direcotry.

Install

$ go get github.com/agatan/groxy

Documentation

See https://godoc.org/github.com/agatan/groxy

License

MIT

Documentation

Overview

Package groxy is a library that provides programmable HTTP/HTTPS proxy. Using groxy, you can intercept http requests and responses. groxy also provides HTTPS hijacking.

Example
// creating a new proxy server instance.
p := &ProxyServer{
	// set HTTPS action (default: HTTPSActionProxy)
	HTTPSAction: HTTPSActionProxy,
}
// define a middleware that recreates request handler based on the original handler (original handler performs as a proxy).
pathLogger := func(h Handler) Handler {
	return func(r *http.Request) (*http.Response, error) {
		fmt.Println(r.URL.Path)
		resp, err := h(r)
		if err != nil {
			return nil, err
		}
		fmt.Println(resp.StatusCode)
		return resp, nil
	}
}
// set the middleware.
p.Use(pathLogger)

proxyserver := httptest.NewServer(p)
defer proxyserver.Close()
proxyurl, _ := url.Parse(proxyserver.URL)

// setup a dummy server.
testserver := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello!"))
}))
defer testserver.Close()

// request via the proxy.
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyurl)}}
resp, _ := client.Get(testserver.URL + "/foo/bar")
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()

fmt.Println(string(body))
Output:

/foo/bar
200
Hello!

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultHTTPHandler

func DefaultHTTPHandler(req *http.Request) (*http.Response, error)

DefaultHTTPHandler pass the request to the target server, and returns its response or error.

Types

type FuncLogger

type FuncLogger func(...interface{})

FuncLogger is a Logger that wraps print function

func (FuncLogger) Print

func (f FuncLogger) Print(args ...interface{})

Print invokes f with args

type HTTPSAction

type HTTPSAction int

HTTPSAction defines how to act for requests via HTTPS.

const (
	// HTTPSActionProxy just performs as proxy server. In this behaviour, middlewares are ignored.
	HTTPSActionProxy HTTPSAction = iota
	// HTTPSActionReject rejects all HTTPS requests (returns http.StatusBadRequest).
	HTTPSActionReject
	// HTTPSActionMITM strips SSL encryption.
	// Builtin certificates is not verified, so clients must accept insecure certificates.
	HTTPSActionMITM
)

type Handler

type Handler func(*http.Request) (*http.Response, error)

Handler handles http.Request and somehow generate http.Response or error.

func DefaultHTTPSHandler

func DefaultHTTPSHandler(tr *http.Transport) Handler

DefaultHTTPSHandler pass the request to the target server, and returns its response or error.

type Logger

type Logger interface {
	Print(...interface{})
}

Logger is an interface to log events.

type Middleware

type Middleware func(Handler) Handler

Middleware wraps original Handler and create new Handler.

type ProxyServer

type ProxyServer struct {
	// Logger is a logger that prints proxy requests.
	Logger Logger
	// NonProxyRequestHandler handles non-proxy requests.
	// If it's nil, non-proxy requests causes http.StatusBadRequest.
	NonProxyRequestHandler http.Handler
	HTTPSAction            HTTPSAction
	// contains filtered or unexported fields
}

ProxyServer is a programmable proxy server instance, behaves as an http.Handler.

Example
// creating a new proxy server instance.
p := &ProxyServer{
	// set HTTPS action (default: HTTPSActionProxy)
	HTTPSAction: HTTPSActionProxy,
}
// if you want to hijack https connection, you can use:
// p.HTTPSAction = HTTPSActionMITM

// ProxyServer implements http.Handler
proxyserver := httptest.NewServer(p)
defer proxyserver.Close()
Output:

func (*ProxyServer) ServeHTTP

func (p *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*ProxyServer) Use

func (p *ProxyServer) Use(ms ...Middleware)

Use adds given middlewares to p's middlewares.

Example (Mitm)
var p ProxyServer
p.HTTPSAction = HTTPSActionMITM
// hijack request!
p.Use(func(h Handler) Handler {
	return func(req *http.Request) (*http.Response, error) {
		message := "hijack!"
		body, _ := ioutil.ReadAll(req.Body)
		fmt.Printf("original request: %v\n", string(body))
		_ = req.Body.Close()
		req.Body = ioutil.NopCloser(strings.NewReader(message))
		req.ContentLength = int64(len(message))
		return h(req)
	}
})

proxyserver := httptest.NewServer(&p)
defer proxyserver.Close()
proxyurl, err := url.Parse(proxyserver.URL)
if err != nil {
	panic(err)
}

// setup a dummy echo server.
testserver := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()
	body, _ := ioutil.ReadAll(r.Body)
	w.Write(body)
}))
defer testserver.Close()

// request via the proxy.
client := &http.Client{
	Transport: &http.Transport{
		Proxy:           http.ProxyURL(proxyurl),
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	},
}
// Post `message!` to the server.
resp, err := client.Post(testserver.URL+"/post", "application/json", strings.NewReader("message!"))
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()

// if there are no proxy server, body is `message!`.
fmt.Printf("response: %v\n", string(body))
Output:

original request: message!
response: hijack!

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

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