Skip to content

Commit

Permalink
Merge pull request ZeroNetX#218 from caryoscelus/upnp-anonymous
Browse files Browse the repository at this point in the history
more anonymous upnp
  • Loading branch information
caryoscelus authored Jul 25, 2023
2 parents db78b2a + f94765f commit dd5976a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
5 changes: 1 addition & 4 deletions src/Peer/PeerPortchecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ def requestUrl(self, url, post_data=None):
return urllib.request.urlopen(req, timeout=20.0)

def portOpen(self, port):
# self.log.info("Not trying to open port using UpnpPunch until it's proven robust...")
# return False

try:
UpnpPunch.ask_to_open_port(port, 'ZeroNet', retries=3, protos=["TCP"])
UpnpPunch.ask_to_open_port(port, retries=3, protos=["TCP"])
self.upnp_port_opened = True
except Exception as err:
self.log.warning("UpnpPunch run error: %s" % Debug.formatException(err))
Expand Down
17 changes: 6 additions & 11 deletions src/util/UpnpPunch.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ def method3():

def _create_open_message(local_ip,
port,
description="UPnPPunch",
protocol="TCP",
upnp_schema='WANIPConnection'):
"""
Expand All @@ -205,14 +204,13 @@ def _create_open_message(local_ip,
</s:Envelope>""".format(port=port,
protocol=protocol,
host_ip=local_ip,
description=description,
description='',
upnp_schema=upnp_schema)
return (REMOVE_WHITESPACE.sub('><', soap_message), 'AddPortMapping')


def _create_close_message(local_ip,
port,
description=None,
protocol='TCP',
upnp_schema='WANIPConnection'):
soap_message = """<?xml version="1.0"?>
Expand Down Expand Up @@ -294,20 +292,19 @@ 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, protos=("TCP", "UDP")):
def _orchestrate_soap_request(ip, port, msg_fn, protos=("TCP", "UDP")):
logger.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'])
msg_fn(ip, port, proto, idg_data['upnp_schema'])
for proto in protos
]

_send_requests(soap_messages, **idg_data)


def _communicate_with_igd(port=15441,
desc="UpnpPunch",
retries=3,
fn=_create_open_message,
protos=("TCP", "UDP")):
Expand All @@ -321,7 +318,7 @@ def _communicate_with_igd(port=15441,
def job(local_ip):
for retry in range(retries):
try:
_orchestrate_soap_request(local_ip, port, fn, desc, protos)
_orchestrate_soap_request(local_ip, port, fn, protos)
return True
except Exception as e:
logger.debug('Upnp request using "{0}" failed: {1}'.format(local_ip, e))
Expand Down Expand Up @@ -357,20 +354,18 @@ def job(local_ip):
return success


def ask_to_open_port(port=15441, desc="UpnpPunch", retries=3, protos=("TCP", "UDP")):
def ask_to_open_port(port=15441, retries=3, protos=("TCP", "UDP")):
logger.debug("Trying to open port %d." % port)
return _communicate_with_igd(port=port,
desc=desc,
retries=retries,
fn=_create_open_message,
protos=protos)


def ask_to_close_port(port=15441, desc="UpnpPunch", retries=3, protos=("TCP", "UDP")):
def ask_to_close_port(port=15441, retries=3, protos=("TCP", "UDP")):
logger.debug("Trying to close port %d." % port)
# retries=1 because multiple successes cause 500 response and failure
return _communicate_with_igd(port=port,
desc=desc,
retries=retries,
fn=_create_close_message,
protos=protos)
Expand Down

0 comments on commit dd5976a

Please sign in to comment.