ipset

package module
v0.0.0-...-1bbf2dd Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2020 License: MIT Imports: 8 Imported by: 0

README

go-ipset

GoDoc

go-ipset provides basic bindings for the ipset kernel utility.

Installation

go get github.com/Z10N0110/go-ipset

Usage

The following are some basic usage examples for go-iptables. For more information, please checkout the godoc.

import "github.com/Z10N0110/ipset"

// Construct a new ipset instance
ipset, err := ipset.New()
if err != nil {
    // Your custom error handling here.
}

// Create a new set
err := ipset.Create("my_set", "hash:ip")
if err != nil {
    // Your custom error handling here.
}
Adding an entry to an ipset
err := ipset.Add("my_set", "127.0.0.1")
if err != nil {
    // Your custom error handling here.
}
Removing an entry from an ipset
err := ipset.Add("my_set", "127.0.0.1")
if err != nil {
    // Your custom error handling here.
}
Refresh named set with new entries
err := ipset.Refresh("my_set", "127.0.0.2", "192.168.1.1")
if err != nil {
    // Your custom error handling here.
}
List all sets names
names, err := ipset.ListSetNames()
if err != nil {
    // Your custom error handling here.
}
Get all entries of a named set
entries, err := ipset.ListEntries("my_set")
if err != nil {
    // Your custom error handling here.
}
Save your ipset to a file
err := ipset.Save("my_set", "/tmp/my_set.txt")
if err != nil {
    // Your custom error handling here.
}
Restore your ipset from a file
err := ipset.Restore("/tmp/my_set.txt")
if err != nil {
    // Your custom error handling here.
}

Documentation

Overview

Package ipset provides a basic wrapper to the ipset utility for IPTables. More information about ipset can be found at: http://ipset.netfilter.org/index.html

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IPSet

type IPSet struct {
	Path string
	// contains filtered or unexported fields
}

IPSet represents a ipset cmd executor.

func New

func New() (*IPSet, error)

New creates a new IPSet.

func (*IPSet) Add

func (set *IPSet) Add(name string, entry string, options ...string) error

Add adds a new entry to the named set.

func (*IPSet) AddUnique

func (set *IPSet) AddUnique(name, entry string, options ...string) error

AddUnique adds a new entry to the named set, if it does not already exist.

func (*IPSet) Create

func (set *IPSet) Create(name string, typ string, options ...string) error

Create creates a new ipset with a given name and type. For more on set types, please see: http://ipset.netfilter.org/ipset.man.html#lbAT. Additional options can be passed to the Create() command. These options must be passed in a sequential key, value order. For example, ipset.Create("test", "hash:ip", "timeout", "300") will add a new set with the timeout option set to a value of 300.

func (*IPSet) Delete

func (set *IPSet) Delete(name string, entry string, options ...string) error

Delete removes an entry from the named set.

func (*IPSet) Destroy

func (set *IPSet) Destroy(name string) error

Destroy destroys a named set, or all sets.

func (*IPSet) Flush

func (set *IPSet) Flush(name string) error

Flush removes all entries from a named set.

func (*IPSet) GetReferences

func (set *IPSet) GetReferences(name string) (int64, error)

GetReferences returns the named set's References

func (*IPSet) List

func (set *IPSet) List(name string, suppressMembers bool) (*Set, error)

List shows the named set by unmarshal xml output.

func (*IPSet) ListEntries

func (set *IPSet) ListEntries(name string) (entries []string, err error)

ListEntries shows the entries of a named set.

func (*IPSet) ListSetNames

func (set *IPSet) ListSetNames() (names []string, err error)

ListSetNames returns a slice containing the name of each set.

func (*IPSet) ListSets

func (set *IPSet) ListSets(suppressMembers bool) (sets []*Set, err error)

ListSets returns a slice of each set.

func (*IPSet) Refresh

func (set *IPSet) Refresh(name string, entries ...string) error

Refresh use swap command to overwrite the set with the specified entries.

func (*IPSet) Rename

func (set *IPSet) Rename(from string, to string) error

Rename changes a set name from one value to another.

func (*IPSet) Restore

func (set *IPSet) Restore(filename string) error

Restore restores a saved set from the given file.

func (*IPSet) Save

func (set *IPSet) Save(name string, filename string) error

Save saves the named set or all sets to the given file.

func (*IPSet) Swap

func (set *IPSet) Swap(from string, to string) error

Swap swaps the content of two existing sets.

func (*IPSet) Test

func (set *IPSet) Test(name string, entry string, options ...string) error

Test tests if an entry exists in the named set. The exit status is zero if the tested entry is in the set, and nonzero if it is missing from the set.

type Set

type Set struct {
	XMLName  xml.Name `xml:"ipset"`
	Name     string   `xml:"name,attr"`
	Type     string   `xml:"type"`
	Revision string   `xml:"revision"`
	Header   struct {
		XMLName    xml.Name `xml:"header"`
		Family     string   `xml:"family"`
		HashSize   int64    `xml:"hashsize"`
		Maxelem    int64    `xml:"maxelem"`
		MemSize    int64    `xml:"memsize"`
		References int64    `xml:"references"`
		Numentries int64    `xml:"numentries"`
	} `xml:"header"`
	Members struct {
		XMLName xml.Name    `xml:"members"`
		Members []*SetEntry `xml:"member"`
	} `xml:"members"`
}

Set ...

type SetEntry

type SetEntry struct {
	XMLName xml.Name `xml:"member"`
	Elem    string   `xml:"elem"`
}

SetEntry ...

type Sets

type Sets struct {
	XMLName xml.Name `xml:"ipsets"`
	Sets    []*Set   `xml:"ipset"`
}

Sets ...

Jump to

Keyboard shortcuts

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