resolvconf

package module
v0.0.0-...-a36852b Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2018 License: MIT Imports: 11 Imported by: 0

README

resolvconf

Build Status Go Report Card Go Documentation

Go package that simplifies manipulating resolv.conf files

The package provides a way to read and parse existing resolv.conf files from an io.Reader or to create a new file. The read objects can then be manipulated and written to a io.Writer object of your choice.

Examples:

package main

import (
	"net"
	"fmt"
	"os"
	"bytes"
	"github.com/Fa1k3n/resolvconf"
)

func main() {
	conf := resolvconf.New()
	
	// Add some options
	conf.Add(resolvconf.NewOption("debug"), resolvconf.NewOption("ndots").Set(3))

	// Add a nameservers
	conf.Add(resolvconf.NewNameserver(net.ParseIP("8.8.8.8")))

	// Add a sortlist
	conf.Add(resolvconf.NewSortItem(net.ParseIP("130.155.160.0")).SetNetmask("255.255.240.0"))

	// Dump to stdout
	conf.Write(os.Stdout)
}

Documentation

Overview

Package resolvconf provides an interface to read, create and manipulate resolv.conf files

Example
res, err := http.Get("https://gist.githubusercontent.com/turadg/7876784/raw/c7f2500fa4762cfe443e30c64c6ed8a888f6ac74/resolv.conf")
if err != nil {
	log.Fatal(err)
}
conf, err := resolvconf.ReadConf(res.Body)
if err != nil {
	log.Fatal(err)
}
res.Body.Close()
conf.Remove(resolvconf.NewNameserver(net.ParseIP("8.8.4.4")))
conf.Add(resolvconf.NewDomain("foo.bar"), resolvconf.NewSortItem(net.ParseIP("130.155.160.0")).SetNetmask(net.ParseIP("255.255.240.0")))
conf.Write(os.Stdout)
Output:

domain foo.bar
nameserver 8.8.8.8

sortlist 130.155.160.0/255.255.240.0

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conf

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

Conf represents a configuration object

func New

func New() *Conf

New creates a new configuration

func ReadConf

func ReadConf(r io.Reader) (*Conf, error)

ReadConf will read a configuration from given io.Reader

Returns a new Conf object when successful otherwise nil and an error

func (*Conf) Add

func (conf *Conf) Add(opts ...ConfItem) error

Add items to the configuration.

Errors are accumulated and can be reinterpreted as an multierror type. Logging will occur if logging has been setup using the EnableLogging call

Example
conf := resolvconf.New()
conf.Add(resolvconf.NewNameserver(net.ParseIP("8.8.8.8")))
conf.Write(os.Stdout)
Output:

nameserver 8.8.8.8
Example (Second)
conf := resolvconf.New()
conf.Add(resolvconf.NewNameserver(net.ParseIP("8.8.8.8")), resolvconf.NewOption("debug"))
conf.Write(os.Stdout)
Output:

nameserver 8.8.8.8

options debug

func (*Conf) EnableLogging

func (conf *Conf) EnableLogging(writer io.Writer) error

EnableLogging enables internal logging with given writer as output, currently only one writer is supported. conf will use LstdFlags for the logging

func (Conf) Find

func (conf Conf) Find(o ConfItem) ConfItem

Find an configure item returns nil if item is not found. Returned will be a pointer to the actual item that can be converted into expected type

func (*Conf) GetDomain

func (conf *Conf) GetDomain() Domain

GetDomain returns current domain

func (*Conf) GetNameservers

func (conf *Conf) GetNameservers() []Nameserver

GetNameservers returns a list of all added nameservers

func (*Conf) GetOptions

func (conf *Conf) GetOptions() []Option

GetOptions returns a list of all added options

func (*Conf) GetSearchDomains

func (conf *Conf) GetSearchDomains() []SearchDomain

GetSearchDomains returns a list of all added SearchDomains

func (*Conf) GetSortItems

func (conf *Conf) GetSortItems() []SortItem

GetSortItems returns list of all added sortitems

func (*Conf) Remove

func (conf *Conf) Remove(opts ...ConfItem) error

Remove items from the configuration

Errors are accumulated and can be reinterpreted as an multierror type. Logging will occur if logging has been setup using the EnableLogging call

Example
conf := resolvconf.New()
ns := resolvconf.NewNameserver(net.ParseIP("8.8.8.8"))
conf.Add(ns, resolvconf.NewNameserver(net.ParseIP("8.8.8.9")))
conf.Remove(ns)
conf.Write(os.Stdout)
Output:

nameserver 8.8.8.9

func (*Conf) Write

func (conf *Conf) Write(w io.Writer) error

Write configuration to an io.Writer

return an error if unsuccessful

type ConfItem

type ConfItem interface {
	fmt.Stringer

	Equal(b ConfItem) bool
	// contains filtered or unexported methods
}

ConfItem is the generic interface all items in a resolv.conf file must implement.

type Domain

type Domain struct {
	Name string
}

Domain is the single domain in a resolv.conf file

func NewDomain

func NewDomain(dom string) *Domain

NewDomain creates a new domain that will be used as value for the 'domain' option in the generated file

func (Domain) Equal

func (dom Domain) Equal(b ConfItem) bool

Equal compares two domains with each other, returns true if equal

func (Domain) String

func (dom Domain) String() string

type Nameserver

type Nameserver struct {
	IP net.IP // IP address
}

Nameserver is the nameserver type

func NewNameserver

func NewNameserver(IP net.IP) *Nameserver

NewNameserver creates a new Nameserver item

func (Nameserver) Equal

func (ns Nameserver) Equal(b ConfItem) bool

Equal compares to nameservers with eachother, returns true if equal

func (Nameserver) String

func (ns Nameserver) String() string

type Option

type Option struct {
	Type  string
	Value int
}

Option represents an option item which must have a Type and some options must have a value

func NewOption

func NewOption(t string) *Option

NewOption creates a new option, val must be a positive number if used. Witout val the option will be interpreted as a bolean e.g. debug , with a val the option will be interpreted as an setvalue, e.g. ndots:5

func (Option) Equal

func (opt Option) Equal(b ConfItem) bool

Equal compares two Option, return true if equal

func (Option) Get

func (opt Option) Get() int

Get returns the option value

func (*Option) Set

func (opt *Option) Set(value int) *Option

Set sets the value of an option

func (Option) String

func (opt Option) String() string

type SearchDomain

type SearchDomain struct {
	Name string
}

SearchDomain is one of the items in the search list

func NewSearchDomain

func NewSearchDomain(dom string) *SearchDomain

NewSearchDomain creates a new search domain that will be added to the 'search' list in the generated file

func (SearchDomain) Equal

func (sd SearchDomain) Equal(b ConfItem) bool

Equal compares two search domains with each other, returns true if equal

func (SearchDomain) String

func (sd SearchDomain) String() string

type SortItem

type SortItem struct {
	Address net.IP
	Netmask net.IP
}

SortItem is one of the items in the sort list, it must have an address and may have an netmask

func NewSortItem

func NewSortItem(addr net.IP) *SortItem

NewSortItem creates a new sortlist that will be added to the 'sort' item in the resolv.conf file. If mask is given the output will be IP/mask e.g. 8.8.8.8/255.255.255.0 otherwise output will be IP only, e.g. 8.8.8.8

func (SortItem) Equal

func (si SortItem) Equal(b ConfItem) bool

Equal compares two SortItems, return true if equal

func (SortItem) GetNetmask

func (si SortItem) GetNetmask() net.IP

GetNetmask returns netmask from an SortItems

func (*SortItem) SetNetmask

func (si *SortItem) SetNetmask(nm net.IP) *SortItem

SetNetmask sets the netmask for an SortItem

func (SortItem) String

func (si SortItem) String() string

Jump to

Keyboard shortcuts

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