ipfilter

package module
v0.0.0-...-42e682c Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2019 License: MIT Imports: 7 Imported by: 2

README

ipfilter

IPFilter provides a simple package for checking GRPC/HTTP requests against a white/black list of IPs or CIDRs

Usage

	list := []string{
		"192.168.1.0/24",
		"23.3.4.3",
		"127.0.0.0/8",
	}
	filter := ipfilter.Whitelist(list)

	ok := filter.Check(net.ParseIP("192.168.1.2"))
	if ok {
		fmt.Println("IP aceptada")
	}

Documentation

Overview

Package ipfilter provides a simple package for checking GRPC/HTTP requests against a white/black list of IPs or CIDRs.

This package is a work in progress and makes no API stability promises.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action bool

Action to be taken with the request

const (
	Deny   Action = false
	Accept Action = true
)

Posible values for Action

type Filter

type Filter struct {
	// Wrapped http handler to control
	Wrapped http.Handler

	AllowedIPs      []net.IP
	DisallowedIPs   []net.IP
	AllowedCIDRs    []*net.IPNet
	DisallowedCIDRs []*net.IPNet
	// Policy is the action to take if no match
	Policy Action
}

Filter is the main structure

func Blacklist

func Blacklist(disallowed []string) Filter

Blacklist creates a filter with the list of ips and/or cidrs passed. Returned filter has an allow policy and all ips and/or cidrs passed will be added to the disallowed lists. Strings that can't be parsed will be ignored.

func Whitelist

func Whitelist(allowed []string) Filter

Whitelist creates a filter with the list of ips and/or cidrs passed. Returned filter has a deny policy and all ips and/or cidrs passed will be added to the allowed lists. Strings that can't be parsed will be ignored.

Example
package main

import (
	"fmt"
	"net"

	"github.com/luisguillenc/ipfilter"
)

func main() {
	list := []string{
		"192.168.1.0/24",
		"23.3.4.3",
		"127.0.0.0/8",
	}
	filter := ipfilter.Whitelist(list)

	ok := filter.Check(net.ParseIP("192.168.1.2"))
	if ok {
		fmt.Println("IP aceptada")
	}
}
Output:

func (Filter) Check

func (f Filter) Check(ip net.IP) Action

Check returns the action to be taken for the ip

func (Filter) Empty

func (f Filter) Empty() bool

Empty resturns true if no list has information

func (Filter) ServeHTTP

func (f Filter) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements middleware for http servers.

func (Filter) StreamServerInterceptor

func (f Filter) StreamServerInterceptor(srv interface{}, ss grpc.ServerStream,
	info *grpc.StreamServerInfo, handler grpc.StreamHandler) error

StreamServerInterceptor implements middleware for grpc servers

func (Filter) UnaryServerInterceptor

func (f Filter) UnaryServerInterceptor(ctx context.Context, req interface{},
	info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)

UnaryServerInterceptor implements middleware for grpc servers

Jump to

Keyboard shortcuts

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