All Projects → gonetx → ipset

gonetx / ipset

Licence: Apache-2.0 license
Golang wrapper of ipset.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to ipset

Blocklist Ipsets
ipsets dynamically updated with firehol's update-ipsets.sh script
Stars: ✭ 2,011 (+9476.19%)
Mutual labels:  ipset
Glider
glider is a forward proxy with multiple protocols support, and also a dns/dhcp server with ipset management features(like dnsmasq).
Stars: ✭ 1,710 (+8042.86%)
Mutual labels:  ipset
nginx ipset blacklist
nginx module to use linux netfilter ipsets as blacklists
Stars: ✭ 34 (+61.9%)
Mutual labels:  ipset
ipset-netgear-r7000-dd-wrt
Packages and kernel modules for ipset support for the Netgear R7000 running DD-WRT firmware
Stars: ✭ 45 (+114.29%)
Mutual labels:  ipset
nftables-example
A playground ruleset to get to know nftables syntax
Stars: ✭ 19 (-9.52%)
Mutual labels:  ipset
blackip
IP Blocklist for Ipset / Squid-Cache
Stars: ✭ 81 (+285.71%)
Mutual labels:  ipset
go-ipset
🔥 Go bindings for the IPtables ipset http://ipset.netfilter.org userspace utility
Stars: ✭ 110 (+423.81%)
Mutual labels:  ipset
httpit
A rapid http(s) benchmark tool written in Go
Stars: ✭ 156 (+642.86%)
Mutual labels:  gonetx

gonetx/ipset

This package is a almost whole Golang wrapper to the ipset userspace utility. It allows Golang programs to easily manipulate ipset.

Visit http://ipset.netfilter.org/ipset.man.html for more ipset command details. And ipset requires version v6.0+.

Installation

Install ipset using the go get command:

go get -u github.com/gonetx/ipset

Quickstart

package main

import (
	"log"
	"time"

	"github.com/gonetx/ipset"
)

func init() {
	if err := ipset.Check(); err != nil {
		panic(err)
	}
}

func main() {
	// create test set even it's exist
	set, _ := ipset.New("test", ipset.HashIp, ipset.Exist(true), ipset.Timeout(time.Hour))
	// output: test
	log.Println(set.Name())

	_ = set.Flush()

	_ = set.Add("1.1.1.1", ipset.Timeout(time.Hour))

	ok, _ := set.Test("1.1.1.1")
	// output: true
	log.Println(ok)

	ok, _ = set.Test("1.1.1.2")
	// output: false
	log.Println(ok)

	info, _ := set.List()
	// output: &{test hash:ip 4 family inet hashsize 1024 maxelem 65536 timeout 3600 216 0 [1.1.1.1 timeout 3599]}
	log.Println(info)

	_ = set.Del("1.1.1.1")

	_ = set.Destroy()
}

Check

Before using this library, you should remember always to call ipset.Check first and handle the error. This method will check if ipset is in OS PATH and if its version is valid.

func init() {
	// err will be ipset.ErrNotFound
	// or ipset.ErrVersionNotSupported
	// if check failed.
	if err := ipset.Check(); err != nil {
		panic(err)
	}
}

New

Use ipset.New to create a set identified with setname and specified set type. If the ipset.Exist option is specified, ipset will ignore the error when the same set (setname and create parameters are identical) already exists.

set, _ := ipset.New("test", ipset.HashIp, ipset.Exist(true), ipset.Netmask(24))

Every set type may has different create options, visit SetType and Option for more details.

Once you created a set, you can use these methods:

// IPSet is abstract of ipset
type IPSet interface {
	// List dumps header data and the entries for the set to an
	// *Info instance. The Resolve option can be used to force
	// action lookups(which may be slow).
	List(options ...Option) (*Info, error)

	// List dumps header data and the entries for the set to the
	// specific file. The Resolve option can be used to force
	// action lookups(which may be slow).
	ListToFile(filename string, options ...Option) error

	// Name returns the set's name
	Name() string

	// Rename the set's action and the new action must not exist.
	Rename(newName string) error

	// Add adds a given entry to the set. If the Exist option is
	// specified, ipset ignores the error if the entry already
	// added to the set.
	Add(entry string, options ...Option) error

	// Del deletes an entry from a set. If the Exist option is
	// specified and the entry is not in the set (maybe already
	// expired), then the command ignores the error.
	Del(entry string, options ...Option) error

	// Test tests whether an entry is in a set or not.
	Test(entry string) (bool, error)

	// Flush flushed all entries from the the set.
	Flush() error

	// Destroy removes the set from kernel.
	Destroy() error

	// Save dumps the set data to a io.Reader in a format that restore
	// can read.
	Save(options ...Option) (io.Reader, error)

	// SaveToFile dumps the set data to s specific file in a format
	// that restore can read.
	SaveToFile(filename string, options ...Option) error

	// Restore restores a saved session from io.Reader generated by
	// save. Set exist to true to ignore exist error.
	Restore(r io.Reader, exist ...bool) error

	// RestoreFromFile restores a saved session from a specific file
	// generated by save. Set exist to true to ignore exist error.
	RestoreFromFile(filename string, exist ...bool) error
}

Swap

Use ipset.Swap to swap the content of two sets, or in another words, exchange the action of two sets. The referred sets must exist and compatible type of sets can be swapped only.

ipset.Swap("foo", "bar")

Flush

Use ipset.Flush to flush all entries from the specified set or flush all sets if none is given.

// Flush foo and bar set
ipset.Flush("foo", "bar")

// Flush all
ipset.Flush()

Destroy

Use ipset.Destroy to removes the specified set or all the sets if none is given. If the set has got reference(s), nothing is done and no set destroyed.

// Destroy foo and bar set
ipset.Destroy("foo", "bar")

// Destroy all
ipset.Destroy()
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].