awl/pkg/resolvers/resolver.go
Sam Therapy b80219019e
All checks were successful
continuous-integration/drone/push Build is passing
Add check for port at the end (#142)
Fixes #141

Before, a failure would add on the port, eg.
```
127.0.0.1:53
127.0.0.1:53:53
127.0.0.1:53:53:53 // Go actually thinks this is now an IPv6 address, interesting
```

Now a check is added so this doesn't happen

Reviewed-on: #142
Reviewed-by: grumbulon <grumbulon@grumbulon.xyz>
2022-10-16 14:25:13 +00:00

69 lines
1.5 KiB
Go

// SPDX-License-Identifier: BSD-3-Clause
package resolvers
import (
"net"
"strconv"
"strings"
"git.froth.zone/sam/awl/pkg/util"
"github.com/miekg/dns"
)
const (
tcp = "tcp"
udp = "udp"
)
// Resolver is the main resolver interface.
type Resolver interface {
LookUp(*dns.Msg) (util.Response, error)
}
// LoadResolver loads the respective resolver for performing a DNS query.
func LoadResolver(opts *util.Options) (Resolver, error) {
switch {
case opts.HTTPS:
opts.Logger.Info("loading DNS-over-HTTPS resolver")
if !strings.HasPrefix(opts.Request.Server, "https://") {
opts.Request.Server = "https://" + opts.Request.Server
}
return &HTTPSResolver{
opts: opts,
}, nil
case opts.QUIC:
opts.Logger.Info("loading DNS-over-QUIC resolver")
if !strings.HasSuffix(opts.Request.Server, ":"+strconv.Itoa(opts.Request.Port)) {
opts.Request.Server = net.JoinHostPort(opts.Request.Server, strconv.Itoa(opts.Request.Port))
}
return &QUICResolver{
opts: opts,
}, nil
case opts.DNSCrypt:
opts.Logger.Info("loading DNSCrypt resolver")
if !strings.HasPrefix(opts.Request.Server, "sdns://") {
opts.Request.Server = "sdns://" + opts.Request.Server
}
return &DNSCryptResolver{
opts: opts,
}, nil
default:
opts.Logger.Info("loading standard/DNS-over-TLS resolver")
if !strings.HasSuffix(opts.Request.Server, ":"+strconv.Itoa(opts.Request.Port)) {
opts.Request.Server = net.JoinHostPort(opts.Request.Server, strconv.Itoa(opts.Request.Port))
}
return &StandardResolver{
opts: opts,
}, nil
}
}