mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Move get_public_ip to an 'util' file
This commit is contained in:
parent
1c752db22b
commit
4dfb1ee777
2 changed files with 48 additions and 38 deletions
|
@ -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()
|
||||
|
|
48
src/yunohost/utils/network.py
Normal file
48
src/yunohost/utils/network.py
Normal file
|
@ -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)
|
||||
|
Loading…
Add table
Reference in a new issue