Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aojea/mem-resolver

memresolver is an in-memory golang resolver that allows to override current golang Lookup func literals
https://github.com/aojea/mem-resolver

dns golang network resolver

Last synced: 18 days ago
JSON representation

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

Awesome Lists containing this project

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.

```go
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`

```go
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.

```go
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](./example_test.go) for a full example.