mirror of
https://github.com/HelloZeroNet/ZeroNet.git
synced 2023-12-14 04:33:03 +01:00
Rev1533, Add uPnP open protocols as parameter
This commit is contained in:
parent
8f6f6abb0e
commit
1849547133
3 changed files with 21 additions and 13 deletions
|
@ -8,7 +8,7 @@ class Config(object):
|
|||
|
||||
def __init__(self, argv):
|
||||
self.version = "0.4.1"
|
||||
self.rev = 1532
|
||||
self.rev = 1533
|
||||
self.argv = argv
|
||||
self.action = None
|
||||
self.config_file = "zeronet.conf"
|
||||
|
|
|
@ -69,7 +69,7 @@ class FileServer(ConnectionServer):
|
|||
|
||||
self.log.info("Trying to open port using UpnpPunch...")
|
||||
try:
|
||||
UpnpPunch.ask_to_open_port(self.port, 'ZeroNet', retries=3)
|
||||
UpnpPunch.ask_to_open_port(self.port, 'ZeroNet', retries=3, protos=["TCP"])
|
||||
except (UpnpPunch.UpnpError, UpnpPunch.IGDError) as err:
|
||||
self.log.error("UpnpPunch run error: %s" %
|
||||
Debug.formatException(err))
|
||||
|
@ -278,7 +278,7 @@ class FileServer(ConnectionServer):
|
|||
if self.running and self.port_opened:
|
||||
self.log.debug('Closing port %d' % self.port)
|
||||
try:
|
||||
UpnpPunch.ask_to_close_port(self.port)
|
||||
UpnpPunch.ask_to_close_port(self.port, protos=["TCP"])
|
||||
self.log.info('Closed port via upnp.')
|
||||
except (UpnpPunch.UpnpError, UpnpPunch.IGDError), err:
|
||||
self.log.info("Failed at attempt to use upnp to close port: %s" % err)
|
||||
|
|
|
@ -266,13 +266,13 @@ def _send_requests(messages, location, upnp_schema, control_path):
|
|||
raise UpnpError('Sending requests using UPnP failed.')
|
||||
|
||||
|
||||
def _orchestrate_soap_request(ip, port, msg_fn, desc=None):
|
||||
def _orchestrate_soap_request(ip, port, msg_fn, desc=None, protos=['TCP', 'UDP']):
|
||||
logging.debug("Trying using local ip: %s" % ip)
|
||||
idg_data = _collect_idg_data(ip)
|
||||
|
||||
soap_messages = [
|
||||
msg_fn(ip, port, desc, proto, idg_data['upnp_schema'])
|
||||
for proto in ['TCP']
|
||||
for proto in protos
|
||||
]
|
||||
|
||||
_send_requests(soap_messages, **idg_data)
|
||||
|
@ -281,7 +281,8 @@ def _orchestrate_soap_request(ip, port, msg_fn, desc=None):
|
|||
def _communicate_with_igd(port=15441,
|
||||
desc="UpnpPunch",
|
||||
retries=3,
|
||||
fn=_create_open_message):
|
||||
fn=_create_open_message,
|
||||
protos=["TCP", "UDP"]):
|
||||
"""
|
||||
Manage sending a message generated by 'fn'.
|
||||
"""
|
||||
|
@ -292,7 +293,7 @@ def _communicate_with_igd(port=15441,
|
|||
|
||||
for local_ip in local_ips:
|
||||
try:
|
||||
_orchestrate_soap_request(local_ip, port, fn, desc)
|
||||
_orchestrate_soap_request(local_ip, port, fn, desc, protos)
|
||||
success = True
|
||||
break
|
||||
except (UpnpError, IGDError) as e:
|
||||
|
@ -307,30 +308,37 @@ def _communicate_with_igd(port=15441,
|
|||
port, retries))
|
||||
|
||||
|
||||
def ask_to_open_port(port=15441, desc="UpnpPunch", retries=3):
|
||||
def ask_to_open_port(port=15441, desc="UpnpPunch", retries=3, protos=["TCP", "UDP"]):
|
||||
logging.debug("Trying to open port %d." % port)
|
||||
_communicate_with_igd(port=port,
|
||||
desc=desc,
|
||||
retries=retries,
|
||||
fn=_create_open_message)
|
||||
fn=_create_open_message,
|
||||
protos=protos)
|
||||
|
||||
|
||||
def ask_to_close_port(port=15441, desc="UpnpPunch", retries=3):
|
||||
def ask_to_close_port(port=15441, desc="UpnpPunch", retries=3, protos=["TCP", "UDP"]):
|
||||
logging.debug("Trying to close port %d." % port)
|
||||
# retries=1 because multiple successes cause 500 response and failure
|
||||
_communicate_with_igd(port=port,
|
||||
desc=desc,
|
||||
retries=1,
|
||||
fn=_create_close_message)
|
||||
fn=_create_close_message,
|
||||
protos=protos)
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from gevent import monkey
|
||||
monkey.patch_socket()
|
||||
logging.getLogger().setLevel(logging.DEBUG)
|
||||
import time
|
||||
|
||||
s = time.time()
|
||||
logging.getLogger().setLevel(logging.DEBUG)
|
||||
print ask_to_open_port(15441, "ZeroNet", retries=3)
|
||||
print "Opening port..."
|
||||
print ask_to_open_port(15443, "ZeroNet", retries=3, protos=["TCP"])
|
||||
print "Done in", time.time()-s
|
||||
|
||||
print "Closing port..."
|
||||
print ask_to_close_port(15443, "ZeroNet", retries=3, protos=["TCP"])
|
||||
print "Done in", time.time()-s
|
||||
|
|
Loading…
Reference in a new issue