real_ip

package module
v0.0.0-...-838d808 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2018 License: MIT Imports: 5 Imported by: 0

README

real-ip

Real IP is a minimal Go library to extract the correct IP from an X-Forwarded-For http header similar to Nginx's realip module. It is based on a heavily modified version of the xff code.

Example

package main

import (
    "net/http"
    "github.com/discovery/real-ip"
)
func main() {
    remote_addr, _ := real_ip.NewRemoteAddr([]string{"10.0.0.0/8", "192.168.0.0/16", "1.2.3.4/32"})
    // (Insert error checking here)

    handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        ip, _ := remote_addr.GetIP(r)
        // (Insert error checking here)
        w.Write([]byte("hello from " + ip.String() + "\n"))
    })

    http.ListenAndServe(":8080", http.Handler(handler))
}

Copyright (c) 2018 Discovery Communications

Licensed under the MIT License. See LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RemoteAddr

type RemoteAddr struct {
	// contains filtered or unexported fields
}

func NewRemoteAddr

func NewRemoteAddr(cidrs []string) (*RemoteAddr, error)

func (*RemoteAddr) GetIP

func (ra *RemoteAddr) GetIP(req *http.Request) (net.IP, error)

Jump to

Keyboard shortcuts

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