From 4dfb1ee77703d579bc6070381b31b5fad601ece5 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Fri, 26 Jan 2018 03:19:22 +0100 Subject: [PATCH] Move get_public_ip to an 'util' file --- src/yunohost/domain.py | 38 --------------------------- src/yunohost/utils/network.py | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 38 deletions(-) create mode 100644 src/yunohost/utils/network.py diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 727a63df3..19a5e55a7 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -30,8 +30,6 @@ import yaml import errno import requests -from urllib import urlopen - from moulinette import m18n, msettings from moulinette.core import MoulinetteError from moulinette.utils.log import getActionLogger @@ -260,42 +258,6 @@ def domain_url_available(auth, domain, path): return available -def get_public_ip(protocol=4): - """Retrieve the public IP address from ip.yunohost.org""" - if protocol == 4: - url = 'https://ip.yunohost.org' - elif protocol == 6: - url = 'https://ip6.yunohost.org' - else: - raise ValueError("invalid protocol version") - - try: - return urlopen(url).read().strip() - except IOError: - logger.debug('cannot retrieve public IPv%d' % protocol, exc_info=1) - raise MoulinetteError(errno.ENETUNREACH, - m18n.n('no_internet_connection')) - -def get_public_ips(): - """ - Retrieve the public IPv4 and v6 from ip. and ip6.yunohost.org - - Returns a 2-tuple (ipv4, ipv6). ipv4 or ipv6 can be None if they were not - found. - """ - - try: - ipv4 = get_public_ip() - except: - ipv4 = None - try: - ipv6 = get_public_ip(6) - except: - ipv6 = None - - return (ipv4, ipv6) - - def _get_maindomain(): with open('/etc/yunohost/current_host', 'r') as f: maindomain = f.readline().rstrip() diff --git a/src/yunohost/utils/network.py b/src/yunohost/utils/network.py new file mode 100644 index 000000000..902d99278 --- /dev/null +++ b/src/yunohost/utils/network.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- + +""" License + + Copyright (C) 2015 YUNOHOST.ORG + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program; if not, see http://www.gnu.org/licenses + +""" +import logging +from urllib import urlopen + +logger = logging.getLogger('yunohost.utils.network') + +def get_public_ip(protocol=4): + """Retrieve the public IP address from ip.yunohost.org""" + + if protocol == 4: + url = 'https://ip.yunohost.org' + elif protocol == 6: + url = 'https://ip6.yunohost.org' + else: + raise ValueError("invalid protocol version") + + try: + return urlopen(url).read().strip() + except IOError: + return None + + +def get_public_ips(): + + ipv4 = get_public_ip() + ipv6 = get_public_ip(6) + + return (ipv4, ipv6) +