multiaddr 0.1.0__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {multiaddr-0.1.0 → multiaddr-0.2.0}/HISTORY.rst +39 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/PKG-INFO +2 -2
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/__init__.py +1 -1
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/certhash.py +11 -1
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/domain.py +0 -13
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/multiaddr.py +92 -48
- multiaddr-0.2.0/multiaddr/resolvers/__init__.py +17 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/resolvers/dns.py +6 -3
- multiaddr-0.2.0/multiaddr/resolvers/util.py +173 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr.egg-info/PKG-INFO +2 -2
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr.egg-info/SOURCES.txt +1 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/pyproject.toml +3 -3
- {multiaddr-0.1.0 → multiaddr-0.2.0}/tests/test_multiaddr.py +83 -2
- {multiaddr-0.1.0 → multiaddr-0.2.0}/tests/test_protocols.py +3 -1
- {multiaddr-0.1.0 → multiaddr-0.2.0}/tests/test_resolvers.py +404 -1
- multiaddr-0.1.0/multiaddr/resolvers/__init__.py +0 -6
- {multiaddr-0.1.0 → multiaddr-0.2.0}/AUTHORS +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/CONTRIBUTING.rst +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/LICENSE-APACHE2 +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/LICENSE-MIT +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/MANIFEST.in +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/README.rst +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/__init__.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/cid.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/fspath.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/garlic32.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/garlic64.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/http_path.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/ip4.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/ip6.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/ipcidr.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/memory.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/onion.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/onion3.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/uint16be.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/codecs/utf8.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/exceptions.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/protocols.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/resolvers/base.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/transforms.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr/utils.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr.egg-info/dependency_links.txt +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr.egg-info/not-zip-safe +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr.egg-info/requires.txt +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/multiaddr.egg-info/top_level.txt +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/setup.cfg +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/tests/__init__.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/tests/test_thin_waist_addresses.py +0 -0
- {multiaddr-0.1.0 → multiaddr-0.2.0}/tests/test_transforms.py +0 -0
|
@@ -3,6 +3,45 @@ History
|
|
|
3
3
|
|
|
4
4
|
.. towncrier release notes start
|
|
5
5
|
|
|
6
|
+
py-multiaddr v0.2.0 (2026-03-17)
|
|
7
|
+
--------------------------------
|
|
8
|
+
|
|
9
|
+
Features
|
|
10
|
+
~~~~~~~~
|
|
11
|
+
|
|
12
|
+
- Add resolver utility functions ported from go-multiaddr-dns: ``matches()``, ``resolve_all()``, ``is_fqdn()``, ``fqdn()``, ``addr_len()``, and ``offset_addr()``. These utilities support DNS-based multiaddr resolution workflows including iterative resolution and FQDN handling. (`#101 <https://github.com/multiformats/py-multiaddr/issues/101>`__)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
Internal Changes - for py-multiaddr Contributors
|
|
16
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
17
|
+
|
|
18
|
+
- Updated the tox GitHub Actions workflow so lint runs only once on Linux with
|
|
19
|
+
Python 3.10, while core tests continue to run across all configured Python
|
|
20
|
+
versions. (`#104 <https://github.com/multiformats/py-multiaddr/issues/104>`__)
|
|
21
|
+
- Aligned local tox defaults with CI by running lint only in ``py310-lint``,
|
|
22
|
+
while keeping core test environments for all configured Python versions. (`#106 <https://github.com/multiformats/py-multiaddr/issues/106>`__)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
Removals
|
|
26
|
+
~~~~~~~~
|
|
27
|
+
|
|
28
|
+
- Removed the module-level ``to_bytes`` and ``to_string`` helpers from ``multiaddr.codecs.domain``.
|
|
29
|
+
Domain codec behavior remains available through ``multiaddr.codecs.domain.Codec``. (`#100 <https://github.com/multiformats/py-multiaddr/issues/100>`__)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
py-multiaddr v0.1.1 (2025-12-07)
|
|
33
|
+
--------------------------------
|
|
34
|
+
|
|
35
|
+
Bugfixes
|
|
36
|
+
~~~~~~~~
|
|
37
|
+
|
|
38
|
+
- Fixed validation for tag-only protocols (protocols that do not accept values).
|
|
39
|
+
Tag-only protocols like ``http``, ``https``, ``tls``, ``noise``, ``webrtc``, etc.
|
|
40
|
+
now correctly reject invalid value assignments via both ``/tag/value`` and
|
|
41
|
+
``/tag=value`` syntax, raising clear error messages that do not include the
|
|
42
|
+
invalid value. (`#98 <https://github.com/multiformats/py-multiaddr/issues/98>`__)
|
|
43
|
+
|
|
44
|
+
|
|
6
45
|
py-multiaddr v0.1.0 (2025-10-27)
|
|
7
46
|
--------------------------------
|
|
8
47
|
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: multiaddr
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Python implementation of jbenet's multiaddr
|
|
5
5
|
Author-email: Steven Buss <steven.buss@gmail.com>
|
|
6
6
|
Maintainer: acul71
|
|
7
7
|
Maintainer-email: pacrob <pacrob@protonmail.com>, Manu Sheel Gupta <manu@seeta.in>
|
|
8
8
|
License: MIT License
|
|
9
9
|
Project-URL: Homepage, https://github.com/multiformats/py-multiaddr
|
|
10
|
-
Project-URL: Download, https://github.com/multiformats/py-multiaddr/tarball/0.
|
|
10
|
+
Project-URL: Download, https://github.com/multiformats/py-multiaddr/tarball/0.2.0
|
|
11
11
|
Keywords: multiaddr
|
|
12
12
|
Classifier: Development Status :: 2 - Pre-Alpha
|
|
13
13
|
Classifier: Intended Audience :: Developers
|
|
@@ -52,12 +52,22 @@ class Codec(CodecBase):
|
|
|
52
52
|
Raises:
|
|
53
53
|
ValueError: If the string is not valid multibase or not a multihash.
|
|
54
54
|
"""
|
|
55
|
+
if not string.startswith("u"):
|
|
56
|
+
raise ValueError("certhash must use base64url multibase prefix 'u'")
|
|
57
|
+
|
|
55
58
|
try:
|
|
56
59
|
# Decode the multibase string to get the raw multihash bytes.
|
|
57
|
-
|
|
60
|
+
decoded = multibase.decode(string)
|
|
58
61
|
except Exception as e:
|
|
59
62
|
raise ValueError(f"Failed to decode multibase string: {string}") from e
|
|
60
63
|
|
|
64
|
+
# Some multibase implementations expose decode metadata as a tuple.
|
|
65
|
+
# Normalize to raw bytes for consistent validation and return type.
|
|
66
|
+
decoded_bytes = decoded[1] if isinstance(decoded, tuple) else decoded
|
|
67
|
+
if not isinstance(decoded_bytes, (bytes, bytearray)):
|
|
68
|
+
raise ValueError("Failed to decode multibase string to bytes")
|
|
69
|
+
decoded_bytes = bytes(decoded_bytes)
|
|
70
|
+
|
|
61
71
|
# Validate that the decoded bytes are a valid multihash.
|
|
62
72
|
self.validate(decoded_bytes)
|
|
63
73
|
return decoded_bytes
|
|
@@ -37,16 +37,3 @@ class Codec(CodecBase):
|
|
|
37
37
|
return value
|
|
38
38
|
except (UnicodeDecodeError, idna.IDNAError) as e:
|
|
39
39
|
raise BinaryParseError(f"Invalid domain name encoding: {e!s}", buf, proto.name, e)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
def to_bytes(proto: Any, string: str) -> bytes:
|
|
43
|
-
# Validate using IDNA, but store as UTF-8
|
|
44
|
-
idna.encode(string, uts46=True)
|
|
45
|
-
return string.encode("utf-8")
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
def to_string(proto: Any, buf: bytes) -> str:
|
|
49
|
-
string = buf.decode("utf-8")
|
|
50
|
-
# Validate using IDNA
|
|
51
|
-
idna.encode(string, uts46=True)
|
|
52
|
-
return string
|
|
@@ -344,56 +344,54 @@ class Multiaddr(collections.abc.Mapping[Any, Any]):
|
|
|
344
344
|
return
|
|
345
345
|
|
|
346
346
|
# Handle other protocols
|
|
347
|
-
|
|
348
|
-
|
|
347
|
+
# Convert to list to allow peeking ahead for validation
|
|
348
|
+
parts_list = addr.strip("/").split("/")
|
|
349
|
+
if not parts_list:
|
|
349
350
|
raise exceptions.StringParseError("empty multiaddr", addr)
|
|
350
351
|
|
|
351
352
|
self._bytes = b""
|
|
352
|
-
|
|
353
|
+
idx: int = 0
|
|
354
|
+
while idx < len(parts_list):
|
|
355
|
+
part = parts_list[idx]
|
|
353
356
|
if not part:
|
|
357
|
+
idx += 1
|
|
354
358
|
continue
|
|
355
359
|
|
|
356
360
|
# Special handling for unix paths
|
|
357
361
|
if part in ("unix",):
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
protocol_path_value = next(parts)
|
|
361
|
-
if not protocol_path_value:
|
|
362
|
-
raise exceptions.StringParseError("empty protocol path", addr)
|
|
363
|
-
|
|
364
|
-
# Join any remaining parts as part of the path
|
|
365
|
-
remaining_parts = []
|
|
366
|
-
while True:
|
|
367
|
-
try:
|
|
368
|
-
next_part = next(parts)
|
|
369
|
-
if not next_part:
|
|
370
|
-
continue
|
|
371
|
-
remaining_parts.append(next_part)
|
|
372
|
-
except StopIteration:
|
|
373
|
-
break
|
|
374
|
-
|
|
375
|
-
if remaining_parts:
|
|
376
|
-
protocol_path_value = protocol_path_value + "/" + "/".join(remaining_parts)
|
|
377
|
-
|
|
378
|
-
proto = protocol_with_name(part)
|
|
379
|
-
codec = codec_by_name(proto.codec)
|
|
380
|
-
if not codec:
|
|
381
|
-
raise exceptions.StringParseError(f"unknown codec: {proto.codec}", addr)
|
|
382
|
-
|
|
383
|
-
try:
|
|
384
|
-
self._bytes += varint.encode(proto.code)
|
|
385
|
-
buf = codec.to_bytes(proto, protocol_path_value)
|
|
386
|
-
# Add length prefix for variable-sized or zero-sized codecs
|
|
387
|
-
if codec.SIZE <= 0:
|
|
388
|
-
self._bytes += varint.encode(len(buf))
|
|
389
|
-
if buf: # Only append buffer if it's not empty
|
|
390
|
-
self._bytes += buf
|
|
391
|
-
except Exception as e:
|
|
392
|
-
raise exceptions.StringParseError(str(e), addr) from e
|
|
393
|
-
continue
|
|
394
|
-
except StopIteration:
|
|
362
|
+
# Get the next part as the path value
|
|
363
|
+
if idx + 1 >= len(parts_list):
|
|
395
364
|
raise exceptions.StringParseError("missing value for unix protocol", addr)
|
|
396
365
|
|
|
366
|
+
protocol_path_value = parts_list[idx + 1]
|
|
367
|
+
if not protocol_path_value:
|
|
368
|
+
raise exceptions.StringParseError("empty protocol path", addr)
|
|
369
|
+
|
|
370
|
+
# Join any remaining parts as part of the path (collect and consume the rest)
|
|
371
|
+
remaining_parts = [p for p in parts_list[idx + 2 :] if p]
|
|
372
|
+
# Consume all remaining parts so outer loop ends
|
|
373
|
+
idx = len(parts_list)
|
|
374
|
+
|
|
375
|
+
if remaining_parts:
|
|
376
|
+
protocol_path_value = protocol_path_value + "/" + "/".join(remaining_parts)
|
|
377
|
+
|
|
378
|
+
proto = protocol_with_name(part)
|
|
379
|
+
codec = codec_by_name(proto.codec)
|
|
380
|
+
if not codec:
|
|
381
|
+
raise exceptions.StringParseError(f"unknown codec: {proto.codec}", addr)
|
|
382
|
+
|
|
383
|
+
try:
|
|
384
|
+
self._bytes += varint.encode(proto.code)
|
|
385
|
+
buf = codec.to_bytes(proto, protocol_path_value)
|
|
386
|
+
# Add length prefix for variable-sized or zero-sized codecs
|
|
387
|
+
if codec.SIZE <= 0:
|
|
388
|
+
self._bytes += varint.encode(len(buf))
|
|
389
|
+
if buf: # Only append buffer if it's not empty
|
|
390
|
+
self._bytes += buf
|
|
391
|
+
except Exception as e:
|
|
392
|
+
raise exceptions.StringParseError(str(e), addr) from e
|
|
393
|
+
continue # Already advanced idx above
|
|
394
|
+
|
|
397
395
|
# Handle other protocols
|
|
398
396
|
# Split protocol name and value if present
|
|
399
397
|
protocol_value: str | None = None
|
|
@@ -407,15 +405,30 @@ class Multiaddr(collections.abc.Mapping[Any, Any]):
|
|
|
407
405
|
except Exception as exc:
|
|
408
406
|
raise exceptions.StringParseError(f"unknown protocol: {proto_name}", addr) from exc
|
|
409
407
|
|
|
408
|
+
# Fix 2: Validate that tag-only protocols don't accept values via = syntax
|
|
409
|
+
if proto.codec is None and protocol_value is not None:
|
|
410
|
+
# Construct address string without the invalid value
|
|
411
|
+
# to avoid including it in error message
|
|
412
|
+
addr_parts_before = parts_list[:idx]
|
|
413
|
+
if addr_parts_before or proto_name:
|
|
414
|
+
addr_up_to_protocol = "/" + "/".join([*addr_parts_before, proto_name])
|
|
415
|
+
else:
|
|
416
|
+
addr_up_to_protocol = "/"
|
|
417
|
+
raise exceptions.StringParseError(
|
|
418
|
+
f"Protocol '{proto.name}' does not take an argument",
|
|
419
|
+
addr_up_to_protocol,
|
|
420
|
+
proto.name,
|
|
421
|
+
)
|
|
422
|
+
|
|
410
423
|
# If the protocol expects a value, get it
|
|
411
424
|
if proto.codec is not None:
|
|
412
425
|
if protocol_value is None:
|
|
413
|
-
|
|
414
|
-
protocol_value = next(parts)
|
|
415
|
-
except StopIteration:
|
|
426
|
+
if idx + 1 >= len(parts_list):
|
|
416
427
|
raise exceptions.StringParseError(
|
|
417
428
|
f"missing value for protocol: {proto_name}", addr
|
|
418
429
|
)
|
|
430
|
+
protocol_value = parts_list[idx + 1]
|
|
431
|
+
idx += 1 # Consume the value part
|
|
419
432
|
# Validate value (optional: could add more checks here)
|
|
420
433
|
# If value looks like a protocol name, that's an error
|
|
421
434
|
if protocol_value is not None:
|
|
@@ -434,13 +447,42 @@ class Multiaddr(collections.abc.Mapping[Any, Any]):
|
|
|
434
447
|
if not codec:
|
|
435
448
|
raise exceptions.StringParseError(f"unknown codec: {proto.codec}", addr)
|
|
436
449
|
|
|
437
|
-
|
|
450
|
+
# Special case: protocols with codec=None are flag protocols
|
|
451
|
+
# (no value, no length prefix, no buffer)
|
|
452
|
+
if proto.codec is None:
|
|
453
|
+
# Encode the protocol code first
|
|
438
454
|
self._bytes += varint.encode(proto.code)
|
|
439
455
|
|
|
440
|
-
#
|
|
441
|
-
#
|
|
442
|
-
|
|
443
|
-
|
|
456
|
+
# Fix 1: Check if next part exists and is not a valid protocol name
|
|
457
|
+
# If it's not a valid protocol, it's an invalid value
|
|
458
|
+
# Look ahead to find the next non-empty part
|
|
459
|
+
next_idx = idx + 1
|
|
460
|
+
while next_idx < len(parts_list) and not parts_list[next_idx]:
|
|
461
|
+
next_idx += 1
|
|
462
|
+
|
|
463
|
+
if next_idx < len(parts_list):
|
|
464
|
+
next_part = parts_list[next_idx]
|
|
465
|
+
try:
|
|
466
|
+
protocol_with_name(next_part)
|
|
467
|
+
# It's a valid protocol name, so advance idx to that part
|
|
468
|
+
idx = next_idx
|
|
469
|
+
continue
|
|
470
|
+
except exceptions.ProtocolNotFoundError:
|
|
471
|
+
# Not a valid protocol name, so it's an invalid value
|
|
472
|
+
# Construct address string up to (but not including) the invalid value
|
|
473
|
+
# to avoid including it in the error message
|
|
474
|
+
addr_up_to_protocol = "/" + "/".join(parts_list[: idx + 1])
|
|
475
|
+
raise exceptions.StringParseError(
|
|
476
|
+
f"Protocol '{proto.name}' does not take an argument",
|
|
477
|
+
addr_up_to_protocol,
|
|
478
|
+
proto.name,
|
|
479
|
+
)
|
|
480
|
+
# No next part, continue normally
|
|
481
|
+
idx += 1
|
|
482
|
+
continue
|
|
483
|
+
|
|
484
|
+
try:
|
|
485
|
+
self._bytes += varint.encode(proto.code)
|
|
444
486
|
|
|
445
487
|
buf = codec.to_bytes(proto, protocol_value or "")
|
|
446
488
|
if codec.SIZE <= 0: # Add length prefix for variable-sized or zero-sized codecs
|
|
@@ -450,6 +492,8 @@ class Multiaddr(collections.abc.Mapping[Any, Any]):
|
|
|
450
492
|
except Exception as e:
|
|
451
493
|
raise exceptions.StringParseError(str(e), addr) from e
|
|
452
494
|
|
|
495
|
+
idx += 1 # Move to next part
|
|
496
|
+
|
|
453
497
|
def _from_bytes(self, addr: bytes) -> None:
|
|
454
498
|
"""Parse a binary multiaddr.
|
|
455
499
|
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""DNS resolution support for multiaddr."""
|
|
2
|
+
|
|
3
|
+
from .base import Resolver
|
|
4
|
+
from .dns import DNSADDR_TXT_PREFIX, DNSResolver
|
|
5
|
+
from .util import addr_len, fqdn, is_fqdn, matches, offset_addr, resolve_all
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"DNSADDR_TXT_PREFIX",
|
|
9
|
+
"DNSResolver",
|
|
10
|
+
"Resolver",
|
|
11
|
+
"addr_len",
|
|
12
|
+
"fqdn",
|
|
13
|
+
"is_fqdn",
|
|
14
|
+
"matches",
|
|
15
|
+
"offset_addr",
|
|
16
|
+
"resolve_all",
|
|
17
|
+
]
|
|
@@ -36,6 +36,9 @@ from ..multiaddr import Multiaddr
|
|
|
36
36
|
from ..protocols import P_DNS, P_DNS4, P_DNS6, P_DNSADDR, Protocol
|
|
37
37
|
from .base import Resolver
|
|
38
38
|
|
|
39
|
+
DNSADDR_TXT_PREFIX = "dnsaddr="
|
|
40
|
+
"""Prefix for dnsaddr TXT records (e.g. ``dnsaddr=/ip4/...``)."""
|
|
41
|
+
|
|
39
42
|
|
|
40
43
|
class DNSResolver(Resolver):
|
|
41
44
|
"""
|
|
@@ -226,8 +229,8 @@ class DNSResolver(Resolver):
|
|
|
226
229
|
else:
|
|
227
230
|
txt_data = str(txt_data_raw)
|
|
228
231
|
logging.debug(f"{indent} TXT: {txt_data}")
|
|
229
|
-
if txt_data.startswith(
|
|
230
|
-
multiaddr_str = txt_data[
|
|
232
|
+
if txt_data.startswith(DNSADDR_TXT_PREFIX):
|
|
233
|
+
multiaddr_str = txt_data[len(DNSADDR_TXT_PREFIX) :]
|
|
231
234
|
multiaddr_str = self._clean_quotes(multiaddr_str).strip()
|
|
232
235
|
logging.debug(f"{indent} Parsed multiaddr: {multiaddr_str}")
|
|
233
236
|
if not multiaddr_str:
|
|
@@ -405,4 +408,4 @@ class DNSResolver(Resolver):
|
|
|
405
408
|
return results
|
|
406
409
|
|
|
407
410
|
|
|
408
|
-
__all__ = ["DNSResolver"]
|
|
411
|
+
__all__ = ["DNSADDR_TXT_PREFIX", "DNSResolver"]
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"""Utility functions for multiaddr resolution."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
from ..exceptions import RecursionLimitError
|
|
8
|
+
from ..multiaddr import Multiaddr
|
|
9
|
+
from ..protocols import P_DNS, P_DNS4, P_DNS6, P_DNSADDR
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from .base import Resolver
|
|
13
|
+
|
|
14
|
+
__all__ = ["addr_len", "fqdn", "is_fqdn", "matches", "offset_addr", "resolve_all"]
|
|
15
|
+
|
|
16
|
+
_DNS_PROTOCOLS = frozenset({P_DNS, P_DNS4, P_DNS6, P_DNSADDR})
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def is_fqdn(s: str) -> bool:
|
|
20
|
+
"""Check if string is a fully qualified domain name (ends with unescaped dot).
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
s: The domain name string to check.
|
|
24
|
+
|
|
25
|
+
Returns:
|
|
26
|
+
``True`` if *s* ends with an unescaped ``'.'``, ``False`` otherwise.
|
|
27
|
+
"""
|
|
28
|
+
if not s:
|
|
29
|
+
return False
|
|
30
|
+
# Count trailing backslashes before the final character
|
|
31
|
+
if s[-1] != ".":
|
|
32
|
+
return False
|
|
33
|
+
# Check if the trailing dot is escaped
|
|
34
|
+
num_backslashes = 0
|
|
35
|
+
for i in range(len(s) - 2, -1, -1):
|
|
36
|
+
if s[i] == "\\":
|
|
37
|
+
num_backslashes += 1
|
|
38
|
+
else:
|
|
39
|
+
break
|
|
40
|
+
# Odd number of backslashes means the dot is escaped
|
|
41
|
+
return num_backslashes % 2 == 0
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def fqdn(s: str) -> str:
|
|
45
|
+
"""Append a trailing dot to *s* if it is not already a FQDN.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
s: The domain name string.
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
The domain name with a trailing ``'.'`` appended if needed.
|
|
52
|
+
|
|
53
|
+
Example::
|
|
54
|
+
|
|
55
|
+
>>> fqdn("example.com")
|
|
56
|
+
'example.com.'
|
|
57
|
+
>>> fqdn("example.com.")
|
|
58
|
+
'example.com.'
|
|
59
|
+
"""
|
|
60
|
+
if is_fqdn(s):
|
|
61
|
+
return s
|
|
62
|
+
return s + "."
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def addr_len(maddr: Multiaddr) -> int:
|
|
66
|
+
"""Count the number of protocol components in a multiaddr.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
maddr: The multiaddr to measure.
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
The number of protocol components.
|
|
73
|
+
"""
|
|
74
|
+
return len(list(maddr.protocols()))
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def offset_addr(maddr: Multiaddr, n: int) -> Multiaddr:
|
|
78
|
+
"""Return a new multiaddr with the first *n* protocol components removed.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
maddr: The source multiaddr.
|
|
82
|
+
n: Number of leading components to skip. Must be >= 0.
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
A new :class:`~multiaddr.Multiaddr` without the first *n* components,
|
|
86
|
+
or ``Multiaddr("/")`` if *n* >= the total number of components.
|
|
87
|
+
|
|
88
|
+
Raises:
|
|
89
|
+
ValueError: If *n* is negative.
|
|
90
|
+
"""
|
|
91
|
+
if n < 0:
|
|
92
|
+
raise ValueError(f"offset must be non-negative, got {n}")
|
|
93
|
+
parts = maddr.split(n)
|
|
94
|
+
if len(parts) <= n:
|
|
95
|
+
return Multiaddr("/")
|
|
96
|
+
return parts[n]
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def matches(maddr: Multiaddr) -> bool:
|
|
100
|
+
"""Check if a multiaddr contains any DNS protocol component.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
maddr: The multiaddr to inspect.
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
``True`` if *maddr* contains a ``dns``, ``dns4``, ``dns6``, or
|
|
107
|
+
``dnsaddr`` component.
|
|
108
|
+
|
|
109
|
+
Example::
|
|
110
|
+
|
|
111
|
+
>>> matches(Multiaddr("/dns4/example.com/tcp/80"))
|
|
112
|
+
True
|
|
113
|
+
>>> matches(Multiaddr("/ip4/127.0.0.1/tcp/80"))
|
|
114
|
+
False
|
|
115
|
+
"""
|
|
116
|
+
return any(p.code in _DNS_PROTOCOLS for p in maddr.protocols())
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
async def resolve_all(
|
|
120
|
+
resolver: Resolver,
|
|
121
|
+
maddr: Multiaddr,
|
|
122
|
+
*,
|
|
123
|
+
max_iterations: int = 32,
|
|
124
|
+
) -> list[Multiaddr]:
|
|
125
|
+
"""Resolve all DNS components in a multiaddr iteratively.
|
|
126
|
+
|
|
127
|
+
Calls ``resolver.resolve()`` in a loop until every returned address is
|
|
128
|
+
free of DNS components.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
resolver: A resolver instance implementing an async ``resolve()`` method.
|
|
132
|
+
maddr: The multiaddr to resolve.
|
|
133
|
+
max_iterations: Safety limit on resolution rounds to prevent infinite
|
|
134
|
+
loops (default ``32``).
|
|
135
|
+
|
|
136
|
+
Returns:
|
|
137
|
+
A list of fully-resolved :class:`~multiaddr.Multiaddr` instances.
|
|
138
|
+
|
|
139
|
+
Raises:
|
|
140
|
+
RecursionLimitError: If DNS components remain after *max_iterations*
|
|
141
|
+
rounds.
|
|
142
|
+
|
|
143
|
+
Example::
|
|
144
|
+
|
|
145
|
+
>>> import trio
|
|
146
|
+
>>> result = trio.run(resolve_all, my_resolver, Multiaddr("/dns4/example.com/tcp/80"))
|
|
147
|
+
"""
|
|
148
|
+
queue = [maddr]
|
|
149
|
+
resolved: list[Multiaddr] = []
|
|
150
|
+
|
|
151
|
+
for _ in range(max_iterations):
|
|
152
|
+
if not queue:
|
|
153
|
+
break
|
|
154
|
+
next_queue: list[Multiaddr] = []
|
|
155
|
+
for addr in queue:
|
|
156
|
+
if not matches(addr):
|
|
157
|
+
resolved.append(addr)
|
|
158
|
+
continue
|
|
159
|
+
results = await resolver.resolve(addr)
|
|
160
|
+
for r in results:
|
|
161
|
+
if matches(r):
|
|
162
|
+
next_queue.append(r)
|
|
163
|
+
else:
|
|
164
|
+
resolved.append(r)
|
|
165
|
+
queue = next_queue
|
|
166
|
+
|
|
167
|
+
if queue:
|
|
168
|
+
raise RecursionLimitError(
|
|
169
|
+
f"resolve_all exceeded {max_iterations} iterations; "
|
|
170
|
+
f"{len(queue)} addresses still contain DNS components"
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
return resolved
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: multiaddr
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Python implementation of jbenet's multiaddr
|
|
5
5
|
Author-email: Steven Buss <steven.buss@gmail.com>
|
|
6
6
|
Maintainer: acul71
|
|
7
7
|
Maintainer-email: pacrob <pacrob@protonmail.com>, Manu Sheel Gupta <manu@seeta.in>
|
|
8
8
|
License: MIT License
|
|
9
9
|
Project-URL: Homepage, https://github.com/multiformats/py-multiaddr
|
|
10
|
-
Project-URL: Download, https://github.com/multiformats/py-multiaddr/tarball/0.
|
|
10
|
+
Project-URL: Download, https://github.com/multiformats/py-multiaddr/tarball/0.2.0
|
|
11
11
|
Keywords: multiaddr
|
|
12
12
|
Classifier: Development Status :: 2 - Pre-Alpha
|
|
13
13
|
Classifier: Intended Audience :: Developers
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "multiaddr"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.2.0"
|
|
8
8
|
description = "Python implementation of jbenet's multiaddr"
|
|
9
9
|
readme = "README.rst"
|
|
10
10
|
authors = [{ name = "Steven Buss", email = "steven.buss@gmail.com" }]
|
|
@@ -44,7 +44,7 @@ dependencies = [
|
|
|
44
44
|
|
|
45
45
|
[project.urls]
|
|
46
46
|
Homepage = "https://github.com/multiformats/py-multiaddr"
|
|
47
|
-
Download = "https://github.com/multiformats/py-multiaddr/tarball/0.
|
|
47
|
+
Download = "https://github.com/multiformats/py-multiaddr/tarball/0.2.0"
|
|
48
48
|
|
|
49
49
|
[project.optional-dependencies]
|
|
50
50
|
dev = [
|
|
@@ -137,7 +137,7 @@ name = "Removals"
|
|
|
137
137
|
showcontent = true
|
|
138
138
|
|
|
139
139
|
[tool.bumpversion]
|
|
140
|
-
current_version = "0.
|
|
140
|
+
current_version = "0.2.0"
|
|
141
141
|
parse = """
|
|
142
142
|
(?P<major>\\d+)
|
|
143
143
|
\\.(?P<minor>\\d+)
|
|
@@ -67,8 +67,7 @@ from multiaddr.protocols import (
|
|
|
67
67
|
"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzu:80",
|
|
68
68
|
"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:-1",
|
|
69
69
|
"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzu@",
|
|
70
|
-
"/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/
|
|
71
|
-
"/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/b2uaraocy6yrdblb4sfptaddgimjmmpy/certhash/zQmbWTwYGcmdyK9CYfNBcfs9nhZs17a6FQ4Y8oea278xx41",
|
|
70
|
+
"/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/b2uaraocy6yrdblb4sfptaddgimjmmp",
|
|
72
71
|
"/udp/1234/sctp",
|
|
73
72
|
"/udp/1234/udt/1234",
|
|
74
73
|
"/udp/1234/utp/1234",
|
|
@@ -142,6 +141,8 @@ def test_invalid(addr_str):
|
|
|
142
141
|
"/ip4/127.0.0.1/tcp/127/webrtc",
|
|
143
142
|
"/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g"
|
|
144
143
|
"/ip4/127.0.0.1/udp/9090/webrtc-direct/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g",
|
|
144
|
+
"/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/u1QEQOFj2IjCsPJFfMAxmQxLGPw",
|
|
145
|
+
"/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/u1QEQOFj2IjCsPJFfMAxmQxLGPw/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g",
|
|
145
146
|
],
|
|
146
147
|
) # nopep8
|
|
147
148
|
def test_valid(addr_str):
|
|
@@ -1022,3 +1023,83 @@ def test_http_path_raw_value_access():
|
|
|
1022
1023
|
from urllib.parse import quote
|
|
1023
1024
|
|
|
1024
1025
|
assert quote(raw_value, safe="") == encoded_value
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
def test_tag_only_protocol_rejects_value_slash_syntax():
|
|
1029
|
+
"""Test that tag-only protocols reject values using /tag/value syntax"""
|
|
1030
|
+
tag_only_protocols = [
|
|
1031
|
+
"webrtc",
|
|
1032
|
+
"webrtc-direct",
|
|
1033
|
+
"noise",
|
|
1034
|
+
"quic",
|
|
1035
|
+
"quic-v1",
|
|
1036
|
+
"tls",
|
|
1037
|
+
"http",
|
|
1038
|
+
"https",
|
|
1039
|
+
"ws",
|
|
1040
|
+
"wss",
|
|
1041
|
+
"p2p-circuit",
|
|
1042
|
+
"webtransport",
|
|
1043
|
+
]
|
|
1044
|
+
|
|
1045
|
+
for proto_name in tag_only_protocols:
|
|
1046
|
+
# Should fail with clear error message
|
|
1047
|
+
with pytest.raises(StringParseError) as exc_info:
|
|
1048
|
+
Multiaddr(f"/{proto_name}/value")
|
|
1049
|
+
assert "does not take an argument" in str(exc_info.value)
|
|
1050
|
+
assert proto_name in str(exc_info.value)
|
|
1051
|
+
|
|
1052
|
+
|
|
1053
|
+
def test_tag_only_protocol_rejects_value_equals_syntax():
|
|
1054
|
+
"""Test that tag-only protocols reject values using /tag=value syntax"""
|
|
1055
|
+
tag_only_protocols = [
|
|
1056
|
+
"webrtc",
|
|
1057
|
+
"webrtc-direct",
|
|
1058
|
+
"noise",
|
|
1059
|
+
"quic",
|
|
1060
|
+
"tls",
|
|
1061
|
+
"http",
|
|
1062
|
+
]
|
|
1063
|
+
|
|
1064
|
+
for proto_name in tag_only_protocols:
|
|
1065
|
+
# Should fail with clear error message
|
|
1066
|
+
with pytest.raises(StringParseError) as exc_info:
|
|
1067
|
+
Multiaddr(f"/{proto_name}=value")
|
|
1068
|
+
assert "does not take an argument" in str(exc_info.value)
|
|
1069
|
+
assert proto_name in str(exc_info.value)
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
def test_tag_only_protocol_allows_valid_combinations():
|
|
1073
|
+
"""Test that tag-only protocols work correctly in valid combinations"""
|
|
1074
|
+
# Single tag protocol
|
|
1075
|
+
assert str(Multiaddr("/webrtc")) == "/webrtc"
|
|
1076
|
+
assert str(Multiaddr("/webrtc-direct")) == "/webrtc-direct"
|
|
1077
|
+
|
|
1078
|
+
# Multiple tag protocols chained
|
|
1079
|
+
assert str(Multiaddr("/webrtc/noise")) == "/webrtc/noise"
|
|
1080
|
+
assert str(Multiaddr("/webrtc-direct/webrtc")) == "/webrtc-direct/webrtc"
|
|
1081
|
+
|
|
1082
|
+
# Tag protocol followed by value protocol
|
|
1083
|
+
assert str(Multiaddr("/webrtc-direct/ip4/127.0.0.1")) == "/webrtc-direct/ip4/127.0.0.1"
|
|
1084
|
+
|
|
1085
|
+
# Complex valid address
|
|
1086
|
+
addr = "/ip4/127.0.0.1/udp/9090/webrtc-direct/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g"
|
|
1087
|
+
assert str(Multiaddr(addr)) == addr
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
def test_tag_only_protocol_error_message_format():
|
|
1091
|
+
"""Test that error messages for tag-only protocols are clear and helpful"""
|
|
1092
|
+
# Test /tag/value syntax
|
|
1093
|
+
with pytest.raises(StringParseError) as exc_info:
|
|
1094
|
+
Multiaddr("/webrtc-direct/invalidvalue")
|
|
1095
|
+
error_msg = str(exc_info.value)
|
|
1096
|
+
assert "does not take an argument" in error_msg
|
|
1097
|
+
assert "webrtc-direct" in error_msg
|
|
1098
|
+
assert "invalidvalue" not in error_msg # Should not mention the invalid value
|
|
1099
|
+
|
|
1100
|
+
# Test /tag=value syntax
|
|
1101
|
+
with pytest.raises(StringParseError) as exc_info:
|
|
1102
|
+
Multiaddr("/webrtc=somevalue")
|
|
1103
|
+
error_msg = str(exc_info.value)
|
|
1104
|
+
assert "does not take an argument" in error_msg
|
|
1105
|
+
assert "webrtc" in error_msg
|
|
@@ -565,7 +565,9 @@ def test_ipcidr_invalid_bytes_inputs():
|
|
|
565
565
|
|
|
566
566
|
# --------CERT-HASH---------
|
|
567
567
|
|
|
568
|
-
|
|
568
|
+
# The multihash package provides `encode` at runtime, but some static
|
|
569
|
+
# checkers (ruff/pyright) may not see it. Ignore the attribute check here.
|
|
570
|
+
VALID_MULTIHASH_BYTES = multihash.encode(b"hello world", "sha2-256") # type: ignore[attr-defined]
|
|
569
571
|
VALID_CERTHASH_STRING = multibase.encode("base64url", VALID_MULTIHASH_BYTES).decode("utf-8")
|
|
570
572
|
|
|
571
573
|
INVALID_BYTES = b"this is not a multihash"
|
|
@@ -9,7 +9,15 @@ import trio
|
|
|
9
9
|
|
|
10
10
|
from multiaddr import Multiaddr
|
|
11
11
|
from multiaddr.exceptions import RecursionLimitError
|
|
12
|
-
from multiaddr.resolvers import
|
|
12
|
+
from multiaddr.resolvers import (
|
|
13
|
+
DNSResolver,
|
|
14
|
+
addr_len,
|
|
15
|
+
fqdn,
|
|
16
|
+
is_fqdn,
|
|
17
|
+
matches,
|
|
18
|
+
offset_addr,
|
|
19
|
+
resolve_all,
|
|
20
|
+
)
|
|
13
21
|
|
|
14
22
|
if sys.version_info >= (3, 11):
|
|
15
23
|
from builtins import BaseExceptionGroup
|
|
@@ -408,3 +416,398 @@ async def test_resolve_dnsaddr_quic_ipv6_zones(dns_resolver):
|
|
|
408
416
|
in the Python implementation.
|
|
409
417
|
"""
|
|
410
418
|
pytest.skip("IPv6 zones have binary encoding issues in Python implementation")
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
# ---------------------------------------------------------------------------
|
|
422
|
+
# Go test equivalents: DNSResolver.resolve()
|
|
423
|
+
# (from go-multiaddr-dns/resolve_test.go)
|
|
424
|
+
# ---------------------------------------------------------------------------
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
@pytest.fixture
|
|
428
|
+
def _go_style_dns_mocks():
|
|
429
|
+
"""Create mock DNS answers matching the Go test setup.
|
|
430
|
+
|
|
431
|
+
Go test constants:
|
|
432
|
+
ip4a = 192.0.2.1, ip4b = 192.0.2.2
|
|
433
|
+
ip6a = 2001:db8::a3, ip6b = 2001:db8::a4
|
|
434
|
+
example.com → A: [ip4a, ip4b], AAAA: [ip6a, ip6b]
|
|
435
|
+
_dnsaddr.example.com → TXT: [dnsaddr=/ip4/192.0.2.1, dnsaddr=/ip6/2001:db8::a3]
|
|
436
|
+
"""
|
|
437
|
+
mock_answer_a = AsyncMock()
|
|
438
|
+
rdata_a1 = AsyncMock()
|
|
439
|
+
rdata_a1.address = "192.0.2.1"
|
|
440
|
+
rdata_a2 = AsyncMock()
|
|
441
|
+
rdata_a2.address = "192.0.2.2"
|
|
442
|
+
mock_answer_a.__iter__.return_value = [rdata_a1, rdata_a2]
|
|
443
|
+
|
|
444
|
+
mock_answer_aaaa = AsyncMock()
|
|
445
|
+
rdata_aaaa1 = AsyncMock()
|
|
446
|
+
rdata_aaaa1.address = "2001:db8::a3"
|
|
447
|
+
rdata_aaaa2 = AsyncMock()
|
|
448
|
+
rdata_aaaa2.address = "2001:db8::a4"
|
|
449
|
+
mock_answer_aaaa.__iter__.return_value = [rdata_aaaa1, rdata_aaaa2]
|
|
450
|
+
|
|
451
|
+
mock_answer_txt = AsyncMock()
|
|
452
|
+
rdata_txt1 = AsyncMock()
|
|
453
|
+
rdata_txt1.strings = ["dnsaddr=/ip4/192.0.2.1"]
|
|
454
|
+
rdata_txt2 = AsyncMock()
|
|
455
|
+
rdata_txt2.strings = ["dnsaddr=/ip6/2001:db8::a3"]
|
|
456
|
+
mock_answer_txt.__iter__.return_value = [rdata_txt1, rdata_txt2]
|
|
457
|
+
|
|
458
|
+
async def side_effect(hostname, record_type):
|
|
459
|
+
if record_type == "A" and hostname in ("example.com", "example.com."):
|
|
460
|
+
return mock_answer_a
|
|
461
|
+
if record_type == "AAAA" and hostname in ("example.com", "example.com."):
|
|
462
|
+
return mock_answer_aaaa
|
|
463
|
+
if record_type == "TXT" and hostname in ("_dnsaddr.example.com", "_dnsaddr.example.com."):
|
|
464
|
+
return mock_answer_txt
|
|
465
|
+
raise dns.resolver.NXDOMAIN()
|
|
466
|
+
|
|
467
|
+
return side_effect
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
@pytest.mark.trio
|
|
471
|
+
async def test_simple_ip_resolve_dns4(dns_resolver, _go_style_dns_mocks):
|
|
472
|
+
"""Go: TestSimpleIPResolve — dns4 resolves to IPv4 addresses only."""
|
|
473
|
+
with patch.object(dns_resolver._resolver, "resolve") as mock:
|
|
474
|
+
mock.side_effect = _go_style_dns_mocks
|
|
475
|
+
result = await dns_resolver.resolve(Multiaddr("/dns4/example.com"))
|
|
476
|
+
assert len(result) == 2
|
|
477
|
+
assert result[0] == Multiaddr("/ip4/192.0.2.1")
|
|
478
|
+
assert result[1] == Multiaddr("/ip4/192.0.2.2")
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
@pytest.mark.trio
|
|
482
|
+
async def test_simple_ip_resolve_dns6(dns_resolver, _go_style_dns_mocks):
|
|
483
|
+
"""Go: TestSimpleIPResolve — dns6 resolves to IPv6 addresses only."""
|
|
484
|
+
with patch.object(dns_resolver._resolver, "resolve") as mock:
|
|
485
|
+
mock.side_effect = _go_style_dns_mocks
|
|
486
|
+
result = await dns_resolver.resolve(Multiaddr("/dns6/example.com"))
|
|
487
|
+
assert len(result) == 2
|
|
488
|
+
assert result[0] == Multiaddr("/ip6/2001:db8::a3")
|
|
489
|
+
assert result[1] == Multiaddr("/ip6/2001:db8::a4")
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
@pytest.mark.trio
|
|
493
|
+
async def test_simple_ip_resolve_dns_both(dns_resolver, _go_style_dns_mocks):
|
|
494
|
+
"""Go: TestSimpleIPResolve — dns resolves to both IPv4 and IPv6."""
|
|
495
|
+
with patch.object(dns_resolver._resolver, "resolve") as mock:
|
|
496
|
+
mock.side_effect = _go_style_dns_mocks
|
|
497
|
+
result = await dns_resolver.resolve(Multiaddr("/dns/example.com"))
|
|
498
|
+
assert len(result) == 4
|
|
499
|
+
assert result[0] == Multiaddr("/ip4/192.0.2.1")
|
|
500
|
+
assert result[1] == Multiaddr("/ip4/192.0.2.2")
|
|
501
|
+
assert result[2] == Multiaddr("/ip6/2001:db8::a3")
|
|
502
|
+
assert result[3] == Multiaddr("/ip6/2001:db8::a4")
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
@pytest.mark.trio
|
|
506
|
+
async def test_resolve_only_once(dns_resolver, _go_style_dns_mocks):
|
|
507
|
+
"""Go: TestResolveOnlyOnce — only the first DNS component is resolved."""
|
|
508
|
+
with patch.object(dns_resolver._resolver, "resolve") as mock:
|
|
509
|
+
mock.side_effect = _go_style_dns_mocks
|
|
510
|
+
result = await dns_resolver.resolve(Multiaddr("/dns4/example.com/quic/dns6/example.com"))
|
|
511
|
+
# dns4 resolved to 2 IPs, dns6 stays unresolved in each
|
|
512
|
+
assert len(result) == 2
|
|
513
|
+
assert result[0] == Multiaddr("/ip4/192.0.2.1/quic/dns6/example.com")
|
|
514
|
+
assert result[1] == Multiaddr("/ip4/192.0.2.2/quic/dns6/example.com")
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
@pytest.mark.trio
|
|
518
|
+
async def test_simple_txt_resolve(dns_resolver, _go_style_dns_mocks):
|
|
519
|
+
"""Go: TestSimpleTXTResolve — dnsaddr resolves via TXT records."""
|
|
520
|
+
with patch.object(dns_resolver._resolver, "resolve") as mock:
|
|
521
|
+
mock.side_effect = _go_style_dns_mocks
|
|
522
|
+
result = await dns_resolver.resolve(Multiaddr("/dnsaddr/example.com"))
|
|
523
|
+
assert len(result) == 2
|
|
524
|
+
assert result[0] == Multiaddr("/ip4/192.0.2.1")
|
|
525
|
+
assert result[1] == Multiaddr("/ip6/2001:db8::a3")
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
@pytest.mark.trio
|
|
529
|
+
async def test_resolve_empty_result(dns_resolver):
|
|
530
|
+
"""Go: TestEmptyResult — dnsaddr for unknown domain returns empty list."""
|
|
531
|
+
with patch.object(dns_resolver._resolver, "resolve", side_effect=dns.resolver.NXDOMAIN):
|
|
532
|
+
result = await dns_resolver.resolve(Multiaddr("/dnsaddr/none.com"))
|
|
533
|
+
assert result == []
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
@pytest.mark.trio
|
|
537
|
+
async def test_resolve_dns4_with_suffix(dns_resolver, _go_style_dns_mocks):
|
|
538
|
+
"""Go: TestSimpleIPResolve variant — dns4 with /tcp suffix preserved."""
|
|
539
|
+
with patch.object(dns_resolver._resolver, "resolve") as mock:
|
|
540
|
+
mock.side_effect = _go_style_dns_mocks
|
|
541
|
+
result = await dns_resolver.resolve(Multiaddr("/dns4/example.com/tcp/80"))
|
|
542
|
+
assert len(result) == 2
|
|
543
|
+
assert result[0] == Multiaddr("/ip4/192.0.2.1/tcp/80")
|
|
544
|
+
assert result[1] == Multiaddr("/ip4/192.0.2.2/tcp/80")
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
# ---------------------------------------------------------------------------
|
|
548
|
+
# Utility function tests
|
|
549
|
+
# ---------------------------------------------------------------------------
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
class TestIsFqdn:
|
|
553
|
+
"""Tests for is_fqdn."""
|
|
554
|
+
|
|
555
|
+
def test_empty(self):
|
|
556
|
+
assert is_fqdn("") is False
|
|
557
|
+
|
|
558
|
+
def test_dot(self):
|
|
559
|
+
assert is_fqdn(".") is True
|
|
560
|
+
|
|
561
|
+
def test_no_trailing_dot(self):
|
|
562
|
+
assert is_fqdn("example.com") is False
|
|
563
|
+
|
|
564
|
+
def test_trailing_dot(self):
|
|
565
|
+
assert is_fqdn("example.com.") is True
|
|
566
|
+
|
|
567
|
+
def test_escaped_dot_in_middle(self):
|
|
568
|
+
# escaped dot in middle, real dot at end
|
|
569
|
+
assert is_fqdn("example\\.com.") is True
|
|
570
|
+
|
|
571
|
+
def test_trailing_escaped_dot(self):
|
|
572
|
+
# trailing dot is escaped → not FQDN
|
|
573
|
+
assert is_fqdn("example.com\\.") is False
|
|
574
|
+
|
|
575
|
+
def test_escaped_backslash_before_dot(self):
|
|
576
|
+
# escaped backslash before real dot
|
|
577
|
+
assert is_fqdn("example.com\\\\.") is True
|
|
578
|
+
|
|
579
|
+
def test_escaped_backslash_and_escaped_dot(self):
|
|
580
|
+
# escaped backslash + escaped dot
|
|
581
|
+
assert is_fqdn("example.com\\\\\\.") is False
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
class TestFqdn:
|
|
585
|
+
"""Tests for fqdn."""
|
|
586
|
+
|
|
587
|
+
def test_empty(self):
|
|
588
|
+
assert fqdn("") == "."
|
|
589
|
+
|
|
590
|
+
def test_dot(self):
|
|
591
|
+
assert fqdn(".") == "."
|
|
592
|
+
|
|
593
|
+
def test_no_trailing_dot(self):
|
|
594
|
+
assert fqdn("example.com") == "example.com."
|
|
595
|
+
|
|
596
|
+
def test_already_fqdn(self):
|
|
597
|
+
assert fqdn("example.com.") == "example.com."
|
|
598
|
+
|
|
599
|
+
def test_escaped_trailing_dot(self):
|
|
600
|
+
# trailing dot is escaped → append a real dot
|
|
601
|
+
assert fqdn("example.com\\.") == "example.com\\.."
|
|
602
|
+
|
|
603
|
+
def test_escaped_backslash_already_fqdn(self):
|
|
604
|
+
# escaped backslash before real dot → already FQDN
|
|
605
|
+
assert fqdn("example.com\\\\.") == "example.com\\\\."
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
class TestAddrLen:
|
|
609
|
+
def test_single_protocol(self):
|
|
610
|
+
assert addr_len(Multiaddr("/ip4/127.0.0.1")) == 1
|
|
611
|
+
|
|
612
|
+
def test_two_protocols(self):
|
|
613
|
+
assert addr_len(Multiaddr("/ip4/127.0.0.1/tcp/80")) == 2
|
|
614
|
+
|
|
615
|
+
def test_three_protocols(self):
|
|
616
|
+
assert addr_len(Multiaddr("/ip4/127.0.0.1/tcp/80/http")) == 3
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
class TestOffsetAddr:
|
|
620
|
+
def test_offset_zero(self):
|
|
621
|
+
ma = Multiaddr("/ip4/127.0.0.1/tcp/80")
|
|
622
|
+
assert offset_addr(ma, 0) == ma
|
|
623
|
+
|
|
624
|
+
def test_offset_one(self):
|
|
625
|
+
ma = Multiaddr("/ip4/127.0.0.1/tcp/80")
|
|
626
|
+
assert offset_addr(ma, 1) == Multiaddr("/tcp/80")
|
|
627
|
+
|
|
628
|
+
def test_offset_all(self):
|
|
629
|
+
ma = Multiaddr("/ip4/127.0.0.1/tcp/80")
|
|
630
|
+
assert offset_addr(ma, 2) == Multiaddr("/")
|
|
631
|
+
|
|
632
|
+
def test_negative_offset(self):
|
|
633
|
+
ma = Multiaddr("/ip4/127.0.0.1/tcp/80")
|
|
634
|
+
with pytest.raises(ValueError, match="non-negative"):
|
|
635
|
+
offset_addr(ma, -1)
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
class TestMatches:
|
|
639
|
+
def test_dns4(self):
|
|
640
|
+
assert matches(Multiaddr("/dns4/example.com/tcp/80")) is True
|
|
641
|
+
|
|
642
|
+
def test_dns6(self):
|
|
643
|
+
assert matches(Multiaddr("/dns6/example.com/tcp/80")) is True
|
|
644
|
+
|
|
645
|
+
def test_dnsaddr(self):
|
|
646
|
+
assert matches(Multiaddr("/dnsaddr/example.com")) is True
|
|
647
|
+
|
|
648
|
+
def test_dns(self):
|
|
649
|
+
assert matches(Multiaddr("/dns/example.com/tcp/80")) is True
|
|
650
|
+
|
|
651
|
+
def test_ip4(self):
|
|
652
|
+
assert matches(Multiaddr("/ip4/127.0.0.1/tcp/80")) is False
|
|
653
|
+
|
|
654
|
+
def test_ip6(self):
|
|
655
|
+
assert matches(Multiaddr("/ip6/::1/tcp/80")) is False
|
|
656
|
+
|
|
657
|
+
def test_dns_not_first_position(self):
|
|
658
|
+
"""Go: TestMatches — DNS at non-first position still matches."""
|
|
659
|
+
assert matches(Multiaddr("/tcp/1234/dns6/example.com")) is True
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
# ---------------------------------------------------------------------------
|
|
663
|
+
# resolve_all tests
|
|
664
|
+
# ---------------------------------------------------------------------------
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
class MockResolver:
|
|
668
|
+
"""A mock resolver for testing resolve_all."""
|
|
669
|
+
|
|
670
|
+
def __init__(self, mapping: dict[str, list[str]]):
|
|
671
|
+
self._mapping = mapping
|
|
672
|
+
|
|
673
|
+
async def resolve(self, maddr: Multiaddr) -> list[Multiaddr]:
|
|
674
|
+
key = str(maddr)
|
|
675
|
+
if key in self._mapping:
|
|
676
|
+
return [Multiaddr(v) for v in self._mapping[key]]
|
|
677
|
+
return [maddr]
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
@pytest.mark.trio
|
|
681
|
+
async def test_resolve_all_no_dns():
|
|
682
|
+
"""Non-DNS multiaddr is returned unchanged."""
|
|
683
|
+
resolver = MockResolver({})
|
|
684
|
+
ma = Multiaddr("/ip4/127.0.0.1/tcp/80")
|
|
685
|
+
result = await resolve_all(resolver, ma)
|
|
686
|
+
assert result == [ma]
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
@pytest.mark.trio
|
|
690
|
+
async def test_resolve_all_single_dns():
|
|
691
|
+
"""Single DNS component is resolved."""
|
|
692
|
+
resolver = MockResolver(
|
|
693
|
+
{
|
|
694
|
+
"/dns4/example.com/tcp/80": ["/ip4/1.2.3.4/tcp/80"],
|
|
695
|
+
}
|
|
696
|
+
)
|
|
697
|
+
result = await resolve_all(resolver, Multiaddr("/dns4/example.com/tcp/80"))
|
|
698
|
+
assert result == [Multiaddr("/ip4/1.2.3.4/tcp/80")]
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
@pytest.mark.trio
|
|
702
|
+
async def test_resolve_all_multiple_dns_components():
|
|
703
|
+
"""Multiple DNS components in one multiaddr are all resolved."""
|
|
704
|
+
resolver = MockResolver(
|
|
705
|
+
{
|
|
706
|
+
"/dns4/a.example.com/quic/dns6/b.example.com": ["/ip4/1.2.3.4/quic/dns6/b.example.com"],
|
|
707
|
+
"/ip4/1.2.3.4/quic/dns6/b.example.com": ["/ip4/1.2.3.4/quic/ip6/::1"],
|
|
708
|
+
}
|
|
709
|
+
)
|
|
710
|
+
result = await resolve_all(resolver, Multiaddr("/dns4/a.example.com/quic/dns6/b.example.com"))
|
|
711
|
+
assert result == [Multiaddr("/ip4/1.2.3.4/quic/ip6/::1")]
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
@pytest.mark.trio
|
|
715
|
+
async def test_resolve_all_chained():
|
|
716
|
+
"""Chained resolution: DNS resolves to another DNS address."""
|
|
717
|
+
resolver = MockResolver(
|
|
718
|
+
{
|
|
719
|
+
"/dns4/a.com": ["/dns4/b.com/tcp/80"],
|
|
720
|
+
"/dns4/b.com/tcp/80": ["/ip4/1.2.3.4/tcp/80"],
|
|
721
|
+
}
|
|
722
|
+
)
|
|
723
|
+
result = await resolve_all(resolver, Multiaddr("/dns4/a.com"))
|
|
724
|
+
assert result == [Multiaddr("/ip4/1.2.3.4/tcp/80")]
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
@pytest.mark.trio
|
|
728
|
+
async def test_resolve_all_max_iterations_exceeded():
|
|
729
|
+
"""Raises RecursionLimitError when DNS never fully resolves."""
|
|
730
|
+
resolver = MockResolver(
|
|
731
|
+
{
|
|
732
|
+
"/dns4/loop.com": ["/dns4/loop.com"],
|
|
733
|
+
}
|
|
734
|
+
)
|
|
735
|
+
with pytest.raises(RecursionLimitError, match="resolve_all exceeded"):
|
|
736
|
+
await resolve_all(resolver, Multiaddr("/dns4/loop.com"), max_iterations=3)
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
@pytest.mark.trio
|
|
740
|
+
async def test_resolve_all_empty_result():
|
|
741
|
+
"""Empty resolve result drops the address without looping."""
|
|
742
|
+
|
|
743
|
+
class EmptyResolver:
|
|
744
|
+
async def resolve(self, maddr: Multiaddr) -> list[Multiaddr]:
|
|
745
|
+
return []
|
|
746
|
+
|
|
747
|
+
result = await resolve_all(EmptyResolver(), Multiaddr("/dns4/gone.com/tcp/80"))
|
|
748
|
+
assert result == []
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
@pytest.mark.trio
|
|
752
|
+
async def test_resolve_all_cartesian_product():
|
|
753
|
+
"""Go: TestResolveMultiple — dns4 x dns6 produces cartesian product.
|
|
754
|
+
|
|
755
|
+
/dns4/example.com/quic/dns6/example.com with dns4→[ip4a, ip4b]
|
|
756
|
+
and dns6→[ip6a, ip6b] produces 4 results.
|
|
757
|
+
"""
|
|
758
|
+
resolver = MockResolver(
|
|
759
|
+
{
|
|
760
|
+
"/dns4/example.com/quic/dns6/example.com": [
|
|
761
|
+
"/ip4/192.0.2.1/quic/dns6/example.com",
|
|
762
|
+
"/ip4/192.0.2.2/quic/dns6/example.com",
|
|
763
|
+
],
|
|
764
|
+
"/ip4/192.0.2.1/quic/dns6/example.com": [
|
|
765
|
+
"/ip4/192.0.2.1/quic/ip6/2001:db8::a3",
|
|
766
|
+
"/ip4/192.0.2.1/quic/ip6/2001:db8::a4",
|
|
767
|
+
],
|
|
768
|
+
"/ip4/192.0.2.2/quic/dns6/example.com": [
|
|
769
|
+
"/ip4/192.0.2.2/quic/ip6/2001:db8::a3",
|
|
770
|
+
"/ip4/192.0.2.2/quic/ip6/2001:db8::a4",
|
|
771
|
+
],
|
|
772
|
+
}
|
|
773
|
+
)
|
|
774
|
+
result = await resolve_all(resolver, Multiaddr("/dns4/example.com/quic/dns6/example.com"))
|
|
775
|
+
expected = [
|
|
776
|
+
Multiaddr("/ip4/192.0.2.1/quic/ip6/2001:db8::a3"),
|
|
777
|
+
Multiaddr("/ip4/192.0.2.1/quic/ip6/2001:db8::a4"),
|
|
778
|
+
Multiaddr("/ip4/192.0.2.2/quic/ip6/2001:db8::a3"),
|
|
779
|
+
Multiaddr("/ip4/192.0.2.2/quic/ip6/2001:db8::a4"),
|
|
780
|
+
]
|
|
781
|
+
assert result == expected
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
@pytest.mark.trio
|
|
785
|
+
async def test_resolve_all_sandwich():
|
|
786
|
+
"""Go: TestResolveMultipleSandwitch — DNS sandwiched between non-DNS protocols.
|
|
787
|
+
|
|
788
|
+
/quic/dns4/example.com/dns6/example.com/http resolves both DNS components.
|
|
789
|
+
"""
|
|
790
|
+
resolver = MockResolver(
|
|
791
|
+
{
|
|
792
|
+
"/quic/dns4/example.com/dns6/example.com/http": [
|
|
793
|
+
"/quic/ip4/192.0.2.1/dns6/example.com/http",
|
|
794
|
+
"/quic/ip4/192.0.2.2/dns6/example.com/http",
|
|
795
|
+
],
|
|
796
|
+
"/quic/ip4/192.0.2.1/dns6/example.com/http": [
|
|
797
|
+
"/quic/ip4/192.0.2.1/ip6/2001:db8::a3/http",
|
|
798
|
+
"/quic/ip4/192.0.2.1/ip6/2001:db8::a4/http",
|
|
799
|
+
],
|
|
800
|
+
"/quic/ip4/192.0.2.2/dns6/example.com/http": [
|
|
801
|
+
"/quic/ip4/192.0.2.2/ip6/2001:db8::a3/http",
|
|
802
|
+
"/quic/ip4/192.0.2.2/ip6/2001:db8::a4/http",
|
|
803
|
+
],
|
|
804
|
+
}
|
|
805
|
+
)
|
|
806
|
+
result = await resolve_all(resolver, Multiaddr("/quic/dns4/example.com/dns6/example.com/http"))
|
|
807
|
+
expected = [
|
|
808
|
+
Multiaddr("/quic/ip4/192.0.2.1/ip6/2001:db8::a3/http"),
|
|
809
|
+
Multiaddr("/quic/ip4/192.0.2.1/ip6/2001:db8::a4/http"),
|
|
810
|
+
Multiaddr("/quic/ip4/192.0.2.2/ip6/2001:db8::a3/http"),
|
|
811
|
+
Multiaddr("/quic/ip4/192.0.2.2/ip6/2001:db8::a4/http"),
|
|
812
|
+
]
|
|
813
|
+
assert result == expected
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|