feat!: complete refactoring of server #1
No files matched your search
+20
-1
@@ -13,10 +13,12 @@ import fcntl
|
|||||||
import hashlib
|
import hashlib
|
||||||
import hmac
|
import hmac
|
||||||
import http
|
import http
|
||||||
|
import http.client
|
||||||
import http.cookiejar
|
import http.cookiejar
|
||||||
import ipaddress
|
import ipaddress
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import ssl
|
||||||
import struct
|
import struct
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
@@ -50,13 +52,30 @@ def totp(secret, now=None):
|
|||||||
return f"{code:06d}"
|
return f"{code:06d}"
|
||||||
|
|
||||||
|
|
||||||
|
class _IPv4Https(urllib.request.HTTPSHandler):
|
||||||
|
"""HTTPS that only connects via IPv4.
|
||||||
|
|
||||||
|
The INWX API account is restricted to the server's IPv4 address, but a
|
||||||
|
confined CGI process may prefer IPv6. Binding to 0.0.0.0 makes
|
||||||
|
socket.create_connection() skip every IPv6 address.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def https_open(self, req):
|
||||||
|
return self.do_open(
|
||||||
|
http.client.HTTPSConnection,
|
||||||
|
req,
|
||||||
|
context=ssl.create_default_context(),
|
||||||
|
source_address=("0.0.0.0", 0),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def inwx_session(cfg):
|
def inwx_session(cfg):
|
||||||
"""Yield call(method, params) -> resData for a logged-in INWX session."""
|
"""Yield call(method, params) -> resData for a logged-in INWX session."""
|
||||||
inwx = cfg["inwx"]
|
inwx = cfg["inwx"]
|
||||||
url = inwx.get("api", API_LIVE)
|
url = inwx.get("api", API_LIVE)
|
||||||
opener = urllib.request.build_opener(
|
opener = urllib.request.build_opener(
|
||||||
urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar())
|
_IPv4Https, urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar())
|
||||||
)
|
)
|
||||||
|
|
||||||
def call(method, params):
|
def call(method, params):
|
||||||
|
|||||||
@@ -8,12 +8,15 @@ INWX_OTE_HOST (an existing A record there) are set in the environment.
|
|||||||
|
|
||||||
import base64
|
import base64
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import http.client
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import socket
|
||||||
import tempfile
|
import tempfile
|
||||||
import threading
|
import threading
|
||||||
import unittest
|
import unittest
|
||||||
|
from unittest import mock
|
||||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||||
|
|
||||||
import snap_dyndns as sd
|
import snap_dyndns as sd
|
||||||
@@ -125,6 +128,24 @@ class HandleTest(unittest.TestCase):
|
|||||||
self.assertEqual(len(self.calls), 2)
|
self.assertEqual(len(self.calls), 2)
|
||||||
|
|
||||||
|
|
||||||
|
class Ipv4BindTest(unittest.TestCase):
|
||||||
|
"""Checks the premise of _IPv4Https: binding to 0.0.0.0 skips IPv6 addresses."""
|
||||||
|
|
||||||
|
def test_source_address_skips_ipv6(self):
|
||||||
|
try:
|
||||||
|
server = socket.create_server(("::1", 0), family=socket.AF_INET6)
|
||||||
|
except OSError:
|
||||||
|
self.skipTest("no IPv6 loopback")
|
||||||
|
self.addCleanup(server.close)
|
||||||
|
port = server.getsockname()[1]
|
||||||
|
try:
|
||||||
|
socket.create_connection(("localhost", port), timeout=2).close()
|
||||||
|
except OSError:
|
||||||
|
self.skipTest("localhost does not resolve to ::1")
|
||||||
|
with self.assertRaises(OSError):
|
||||||
|
socket.create_connection(("localhost", port), timeout=2, source_address=("0.0.0.0", 0))
|
||||||
|
|
||||||
|
|
||||||
class FakeInwx(ThreadingHTTPServer):
|
class FakeInwx(ThreadingHTTPServer):
|
||||||
"""Minimal INWX JSON-RPC stand-in with cookie session and optional 2FA."""
|
"""Minimal INWX JSON-RPC stand-in with cookie session and optional 2FA."""
|
||||||
|
|
||||||
@@ -228,6 +249,20 @@ class InwxClientTest(unittest.TestCase):
|
|||||||
sd.inwx_update(cfg, "example.com", HOST, "A", "1.2.3.4")
|
sd.inwx_update(cfg, "example.com", HOST, "A", "1.2.3.4")
|
||||||
self.assertNotIn("wrong-password", str(ctx.exception))
|
self.assertNotIn("wrong-password", str(ctx.exception))
|
||||||
|
|
||||||
|
def test_https_connection_is_ipv4_only(self):
|
||||||
|
seen = {}
|
||||||
|
|
||||||
|
class FakeConnection(http.client.HTTPSConnection):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
seen.update(kwargs)
|
||||||
|
raise OSError("stop here")
|
||||||
|
|
||||||
|
cfg = {"inwx": {"user": "u", "pass": "p", "api": "https://inwx.invalid/"}}
|
||||||
|
with mock.patch("http.client.HTTPSConnection", FakeConnection):
|
||||||
|
with self.assertRaises(OSError):
|
||||||
|
sd.inwx_update(cfg, "example.com", HOST, "A", "1.2.3.4")
|
||||||
|
self.assertEqual(seen["source_address"], ("0.0.0.0", 0))
|
||||||
|
|
||||||
def test_end_to_end_only_changes_reach_inwx(self):
|
def test_end_to_end_only_changes_reach_inwx(self):
|
||||||
srv = self.start()
|
srv = self.start()
|
||||||
with tempfile.TemporaryDirectory() as tmp:
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
|||||||
Reference in new issue
Block a user