rip

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2021 License: MIT Imports: 3 Imported by: 2

README

rip

GoDoc

Go package that can be used to get client's real public IP. Based on https://github.com/tomasen/realip.

Features
  • Parses IPs from X-Real-IP
  • Parses IPs from X-Forwarded-For
  • Excludes local/private address by default
  • Custom filtering options for X-Forwarded-For IPs

Examples

Basic usage
package main

import "github.com/ripexz/rip"

func (h *Handler) ServeIndexPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	clientIP := rip.FromRequest(r, nil)
	log.Println("GET / from", clientIP)
}
AWS ELB
clientIP := rip.FromRequest(r, rip.FilterAWS)
Custom Filtering
clientIP := rip.FromRequest(r, func(ips []string) (string, bool) {
	// your custom logic here
	return "127.0.0.1", true
})

Contributing

Please make sure your code:

  • Passes the configured golangci-lint checks.
  • Passes the tests.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FilterAWS

func FilterAWS(ips []string) (string, bool)

FilterAWS returns the last address in the X-Forwarded-For chain. This is intended for services behind AWS ELB as it appends the client IP address if one is already provided, to prevent spoofing. For more info see: https://git.io/Jf5nh

func FilterDefault

func FilterDefault(ips []string) (string, bool)

FilterDefault is used if no Filter function is passed.

func FilterPublicAddress

func FilterPublicAddress(ips []string) (string, bool)

FilterPublicAddress returns the first address that is not under private CIDR blocks. List of private CIDR blocks can be seen on:

https://en.wikipedia.org/wiki/Private_network

https://en.wikipedia.org/wiki/Link-local_address

func FromRequest

func FromRequest(r *http.Request, filter Filter) string

FromRequest returns client's real public IP address from http request headers.

Types

type Filter

type Filter func(ips []string) (string, bool)

Filter allows customisation for detecting or removing specific IPs.

Jump to

Keyboard shortcuts

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