realip

package module
v0.0.0-...-97ef268 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2015 License: Apache-2.0 Imports: 4 Imported by: 0

README

go-real-ip

GoDoc

Simple net/http middleware inspired by nginx realip module

Documentation

Overview

Package realip mimics the nginx realip module. It rewrites RemoteAddr in an http.Request when the connection is from a trusted network.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RealIP

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

RealIP is used to override RemoteAddr for net/http.

func New

func New(headers []string, nets []string) (*RealIP, error)

New creates a new RealIP that uses the given headers and networks. The first matching header is used.

Example
// simple handler
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
	// realip will rewrite RemoteAddr
	fmt.Fprintf(w, "Hello: %s\n", r.RemoteAddr)
})

// we only will rewrite RemoteAddr if connection is from our "trusted" loadbalancers
// we allow localhost for testing
ri, err := realip.New([]string{"X-Forwarded-For", "X-Real-IP"}, []string{"8.8.0.0/16", "127.0.0.0/8"})
if err != nil {
	// do something with error
}

// this wraps all HTTP requests
http.ListenAndServe(":8080", ri.Handler(http.DefaultServeMux))
Output:

func (*RealIP) Handler

func (ri *RealIP) Handler(h http.Handler) http.Handler

Handler wraps another Handler.

Jump to

Keyboard shortcuts

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