From f2ac11bfc104c6a19cd4302e7c7bc145eb45b81c Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Wed, 21 Feb 2024 15:46:06 +0100 Subject: [PATCH] update CHANGELOG, fiddle more with doq proxy code --- CHANGELOG.md | 8 +++++++- src/dns_exporter/collector.py | 21 ++++++++++++--------- src/dns_exporter/config.py | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 995629a..b2d129d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased -- No changes yet +### Added +- Unit tests for proxy code + + +### Fixed +- Proxy support for DoQ should work properly now + ## [v1.0.0-beta5] - 2024-02-20 diff --git a/src/dns_exporter/collector.py b/src/dns_exporter/collector.py index ad24cbb..99f3f03 100644 --- a/src/dns_exporter/collector.py +++ b/src/dns_exporter/collector.py @@ -56,7 +56,6 @@ def __init__( self.config = config self.query = query self.labels = labels - # set proxy? if self.config.proxy: socks.set_default_proxy( @@ -64,20 +63,24 @@ def __init__( addr=self.config.proxy.hostname, port=self.config.proxy.port, ) + # use proxy socket for protocols udp, tcp, doh dns.query.socket_factory = socks.socksocket - # this should begin working pending the next dnspython release + # for protocol doq - method depends on the dnspython version # https://github.com/rthalley/dnspython/issues/1059 - # dns.quic._sync.socket_factory = socks.socksocket - # patch socket.socket until then - socket.socket = socks.socksocket # type: ignore + if hasattr(dns.quic._sync, "socket_factory"): + dns.quic._sync.socket_factory = socks.socksocket + else: + socket.socket = socks.socksocket # type: ignore logger.debug(f"Using proxy {self.config.proxy.geturl()}") else: + # restore regular socket.socket for protocols udp, tcp, doh dns.query.socket_factory = socket.socket - # this should begin working pending the next dnspython release + # for protocol doq - method depends on the dnspython version # https://github.com/rthalley/dnspython/issues/1059 - # dns.quic._sync.socket_factory = socket.socket - # unpatch socket.socket until then - socket.socket = _socket # type: ignore + if hasattr(dns.quic._sync, "socket_factory"): + dns.quic._sync.socket_factory = socket.socket + else: + socket.socket = _socket # type: ignore logger.debug("Not using a proxy for this request") def describe(self) -> Iterator[Union[CounterMetricFamily, GaugeMetricFamily]]: diff --git a/src/dns_exporter/config.py b/src/dns_exporter/config.py index 3ba31ac..5b612e2 100644 --- a/src/dns_exporter/config.py +++ b/src/dns_exporter/config.py @@ -266,7 +266,7 @@ def __post_init__(self) -> None: # validate proxy if self.proxy: - # proxy support doesn't work for DoT and DoQ for now + # proxy support doesn't work for DoT for now if self.protocol in ["dot"]: logger.error(f"proxy not valid for protocol {self.protocol}") raise ConfigError(