Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions dnsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rdns
import (
"crypto/tls"
"net"
"time"

"github.com/miekg/dns"
"github.com/sirupsen/logrus"
Expand All @@ -20,6 +21,10 @@ type DNSClient struct {
type DNSClientOptions struct {
// Local IP to use for outbound connections. If nil, a local address is chosen.
LocalAddr net.IP

// Timeout is the maximum amount of time a dial will wait for
// a connect to complete.
Timeout time.Duration
}

var _ Resolver = &DNSClient{}
Expand All @@ -30,14 +35,13 @@ func NewDNSClient(id, endpoint, network string, opt DNSClientOptions) (*DNSClien
if err := validEndpoint(endpoint); err != nil {
return nil, err
}
// Use a custom dialer if a local address was provided
var dialer *net.Dialer
dialer := &net.Dialer{Timeout: opt.Timeout}
if opt.LocalAddr != nil {
switch network {
case "tcp":
dialer = &net.Dialer{LocalAddr: &net.TCPAddr{IP: opt.LocalAddr}}
dialer.LocalAddr = &net.TCPAddr{IP: opt.LocalAddr}
case "udp":
dialer = &net.Dialer{LocalAddr: &net.UDPAddr{IP: opt.LocalAddr}}
dialer.LocalAddr = &net.UDPAddr{IP: opt.LocalAddr}
}
}

Expand Down
4 changes: 4 additions & 0 deletions dohclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ type DoHClientOptions struct {
// Local IP to use for outbound connections. If nil, a local address is chosen.
LocalAddr net.IP

// Timeout is the maximum amount of time a dial will wait for
// a connect to complete.
Timeout time.Duration

TLSConfig *tls.Config
}

Expand Down
10 changes: 7 additions & 3 deletions dotclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rdns
import (
"crypto/tls"
"net"
"time"

"github.com/miekg/dns"
"github.com/pkg/errors"
Expand All @@ -26,6 +27,10 @@ type DoTClientOptions struct {
// Local IP to use for outbound connections. If nil, a local address is chosen.
LocalAddr net.IP

// Timeout is the maximum amount of time a dial will wait for
// a connect to complete.
Timeout time.Duration

TLSConfig *tls.Config
}

Expand All @@ -37,10 +42,9 @@ func NewDoTClient(id, endpoint string, opt DoTClientOptions) (*DoTClient, error)
return nil, err
}

// Use a custom dialer if a local address was provided
var dialer *net.Dialer
dialer := &net.Dialer{Timeout: opt.Timeout}
if opt.LocalAddr != nil {
dialer = &net.Dialer{LocalAddr: &net.TCPAddr{IP: opt.LocalAddr}}
dialer.LocalAddr = &net.TCPAddr{IP: opt.LocalAddr}
}
client := &dns.Client{
Net: "tcp-tls",
Expand Down