Skip to content

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

License

Notifications You must be signed in to change notification settings

aojea/mem-resolver

Repository files navigation

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages