resolver

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

README

mem-resolver

memresolver is an in-memory golang resolver that allows to override current golang Lookup func literals

How to use it

Create your custom Lookup function, is it common to overwrite LookupIP, that is the one used by the Dialer so we can redirect custom domains, per example.

func myLookupIP(ctx context.Context, network, host string) ([]net.IP, error) {
	// fqdn appends a dot
	if "mycustom.mydomain" == strings.TrimSuffix(host, ".") {
		return []net.IP{net.ParseIP("127.0.0.1")}, nil
	}
	return net.DefaultResolver.LookupIP(ctx, network, host)
}

Once we have our cusotm Lookup function we create a custom net.Resolver

	f := &MemResolver{
		LookupIP: myLookupIP,
	}
	// override lookupIP
	resolver := NewMemoryResolver(f)

This custom resolver implements a Dial function that can be used to override the net.Dialer resolver.

	tr := &http.Transport{
		Proxy: http.ProxyFromEnvironment,
		DialContext: (&net.Dialer{
			Timeout:   30 * time.Second,
			KeepAlive: 30 * time.Second,
			Resolver:  resolver,
		}).DialContext,
	}
	client := &http.Client{
		Transport: tr,
	}

Check example_test.go for a full example.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMemoryResolver

func NewMemoryResolver(r *MemResolver) *net.Resolver

MemoryResolver returns an in-memory resolver that can override golang Lookup functions.

Types

type MemResolver

type MemResolver struct {
	LookupAddr  func(ctx context.Context, addr string) (names []string, err error)
	LookupCNAME func(ctx context.Context, host string) (cname string, err error)
	LookupHost  func(ctx context.Context, host string) (addrs []string, err error)
	LookupIP    func(ctx context.Context, network, host string) ([]net.IP, error)
	LookupMX    func(ctx context.Context, name string) ([]*net.MX, error)
	LookupNS    func(ctx context.Context, name string) ([]*net.NS, error)
	LookupPort  func(ctx context.Context, network, service string) (port int, err error)
	LookupSRV   func(ctx context.Context, service, proto, name string) (cname string, addrs []*net.SRV, err error)
	LookupTXT   func(ctx context.Context, name string) ([]string, error)
}

MemResolver implement an in memory resolver that receives DNS questions and executes the corresponding Lookup functions. If the corresponding Lookup function is not present, it uses the DefaultResolver ones.

func (*MemResolver) Dial

func (r *MemResolver) Dial(ctx context.Context, network, address string) (net.Conn, error)

Dial creates an in memory connection to the in-memory resolver. Used to create a custom net.Resolver

Jump to

Keyboard shortcuts

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