server: 修 dns_upstream host:port 解析(此前硬编码 53 致云端 DNS 超时)
This commit is contained in:
+11
-1
@@ -57,6 +57,16 @@ class CloudConfig:
|
||||
self.key_file = str(cert.get("key_file", ""))
|
||||
|
||||
|
||||
def _upstream_addr(dns_upstream: str) -> tuple[str, int]:
|
||||
"""解析 dns_upstream 的 host:port(缺省 53)。"""
|
||||
h = dns_upstream.strip()
|
||||
if h.count(":") == 1:
|
||||
name, _, port_s = h.rpartition(":")
|
||||
if port_s.isdigit():
|
||||
return name, int(port_s)
|
||||
return h, 53
|
||||
|
||||
|
||||
def _denied(host: str, suffixes: list[str]) -> bool:
|
||||
low = host.lower()
|
||||
for s in suffixes:
|
||||
@@ -218,7 +228,7 @@ class _ConnThread(threading.Thread):
|
||||
up = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
try:
|
||||
up.settimeout(5)
|
||||
up.sendto(payload, (self.conf.dns_upstream, 53))
|
||||
up.sendto(payload, _upstream_addr(self.conf.dns_upstream))
|
||||
resp, _ = up.recvfrom(4096)
|
||||
finally:
|
||||
up.close()
|
||||
|
||||
@@ -4,7 +4,7 @@ from __future__ import annotations
|
||||
import unittest
|
||||
|
||||
from channel.httpmsg import parse_request, parse_request_head
|
||||
from channel.server import _denied, _error_resp
|
||||
from channel.server import _denied, _error_resp, _upstream_addr
|
||||
|
||||
|
||||
def _get(host: str = "example.com", path: str = "/a?b=1") -> bytes:
|
||||
@@ -82,6 +82,10 @@ class ServerHelperTest(unittest.TestCase):
|
||||
self.assertTrue(resp.startswith(b"HTTP/1.1 502"))
|
||||
self.assertIn(b"Content-Length:", resp)
|
||||
|
||||
def test_upstream_addr(self) -> None:
|
||||
self.assertEqual(_upstream_addr("127.0.0.1:5353"), ("127.0.0.1", 5353))
|
||||
self.assertEqual(_upstream_addr("223.5.5.5"), ("223.5.5.5", 53))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user