| 1 |
#!/usr/bin/env python3 |
| 2 |
"""Example of adding and deleting a port mapping.""" |
| 3 |
|
| 4 |
import asyncio |
| 5 |
import ipaddress |
| 6 |
import sys |
| 7 |
from datetime import timedelta |
| 8 |
from typing import cast |
| 9 |
|
| 10 |
from async_upnp_client.aiohttp import AiohttpRequester |
| 11 |
from async_upnp_client.client_factory import UpnpFactory |
| 12 |
from async_upnp_client.profiles.igd import IgdDevice |
| 13 |
from async_upnp_client.utils import CaseInsensitiveDict, get_local_ip |
| 14 |
|
| 15 |
SOURCE = ("0.0.0.0", 0) |
| 16 |
|
| 17 |
|
| 18 |
async def discover_igd_devices() -> set[CaseInsensitiveDict]: |
| 19 |
"""Discover IGD devices.""" |
| 20 |
# Do the search, this blocks for timeout (4 seconds, default). |
| 21 |
discoveries = await IgdDevice.async_search(source=SOURCE) |
| 22 |
if not discoveries: |
| 23 |
print("Could not find device") |
| 24 |
sys.exit(1) |
| 25 |
|
| 26 |
return discoveries |
| 27 |
|
| 28 |
|
| 29 |
async def build_igd_device(discovery: CaseInsensitiveDict) -> IgdDevice: |
| 30 |
"""Find and construct device.""" |
| 31 |
location = discovery["location"] |
| 32 |
requester = AiohttpRequester() |
| 33 |
factory = UpnpFactory(requester, non_strict=True) |
| 34 |
device = await factory.async_create_device(description_url=location) |
| 35 |
return IgdDevice(device, None) |
| 36 |
|
| 37 |
|
| 38 |
async def async_add_pinhole(igd_device: IgdDevice) -> int: |
| 39 |
"""Add Pinhole.""" |
| 40 |
remote_host: ipaddress.IPv6Address = ipaddress.ip_address("::1") |
| 41 |
internal_client: ipaddress.IPv6Address = ipaddress.ip_address("fe80::1") |
| 42 |
protocol = 6 # TCP=6, UDP=17 |
| 43 |
pinhole_id = await igd_device.async_add_pinhole( |
| 44 |
remote_host=remote_host, |
| 45 |
remote_port=43210, |
| 46 |
internal_client=internal_client, |
| 47 |
internal_port=54321, |
| 48 |
protocol=protocol, |
| 49 |
lease_time=timedelta(seconds=7200), |
| 50 |
) |
| 51 |
return pinhole_id |
| 52 |
|
| 53 |
|
| 54 |
async def async_del_pinhole(igd_device: IgdDevice, pinhole_id: int) -> None: |
| 55 |
"""Delete port mapping.""" |
| 56 |
await igd_device.async_delete_pinhole( |
| 57 |
pinhole_id=pinhole_id, |
| 58 |
) |
| 59 |
|
| 60 |
|
| 61 |
async def async_main() -> None: |
| 62 |
"""Async main.""" |
| 63 |
discoveries = await discover_igd_devices() |
| 64 |
print(f"Discoveries: {discoveries}") |
| 65 |
discovery = list(discoveries)[0] |
| 66 |
print(f"Using device at location: {discovery['location']}") |
| 67 |
igd_device = await build_igd_device(discovery) |
| 68 |
|
| 69 |
print("Creating pinhole") |
| 70 |
pinhole_id = await async_add_pinhole(igd_device) |
| 71 |
print("Pinhole ID:", pinhole_id) |
| 72 |
|
| 73 |
await asyncio.sleep(5) |
| 74 |
|
| 75 |
print("Deleting pinhole") |
| 76 |
await async_del_pinhole(igd_device, pinhole_id) |
| 77 |
|
| 78 |
|
| 79 |
def main() -> None: |
| 80 |
"""Main.""" |
| 81 |
try: |
| 82 |
asyncio.run(async_main()) |
| 83 |
except KeyboardInterrupt: |
| 84 |
pass |
| 85 |
|
| 86 |
|
| 87 |
if __name__ == "__main__": |
| 88 |
main() |