AdGuardHome/internal/aghtest/resolver.go
Ainar Garipov c08bf86b71 Pull request: all: update go and backend tools
Updates #2275.

Squashed commit of the following:

commit f24c26cd2b49fac00a581936da4ccb13ca341bc9
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Mar 10 21:33:15 2021 +0300

    aghtest: imp docs

commit 46f5b06f9743e800b489e8c30af07d24bfdcf989
Merge: bfb852cb 55d4c7ee
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Mar 10 21:32:32 2021 +0300

    Merge branch 'master' into 2275-upd

commit bfb852cbc74ec219a41e985f2dcb090d58299aee
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Mar 10 19:06:32 2021 +0300

    scripts: rem unsupported platform

commit c1645e247f18d384a980c60d3a94b9363f83f174
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Mar 10 18:47:57 2021 +0300

    all: rollback more

commit 989811b5e38498234dc11baf5dd153c76b9dada4
Merge: 976bdfbd 2d704242
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Mar 10 18:30:42 2021 +0300

    Merge branch 'master' into 2275-upd

commit 976bdfbdd44983f4cd657a486b94ff63f5885fd5
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Mar 10 18:28:23 2021 +0300

    aghtest: fix os_windows

commit 9e85080eefe882d72c939969f7008e3c46467c0c
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Mar 10 18:15:37 2021 +0300

    all: rewrite windows workaround, imp code, docs

commit 35a0b1d8656640a962fe9ae019c3d665f42707ce
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Jan 21 20:38:17 2021 +0300

    all: update go and backend tools
2021-03-11 12:17:54 +03:00

57 lines
1.3 KiB
Go

package aghtest
import (
"context"
"crypto/sha256"
"net"
"sync"
)
// TestResolver is a Resolver for tests.
type TestResolver struct {
counter int
counterLock sync.Mutex
}
// HostToIPs generates IPv4 and IPv6 from host.
func (r *TestResolver) HostToIPs(host string) (ipv4, ipv6 net.IP) {
hash := sha256.Sum256([]byte(host))
return net.IP(hash[:4]), net.IP(hash[4:20])
}
// LookupIP implements Resolver interface for *testResolver. It returns the
// slice of net.IP with IPv4 and IPv6 instances.
func (r *TestResolver) LookupIP(_ context.Context, _, host string) (ips []net.IP, err error) {
ipv4, ipv6 := r.HostToIPs(host)
addrs := []net.IP{ipv4, ipv6}
r.counterLock.Lock()
defer r.counterLock.Unlock()
r.counter++
return addrs, nil
}
// LookupHost implements Resolver interface for *testResolver. It returns the
// slice of IPv4 and IPv6 instances converted to strings.
func (r *TestResolver) LookupHost(host string) (addrs []string, err error) {
ipv4, ipv6 := r.HostToIPs(host)
r.counterLock.Lock()
defer r.counterLock.Unlock()
r.counter++
return []string{
ipv4.String(),
ipv6.String(),
}, nil
}
// Counter returns the number of requests handled.
func (r *TestResolver) Counter() int {
r.counterLock.Lock()
defer r.counterLock.Unlock()
return r.counter
}