Skip to content

ferluci/fasthttp-realip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

7548412 · May 11, 2022

History

41 Commits
Feb 20, 2020
May 22, 2018
May 11, 2022
May 11, 2022
Feb 19, 2020
Mar 18, 2020
Feb 20, 2020

Repository files navigation

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-Original-Forwarded-For
  3. 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.)
  4. CF-Connecting-IP (Cloudflare)
  5. Fastly-Client-Ip (Fastly CDN and Firebase hosting header when forwared to a cloud function)
  6. True-Client-Ip (Akamai and Cloudflare)
  7. X-Real-IP (Nginx proxy/FastCGI)
  8. X-Forwarded, Forwarded-For and Forwarded (Variations of #2)
  9. 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: