From f2a7afe514b1c16fe705daf3f73774328bc6fd81 Mon Sep 17 00:00:00 2001 From: Andreas Groll <10852221+holygrolli@users.noreply.github.com> Date: Wed, 17 May 2023 01:35:42 +0200 Subject: [PATCH] feat: add upstream_dns_over_socks5 option (#51) --- config.defaults.yaml | 2 ++ dns.go | 1 + main.go | 37 ++++++++++++++++++++++--------------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/config.defaults.yaml b/config.defaults.yaml index c25b509..25fabcd 100644 --- a/config.defaults.yaml +++ b/config.defaults.yaml @@ -1,6 +1,8 @@ general: # Upsteam DNS URI. examples: Upstream DNS URI. examples: udp://1.1.1.1:53, tcp://1.1.1.1:53, tcp-tls://1.1.1.1:853, https://dns.google/dns-query upstream_dns: udp://8.8.8.8:53 + # enable send DNS through socks5 + upstream_dns_over_socks5: false # Use a SOCKS proxy for upstream HTTP/HTTPS traffic. Example: socks5://admin: upstream_socks5: # DNS Port to listen on. Should remain 53 in most cases. MUST NOT be empty diff --git a/dns.go b/dns.go index 1baf3d7..52b965d 100644 --- a/dns.go +++ b/dns.go @@ -83,6 +83,7 @@ func processQuestion(q dns.Question, decision acl.Decision) ([]dns.RR, error) { // Otherwise do an upstream query and use that answer. default: + dnslog.Debug().Msgf("perform external query for domain %s", q.Name) resp, rtt, err := c.dnsClient.performExternalAQuery(q.Name, q.Qtype) if err != nil { return nil, err diff --git a/main.go b/main.go index db06120..23f48f4 100644 --- a/main.go +++ b/main.go @@ -37,20 +37,21 @@ import ( ) type runConfig struct { - PublicIPv4 string `yaml:"public_ipv4"` - PublicIPv6 string `yaml:"public_ipv6"` - UpstreamDNS string `yaml:"upstream_dns"` - UpstreamSOCKS5 string `yaml:"upstream_socks5"` - BindDNSOverUDP string `yaml:"bind_dns_over_udp"` - BindDNSOverTCP string `yaml:"bind_dns_over_tcp"` - BindDNSOverTLS string `yaml:"bind_dns_over_tls"` - BindDNSOverQuic string `yaml:"bind_dns_over_quic"` - TLSCert string `yaml:"tls_cert"` - TLSKey string `yaml:"tls_key"` - BindHTTP string `yaml:"bind_http"` - BindHTTPS string `yaml:"bind_https"` - Interface string `yaml:"interface"` - BindPrometheus string `yaml:"bind_prometheus"` + PublicIPv4 string `yaml:"public_ipv4"` + PublicIPv6 string `yaml:"public_ipv6"` + UpstreamDNS string `yaml:"upstream_dns"` + UpstreamDNSOverSocks5 bool `yaml:"upstream_dns_over_socks5"` + UpstreamSOCKS5 string `yaml:"upstream_socks5"` + BindDNSOverUDP string `yaml:"bind_dns_over_udp"` + BindDNSOverTCP string `yaml:"bind_dns_over_tcp"` + BindDNSOverTLS string `yaml:"bind_dns_over_tls"` + BindDNSOverQuic string `yaml:"bind_dns_over_quic"` + TLSCert string `yaml:"tls_cert"` + TLSKey string `yaml:"tls_key"` + BindHTTP string `yaml:"bind_http"` + BindHTTPS string `yaml:"bind_https"` + Interface string `yaml:"interface"` + BindPrometheus string `yaml:"bind_prometheus"` acl []acl.ACL @@ -273,6 +274,7 @@ func main() { } c.UpstreamDNS = generalConfig.String("upstream_dns") + c.UpstreamDNSOverSocks5 = generalConfig.Bool("upstream_dns_over_socks5") c.UpstreamSOCKS5 = generalConfig.String("upstream_socks5") c.BindDNSOverUDP = generalConfig.String("bind_dns_over_udp") c.BindDNSOverTCP = generalConfig.String("bind_dns_over_tcp") @@ -382,7 +384,12 @@ func main() { c.dialer = proxy.Direct } - tmp, err := dnsclient.New(c.UpstreamDNS, true, c.UpstreamSOCKS5) + dnsProxy := c.UpstreamSOCKS5 + if c.UpstreamSOCKS5 != "" && !c.UpstreamDNSOverSocks5 { + logger.Debug().Msg("disabling socks5 for dns") + dnsProxy = "" + } + tmp, err := dnsclient.New(c.UpstreamDNS, true, dnsProxy) if err != nil { logger.Error().Msgf("error setting up dns client, removing proxy if provided: %v", err) tmp, err = dnsclient.New(c.UpstreamDNS, false, "")