realip

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2021 License: MIT Imports: 4 Imported by: 4

README

realip

Test Status Coverage Status MIT License GoDoc

The realip detects client real ip in Go's HTTP middleware layer.

Synopsis

_, ipnet, _ := net.ParseCIDR("192.168.0.0/16")
var middleware func(http.Handler) http.Handler = realip.MustMiddleware(&realip.Config{
    RealIPFrom:      []*net.IPNet{ipnet},
    RealIPHeader:    realip.HeaderXForwardedFor,
    RealIPRecursive: true,
})
var handler http.HandlerFunc = func(w http.ResponseWriter, req *http.Request) {
    fmt.Fprintf(w, req.Header.Get("X-Real-IP"))
})
handler = middleware(handler)

Description

The realip package implements detecting client real IP mechanisms from request headers in Go's HTTP middleware layer.

This realizes the similar function as Nginx's ngx_http_realip_module in the layer inside Go. Therefore, the setting property names and behaviors are also close to ngx_http_realip_module.

The realip provides Go's HTTP Middleware. It detects the client's real ip from the request and sets it in the specified request header (Default: X-Real-IP).

Installation

% go get github.com/natureglobal/realip

Author

Documentation

Overview

Package realip provides cliant real ip detection mechanisms from http.Request in Go's HTTP middleware layer.

_, ipnet, _ := net.ParseCIDR("192.168.0.0/16")
var middleware func(http.Handler) http.Handler = realip.MustMiddleware(&realip.Config{
	RealIPFrom:      []*net.IPNet{ipnet},
	RealIPHeader:    realip.HeaderXForwardedFor,
	RealIPRecursive: true,
})
var handler http.HandlerFunc = func(w http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(w, req.Header.Get("X-Real-IP"))
})
handler = middleware(handler)
l, _ := net.Listen("tcp", "127.0.0.1:8080")

http.Serve(l, handler)

This realizes the same function as Nginx's ngx_http_realip_module in the layer inside Go. Therefore, the setting property names and behaviors are also close to ngx_http_realip_module.

The realip provides Go's HTTP Middleware. It detects the client's real ip from the request and sets it in the specified request header (Default: X-Real-IP).

Index

Constants

View Source
const (
	HeaderXForwardedFor = "X-Forwarded-For"
	HeaderXRealIP       = "X-Real-IP"
)

Popular Headers

Variables

This section is empty.

Functions

func Middleware

func Middleware(c *Config) (func(http.Handler) http.Handler, error)

Middleware returns a http middleware detecting the real IP of the client from request and set it in the request header.

func MustMiddleware

func MustMiddleware(c *Config) func(http.Handler) http.Handler

MustMiddleware returns a http middleware of realip. It panics when error occurred.

Types

type Config

type Config struct {
	RealIPFrom      []*net.IPNet
	RealIPHeader    string
	RealIPRecursive bool

	SetHeader string
}

Config is configuration for realip middleware. The fields naming of it is similar to ngx_http_realip_module.

Jump to

Keyboard shortcuts

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