multiaddr 0.0.11__py3-none-any.whl

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.
@@ -0,0 +1,408 @@
1
+ """
2
+ DNS resolver implementation for multiaddr.
3
+
4
+ This module provides DNS resolution for multiaddrs, supporting the following protocols:
5
+ - /dns, /dns4, /dns6: Standard DNS resolution for IPv4 and IPv6
6
+ - /dnsaddr: Recursive TXT record resolution for libp2p bootstrap nodes
7
+
8
+ Features:
9
+ - Recursive resolution for /dnsaddr, /dns4, and /dns6
10
+ - Peer ID preservation during resolution
11
+ - Timeout and cancellation support (Trio)
12
+ - Error handling for recursion limits and DNS failures
13
+
14
+ Example usage:
15
+ from multiaddr import Multiaddr
16
+ ma = Multiaddr("/dns4/example.com/tcp/443")
17
+ resolved = await ma.resolve()
18
+ print(resolved)
19
+ # [Multiaddr("/ip4/93.184.216.34/tcp/443")]
20
+ """
21
+
22
+ import logging
23
+ import re
24
+ from typing import Any, cast
25
+
26
+ import dns.asyncresolver
27
+ import dns.rdataclass
28
+ import dns.rdtypes.ANY.TXT # type: ignore
29
+ import dns.rdtypes.IN.A
30
+ import dns.rdtypes.IN.AAAA
31
+ import dns.resolver
32
+ import trio
33
+
34
+ from ..exceptions import RecursionLimitError, ResolutionError
35
+ from ..multiaddr import Multiaddr
36
+ from ..protocols import P_DNS, P_DNS4, P_DNS6, P_DNSADDR, Protocol
37
+ from .base import Resolver
38
+
39
+
40
+ class DNSResolver(Resolver):
41
+ """
42
+ DNS resolver for multiaddr.
43
+
44
+ Resolves /dns, /dns4, /dns6, and /dnsaddr multiaddrs to their underlying IP addresses.
45
+ Supports recursive resolution for DNSADDR records and protocol-specific resolution for
46
+ DNS4/DNS6.
47
+ """
48
+
49
+ MAX_RECURSIVE_DEPTH = 32
50
+ DEFAULT_TIMEOUT = 5.0 # 5 seconds timeout
51
+
52
+ def __init__(self) -> None:
53
+ """Initialize the DNS resolver."""
54
+ self._resolver = dns.asyncresolver.Resolver()
55
+
56
+ async def resolve(
57
+ self, maddr: "Multiaddr", options: dict[str, Any] | None = None
58
+ ) -> list["Multiaddr"]:
59
+ """
60
+ Resolve a DNS multiaddr to its actual addresses.
61
+
62
+ Args:
63
+ maddr: The multiaddr to resolve
64
+ options: Optional configuration options (e.g., max_recursive_depth, signal)
65
+
66
+ Returns:
67
+ A list of resolved multiaddrs
68
+
69
+ Raises:
70
+ ResolutionError: If resolution fails
71
+ RecursionLimitError: If maximum recursive depth is reached
72
+ trio.Cancelled: If the operation is cancelled
73
+ """
74
+ protocols: list[Protocol] = list(maddr.protocols())
75
+ if not protocols:
76
+ raise ResolutionError("empty multiaddr")
77
+
78
+ first_protocol = protocols[0]
79
+ if first_protocol.code not in (P_DNS, P_DNS4, P_DNS6, P_DNSADDR):
80
+ return [maddr]
81
+
82
+ # Get the hostname and clean it of quotes
83
+ hostname = maddr.value_for_protocol(first_protocol.code)
84
+ if not hostname:
85
+ return [maddr]
86
+
87
+ # Remove quotes from hostname
88
+ hostname = self._clean_quotes(hostname)
89
+
90
+ # Get max recursive depth from options or use default
91
+ max_depth = (
92
+ options.get("max_recursive_depth", self.MAX_RECURSIVE_DEPTH)
93
+ if options
94
+ else self.MAX_RECURSIVE_DEPTH
95
+ )
96
+
97
+ # Get signal from options if provided
98
+ signal = options.get("signal") if options else None
99
+
100
+ try:
101
+ if first_protocol.code == P_DNSADDR:
102
+ resolved = await self._resolve_dnsaddr(
103
+ hostname,
104
+ maddr,
105
+ max_depth,
106
+ signal,
107
+ )
108
+ return resolved # Do not fallback to [maddr]
109
+ else:
110
+ resolved = await self._resolve_dns_with_stack(maddr, signal)
111
+ return resolved if resolved else [maddr] # Classic DNS fallback remains
112
+ except RecursionLimitError:
113
+ # Do not wrap RecursionLimitError so tests can catch it
114
+ raise
115
+ except Exception as e:
116
+ raise ResolutionError(f"Failed to resolve {hostname}: {e!s}")
117
+
118
+ def _clean_quotes(self, text: str) -> str:
119
+ """Remove quotes from a string.
120
+
121
+ Args:
122
+ text: The text to clean
123
+
124
+ Returns:
125
+ The cleaned text without quotes
126
+ """
127
+ # Remove all types of quotes (single, double, mixed)
128
+ return re.sub(r'[\'"\s]+', "", text)
129
+
130
+ async def _resolve_dnsaddr(
131
+ self,
132
+ hostname: str,
133
+ original_ma: "Multiaddr",
134
+ max_depth: int,
135
+ signal: trio.CancelScope | None = None,
136
+ ) -> list["Multiaddr"]:
137
+ """
138
+ Resolve a DNSADDR record according to libp2p specification.
139
+
140
+ Queries TXT records from _dnsaddr.<hostname> and parses dnsaddr=<multiaddr> entries.
141
+ Recursively resolves /dns4 and /dns6 entries to /ip4 and /ip6 addresses.
142
+
143
+ Args:
144
+ hostname: The hostname to resolve
145
+ original_ma: The original multiaddr being resolved
146
+ max_depth: Maximum depth for recursive resolution
147
+ signal: Optional signal for cancellation
148
+
149
+ Returns:
150
+ A list of resolved multiaddrs
151
+
152
+ Raises:
153
+ ResolutionError: If resolution fails
154
+ RecursionLimitError: If maximum recursive depth is reached
155
+ trio.Cancelled: If the operation is cancelled
156
+ """
157
+ if max_depth <= 0:
158
+ raise RecursionLimitError(f"Maximum recursive depth exceeded for {hostname}")
159
+
160
+ # Get the peer ID if present
161
+ peer_id = None
162
+ try:
163
+ peer_id = original_ma.get_peer_id()
164
+ except Exception:
165
+ # If there's no peer ID, that's fine - we'll just resolve the address
166
+ pass
167
+
168
+ # Query TXT records from _dnsaddr.<hostname> according to libp2p spec
169
+ dnsaddr_hostname = f"_dnsaddr.{hostname}"
170
+
171
+ try:
172
+ if signal:
173
+ # Use the provided signal for cancellation
174
+ with signal:
175
+ return await self._query_dnsaddr_txt_records(
176
+ dnsaddr_hostname, peer_id, max_depth, signal
177
+ )
178
+ else:
179
+ # Use default timeout-based cancellation
180
+ with trio.CancelScope() as cancel_scope: # type: ignore[call-arg]
181
+ # Set a timeout for DNS resolution
182
+ cancel_scope.deadline = trio.current_time() + self.DEFAULT_TIMEOUT
183
+ cancel_scope.cancelled_caught = True # type: ignore[misc]
184
+
185
+ return await self._query_dnsaddr_txt_records(
186
+ dnsaddr_hostname, peer_id, max_depth, cancel_scope
187
+ )
188
+ except Exception as e:
189
+ raise ResolutionError(f"Failed to resolve DNSADDR {hostname}: {e!s}")
190
+
191
+ async def _query_dnsaddr_txt_records(
192
+ self,
193
+ dnsaddr_hostname: str,
194
+ peer_id: str | None,
195
+ max_depth: int,
196
+ signal: trio.CancelScope | None = None,
197
+ _debug_level: int = 0,
198
+ ) -> list["Multiaddr"]:
199
+ """
200
+ Query TXT records and parse dnsaddr=<multiaddr> entries.
201
+
202
+ Args:
203
+ dnsaddr_hostname: The _dnsaddr.<hostname> to query
204
+ peer_id: Optional peer ID to filter results
205
+ max_depth: Maximum depth for recursive resolution
206
+ signal: Optional signal for cancellation
207
+ _debug_level: Internal, for debug output indentation
208
+
209
+ Returns:
210
+ A list of resolved multiaddrs
211
+ """
212
+ results = []
213
+ indent = " " * _debug_level
214
+ try:
215
+ answer = await self._resolver.resolve(dnsaddr_hostname, "TXT")
216
+ logging.debug(
217
+ f"{indent}Queried TXT for {dnsaddr_hostname}, "
218
+ f"found {len(answer)} records (depth {max_depth})"
219
+ )
220
+ for rdata in answer:
221
+ # Cast to TXT record type for proper attribute access
222
+ txt_rdata = cast(dns.rdtypes.ANY.TXT.TXT, rdata)
223
+ txt_data_raw = txt_rdata.strings[0] if txt_rdata.strings else ""
224
+ if isinstance(txt_data_raw, bytes):
225
+ txt_data = txt_data_raw.decode("utf-8")
226
+ else:
227
+ txt_data = str(txt_data_raw)
228
+ logging.debug(f"{indent} TXT: {txt_data}")
229
+ if txt_data.startswith("dnsaddr="):
230
+ multiaddr_str = txt_data[8:]
231
+ multiaddr_str = self._clean_quotes(multiaddr_str).strip()
232
+ logging.debug(f"{indent} Parsed multiaddr: {multiaddr_str}")
233
+ if not multiaddr_str:
234
+ continue
235
+ try:
236
+ parsed_ma = Multiaddr(multiaddr_str)
237
+ try:
238
+ parsed_peer_id = parsed_ma.get_peer_id()
239
+ logging.debug(f"{indent} Peer ID: {parsed_peer_id}")
240
+ except Exception:
241
+ logging.debug(f"{indent} No peer ID")
242
+ if peer_id:
243
+ try:
244
+ parsed_peer_id = parsed_ma.get_peer_id()
245
+ if parsed_peer_id != peer_id:
246
+ logging.debug(f"{indent} Skipping (peer ID mismatch)")
247
+ continue
248
+ except Exception:
249
+ logging.debug(f"{indent} Skipping (no peer ID in multiaddr)")
250
+ continue
251
+ if (
252
+ multiaddr_str.startswith("/dnsaddr")
253
+ or multiaddr_str.startswith("/dns4")
254
+ or multiaddr_str.startswith("/dns6")
255
+ ):
256
+ try:
257
+ logging.debug(f"{indent} Recursing into {multiaddr_str}")
258
+ recursive_options = {"max_recursive_depth": max_depth - 1}
259
+ resolved = await self.resolve(parsed_ma, recursive_options)
260
+ for r in resolved:
261
+ # Only append if not a dnsaddr/dns4/dns6 (i.e., only final IPs)
262
+ if not any(
263
+ p.name in ("dnsaddr", "dns4", "dns6") for p in r.protocols()
264
+ ):
265
+ logging.debug(f"{indent} Final resolved: {r}")
266
+ results.append(r)
267
+ except RecursionLimitError:
268
+ logging.debug(f"{indent} Recursion limit hit!")
269
+ continue
270
+ else:
271
+ logging.debug(f"{indent} Final resolved: {parsed_ma}")
272
+ results.append(parsed_ma)
273
+ except Exception as e:
274
+ logging.debug(f"{indent} Error parsing multiaddr: {e}")
275
+ continue
276
+ except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN):
277
+ logging.debug(f"{indent}No TXT records found for {dnsaddr_hostname}")
278
+ pass
279
+ except Exception as e:
280
+ logging.debug(f"{indent}Error querying TXT records for {dnsaddr_hostname}: {e}")
281
+ raise ResolutionError(f"Failed to query TXT records for {dnsaddr_hostname}: {e!s}")
282
+ return results
283
+
284
+ async def _resolve_dns(
285
+ self, hostname: str, protocol_code: int, signal: trio.CancelScope | None = None
286
+ ) -> list["Multiaddr"]:
287
+ """Resolve a DNS record.
288
+
289
+ Args:
290
+ hostname: The hostname to resolve
291
+ protocol_code: The protocol code (DNS, DNS4, or DNS6)
292
+ signal: Optional signal for cancellation
293
+
294
+ Returns:
295
+ A list of resolved multiaddrs
296
+
297
+ Raises:
298
+ ResolutionError: If resolution fails
299
+ trio.Cancelled: If the operation is cancelled
300
+ """
301
+ try:
302
+ if signal:
303
+ # Use the provided signal for cancellation
304
+ with signal:
305
+ results = []
306
+ if protocol_code in (P_DNS, P_DNS4):
307
+ try:
308
+ answer = await self._resolver.resolve(hostname, "A")
309
+ for rdata in answer:
310
+ address = str(cast(dns.rdtypes.IN.A.A, rdata).address)
311
+ results.append(Multiaddr(f"/ip4/{address}"))
312
+ except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN):
313
+ pass
314
+ if protocol_code in (P_DNS, P_DNS6):
315
+ try:
316
+ answer = await self._resolver.resolve(hostname, "AAAA")
317
+ for rdata in answer:
318
+ address = str(cast(dns.rdtypes.IN.AAAA.AAAA, rdata).address)
319
+ results.append(Multiaddr(f"/ip6/{address}"))
320
+ except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN):
321
+ pass
322
+ return results
323
+ else:
324
+ # No signal provided, proceed without cancellation
325
+ results = []
326
+ if protocol_code in (P_DNS, P_DNS4):
327
+ try:
328
+ answer = await self._resolver.resolve(hostname, "A")
329
+ for rdata in answer:
330
+ address = str(cast(dns.rdtypes.IN.A.A, rdata).address)
331
+ results.append(Multiaddr(f"/ip4/{address}"))
332
+ except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN):
333
+ pass
334
+ if protocol_code in (P_DNS, P_DNS6):
335
+ try:
336
+ answer = await self._resolver.resolve(hostname, "AAAA")
337
+ for rdata in answer:
338
+ address = str(cast(dns.rdtypes.IN.AAAA.AAAA, rdata).address)
339
+ results.append(Multiaddr(f"/ip6/{address}"))
340
+ except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN):
341
+ pass
342
+ return results
343
+ except Exception as e:
344
+ raise ResolutionError(f"Failed to resolve DNS {hostname}: {e!s}")
345
+
346
+ async def _resolve_dns_with_stack(
347
+ self, maddr: "Multiaddr", signal: trio.CancelScope | None = None
348
+ ) -> list["Multiaddr"]:
349
+ """Resolve a DNS record while preserving the rest of the multiaddr stack.
350
+
351
+ This method handles cases like /dns4/host/tcp/port by resolving the DNS part
352
+ and keeping the rest of the multiaddr intact.
353
+
354
+ Args:
355
+ maddr: The multiaddr to resolve
356
+ signal: Optional signal for cancellation
357
+
358
+ Returns:
359
+ A list of resolved multiaddrs with preserved stack
360
+
361
+ Raises:
362
+ ResolutionError: If resolution fails
363
+ trio.Cancelled: If the operation is cancelled
364
+ """
365
+ protocols = list(maddr.protocols())
366
+ if not protocols:
367
+ return [maddr]
368
+
369
+ first_protocol = protocols[0]
370
+ if first_protocol.code not in (P_DNS, P_DNS4, P_DNS6):
371
+ return [maddr]
372
+
373
+ # Get the hostname
374
+ hostname = maddr.value_for_protocol(first_protocol.code)
375
+ if not hostname:
376
+ return [maddr]
377
+
378
+ # Remove quotes from hostname
379
+ hostname = self._clean_quotes(hostname)
380
+
381
+ # Get the resolved IP addresses
382
+ resolved_ips = await self._resolve_dns(hostname, first_protocol.code, signal)
383
+ if not resolved_ips:
384
+ return [maddr]
385
+
386
+ # Split the multiaddr to get the remaining stack (everything after the DNS part)
387
+ parts = maddr.split(1) # Split after the first protocol
388
+ if len(parts) < 2:
389
+ # No remaining stack, just return the resolved IPs
390
+ return resolved_ips
391
+
392
+ remaining_stack = parts[1] # Everything after the DNS part
393
+
394
+ results = []
395
+ for ip_maddr in resolved_ips:
396
+ # Combine the resolved IP with the remaining stack
397
+ if remaining_stack.protocols():
398
+ # There's a remaining stack, encapsulate it
399
+ combined = ip_maddr.encapsulate(remaining_stack)
400
+ else:
401
+ # No remaining stack, just use the IP
402
+ combined = ip_maddr
403
+ results.append(combined)
404
+
405
+ return results
406
+
407
+
408
+ __all__ = ["DNSResolver"]
@@ -0,0 +1,174 @@
1
+ import io
2
+ import logging
3
+ from collections.abc import Generator
4
+ from io import BytesIO
5
+
6
+ import varint
7
+
8
+ from . import exceptions
9
+ from .codecs import CodecBase, codec_by_name
10
+ from .protocols import Protocol, protocol_with_code, protocol_with_name
11
+
12
+ logger = logging.getLogger(__name__)
13
+
14
+
15
+ def string_to_bytes(string: str) -> bytes:
16
+ bs: list[bytes] = []
17
+ for proto, codec, value in string_iter(string):
18
+ logger.debug(
19
+ f"[DEBUG string_to_bytes] LOOP: proto={proto.name}, codec={codec}, value={value}"
20
+ )
21
+ logger.debug(
22
+ f"[DEBUG string_to_bytes] Processing: proto={proto.name}, "
23
+ f"codec.SIZE={getattr(codec, 'SIZE', None) if codec else None}, value={value}"
24
+ )
25
+ logger.debug(f"[DEBUG string_to_bytes] Protocol code: {proto.code}")
26
+ encoded_code = varint.encode(proto.code)
27
+ logger.debug(f"[DEBUG string_to_bytes] Encoded protocol code: {encoded_code}")
28
+ bs.append(encoded_code)
29
+
30
+ # Special case: protocols with codec=None are flag protocols
31
+ # (no value, no length prefix, no buffer)
32
+ if codec is None:
33
+ logger.debug(
34
+ f"[DEBUG string_to_bytes] Protocol {proto.name} has no data, "
35
+ "skipping value encoding"
36
+ )
37
+ continue
38
+
39
+ if value is None:
40
+ raise ValueError("Value cannot be None")
41
+ try:
42
+ logger.debug(f"[DEBUG string_to_bytes] Raw CID value before encoding: {value}")
43
+ buf = codec.to_bytes(proto, value)
44
+ logger.debug(f"[DEBUG string_to_bytes] Generated buf: proto={proto.name}, buf={buf!r}")
45
+ except Exception as exc:
46
+ logger.debug(f"[DEBUG string_to_bytes] Error: {exc}")
47
+ raise exceptions.StringParseError(str(exc), string) from exc
48
+ logger.debug(
49
+ f"[DEBUG string_to_bytes] Appending: proto={proto.name}, "
50
+ f"codec.SIZE={getattr(codec, 'SIZE', None)}"
51
+ )
52
+ # Only add length prefix for variable-sized codecs (SIZE <= 0)
53
+ if codec.SIZE <= 0:
54
+ bs.append(varint.encode(len(buf)))
55
+ logger.debug(
56
+ f"[DEBUG string_to_bytes] Appending varint length: {varint.encode(len(buf))}"
57
+ )
58
+ # Only append the buffer if it's not empty
59
+ if buf:
60
+ bs.append(buf)
61
+ logger.debug(f"[DEBUG string_to_bytes] Final bs: {bs}")
62
+ return b"".join(bs)
63
+
64
+
65
+ def bytes_to_string(buf: bytes) -> str:
66
+ """Convert a binary multiaddr to its string representation
67
+
68
+ Raises
69
+ ------
70
+ ~multiaddr.exceptions.BinaryParseError
71
+ The given bytes are not a valid multiaddr.
72
+ """
73
+ if not buf:
74
+ return ""
75
+ bs = BytesIO(buf)
76
+ strings = []
77
+ code = None
78
+ proto = None
79
+ while bs.tell() < len(buf):
80
+ try:
81
+ code = varint.decode_stream(bs)
82
+ logger.debug(f"[DEBUG bytes_to_string] Decoded protocol code: {code}")
83
+ proto = protocol_with_code(code)
84
+ logger.debug(f"[DEBUG bytes_to_string] Protocol name: {proto.name}")
85
+ if proto.codec is not None:
86
+ codec = codec_by_name(proto.codec)
87
+ if codec.SIZE > 0:
88
+ value = codec.to_string(proto, bs.read(codec.SIZE // 8))
89
+ else:
90
+ # For variable-sized codecs,
91
+ # read the length prefix but don't pass it to the codec
92
+ size = varint.decode_stream(bs)
93
+ value = codec.to_string(proto, bs.read(size))
94
+ logger.debug(f"[DEBUG] bytes_to_string: proto={proto.name}, value='{value}'")
95
+ if codec.IS_PATH and value.startswith("/"):
96
+ strings.append("/" + proto.name + value) # type: ignore[arg-type]
97
+ else:
98
+ strings.append("/" + proto.name + "/" + value) # type: ignore[arg-type]
99
+ else:
100
+ strings.append("/" + proto.name) # type: ignore[arg-type]
101
+ except Exception as exc:
102
+ # Use the code as the protocol identifier if proto is not available
103
+ # Ensure we always have either a string or an integer
104
+ protocol_id = proto.name if proto is not None else (code if code is not None else 0)
105
+ raise exceptions.BinaryParseError(str(exc), buf, protocol_id, exc) from exc
106
+ return "".join(strings)
107
+
108
+
109
+ def size_for_addr(codec: CodecBase, buf_io: io.BytesIO) -> int:
110
+ if codec.SIZE >= 0:
111
+ return codec.SIZE // 8
112
+ else:
113
+ return varint.decode_stream(buf_io)
114
+
115
+
116
+ def string_iter(
117
+ string: str,
118
+ ) -> Generator[tuple[Protocol, CodecBase | None, str | None], None, None]:
119
+ """Iterate over the parts of a string multiaddr.
120
+
121
+ Args:
122
+ string: The string multiaddr to iterate over
123
+
124
+ Yields:
125
+ A tuple of (protocol, codec, value) for each part of the multiaddr
126
+ """
127
+ if not string:
128
+ return
129
+
130
+ parts = string.strip("/").split("/")
131
+ i = 0
132
+ while i < len(parts):
133
+ proto_name = parts[i]
134
+ try:
135
+ proto = protocol_with_name(proto_name)
136
+ except exceptions.ProtocolNotFoundError as exc:
137
+ raise exceptions.StringParseError(str(exc), string) from exc
138
+
139
+ codec = codec_by_name(proto.codec)
140
+ value = None
141
+
142
+ if proto.codec is not None:
143
+ if i + 1 >= len(parts):
144
+ raise exceptions.StringParseError(
145
+ f"missing value for protocol: {proto_name}", string
146
+ )
147
+ value = parts[i + 1]
148
+ i += 1 # Skip the next part since we used it as value
149
+ logger.debug(f"[DEBUG string_iter] Using next part as value: {value}")
150
+ yield proto, codec, value
151
+ else:
152
+ logger.debug(f"[DEBUG string_iter] No value found for protocol {proto.name}")
153
+ yield proto, codec, None
154
+ i += 1
155
+
156
+
157
+ def bytes_iter(buf: bytes) -> Generator[tuple[int, Protocol, CodecBase, bytes], None, None]:
158
+ buf_io = io.BytesIO(buf)
159
+ while buf_io.tell() < len(buf):
160
+ offset = buf_io.tell()
161
+ code = varint.decode_stream(buf_io)
162
+ proto = None
163
+ try:
164
+ proto = protocol_with_code(code)
165
+ codec = codec_by_name(proto.codec)
166
+ except (ImportError, exceptions.ProtocolNotFoundError) as exc:
167
+ raise exceptions.BinaryParseError(
168
+ "Unknown Protocol",
169
+ buf,
170
+ proto.name if proto else code,
171
+ ) from exc
172
+
173
+ size = size_for_addr(codec, buf_io)
174
+ yield offset, proto, codec, buf_io.read(size)
multiaddr/utils.py ADDED
@@ -0,0 +1,119 @@
1
+ import socket
2
+ from typing import Any
3
+
4
+ import psutil
5
+
6
+ from .multiaddr import Multiaddr
7
+
8
+
9
+ def is_wildcard(ip: str) -> bool:
10
+ """Check if an IP address is a wildcard address."""
11
+ return ip in ["0.0.0.0", "::"]
12
+
13
+
14
+ def get_network_addrs(family: int) -> list[str]:
15
+ """Get all network addresses for a given IP family (4 for IPv4, 6 for IPv6)."""
16
+ addresses = []
17
+ for iface, addrs in psutil.net_if_addrs().items():
18
+ for addr in addrs:
19
+ if family == 4 and addr.family == socket.AF_INET:
20
+ if addr.address != "127.0.0.1" and not is_link_local_ip(addr.address):
21
+ addresses.append(addr.address)
22
+ elif family == 6 and addr.family == socket.AF_INET6:
23
+ if not addr.address.startswith("::1") and not is_link_local_ip(addr.address):
24
+ # Remove the %scope_id if present
25
+ addresses.append(addr.address.split("%")[0])
26
+ return addresses
27
+
28
+
29
+ def is_link_local_ip(ip: str) -> bool:
30
+ """Check if an IP address is link-local."""
31
+ if ":" in ip: # IPv6
32
+ return ip.startswith("fe80:")
33
+ else: # IPv4
34
+ parts = ip.split(".")
35
+ return len(parts) == 4 and parts[0] == "169" and parts[1] == "254"
36
+
37
+
38
+ def get_multiaddr_options(ma: Multiaddr) -> dict[str, Any] | None:
39
+ """Extract options from a multiaddr (similar to toOptions() in JS).
40
+
41
+ Returns a dictionary with 'family', 'host', 'transport', and 'port' keys,
42
+ or None if the multiaddr doesn't represent a thin waist address.
43
+ """
44
+ if ma is None:
45
+ return None
46
+
47
+ # Parse the multiaddr to extract IP and transport information
48
+ parts = str(ma).strip("/").split("/")
49
+
50
+ if len(parts) < 4:
51
+ return None
52
+
53
+ # Look for IP protocol (ip4 or ip6)
54
+ ip_proto = None
55
+ ip_addr = None
56
+ transport_proto = None
57
+ port = None
58
+
59
+ for i, part in enumerate(parts):
60
+ if part in ["ip4", "ip6"]:
61
+ if i + 1 < len(parts):
62
+ ip_proto = part
63
+ ip_addr = parts[i + 1]
64
+ elif part in ["tcp", "udp"]:
65
+ if i + 1 < len(parts):
66
+ transport_proto = part
67
+ try:
68
+ port = int(parts[i + 1])
69
+ except (ValueError, IndexError):
70
+ return None
71
+
72
+ if not all([ip_proto, ip_addr, transport_proto, port]):
73
+ return None
74
+
75
+ family = 4 if ip_proto == "ip4" else 6
76
+
77
+ return {"family": family, "host": ip_addr, "transport": transport_proto, "port": port}
78
+
79
+
80
+ def get_thin_waist_addresses(
81
+ ma: Multiaddr | None = None, port: int | None = None
82
+ ) -> list[Multiaddr]:
83
+ """Get all thin waist addresses on the current host that match the family of the
84
+ passed multiaddr and optionally override the port.
85
+
86
+ Wildcard IP4/6 addresses will be expanded into all available interfaces.
87
+
88
+ Args:
89
+ ma: The multiaddr to process. If None, returns empty list.
90
+ port: Optional port to override the port in the multiaddr.
91
+
92
+ Returns:
93
+ List of Multiaddr objects representing thin waist addresses.
94
+ """
95
+ if ma is None:
96
+ return []
97
+
98
+ options = get_multiaddr_options(ma)
99
+ if options is None:
100
+ return []
101
+
102
+ # Use provided port or fall back to the one in the multiaddr
103
+ target_port = port if port is not None else options["port"]
104
+
105
+ ip_proto = "ip4" if options["family"] == 4 else "ip6"
106
+
107
+ if is_wildcard(options["host"]):
108
+ # Expand wildcard addresses to all available interfaces
109
+ addrs = []
110
+ for host in get_network_addrs(options["family"]):
111
+ if not is_link_local_ip(host):
112
+ # Correct multiaddr format: /ip4/host/tcp/port or /ip6/host/tcp/port
113
+ addr_str = f"/{ip_proto}/{host}/{options['transport']}/{target_port}"
114
+ addrs.append(Multiaddr(addr_str))
115
+ return addrs
116
+ else:
117
+ # Return the specific address
118
+ addr_str = f"/{ip_proto}/{options['host']}/{options['transport']}/{target_port}"
119
+ return [Multiaddr(addr_str)]