From 6b7d575e9556183096e82c4d1fd739944cfecb53 Mon Sep 17 00:00:00 2001 From: Job Evers Date: Mon, 17 Oct 2016 20:00:24 -0500 Subject: [PATCH] use lbry.io to check for an internet connection --- lbrynet/core/utils.py | 25 ++++++++++++++++--- lbrynet/lbrynet_daemon/Daemon.py | 11 +------- lbrynet/lbrynet_daemon/DaemonControl.py | 11 ++------ packaging/osx/lbry-osx-app/lbrygui/LBRYApp.py | 11 +++----- .../windows/lbry-win32-app/LBRYWin32App.py | 12 +++------ 5 files changed, 31 insertions(+), 39 deletions(-) diff --git a/lbrynet/core/utils.py b/lbrynet/core/utils.py index f9f114233..9491c26fa 100644 --- a/lbrynet/core/utils.py +++ b/lbrynet/core/utils.py @@ -1,16 +1,22 @@ import base64 +import datetime import distutils.version +import logging +import json import random import os -import json +import socket import yaml -import datetime from lbrynet.core.cryptoutils import get_lbry_hash_obj + blobhash_length = get_lbry_hash_obj().digest_size * 2 # digest_size is in bytes, and blob hashes are hex encoded +log = logging.getLogger(__name__) + + def generate_id(num=None): h = get_lbry_hash_obj() if num is not None: @@ -81,4 +87,17 @@ def save_settings(path, settings): def today(): - return datetime.datetime.today() \ No newline at end of file + return datetime.datetime.today() + + +def check_connection(server="www.lbry.io", port=80): + """Attempts to open a socket to server:port and returns True if successful.""" + try: + host = socket.gethostbyname(server) + s = socket.create_connection((host, port), 2) + return True + except Exception as ex: + log.info( + "Failed to connect to %s:%s. Maybe the internet connection is not working", + server, port, exc_info=True) + return False diff --git a/lbrynet/lbrynet_daemon/Daemon.py b/lbrynet/lbrynet_daemon/Daemon.py index ded04a7d1..50528e2c8 100644 --- a/lbrynet/lbrynet_daemon/Daemon.py +++ b/lbrynet/lbrynet_daemon/Daemon.py @@ -5,7 +5,6 @@ import os import platform import random import re -import socket import string import subprocess import sys @@ -130,8 +129,6 @@ OK_CODE = 200 # TODO alert if your copy of a lbry file is out of date with the name record -REMOTE_SERVER = "www.google.com" - class Parameters(object): def __init__(self, **kwargs): @@ -599,13 +596,7 @@ class Daemon(jsonrpc.JSONRPC): self._events = analytics.Events(context, base58.b58encode(self.lbryid), self._session_id) def _check_network_connection(self): - try: - host = socket.gethostbyname(REMOTE_SERVER) - s = socket.create_connection((host, 80), 2) - self.connected_to_internet = True - except: - log.info("Internet connection not working") - self.connected_to_internet = False + self.connected_to_internet = utils.check_connection() def _check_lbrynet_connection(self): def _log_success(): diff --git a/lbrynet/lbrynet_daemon/DaemonControl.py b/lbrynet/lbrynet_daemon/DaemonControl.py index ca7041149..3c244b6eb 100644 --- a/lbrynet/lbrynet_daemon/DaemonControl.py +++ b/lbrynet/lbrynet_daemon/DaemonControl.py @@ -4,7 +4,6 @@ import logging.handlers import os import webbrowser import sys -import socket from appdirs import user_data_dir from twisted.web import server @@ -12,6 +11,7 @@ from twisted.internet import reactor, defer from jsonrpc.proxy import JSONRPCProxy from lbrynet.core import log_support +from lbrynet.core import utils from lbrynet.lbrynet_daemon.DaemonServer import DaemonServer from lbrynet.lbrynet_daemon.DaemonRequest import DaemonRequest from lbrynet.conf import API_CONNECTION_STRING, API_INTERFACE, API_PORT, \ @@ -30,19 +30,12 @@ lbrynet_log = os.path.join(log_dir, LOG_FILE_NAME) log = logging.getLogger(__name__) -REMOTE_SERVER = "www.google.com" - if getattr(sys, 'frozen', False) and os.name == "nt": os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(os.path.dirname(sys.executable), "cacert.pem") def test_internet_connection(): - try: - host = socket.gethostbyname(REMOTE_SERVER) - s = socket.create_connection((host, 80), 2) - return True - except: - return False + return utils.check_connection() def stop(): diff --git a/packaging/osx/lbry-osx-app/lbrygui/LBRYApp.py b/packaging/osx/lbry-osx-app/lbrygui/LBRYApp.py index c690023d9..acc5f13f6 100644 --- a/packaging/osx/lbry-osx-app/lbrygui/LBRYApp.py +++ b/packaging/osx/lbry-osx-app/lbrygui/LBRYApp.py @@ -31,22 +31,17 @@ from lbrynet.lbrynet_daemon.DaemonServer import DaemonServer from lbrynet.lbrynet_daemon.DaemonRequest import DaemonRequest from lbrynet.conf import API_PORT, API_INTERFACE, ICON_PATH, APP_NAME from lbrynet.conf import UI_ADDRESS +from lbrynet.core import utils + if platform.mac_ver()[0] >= "10.10": from LBRYNotify import LBRYNotify log = logging.getLogger(__name__) -REMOTE_SERVER = "www.google.com" - def test_internet_connection(): - try: - host = socket.gethostbyname(REMOTE_SERVER) - s = socket.create_connection((host, 80), 2) - return True - except: - return False + return utils.check_connection() class LBRYDaemonApp(AppKit.NSApplication): diff --git a/packaging/windows/lbry-win32-app/LBRYWin32App.py b/packaging/windows/lbry-win32-app/LBRYWin32App.py index 1a35e29b4..885fda283 100644 --- a/packaging/windows/lbry-win32-app/LBRYWin32App.py +++ b/packaging/windows/lbry-win32-app/LBRYWin32App.py @@ -18,6 +18,7 @@ try: except ImportError: import win32gui +from lbrynet.core import utils from lbrynet.lbrynet_daemon.DaemonServer import DaemonServer from lbrynet.lbrynet_daemon.DaemonRequest import DaemonRequest from lbrynet.conf import API_PORT, API_INTERFACE, ICON_PATH, APP_NAME @@ -36,16 +37,9 @@ log = logging.getLogger(__name__) if getattr(sys, 'frozen', False) and os.name == "nt": os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(os.path.dirname(sys.executable), "cacert.pem") -REMOTE_SERVER = "www.google.com" - def test_internet_connection(): - try: - host = socket.gethostbyname(REMOTE_SERVER) - s = socket.create_connection((host, 80), 2) - return True - except: - return False + return utils.check_connection() def non_string_iterable(obj): @@ -314,4 +308,4 @@ if __name__ == '__main__': if start_daemon: main(lbry_name) else: - LBRYURIHandler.open_address(lbry_name) \ No newline at end of file + LBRYURIHandler.open_address(lbry_name)