| 1 |
|
| 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 |
|
| 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_port_mapping(igd_device: IgdDevice) -> None: |
| 39 |
"""Add port mapping.""" |
| 40 |
external_ip_address = await igd_device.async_get_external_ip_address() |
| 41 |
if not external_ip_address: |
| 42 |
print("Could not get external IP address") |
| 43 |
sys.exit(1) |
| 44 |
|
| 45 |
remote_host = ipaddress.ip_address(external_ip_address) |
| 46 |
remote_host_ipv4 = cast(ipaddress.IPv4Address, remote_host) |
| 47 |
local_ip = ipaddress.ip_address(get_local_ip()) |
| 48 |
local_ip_ipv4 = cast(ipaddress.IPv4Address, local_ip) |
| 49 |
|
| 50 |
|
| 51 |
enabled = True |
| 52 |
mapping_name = "Bombsquad" |
| 53 |
await igd_device.async_add_port_mapping( |
| 54 |
remote_host=remote_host_ipv4, |
| 55 |
external_port=43210, |
| 56 |
internal_client=local_ip_ipv4, |
| 57 |
internal_port=43210, |
| 58 |
protocol="UDP", |
| 59 |
enabled=enabled, |
| 60 |
description=mapping_name, |
| 61 |
lease_duration=timedelta(seconds=7200), |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
async def async_del_port_mapping(igd_device: IgdDevice) -> None: |
| 66 |
"""Delete port mapping.""" |
| 67 |
external_ip_address = await igd_device.async_get_external_ip_address() |
| 68 |
if not external_ip_address: |
| 69 |
print("Could not get external IP address") |
| 70 |
sys.exit(1) |
| 71 |
|
| 72 |
remote_host = ipaddress.ip_address(external_ip_address) |
| 73 |
remote_host_ipv4 = cast(ipaddress.IPv4Address, remote_host) |
| 74 |
await igd_device.async_delete_port_mapping( |
| 75 |
remote_host=remote_host_ipv4, |
| 76 |
external_port=43210, |
| 77 |
protocol="UDP", |
| 78 |
) |
| 79 |
|
| 80 |
|
| 81 |
async def async_main() -> None: |
| 82 |
"""Async main.""" |
| 83 |
discoveries = await discover_igd_devices() |
| 84 |
print(f"Discoveries: {discoveries}") |
| 85 |
discovery = list(discoveries)[0] |
| 86 |
print(f"Using device at location: {discovery['location']}") |
| 87 |
igd_device = await build_igd_device(discovery) |
| 88 |
|
| 89 |
print("Creating port mapping") |
| 90 |
await async_add_port_mapping(igd_device) |
| 91 |
|
| 92 |
await asyncio.sleep(5) |
| 93 |
|
| 94 |
print("Deleting port mapping") |
| 95 |
await async_del_port_mapping(igd_device) |
| 96 |
|
| 97 |
|
| 98 |
def main() -> None: |
| 99 |
"""Main.""" |
| 100 |
try: |
| 101 |
asyncio.run(async_main()) |
| 102 |
except KeyboardInterrupt: |
| 103 |
pass |
| 104 |
|
| 105 |
|
| 106 |
if __name__ == "__main__": |
| 107 |
main() |