realip

package
v0.0.0-...-16de10f Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2018 License: MIT Imports: 3 Imported by: 1

README

Middleware RealIP

DHTTP server middleware to get client real IP from header

Intro

It will try X-Forwarded-For first, than X-Real-IP. If both are invalid it parse by http.Request.RemoteAddr.

Config

  • FromHeader : try to get from this header first. Which you can use custom header or X-Real-IP.

Method

  • Get (r *DHTTP.Request) net.IP
    get real IP in net.IP from request. Return nil if not found.
  • GetBy (name string, r *DHTTP.Request) net.IP
    get real IP in net.IP from request by name. Return nil if not found.
  • GetIPV4 (r *DHTTP.Request) string
    get real IP in ipv4. "127.0.0.1" if loopback. Empty string if not found.
  • GetIPV6 (r *DHTTP.Request) string
    get real IP in ipv6. "::1" if loopback. Empty string if not found.

Example

Default

server.Use(realip.Middleware())

With Config

conf := realip.Config()
server.Use()

Get Result

// RouteHandlerFunc
func(w dhttp.ResponseWriter, r *dhttp.Request) {
  ip := realip.Get(r)
  // do your handling
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(r *dhttp.Request) net.IP

Get get net.IP from request. Return nil if not found.

func GetBy

func GetBy(name string, r *dhttp.Request) net.IP

GetBy get net.IP from request by name. Return nil if not found.

func GetIPV4

func GetIPV4(r *dhttp.Request) string

GetIPV4 get ipv4 string from request. Return "127.0.0.1" if it is loopback. Return empty string if not found.

func GetIPV6

func GetIPV6(r *dhttp.Request) string

GetIPV6 get ipv6 string from request. Return "::1" if it is loopback. Return empty string if not found.

func Middleware

func Middleware(config ...Config) *dhttp.Middleware

Middleware to get client IP (in string) from header "X-Forwarded-For" or "X-Real-IP". It will return RemoteAdd if headers not found.

Types

type Config

type Config struct {
	FromHeader string
}

Config realip middleware config

Jump to

Keyboard shortcuts

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