jtriley / pystun

A Python STUN client for getting NAT type and external IP
https://github.com/jtriley/pystun
MIT License
436 stars 154 forks source link

Fixed multiple bugs with pystun and added async support #36

Open robertsdotpm opened 1 year ago

robertsdotpm commented 1 year ago

Hello everyone,

Over the past year I've been working on a new p2p networking library in Python. The software makes heavy use of STUN and uses this library as the basis for NAT enumeration, address lookups, and mapping queries. The software is great. But presently there are a few bugs in the NAT test logic and validation code. The socket code is also 'blocking' so if it's used in a Python program it will prevent anything else from running.

I've fixed these issues and added support for a large number of fallback STUN servers. My code fully supports IPv6 and TCP, too.

Here's a usage example:

Start the REPR with await support python3 -m asyncio

from p2pd import *

netifaces = await init_p2pd()

# Load default interface.
i = await Interface() 

# Do STUN calls.
s = STUNClient(interface=i)
wan_ip = await s.get_wan_ip()
mapping = await s.get_mapping(TCP, do_close=1)
nat_info = await s.get_nat_info()

If you print nat_info you'll notice it also includes information on the NATs port allocation behaviour and any observed patterns. The other functions work similarly to pystun. You can also call await i.load_nat() to have the interface load it's associated NAT information.

More information on async network programming can be found at p2pd.net.