Skip to content

This library for testing individual IPs against a blocklist.

Notifications You must be signed in to change notification settings

LeakIX/golang-ipfilter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Golang IpFilter

This library for testing individual IPs against a blocklist.

An HTTP source can be provided for regular updates of the blocklist.

Basic usage

package main

import (
	"github.com/LeakIX/golang-ipfilter"
	"log"
	"time"
)

func main() {
	// Create a new filter, including private ranges, updating its blocklist from remote every 60 seconds :
	ipFilter, err := filter.NewIpFilter(
		filter.WithRanges(filter.PrivateRanges...),
		filter.WithHttpRefresh("https://some.website/blocked-networks.txt", 60*time.Second))
	if err != nil {
		log.Fatalln(err)
	}
	// Add a range to the filter
	err = ipFilter.AddRange("192.168.1.0/24")
	if err != nil {
		log.Fatalln(err)
	}
	// Handle HTTP refresh errors
	go func() {
		for {
			err := <- ipFilter.HttpErrorChan
			log.Println(err)
		}
	}()
	if ipFilter.IsIpAllowed("127.0.0.1") {
		// 127.0.0.1 is allowed
	} else {
		// 127.0.0.1 is not allowed
	}
}

About

This library for testing individual IPs against a blocklist.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages