Documentation ¶
Index ¶
- Variables
- func IPToCountry(db *maxminddb.Reader, ipstr string) string
- func NetIPToCountry(db *maxminddb.Reader, ip net.IP) string
- type IPFilter
- func (f *IPFilter) AllowCountry(code string)
- func (f *IPFilter) AllowIP(ip string) bool
- func (f *IPFilter) Allowed(ipstr string) bool
- func (f *IPFilter) BlockCountry(code string)
- func (f *IPFilter) BlockIP(ip string) bool
- func (f *IPFilter) Blocked(ip string) bool
- func (f *IPFilter) IPToCountry(ipstr string) string
- func (f *IPFilter) NetAllowed(ip net.IP) bool
- func (f *IPFilter) NetBlocked(ip net.IP) bool
- func (f *IPFilter) NetIPToCountry(ip net.IP) string
- func (m *IPFilter) Serve(ctx *iris.Context)
- func (f *IPFilter) ToggleCountry(code string, allowed bool)
- func (f *IPFilter) ToggleDefault(allowed bool)
- func (f *IPFilter) ToggleIP(str string, allowed bool) bool
- type Options
Constants ¶
This section is empty.
Variables ¶
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 ¶
IPToCountry 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 ¶
New blocks during database intilisation and checks validity IP strings. returns an error on failure.
func NewLazy ¶
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 (*IPFilter) BlockCountry ¶
func (*IPFilter) IPToCountry ¶
IP string to ISO country code. Returns an empty string when cannot determine country.
func (*IPFilter) NetAllowed ¶
Allowed returns if a given net.IP can pass through the filter
func (*IPFilter) NetBlocked ¶
Blocked returns if a given net.IP can NOT pass through the filter
func (*IPFilter) NetIPToCountry ¶
net.IP to ISO country code. Returns an empty string when cannot determine country.
func (*IPFilter) ToggleCountry ¶
ToggleCountry alters a specific country setting
func (*IPFilter) ToggleDefault ¶
ToggleDefault alters the default setting
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.