realip

package module
v0.0.0-...-1428f15 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2020 License: MIT Imports: 5 Imported by: 0

README

FastHTTP - RealIP

GoDoc

Go package that can be used to get client's real public IP from Fast HTTP request, which usually useful for logging HTTP server.

This is fork from realip for Fast HTTP with some imporvements.

Feature
  • Follows the rule of X-Real-IP
  • Follows the rule of X-Forwarded-For
  • Exclude local or private address

How It Works

It looks for specific headers in the request and falls back to some defaults if they do not exist.

The user ip is determined by the following order:

  1. X-Client-IP
  2. X-Forwarded-For (Header may return multiple IP addresses in the format: "client IP, proxy 1 IP, proxy 2 IP", so we take the the first one.)
  3. CF-Connecting-IP (Cloudflare)
  4. Fastly-Client-Ip (Fastly CDN and Firebase hosting header when forwared to a cloud function)
  5. True-Client-Ip (Akamai and Cloudflare)
  6. X-Real-IP (Nginx proxy/FastCGI)
  7. X-Forwarded, Forwarded-For and Forwarded (Variations of #2)
  8. ctx.RemoteAddr().String()

Install

go get -u github.com/valyala/fasthttp

Example

package main

import (   
    "log"
    "github.com/valyala/fasthttp"
    "github.com/Ferluci/fast-realip"
)

func main() {  
    if err := fasthttp.ListenAndServe(":8080", realipHandler); err != nil {
        log.Fatalf("Error in ListenAndServe: %s", err)
    }
}

func realipHandler(ctx *fasthttp.RequestCtx) {
    clientIP := realip.FromRequest(ctx)
    log.Println("GET / from", clientIP)
}


Developing

Commited code must pass:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromRequest

func FromRequest(ctx *fasthttp.RequestCtx) string

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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