STUN client for address lookups
STUN is a protocol that can be used to lookup a computers IP address. It provides information on external port mappings and some servers also support being able to send replies back from different IP addresses. These servers are very important [for peer-to-peer networking] because they allow for the determination of any NATs used by a home router.
There are many public STUN servers that can be used for basic functionality. P2PD uses STUN to determine WAN IPs, NAT details, and port mappings. Here is how to use the STUN client.
from p2pd import *
# NOTE: Servers may change IPs!
# Try use IP4 for af with another IP if it doesn't work.
async def example():
# Load default interface.
nic = await Interface()
dest = ("stun.hot-chilli.net", 3478)
# Use the first address family support for your NIC.
af = nic.supported()[0]
# Load STUN client.
client = STUNClient(af, dest, nic, proto=UDP, mode=RFC3489)
# Make some BIND requests.
wan_ip = await client.get_wan_ip()
ret = await client.get_mapping()
print(wan_ip)
print(ret)
if __name__ == '__main__':
async_test(example)