STUN client for address lookups
STUN is a protocol that can be used to lookup a peer’s external address. It also provides information on port mappings and some servers with multiple addresses support the optional feature of receiving connections back from another address. Consequently, such servers allow one to determine the type of NAT you’re behind (if any.)
There are many public STUN servers that can be used for basic functionality. But very few support TCP, IPv6, or multiple addresses, and the ones that do are very unreliable and often experience downtime. P2PD uses STUN heavily to determine the routes that belong to interfaces and their NAT information. The only way to ensure the software worked reliability was to run a STUN server with multiple IPv4/6s on TCP and UDP.
Here is how to use the STUN client.
from p2pd import *
async def example():
i = await Interface().start()
nat = await i.load_nat()
print(i)
#
# Test echo server with AF.
af = i.supported()[0]
stun_client = (await get_stun_clients(af, 1, i, TCP))[0]
wan_ip = await stun_client.get_wan_ip()
ret = await stun_client.get_mapping()
print(wan_ip)
print(ret)
pipe = ret[-1]
await pipe.close()
if __name__ == '__main__':
async_test(example)