xff

package module
v0.0.0-...-671bd28 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2021 License: MIT Imports: 5 Imported by: 71

README

X-Forwarded-For middleware fo Go godoc Build Status

Package xff is a net/http middleware/handler to parse Forwarded HTTP Extension in Golang.

Example usage

Install xff:

go get github.com/sebest/xff

Edit server.go:

package main

import (
  "net/http"

  "github.com/sebest/xff"
)

func main() {
  handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("hello from " + r.RemoteAddr + "\n"))
  })

  xffmw, _ := xff.Default()
  http.ListenAndServe(":8080", xffmw.Handler(handler))
}

Then run your server:

go run server.go

The server now runs on localhost:8080:

$ curl -D - -H 'X-Forwarded-For: 42.42.42.42' http://localhost:8080/
HTTP/1.1 200 OK
Date: Fri, 20 Feb 2015 20:03:02 GMT
Content-Length: 29
Content-Type: text/plain; charset=utf-8

hello from 42.42.42.42:52661

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRemoteAddr

func GetRemoteAddr(r *http.Request) string

GetRemoteAddr parses the given request, resolves the X-Forwarded-For header and returns the resolved remote address.

func GetRemoteAddrIfAllowed

func GetRemoteAddrIfAllowed(r *http.Request, allowed func(sip string) bool) string

GetRemoteAddrIfAllowed parses the given request, resolves the X-Forwarded-For header and returns the resolved remote address if allowed.

func IsPublicIP

func IsPublicIP(ip net.IP) bool

IsPublicIP returns true if the given IP can be routed on the Internet.

func Parse

func Parse(ipList string) string

Parse parses the value of the X-Forwarded-For Header and returns the IP address.

Types

type Options

type Options struct {
	// AllowedSubnets is a list of Subnets from which we will accept the
	// X-Forwarded-For header.
	// If this list is empty we will accept every Subnets (default).
	AllowedSubnets []string
	// Debugging flag adds additional output to debug server side XFF issues.
	Debug bool
}

Options is a configuration container to setup the XFF middleware.

type XFF

type XFF struct {
	// Debug logger
	Log *log.Logger
	// contains filtered or unexported fields
}

XFF http handler

func Default

func Default() (*XFF, error)

Default creates a new XFF handler with default options.

func New

func New(options Options) (*XFF, error)

New creates a new XFF handler with the provided options.

func (*XFF) Handler

func (xff *XFF) Handler(h http.Handler) http.Handler

Handler updates RemoteAdd from X-Fowarded-For Headers.

func (*XFF) HandlerFunc

func (xff *XFF) HandlerFunc(w http.ResponseWriter, r *http.Request)

HandlerFunc provides Martini compatible handler

func (*XFF) ServeHTTP

func (xff *XFF) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

Negroni compatible interface

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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