feat: add support for tcp, dot

pull/2/head
Karan Sharma 2020-12-12 13:16:45 +05:30
parent 8bcd940685
commit 0aa3b36b35
6 changed files with 48 additions and 22 deletions

View File

@ -2,10 +2,14 @@
## Resolver ## Resolver
- [x] Create a DNS Resolver struct - [x] Create a DNS Resolver struct
- [ ] Add methods to initialise the config, set defaults - [x]] Add methods to initialise the config, set defaults
- [x] Add a resolve method - [x] Add a resolve method
- [x] Make it separate from Hub - [x] Make it separate from Hub
- [ ] Parse output into separate fields - [ ] Parse output into separate fields
- [ ] Test UDP6
- [x] Add DOH support
- [x] Add DOT support
- [x] Add DNS protocol on TCP mode support.
## CLI Features ## CLI Features
- [ ] `digfile` - [ ] `digfile`

View File

@ -69,16 +69,26 @@ func main() {
&cli.BoolFlag{ &cli.BoolFlag{
Name: "udp", Name: "udp",
Usage: "Use the DNS protocol over UDP", Usage: "Use the DNS protocol over UDP",
Aliases: []string{"U"},
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "tcp", Name: "tcp",
Usage: "Use the DNS protocol over TCP", Usage: "Use the DNS protocol over TCP",
Aliases: []string{"T"},
Destination: &hub.QueryFlags.UseTCP,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "https", Name: "https",
Usage: "Use the DNS-over-HTTPS protocol", Usage: "Use the DNS-over-HTTPS protocol",
Aliases: []string{"H"},
Destination: &hub.QueryFlags.IsDOH, Destination: &hub.QueryFlags.IsDOH,
}, },
&cli.BoolFlag{
Name: "tls",
Usage: "Use the DNS-over-TLS",
Aliases: []string{"S"},
Destination: &hub.QueryFlags.IsDOT,
},
&cli.BoolFlag{ &cli.BoolFlag{
Name: "ipv6", Name: "ipv6",
Aliases: []string{"6"}, Aliases: []string{"6"},

View File

@ -25,7 +25,7 @@ type QueryFlags struct {
IsDOH bool IsDOH bool
IsDOT bool IsDOT bool
IsUDP bool IsUDP bool
IsTLS bool UseTCP bool
UseIPv4 bool UseIPv4 bool
UseIPv6 bool UseIPv6 bool
} }

View File

@ -7,7 +7,8 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
// loadResolver checks // loadResolver checks for various flags and initialises
// the correct resolver based on the config.
func (hub *Hub) loadResolver(c *cli.Context) error { func (hub *Hub) loadResolver(c *cli.Context) error {
// check if DOH flag is set. // check if DOH flag is set.
if hub.QueryFlags.IsDOH { if hub.QueryFlags.IsDOH {
@ -18,11 +19,6 @@ func (hub *Hub) loadResolver(c *cli.Context) error {
hub.Resolver = rslvr hub.Resolver = rslvr
return nil return nil
} }
// check if DOT flag is set.
// check if TCP flag is set.
// fallback to good ol UDP.
if len(hub.QueryFlags.Nameservers.Value()) == 0 { if len(hub.QueryFlags.Nameservers.Value()) == 0 {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
// TODO: Add a method for reading system default nameserver in windows. // TODO: Add a method for reading system default nameserver in windows.
@ -38,6 +34,8 @@ func (hub *Hub) loadResolver(c *cli.Context) error {
rslvr, err := resolvers.NewClassicResolver(hub.QueryFlags.Nameservers.Value(), resolvers.ClassicResolverOpts{ rslvr, err := resolvers.NewClassicResolver(hub.QueryFlags.Nameservers.Value(), resolvers.ClassicResolverOpts{
UseIPv4: hub.QueryFlags.UseIPv4, UseIPv4: hub.QueryFlags.UseIPv4,
UseIPv6: hub.QueryFlags.UseIPv6, UseIPv6: hub.QueryFlags.UseIPv6,
UseTLS: hub.QueryFlags.IsDOT,
UseTCP: hub.QueryFlags.UseTCP,
}) })
if err != nil { if err != nil {
return err return err

View File

@ -7,6 +7,15 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
) )
const (
// DefaultUDPPort specifies the default port for a DNS server connecting over UDP
DefaultUDPPort = "53"
// DefaultTLSPort specifies the default port for a DNS server connecting over TCP over TLS
DefaultTLSPort = "853"
//DefaultResolvConfPath specifies path to default resolv config file on UNIX.
DefaultResolvConfPath = "/etc/resolv.conf"
)
// ClassicResolver represents the config options for setting up a Resolver. // ClassicResolver represents the config options for setting up a Resolver.
type ClassicResolver struct { type ClassicResolver struct {
client *dns.Client client *dns.Client
@ -18,24 +27,24 @@ type ClassicResolverOpts struct {
UseIPv4 bool UseIPv4 bool
UseIPv6 bool UseIPv6 bool
UseTCP bool UseTCP bool
UseTLS bool
} }
//DefaultResolvConfPath specifies path to default resolv config file on UNIX.
const DefaultResolvConfPath = "/etc/resolv.conf"
// NewClassicResolver accepts a list of nameservers and configures a DNS resolver. // NewClassicResolver accepts a list of nameservers and configures a DNS resolver.
func NewClassicResolver(servers []string, opts ClassicResolverOpts) (Resolver, error) { func NewClassicResolver(servers []string, opts ClassicResolverOpts) (Resolver, error) {
client := &dns.Client{} client := &dns.Client{}
var nameservers []string var nameservers []string
for _, srv := range servers { for _, srv := range servers {
if i := net.ParseIP(srv); i != nil { if i := net.ParseIP(srv); i != nil {
nameservers = append(nameservers, net.JoinHostPort(srv, "53")) // if no port specified in nameserver, append defaults.
if opts.UseTLS == true {
nameservers = append(nameservers, net.JoinHostPort(srv, DefaultTLSPort))
} else { } else {
host, port, err := net.SplitHostPort(srv) nameservers = append(nameservers, net.JoinHostPort(srv, DefaultUDPPort))
if err != nil {
return nil, err
} }
nameservers = append(nameservers, fmt.Sprintf("%s:%s", host, port)) } else {
// use the port user specified.
nameservers = append(nameservers, srv)
} }
} }
client.Net = "udp" client.Net = "udp"
@ -45,6 +54,12 @@ func NewClassicResolver(servers []string, opts ClassicResolverOpts) (Resolver, e
if opts.UseIPv6 { if opts.UseIPv6 {
client.Net = "udp6" client.Net = "udp6"
} }
if opts.UseTCP {
client.Net = "tcp"
}
if opts.UseTLS {
client.Net = "tcp-tls"
}
return &ClassicResolver{ return &ClassicResolver{
client: client, client: client,
servers: nameservers, servers: nameservers,

View File

@ -1 +0,0 @@
package resolvers