clientip

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: Apache-2.0 Imports: 3 Imported by: 1

README

Real IP Lookup Module

GitHub tag (latest SemVer) Go Reference License

Tests CodeQL Analysis GolangCI Lint Go Report Card

This Go module provides functionality to accurately determine a client's IP address from HTTP requests, considering various headers that might contain the real IP address, especially when your application is behind a proxy or load balancer.

Features

  • LookupFromRequest: Retrieve the client IP address directly from an HTTP request with consideration for custom headers.
  • Middleware: An easy-to-integrate middleware function for HTTP servers that automatically sets the RemoteAddr field of the request to the client's real IP address.

Installation

To install the module, use the following command:

go get github.com/dmitrymomot/clientip

Usage

LookupFromRequest Function

LookupFromRequest retrieves the client IP address from the provided HTTP request. It checks a list of headers for the IP address and uses a default set if none are specified.

import "github.com/dmitrymomot/clientip"

func handler(w http.ResponseWriter, r *http.Request) {
    clientIP := clientip.LookupFromRequest(r)
    fmt.Fprintf(w, "Client IP: %s", clientIP)
}

With custom headers:

import "github.com/dmitrymomot/clientip"

func handler(w http.ResponseWriter, r *http.Request) {
    clientIP := clientip.LookupFromRequest(r, "X-Custom-Real-IP")
    fmt.Fprintf(w, "Client IP: %s", clientIP)
}
Middleware

The Middleware function returns a middleware handler that modifies the request's RemoteAddr based on the client IP address found in the specified headers.

import (
    "net/http"
    "github.com/dmitrymomot/clientip"
)

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/", handler)

    // Init the middleware with custom header. If not specified, the default headers are used.
    mdw := clientip.Middleware("X-Custom-Real-IP")

    http.ListenAndServe(":8080", mdw(mux))
}

func handler(w http.ResponseWriter, r *http.Request) {
    // The RemoteAddr now contains the real client IP
    fmt.Fprintf(w, "Client IP: %s", r.RemoteAddr)
}

Customization

Both LookupFromRequest and Middleware functions accept an optional list of headers to consider when looking for the client IP address. By default, a predefined set of common headers used for forwarding client IPs in proxy setups are checked.

Contributing

If you wish to contribute to this project, please fork the repository and submit a pull request.

License

This project is licensed under the Apache 2.0 - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LookupFromRequest

func LookupFromRequest(r *http.Request, headers ...string) string

LookupFromRequest retrieves the client IP address from the provided HTTP request. It accepts an optional list of header names to check for the IP address. If no headers are specified, it uses a default set of headers. The function returns the client IP address as a string.

func Middleware

func Middleware(headers ...string) func(http.Handler) http.Handler

Middleware is a function that returns a middleware handler. It takes a variadic number of headers as input parameters. The returned middleware handler modifies the request's RemoteAddr based on the values of the specified headers. If any of the headers are found in the request, the RemoteAddr is updated with the corresponding header value. The modified request is then passed to the next handler in the chain.

Types

This section is empty.

Jump to

Keyboard shortcuts

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