proxy

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2021 License: MIT Imports: 3 Imported by: 5

README

Chi proxy middleware

Forwarded headers middleware to use if application is run behind reverse proxy.

Documentation codecov Go Report Card Build Status

Usage

Import using:

import "github.com/chi-middleware/proxy"

Use middleware with default options (trusted from proxy 127.0.0.1 and trusts only last IP address provided in header):

    r := chi.NewRouter()
    r.Use(proxy.ForwardedHeaders())

Extend default options:

    r := chi.NewRouter()
    r.Use(proxy.ForwardedHeaders(
        proxy.NewForwardedHeadersOptions().
            WithForwardLimit(2).
            ClearTrustedProxies().AddTrustedProxy("10.0.0.1"),
    ))

Provide custom options:

    r := chi.NewRouter()
    r.Use(proxy.ForwardedHeaders(&ForwardedHeadersOptions{
        ForwardLimit: 1,
        TrustedProxies: []net.IP{
            net.IPv4(10, 0, 0, 1),
        },
    }))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForwardedHeaders

func ForwardedHeaders(options ...*ForwardedHeadersOptions) func(h http.Handler) http.Handler

ForwardedHeaders is a middleware that sets a http.Request's RemoteAddr to the results of parsing either the X-Real-IP header or the X-Forwarded-For header (in that order).

Types

type ForwardedHeadersOptions

type ForwardedHeadersOptions struct {
	// ForwardLimit limits the number of entries in the headers that will be processed.
	// The default value is 1. Set to 0 to disable the limit.
	ForwardLimit int
	// TrustingAllProxies option sets to trust all proxies.
	TrustingAllProxies bool
	// KnownProxies represents addresses of trusted proxies.
	TrustedProxies []net.IP
	// TrustedNetworks represents addresses of trusted networks.
	TrustedNetworks []*net.IPNet
}

ForwardedHeadersOptions represents options for forwarded header middleware

func NewForwardedHeadersOptions

func NewForwardedHeadersOptions() *ForwardedHeadersOptions

NewForwardedHeadersOptions creates new middleware options

func (*ForwardedHeadersOptions) AddTrustedNetwork

func (opts *ForwardedHeadersOptions) AddTrustedNetwork(cidr string) *ForwardedHeadersOptions

AddTrustedNetwork adds network to trusted network list

func (*ForwardedHeadersOptions) AddTrustedProxy

func (opts *ForwardedHeadersOptions) AddTrustedProxy(ip string) *ForwardedHeadersOptions

AddTrustedProxy adds proxy IP to trusted proxy list

func (*ForwardedHeadersOptions) ClearTrustedNetworks

func (opts *ForwardedHeadersOptions) ClearTrustedNetworks() *ForwardedHeadersOptions

ClearTrustedNetworks clears trusted network list

func (*ForwardedHeadersOptions) ClearTrustedProxies

func (opts *ForwardedHeadersOptions) ClearTrustedProxies() *ForwardedHeadersOptions

ClearTrustedProxies clears trusted proxy list

func (*ForwardedHeadersOptions) TrustAllProxies added in v1.1.0

func (opts *ForwardedHeadersOptions) TrustAllProxies() *ForwardedHeadersOptions

TrustAllProxies sets to trust all proxies

func (*ForwardedHeadersOptions) WithForwardLimit

func (opts *ForwardedHeadersOptions) WithForwardLimit(limit int) *ForwardedHeadersOptions

WithForwardLimit sets number of entries to be processed

Jump to

Keyboard shortcuts

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