proxyaddr

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

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

Go to latest
Published: Dec 18, 2016 License: MIT Imports: 3 Imported by: 2

README

proxyaddr

GoDoc Build Status Coverage Status Go Report Card

Package proxyaddr is a middleware for determining client address for proxied requests in Go.

After proxyaddr is set with trusted proxies (such as local nginx, HAProxy, etc.), it tries to get the actual IP address of clients from x-forwarded-for HTTP header. If presents, proxyaddr sets the IP address to RemoteAddr field of the http.Request instance. Note the port number in RemoteAddr is left unchanged.

Install

go get github.com/zesik/proxyaddr

Quick Example

Using with standard net/http:

package main

import (
	"net/http"

	"github.com/zesik/proxyaddr"
)

func main() {
	pa := &proxyaddr.ProxyAddr{}
	pa.Init(proxyaddr.CIDRLoopback)
	http.ListenAndServe(":8080", pa.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(r.RemoteAddr))
	})))
}

Using with Negroni:

package main

import (
	"net/http"

	"github.com/urfave/negroni"
	"github.com/zesik/proxyaddr"
)

func main() {
	pa := &proxyaddr.ProxyAddr{}
	pa.Init(proxyaddr.CIDRLoopback)

	n := negroni.New()
	n.Use(pa)
	n.UseHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(r.RemoteAddr))
	}))

	http.ListenAndServe(":8080", n)
}

A quick test with curl:

$ curl localhost:8080
[::1]:53998

$ curl -H "x-forwarded-for: 1.2.3.4, 5.6.7.8" localhost:8080
5.6.7.8:53999

License

MIT

Documentation

Overview

Package proxyaddr is a middleware for determining client address for proxied requests.

Index

Constants

View Source
const (
	// CIDRLoopback represents IPv4 and IPv6 loopback addresses
	CIDRLoopback = "loopback"

	// CIDRLinkLocal represents IPv4 and IPv6 link-local addresses
	CIDRLinkLocal = "linklocal"

	// CIDRUniqueLocal represents IPv4 private addresses and IPv6 unique local addresses
	CIDRUniqueLocal = "uniquelocal"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ProxyAddr

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

ProxyAddr stores the group of CIDR of trusted proxies. With these and HTTP "x-forwarded-for" header, ProxyAddr determines actual IP address of the connecting client.

func (*ProxyAddr) Handler

func (pa *ProxyAddr) Handler(h http.Handler) http.Handler

Handler is the plain http.Handler which updates RemoteAddr with actual IP from x-forwarded-for header.

func (*ProxyAddr) Init

func (pa *ProxyAddr) Init(trustedProxies ...string) error

Init initializes the list of trusted proxies with provided string values. Without initializing, ProxyAddr trusts no IP address at all, and does not change RemoteAddr field.

Parameters of Init can only be exported constants of this package or CIDR-formatted strings (like 10.0.0.0/8).

func (*ProxyAddr) ServeHTTP

func (pa *ProxyAddr) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

ServeHTTP is the implementation of Negroni middleware interface.

Jump to

Keyboard shortcuts

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