Fix in the refresh dead peers in registry + added info about hello interval
[re6stnet.git] / upnpigd.py
1 import miniupnpc
2 import socket
3
4 # return (address, port)
5 def ForwardViaUPnP(local_port, protos):
6 u = miniupnpc.UPnP()
7 u.discoverdelay = 200
8 u.discover()
9 u.selectigd()
10 external_port = 1000
11
12 while True:
13 if 'udp' in protos:
14 while u.getspecificportmapping(external_port, 'UDP') != None :
15 external_port += 1
16 if external_port == 65536:
17 raise Exception
18 if 'tcp-server' in protos:
19 while u.getspecificportmapping(external_port, 'TCP') != None :
20 external_port += 1
21 if external_port == 65536:
22 raise Exception
23
24 if 'udp' in protos:
25 u.addportmapping(external_port, 'UDP', u.lanaddr, local_port,
26 'Vifib openvpn server', '')
27 if 'tcp-server' in protos:
28 u.addportmapping(external_port, 'TCP', u.lanaddr, local_port,
29 'Vifib openvpn server', '')
30
31 print (u.externalipaddress(), external_port)
32 return (u.externalipaddress(), external_port)
33