Giter Site home page Giter Site logo

http-proxy's Introduction

HTTP Proxy in Go

Build Statusย  Sourcegraph

Run

  • Go 1.7 is the minimum supported version of Go

First get dependencies:

go get -t

Then run with:

go run http_proxy.go

Build your own Proxy

This proxy is built around the classical Middleware pattern. You can see examples in the forward and httpconnect packges. They can be chained together forming a series of filters.

See this code snippet:

// Middleware: Forward HTTP Messages
forwarder, err := forward.New(nil, forward.IdleTimeoutSetter(time.Duration(*idleClose)*time.Second))
if err != nil {
	log.Error(err)
}

// Middleware: Handle HTTP CONNECT
httpConnect, err := httpconnect.New(forwarder, httpconnect.IdleTimeoutSetter(time.Duration(*idleClose)*time.Second))
if err != nil {
	log.Error(err)
}

...

Additionally, this proxy uses the concept of connection wrappers, which work as a series of wrappers over the listeners generating the connections, and the connections themselves.

The following is an extract of the default listeners you can find in this proxy. You need to provide functions that take the previous listener and produce a new one, wrapping it in the process. Note that the generated connections must implement StateAwareConn. See more examples in listeners.

srv.AddListenerWrappers(
	// Limit max number of simultaneous connections
	func(ls net.Listener) net.Listener {
		return listeners.NewLimitedListener(ls, *maxConns)
	},

    // Close connections after 30 seconds of no activity
	func(ls net.Listener) net.Listener {
		return listeners.NewIdleConnListener(ls, time.Duration(*idleClose)*time.Second)
	},
)

Test

Run tests

go test

Use this for verbose output:

TRACE=1 go test

Manual testing

*Keep in mind that cURL doesn't support tunneling through an HTTPS proxy, so if you use the -https option you have to use other tools for testing.

Run the server as follows:

go run http_proxy.go

Test direct proxying with cURL:

curl -kvx localhost:8080 http://www.google.com/humans.txt
curl -kvx localhost:8080 https://www.google.com/humans.txt

Test HTTP connect with cURL:

curl -kpvx localhost:8080 http://www.google.com/humans.txt
curl -kpvx localhost:8080 https://www.google.com/humans.txt

http-proxy's People

Contributors

aranhoide avatar fffw avatar joesis avatar myleshorton avatar oxtoacart avatar uaalto avatar xiam avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.