ipfilter

package
v0.0.0-...-0e8a65c Latest Latest
Warning

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

Go to latest
Published: May 20, 2017 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DBPublicURL = "http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz"
	DBTempPath  = filepath.Join(os.TempDir(), "ipfilter-GeoLite2-Country.mmdb.gz")
)

Functions

func IPToCountry

func IPToCountry(db *maxminddb.Reader, ipstr string) string

IPToCountry is a simple IP-country code lookup. Returns an empty string when cannot determine country.

func NetIPToCountry

func NetIPToCountry(db *maxminddb.Reader, ip net.IP) string

NetIPToCountry is a simple IP-country code lookup. Returns an empty string when cannot determine country.

Types

type IPFilter

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

func New

func New(opts Options) *IPFilter

New blocks during database intilisation and checks validity IP strings. returns an error on failure.

func NewLazy

func NewLazy(opts Options) *IPFilter

NewLazy performs database intilisation in a goroutine. During this intilisation, any DB (country code) lookups will be skipped. Errors will be logged instead of returned.

func (*IPFilter) AllowCountry

func (f *IPFilter) AllowCountry(code string)

func (*IPFilter) AllowIP

func (f *IPFilter) AllowIP(ip string) bool

func (*IPFilter) Allowed

func (f *IPFilter) Allowed(ipstr string) bool

Allowed returns if a given IP can pass through the filter

func (*IPFilter) BlockCountry

func (f *IPFilter) BlockCountry(code string)

func (*IPFilter) BlockIP

func (f *IPFilter) BlockIP(ip string) bool

func (*IPFilter) Blocked

func (f *IPFilter) Blocked(ip string) bool

Blocked returns if a given IP can NOT pass through the filter

func (*IPFilter) IPToCountry

func (f *IPFilter) IPToCountry(ipstr string) string

IP string to ISO country code. Returns an empty string when cannot determine country.

func (*IPFilter) NetAllowed

func (f *IPFilter) NetAllowed(ip net.IP) bool

Allowed returns if a given net.IP can pass through the filter

func (*IPFilter) NetBlocked

func (f *IPFilter) NetBlocked(ip net.IP) bool

Blocked returns if a given net.IP can NOT pass through the filter

func (*IPFilter) NetIPToCountry

func (f *IPFilter) NetIPToCountry(ip net.IP) string

net.IP to ISO country code. Returns an empty string when cannot determine country.

func (*IPFilter) Serve

func (m *IPFilter) Serve(ctx *iris.Context)

func (*IPFilter) ToggleCountry

func (f *IPFilter) ToggleCountry(code string, allowed bool)

ToggleCountry alters a specific country setting

func (*IPFilter) ToggleDefault

func (f *IPFilter) ToggleDefault(allowed bool)

ToggleDefault alters the default setting

func (*IPFilter) ToggleIP

func (f *IPFilter) ToggleIP(str string, allowed bool) bool

type Options

type Options struct {
	//explicity allowed IPs
	AllowedIPs []string
	//explicity blocked IPs
	BlockedIPs []string
	//explicity allowed country ISO codes
	AllowedCountries []string
	//explicity blocked country ISO codes
	BlockedCountries []string
	//in-memory GeoLite2-Country.mmdb file,
	//if not provided falls back to IPDBPath
	IPDB []byte
	//path to GeoLite2-Country.mmdb[.gz] file,
	//if not provided falls back to automatically fetch
	IPDBPath string
	//disable automatic fetch of GeoLite2-Country.mmdb file
	//by default, it will be first look in os.TempDir, if missing
	//it will be fetched, cached on disk, then loaded into memory (~19MB)
	IPDBNoFetch bool
	//block by default (defaults to allow)
	BlockByDefault bool
	//update the downloaded filter db
	Refresh bool
}

Options for IPFilter. Allowed takes precendence over Blocked. IPs can be IPv4 or IPv6 and can optionally contain subnet masks (/24). Note however, determining if a given IP is included in a subnet requires a linear scan so is less performant than looking up single IPs.

This could be improved with some algorithmic magic.

Jump to

Keyboard shortcuts

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