From e9efd9f68a150870429f6f6077093506ad8abe84 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Wed, 7 Nov 2018 18:35:32 -0500 Subject: [PATCH] improve get_external_ip --- lbrynet/extras/cli.py | 2 +- lbrynet/extras/daemon/Components.py | 25 ++++++++++++++++++------- lbrynet/extras/daemon/DaemonControl.py | 2 +- lbrynet/system_info.py | 15 +-------------- tests/unit/test_cli.py | 2 +- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lbrynet/extras/cli.py b/lbrynet/extras/cli.py index 78721617e..91ebdb447 100644 --- a/lbrynet/extras/cli.py +++ b/lbrynet/extras/cli.py @@ -140,7 +140,7 @@ def main(argv=None): elif method in ['version', '--version', '-v']: print("{lbrynet_name} {lbrynet_version}".format( - lbrynet_name=lbrynet_name, **get_platform(get_ip=False) + lbrynet_name=lbrynet_name, **get_platform() )) return 0 diff --git a/lbrynet/extras/daemon/Components.py b/lbrynet/extras/daemon/Components.py index 15158c3ae..cbcf06ed5 100644 --- a/lbrynet/extras/daemon/Components.py +++ b/lbrynet/extras/daemon/Components.py @@ -2,6 +2,7 @@ import os import asyncio import logging import treq +import json import math import binascii from hashlib import sha256 @@ -11,7 +12,7 @@ import lbrynet.schema from aioupnp import __version__ as aioupnp_version from aioupnp.upnp import UPnP from aioupnp.fault import UPnPError -from lbrynet import conf, system_info +from lbrynet import conf from lbrynet.utils import DeferredDict, generate_id from lbrynet.p2p.PaymentRateManager import OnlyFreePaymentsManager from lbrynet.p2p.RateLimiter import RateLimiter @@ -93,9 +94,19 @@ class ConfigSettings: return conf.settings.node_id @staticmethod - def get_external_ip(): - platform = system_info.get_platform(get_ip=True) - return platform['ip'] + @defer.inlineCallbacks + def get_external_ip(): # used if upnp is disabled or non-functioning + try: + buf = [] + response = yield treq.get("https://api.lbry.io/ip") + yield treq.collect(response, buf.append) + parsed = json.loads(b"".join(buf).decode()) + if parsed['success']: + return parsed['data']['ip'] + return + except Exception as err: + return + # Shorthand for common ConfigSettings methods @@ -429,7 +440,7 @@ class DHTComponent(Component): external_ip = self.upnp_component.external_ip if not external_ip: log.warning("UPnP component failed to get external ip") - external_ip = CS.get_external_ip() + external_ip = yield CS.get_external_ip() if not external_ip: log.warning("failed to get external ip") @@ -705,7 +716,7 @@ class UPnPComponent(Component): if external_ip == "0.0.0.0" or not external_ip: log.warning("unable to get external ip from UPnP, checking lbry.io fallback") - external_ip = CS.get_external_ip() + external_ip = yield CS.get_external_ip() if self.external_ip and self.external_ip != external_ip: log.info("external ip changed from %s to %s", self.external_ip, external_ip) self.external_ip = external_ip @@ -761,7 +772,7 @@ class UPnPComponent(Component): def start(self): log.info("detecting external ip") if not self.use_upnp: - self.external_ip = CS.get_external_ip() + self.external_ip = yield CS.get_external_ip() return success = False yield self._maintain_redirects() diff --git a/lbrynet/extras/daemon/DaemonControl.py b/lbrynet/extras/daemon/DaemonControl.py index 1cb0a8a4a..8542c86c4 100644 --- a/lbrynet/extras/daemon/DaemonControl.py +++ b/lbrynet/extras/daemon/DaemonControl.py @@ -53,7 +53,7 @@ def start(argv=None, conf_path=None): conf.settings.update({'use_auth_http': args.useauth}, data_types=(conf.TYPE_CLI,)) if args.version: - version = system_info.get_platform(get_ip=False) + version = system_info.get_platform() version['installation_id'] = conf.settings.installation_id print(utils.json_dumps_pretty(version)) return diff --git a/lbrynet/system_info.py b/lbrynet/system_info.py index 1c1e3c83a..7e42c25b8 100644 --- a/lbrynet/system_info.py +++ b/lbrynet/system_info.py @@ -1,9 +1,6 @@ import platform -import json import os -from six.moves.urllib import request -from six.moves.urllib.error import URLError from lbrynet.schema import __version__ as schema_version from lbrynet import build_type, __version__ as lbrynet_version import logging.handlers @@ -15,7 +12,7 @@ def get_lbrynet_version() -> str: return lbrynet_version -def get_platform(get_ip: bool = True) -> dict: +def get_platform() -> dict: p = { "processor": platform.processor(), "python_version": platform.python_version(), @@ -34,14 +31,4 @@ def get_platform(get_ip: bool = True) -> dict: except ModuleNotFoundError: pass - # TODO: remove this from get_platform and add a get_external_ip function using treq - if get_ip: - try: - response = json.loads(request.urlopen("https://api.lbry.io/ip").read()) - if not response['success']: - raise URLError("failed to get external ip") - p['ip'] = response['data']['ip'] - except (URLError, AssertionError): - p['ip'] = "Could not determine IP" - return p diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index a3b49047c..267dcbc15 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -67,7 +67,7 @@ class CLITest(unittest.TestCase): main(['version']) self.assertEqual( actual_output.getvalue().strip(), - "lbrynet {lbrynet_version}".format(**get_platform(get_ip=False)) + "lbrynet {lbrynet_version}".format(**get_platform()) ) def test_invalid_command(self):