dnsproxy

package module
v0.0.0-...-4c302a9 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2018 License: MIT Imports: 10 Imported by: 1

README

A simple library for DNS proxy

This is a completed DNS proxy.

Usage

Like example as cmd/dnsproxy

go get github.com/Asphaltt/dnsproxy-go

and use the library in your project.

	cfg := &dnsproxy.Config{
		Addr:          ":53",
		UpServers:     []string{"8.8.8.8"},
		WithCache:     true,
		CacheFile:     "cache.json",
		WorkerPoolMin: 100,
		WorkerPoolMax: 1000,
	}

	if err := dnsproxy.Start(cfg); err != nil {
		return
	}
	defer dnsproxy.Close()

Or you can compile cmd/dnsproxy to run as a dns proxy server.

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound        = errors.New("Not Found")
	ErrServerFailed    = errors.New("Server Failed")
	ErrInvalidResponse = errors.New("Invalid Response")
	ErrUnexpectedResp  = errors.New("Unexpected Response")
	ErrHugePacket      = errors.New("Huge Packet")
	ErrCyclicCNAME     = errors.New("Maybe cyclic CNAME")
)

predefined errors

Functions

func Close

func Close() error

Close closes the running dnsproxy

func FindCname

func FindCname(msg *dns.Msg) (string, bool)

FindCname gets the final cname in the msg.

func FindExtras

func FindExtras(msg *dns.Msg) []string

FindExtras gets a string array from msg's extra rr

func FindNS

func FindNS(msg *dns.Msg) ([]string, bool)

FindNS gets a string array from msg's ns rr, which has no answer.

func FindNSExtras

func FindNSExtras(msg *dns.Msg) (*dns.Msg, bool)

FindNSExtras gets a copied message from msg, the copy's answer is msg's extra

func GotAnswer

func GotAnswer(msg *dns.Msg) bool

GotAnswer gets whether do get the answer, whose RRtype is same as Qtype or the NS RR is a SOA

func IsEmptyResponse

func IsEmptyResponse(msg *dns.Msg) bool

IsEmptyResponse gets whether the msg is an empty response which has no answer, no ns and no extra

func IsSuccessfulResponse

func IsSuccessfulResponse(msg *dns.Msg) bool

IsSuccessfulResponse gets whether the msg is a successful response

func NewQuery

func NewQuery(names []string) *dns.Msg

NewQuery creates a new dns query messge

func NewResponse

func NewResponse(rcode int) *dns.Msg

NewResponse creates a new dns response message with the gived rcode

func Start

func Start(cfg *Config) error

Start starts to run dnsproxy-server.

Types

type Config

type Config struct {
	// address to listen on
	Addr string

	// up dns servers to proxy
	UpServers []string

	// proxy with dns cache
	WithCache bool
	CacheFile string

	// worker pool size
	WorkerPoolMin, WorkerPoolMax int
}

Config is the config about the dnsproxy

type Record

type Record struct {
	Expired time.Time
	Msg     *dns.Msg
}

Record is a cached record, Expired is the expire timestamp, Msg is the dns message.

func NewRecord

func NewRecord(msg *dns.Msg) (*Record, bool)

NewRecord creates a new record from msg.

func (*Record) IsExpired

func (r *Record) IsExpired() bool

IsExpired gets whether the record has been expired.

type Trie

type Trie struct {
	sync.RWMutex
	IsLeaf bool
	Next   map[rune]*Trie
	Data   interface{}
}

Trie is a standard trie tree for dns.

func NewTrie

func NewTrie() *Trie

NewTrie creates a new trie.

func (*Trie) Add

func (t *Trie) Add(name string, r *Record)

Add adds a record to the trie.

func (*Trie) Delete

func (t *Trie) Delete(name string)

Delete deletes the data whose key is name.

func (*Trie) Find

func (t *Trie) Find(name string) (val interface{}, ok bool)

Find finds the data of the key `name`

func (*Trie) Get

func (t *Trie) Get(name string) (*Record, bool)

Get gets a record from trie whose key is name. Delete the record, if the record has been expired.

func (*Trie) Insert

func (t *Trie) Insert(name string, data interface{})

Insert inserts data to the trie.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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