ip

package module
v0.0.0-...-03db560 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2016 License: MIT Imports: 5 Imported by: 0

README

ip Build Status GoDoc Coverage Status Go Report Card

IP range based filtering and multiplexer for your proxies. Supports IP v4/v6 expressions, CIDR ranges, subnets...

Implements a middleware layer, so you can use it as multiplexer.

Installation

go get -u gopkg.in/vinxi/ip.v0

API

See godoc reference.

Example

Allow reserved loopback CIDR ranges
package main

import (
  "fmt"

  "gopkg.in/vinxi/ip.v0"
  "gopkg.in/vinxi/vinxi.v0"
)

const port = 3100

func main() {
  // Create a new vinxi proxy
  vs := vinxi.NewServer(vinxi.ServerOptions{Port: port})

  // Attach the IP range filter
  vs.Use(ip.New("127.0.0.1/8", "::1/64"))

  // Target server to forward
  vs.Forward("http://httpbin.org")

  fmt.Printf("Server listening on port: %d\n", port)
  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}
Use filter as multiplexer for middleware composition
package main

import (
  "fmt"
  "net/http"

  "gopkg.in/vinxi/ip.v0"
  "gopkg.in/vinxi/vinxi.v0"
)

const port = 3100

func main() {
  // Create a new vinxi proxy
  vs := vinxi.NewServer(vinxi.ServerOptions{Port: port})

  // Create the IP filter
  filter := ip.New("127.0.0.1/8", "::1/64")

  // Attach a filter level middleware handler
  filter.Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
    w.Header().Set("IP-Allowed", r.RemoteAddr)
    h.ServeHTTP(w, r)
  })

  // Register the filter in vinxi
  vs.Use(filter)

  // Target server to forward
  vs.Forward("http://httpbin.org")

  fmt.Printf("Server listening on port: %d\n", port)
  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}

License

MIT

Documentation

Overview

Package ip filters HTTP traffic based on IP ranges. Supports IP v4/v6 and CIDR expressions. It also provides a middleware layer, useful for composition and multiplexing.

Index

Constants

View Source
const Version = "0.1.0"

Version defines the current package semantic version.

Variables

View Source
var ForbiddenResponder = func(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(403)
	w.Write([]byte("Forbidden: client IP not allowed"))
}

ForbiddenResponder is used as default function to repond when the IP is not allowed. You can customize it via Filter.SetResponder(fn).

Functions

This section is empty.

Types

type Filter

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

Filter implements a IP range based authorization filter for incoming HTTP traffic.

func New

func New(ranges ...string) *Filter

New creates a new IP filter based on the given IP CIDR ranges.

func (*Filter) Filter

func (f *Filter) Filter(fn ...FilterFunc)

Filter registers a new filter function. If the filter matches, the client IP won't be validated.

func (*Filter) FilterHTTP

func (f *Filter) FilterHTTP(h http.Handler) func(w http.ResponseWriter, r *http.Request)

FilterHTTP filters an incoming HTTP request based on the client IP. If some filter passes, the request won't be limited.

func (*Filter) Register

func (f *Filter) Register(mw layer.Middleware)

Register registers the middleware handler.

func (*Filter) SetResponder

func (f *Filter) SetResponder(fn http.HandlerFunc)

SetResponder sets a custom function to reply in case that an IP not allowed.

func (*Filter) Use

func (f *Filter) Use(handler interface{}) *Filter

Use registers a new middleware handler in the middleware stack. The middleware will be executed only if the client IP is allowed.

func (*Filter) UseFinalHandler

func (f *Filter) UseFinalHandler(handler http.Handler) *Filter

UseFinalHandler registers a new middleware handler in the middleware stack. The middleware will be executed only if the client IP is allowed.

func (*Filter) UsePhase

func (f *Filter) UsePhase(phase string, handler interface{}) *Filter

UsePhase registers a new middleware handler in the middleware stack. The middleware will be executed only if the client IP is allowed.

type FilterFunc

type FilterFunc func(r *http.Request) bool

FilterFunc represents the filter function signature used to determine if should apply not the IP filtering.

Directories

Path Synopsis
_examples
mux

Jump to

Keyboard shortcuts

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