Skip to content

Commit 0e94489

Browse files
committed
nat: fix async_upnp_client compatibility — traverse embedded devices, fix bool type, deduplicate
- Guard on_response with early return if _service already found (SSDP sends one response per device sub-type, so the callback fires ~9x) - Extract location from CaseInsensitiveDict headers (newer async_upnp_client passes headers dict, not a response object) - Recursively traverse embedded_devices to find WANIPConnection service (root device only exposes Layer3Forwarding; WANIPConnection lives in the InternetGatewayDevice → WANDevice → WANConnectionDevice subtree) - Pass NewEnabled=True (bool) instead of 1; async_upnp_client validates the UPnP boolean type strictly
1 parent e40687e commit 0e94489

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

quarkchain/p2p/nat.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,21 @@ async def _discover(self, session):
114114
factory = UpnpFactory(requester)
115115

116116
async def on_response(response):
117+
if self._service:
118+
return
119+
# async_upnp_client passes headers as a CaseInsensitiveDict
120+
location = response.get("location") if hasattr(response, "get") else getattr(response, "location", None)
121+
if not location:
122+
return
117123
try:
118-
device = await factory.async_create_device(response.location)
124+
device = await factory.async_create_device(location)
119125

120-
for service in device.services.values():
126+
def _iter_services(dev):
127+
yield from dev.services.values()
128+
for sub in getattr(dev, "embedded_devices", {}).values():
129+
yield from _iter_services(sub)
130+
131+
for service in _iter_services(device):
121132
if "WANIPConn" in service.service_type:
122133
self._service = service
123134
self.logger.info("Found UPnP WANIP service")
@@ -149,7 +160,7 @@ async def _add_port_mapping(self) -> None:
149160
NewProtocol=protocol,
150161
NewInternalPort=self.port,
151162
NewInternalClient=internal_ip,
152-
NewEnabled=1,
163+
NewEnabled=True,
153164
NewPortMappingDescription=description,
154165
NewLeaseDuration=self._nat_portmap_lifetime,
155166
)

0 commit comments

Comments
 (0)