improve get_external_ip
This commit is contained in:
parent
7b32e2ff7c
commit
e9efd9f68a
5 changed files with 22 additions and 24 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in a new issue