httpproxyfailover

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2020 License: MIT Imports: 14 Imported by: 0

README

httpproxyfailover

A proxy server for HTTP proxies that picks out one of available HTTP proxies.

Installation

As a command
go get github.com/ichiban/httpproxyfailover/cmd/httpproxyfailover
As a library
go get github.com/ichiban/httpproxyfailover

Usage

Fail over

Let's say we have 2 HTTP proxies running, namely http://localhost:8081 and http://localhost:8082. We use the first one mainly but in case that the first one is down, we want to use the second one.

$ curl -D- -p -x http://localhost:8081 https://httpbin.org/status/200
HTTP/1.1 502 Bad Gateway
Content-Type: text/plain; charset=utf-8
X-Content-Type-Options: nosniff
Date: Sun, 13 Sep 2020 05:46:00 GMT
Content-Length: 1

curl: (56) Received HTTP code 502 from proxy after CONNECT
$ curl -D- -p -x http://localhost:8082 https://httpbin.org/status/200
HTTP/1.1 200 OK
Content-Length: 0
Date: Sun, 13 Sep 2020 05:46:36 GMT

HTTP/2 200 
date: Sun, 13 Sep 2020 05:46:36 GMT
content-type: text/html; charset=utf-8
content-length: 0
server: gunicorn/19.9.0
access-control-allow-origin: *
access-control-allow-credentials: true

By using httpproxyfailover, the trial and error shown above can be done automatically.

$ httpproxyfailover -p 8080 http://localhost:8081 http://localhost:8082
$ curl -D- -p -x http://localhost:8080 https://httpbin.org/status/200
HTTP/1.1 200 OK
Date: Sun, 13 Sep 2020 06:01:39 GMT
Content-Length: 0

HTTP/2 200 
date: Sun, 13 Sep 2020 06:01:40 GMT
content-type: text/html; charset=utf-8
content-length: 0
server: gunicorn/19.9.0
access-control-allow-origin: *
access-control-allow-credentials: true

Tags

By prepending curly-bracketed words in front of the URLs, you can assign tags to the backend proxies.

$ httpproxyfailover -p 8080 '{foo}http://localhost:8081' '{bar}http://localhost:8082' '{foo}{bar}http://localhost:8083'

You can specify a tag in username part of the request. curl -xp http://foo@localhost:8080 https://httpbin.org/status/200 will try http://localhost:8081 and http://localhost:8083 while curl -px http://bar@localhost:8080 https://httpbin.org/status/200 tries http://localhost:8082 and http://localhost:8083.

You can also specify multiple tags. curl -xp http://foo,bar@localhost:8080 https://httpbin.org/status/200 will try http://localhost:8083 only.

Variables

Similar to tags, you can also use variables in the backend URLs.

$ httpproxyfailover -p 8080 'http://{foo}.{domain}:8081' 'http://{bar}.{domain}:8082'

curl -px http://domain=example.com,foo=proxy1@localhost:8080 https://httpbin.org/status/200 will try http://proxy1.example.com:8081 while curl -px http://domain=example.com,bar=proxy2@localhost:8080 https://httpbin.org/status/200 tries http://proxy2.example.com:8082

License

Distributed under the MIT license. See LICENSE for more information.

Contributing

  1. Fork it (https://github.com/ichiban/httpproxyfailover/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Documentation

Overview

Package httpproxyfailover provides a means to construct a fault-tolerant HTTP proxy out of multiple somewhat unreliable HTTP proxies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Proxy

type Proxy struct {
	// Backends hold backend HTTP proxies. Proxy tries backend HTTP proxies in order of the slice and use the first one
	// that responds with a successful status code (2XX).
	Backends []string

	// Timeout sets the deadline of trial of each backend HTTP proxy if provided.
	Timeout time.Duration

	// TLSHandshake requires further check on each backend. If set, a backend which speaks TLS is considered available
	// if not only it responds a CONNECT request with a successful status code (2XX) but also a TLS handshake succeeds.
	// This check occurs in a different TCP connection. So there's no guarantee that the proxy connection also succeeds
	// with a TLS handshake.
	TLSHandshake *tls.Config

	// Callback is signaled after every trial of the backend HTTP proxies if provided.
	// The first argument is the CONNECT request, the second argument is the backend HTTP proxy in trial, and the last
	// argument is the resulting error which is nil if it succeeded.
	Callback func(*http.Request, string, error)
	// contains filtered or unexported fields
}

Proxy is a proxy for backend HTTP proxies.

func (*Proxy) EnableTemplates added in v0.3.0

func (p *Proxy) EnableTemplates() error

EnableTemplates() parses backends as URI templates. Proxy will connect to only applicable backends which template variables are satisfied. The values for template variables are populated from the credentials in Proxy-Authorization header. The substring before the first ':' (usually considered as username) should be the form of a list of key-value pairs (`k1=v1,k2=v2`). Each pair is separated by '=' without whitespaces, and those pairs are separated by ',' without whitespaces. Optionally, you can omit '=' and the value (`k1=v1,k2=v2,tag`). Then it's considered a pair of the key and empty string (`k1=v1,k2=v2,tag=`).

func (Proxy) ServeHTTP

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

Directories

Path Synopsis
cmd
testproxy command

Jump to

Keyboard shortcuts

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