d57c78bc71
This is how I did it: 1. `git clone https://github.com/ooni/probe-engine internal/engine` 2. ``` (cd internal/engine && git describe --tags) v0.23.0 ``` 3. `nvim go.mod` (merging `go.mod` with `internal/engine/go.mod` 4. `rm -rf internal/.git internal/engine/go.{mod,sum}` 5. `git add internal/engine` 6. `find . -type f -name \*.go -exec sed -i 's@/ooni/probe-engine@/ooni/probe-cli/v3/internal/engine@g' {} \;` 7. `go build ./...` (passes) 8. `go test -race ./...` (temporary failure on RiseupVPN) 9. `go mod tidy` 10. this commit message Once this piece of work is done, we can build a new version of `ooniprobe` that is using `internal/engine` directly. We need to do more work to ensure all the other functionality in `probe-engine` (e.g. making mobile packages) are still WAI. Part of https://github.com/ooni/probe/issues/1335
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package resolver
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
// The Decoder decodes a DNS reply into A or AAAA entries. It will use the
|
|
// provided qtype and only look for mathing entries. It will return error if
|
|
// there are no entries for the requested qtype inside the reply.
|
|
type Decoder interface {
|
|
Decode(qtype uint16, data []byte) ([]string, error)
|
|
}
|
|
|
|
// MiekgDecoder uses github.com/miekg/dns to implement the Decoder.
|
|
type MiekgDecoder struct{}
|
|
|
|
// Decode implements Decoder.Decode.
|
|
func (d MiekgDecoder) Decode(qtype uint16, data []byte) ([]string, error) {
|
|
reply := new(dns.Msg)
|
|
if err := reply.Unpack(data); err != nil {
|
|
return nil, err
|
|
}
|
|
// TODO(bassosimone): map more errors to net.DNSError names
|
|
switch reply.Rcode {
|
|
case dns.RcodeSuccess:
|
|
case dns.RcodeNameError:
|
|
return nil, errors.New("ooniresolver: no such host")
|
|
default:
|
|
return nil, errors.New("ooniresolver: query failed")
|
|
}
|
|
var addrs []string
|
|
for _, answer := range reply.Answer {
|
|
switch qtype {
|
|
case dns.TypeA:
|
|
if rra, ok := answer.(*dns.A); ok {
|
|
ip := rra.A
|
|
addrs = append(addrs, ip.String())
|
|
}
|
|
case dns.TypeAAAA:
|
|
if rra, ok := answer.(*dns.AAAA); ok {
|
|
ip := rra.AAAA
|
|
addrs = append(addrs, ip.String())
|
|
}
|
|
}
|
|
}
|
|
if len(addrs) <= 0 {
|
|
return nil, errors.New("ooniresolver: no response returned")
|
|
}
|
|
return addrs, nil
|
|
}
|
|
|
|
var _ Decoder = MiekgDecoder{}
|