Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/aojea/mem-resolver
- Owner: aojea
- License: apache-2.0
- Created: 2021-09-28T10:13:59.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-09-28T15:54:57.000Z (about 3 years ago)
- Last Synced: 2023-07-27T21:55:01.339Z (over 1 year ago)
- Topics: dns, golang, network, resolver
- Language: Go
- Homepage:
- Size: 20.5 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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.