mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Merge pull request #424 from YunoHost/clean-get-public-ip
[fix] Simplify get_public_ip
This commit is contained in:
commit
fd3a17cc7e
6 changed files with 59 additions and 72 deletions
|
@ -44,6 +44,7 @@ from moulinette.core import MoulinetteError
|
|||
from moulinette.utils.log import getActionLogger
|
||||
|
||||
import yunohost.domain
|
||||
from yunohost.utils.network import get_public_ip
|
||||
|
||||
from moulinette import m18n
|
||||
from yunohost.app import app_ssowatconf
|
||||
|
@ -809,7 +810,7 @@ def _backup_current_cert(domain):
|
|||
|
||||
|
||||
def _check_domain_is_ready_for_ACME(domain):
|
||||
public_ip = yunohost.domain.get_public_ip()
|
||||
public_ip = get_public_ip()
|
||||
|
||||
# Check if IP from DNS matches public IP
|
||||
if not _dns_ip_match_public_ip(public_ip, domain):
|
||||
|
@ -856,14 +857,9 @@ def _regen_dnsmasq_if_needed():
|
|||
"""
|
||||
Update the dnsmasq conf if some IPs are not up to date...
|
||||
"""
|
||||
try:
|
||||
ipv4 = yunohost.domain.get_public_ip()
|
||||
except:
|
||||
ipv4 = None
|
||||
try:
|
||||
ipv6 = yunohost.domain.get_public_ip(6)
|
||||
except:
|
||||
ipv6 = None
|
||||
|
||||
ipv4 = get_public_ip()
|
||||
ipv6 = get_public_ip(6)
|
||||
|
||||
do_regen = False
|
||||
|
||||
|
|
|
@ -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
|
||||
|
@ -39,6 +37,7 @@ from moulinette.utils.log import getActionLogger
|
|||
import yunohost.certificate
|
||||
|
||||
from yunohost.service import service_regen_conf
|
||||
from yunohost.utils.network import get_public_ip
|
||||
|
||||
logger = getActionLogger('yunohost.domain')
|
||||
|
||||
|
@ -260,42 +259,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()
|
||||
|
@ -356,15 +319,8 @@ def _build_dns_conf(domain, ttl=3600):
|
|||
}
|
||||
"""
|
||||
|
||||
try:
|
||||
ipv4 = get_public_ip()
|
||||
except:
|
||||
ipv4 = None
|
||||
|
||||
try:
|
||||
ipv6 = get_public_ip(6)
|
||||
except:
|
||||
ipv6 = None
|
||||
ipv4 = get_public_ip()
|
||||
ipv6 = get_public_ip(6)
|
||||
|
||||
basic = []
|
||||
|
||||
|
|
|
@ -39,7 +39,8 @@ from moulinette.utils.log import getActionLogger
|
|||
from moulinette.utils.filesystem import read_file, write_to_file, rm
|
||||
from moulinette.utils.network import download_json
|
||||
|
||||
from yunohost.domain import get_public_ips, _get_maindomain, _build_dns_conf
|
||||
from yunohost.domain import _get_maindomain, _build_dns_conf
|
||||
from yunohost.utils.network import get_public_ip
|
||||
|
||||
logger = getActionLogger('yunohost.dyndns')
|
||||
|
||||
|
@ -193,7 +194,8 @@ def dyndns_update(dyn_host="dyndns.yunohost.org", domain=None, key=None,
|
|||
old_ipv6 = read_file(OLD_IPV6_FILE).rstrip()
|
||||
|
||||
# Get current IPv4 and IPv6
|
||||
(ipv4_, ipv6_) = get_public_ips()
|
||||
ipv4_ = get_public_ip()
|
||||
ipv6_ = get_public_ip(6)
|
||||
|
||||
if ipv4 is None:
|
||||
ipv4 = ipv4_
|
||||
|
|
|
@ -41,7 +41,8 @@ from moulinette import m18n
|
|||
from moulinette.core import MoulinetteError
|
||||
from moulinette.utils.log import getActionLogger
|
||||
|
||||
from yunohost.domain import get_public_ip, _get_maindomain
|
||||
from yunohost.utils.network import get_public_ip
|
||||
from yunohost.domain import _get_maindomain
|
||||
|
||||
logger = getActionLogger('yunohost.monitor')
|
||||
|
||||
|
@ -210,10 +211,7 @@ def monitor_network(units=None, human_readable=False):
|
|||
else:
|
||||
logger.debug('interface name %s was not found', iname)
|
||||
elif u == 'infos':
|
||||
try:
|
||||
p_ipv4 = get_public_ip()
|
||||
except:
|
||||
p_ipv4 = 'unknown'
|
||||
p_ipv4 = get_public_ip() or 'unknown'
|
||||
|
||||
l_ip = 'unknown'
|
||||
for name, addrs in devices.items():
|
||||
|
|
|
@ -45,12 +45,13 @@ from moulinette.utils.log import getActionLogger
|
|||
from moulinette.utils.process import check_output
|
||||
from moulinette.utils.filesystem import read_json, write_to_json
|
||||
from yunohost.app import app_fetchlist, app_info, app_upgrade, app_ssowatconf, app_list, _install_appslist_fetch_cron
|
||||
from yunohost.domain import domain_add, domain_list, get_public_ip, _get_maindomain, _set_maindomain
|
||||
from yunohost.domain import domain_add, domain_list, _get_maindomain, _set_maindomain
|
||||
from yunohost.dyndns import _dyndns_available, _dyndns_provides
|
||||
from yunohost.firewall import firewall_upnp
|
||||
from yunohost.service import service_status, service_regen_conf, service_log, service_start, service_enable
|
||||
from yunohost.monitor import monitor_disk, monitor_system
|
||||
from yunohost.utils.packages import ynh_packages_version
|
||||
from yunohost.utils.network import get_public_ip
|
||||
|
||||
# FIXME this is a duplicate from apps.py
|
||||
APPS_SETTING_PATH = '/etc/yunohost/apps/'
|
||||
|
@ -621,16 +622,11 @@ def tools_diagnosis(auth, private=False):
|
|||
# Private data
|
||||
if private:
|
||||
diagnosis['private'] = OrderedDict()
|
||||
|
||||
# Public IP
|
||||
diagnosis['private']['public_ip'] = {}
|
||||
try:
|
||||
diagnosis['private']['public_ip']['IPv4'] = get_public_ip(4)
|
||||
except MoulinetteError as e:
|
||||
pass
|
||||
try:
|
||||
diagnosis['private']['public_ip']['IPv6'] = get_public_ip(6)
|
||||
except MoulinetteError as e:
|
||||
pass
|
||||
diagnosis['private']['public_ip']['IPv4'] = get_public_ip(4)
|
||||
diagnosis['private']['public_ip']['IPv6'] = get_public_ip(6)
|
||||
|
||||
# Domains
|
||||
diagnosis['private']['domains'] = domain_list(auth)['domains']
|
||||
|
|
39
src/yunohost/utils/network.py
Normal file
39
src/yunohost/utils/network.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
""" License
|
||||
|
||||
Copyright (C) 2017 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
|
Loading…
Add table
Reference in a new issue