コード例 #1
0
def import_connection(file_path,
                      connection_name,
                      username=None,
                      password=None,
                      dns_list=None,
                      ipv6=False):
    try:
        # Create a temporary config with the new name, for importing (and delete afterwards)
        temp_path = os.path.join(os.path.dirname(file_path),
                                 connection_name + '.ovpn')
        shutil.copy(file_path, temp_path)

        output = subprocess.run([
            'nmcli', 'connection', 'import', 'type', 'openvpn', 'file',
            temp_path
        ],
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
        os.remove(temp_path)
        output.check_returncode()

        config = ConnectionConfig(connection_name)
        if config.path:  # If the config has a path, then it was loaded correctly
            if username and password:
                config.set_credentials(username, password)

            if dns_list:
                config.set_dns_nameservers(dns_list)

            if not ipv6:
                config.disable_ipv6()

            user = utils.get_current_user()
            config.set_user(user)

            config.save()
        else:
            return False

        return True

    except subprocess.CalledProcessError:
        error = utils.format_std_string(output.stderr)
        logger.error(error)
        return False

    except Exception as ex:
        logger.error(ex)
        return False
コード例 #2
0
import os
from nordnm import utils

__username__ = utils.get_current_user()

USER_HOME = os.path.expanduser('~' + __username__)
ROOT = os.path.join(USER_HOME, '.nordnm/')
OVPN_CONFIGS = os.path.join(ROOT, 'configs/')
CONFIG_INFO = os.path.join(OVPN_CONFIGS, '.info')
SETTINGS = os.path.join(ROOT, 'settings.conf')
ACTIVE_SERVERS = os.path.join(ROOT, '.active_servers')
CREDENTIALS = os.path.join(ROOT, 'credentials.conf')
MAC_CONFIG = "/usr/lib/NetworkManager/conf.d/nordnm_mac.conf"
AUTO_CONNECT_SCRIPT = "/etc/NetworkManager/dispatcher.d/nordnm_autoconnect_" + __username__
KILLSWITCH_SCRIPT = "/etc/NetworkManager/dispatcher.d/nordnm_killswitch_" + __username__
IPV6_SCRIPT = "/etc/NetworkManager/dispatcher.d/10_vpn_ipv6_" + __username__
SYSTEM_CONNECTIONS = "/etc/NetworkManager/system-connections/"
KILLSWITCH_DATA = os.path.join(ROOT, '.killswitch')

# Legacy paths for cleanly updating. These files are removed on startup, if found
LEGACY_FILES = [
    "/etc/NetworkManager/dispatcher.d/auto_vpn",
    "/etc/NetworkManager/dispatcher.d/killswitch_vpn",
    "/etc/NetworkManager/dispatcher.d/nordnm_dns_" + __username__
]
コード例 #3
0
def import_connection(file_path,
                      connection_name,
                      username=None,
                      password=None,
                      dns_list=None,
                      ipv6=False):
    def nmcli_import():
        try:
            # Create a temporary config with the connection name, so we can import the config with its prettified name
            temp_path = os.path.join(os.path.dirname(file_path),
                                     connection_name + '.ovpn')
            shutil.copy(file_path, temp_path)
        except Exception as ex:
            logger.error("Failed to copy configuration file: %s" % ex)
            return False

        try:
            output = subprocess.run([
                'nmcli', 'connection', 'import', 'type', 'openvpn', 'file',
                temp_path
            ],
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            os.remove(
                temp_path)  # Remove the temporary renamed config we created
            output.check_returncode()
        except subprocess.CalledProcessError:
            error = utils.format_std_string(output.stderr)
            logger.error("Could not add options to the connection: %s" % error)
        except Exception as ex:
            logger.error(ex)

        return False

    if not utils.run_as_root(nmcli_import):
        return False

    # Populate all connection options into connection_options
    connection_options = {
        '+vpn.secrets': ['password='******'+vpn.data': ['username='******'password-flags=0'],
        '+connection.permissions': ['user:'******'ipv6.method'] = ['ignore']

    if dns_list:
        dns_string = ';'.join(map(str, dns_list))
        connection_options['+ipv4.dns'] = [dns_string]
        connection_options['+ipv4.ignore-auto-dns'] = ['true']

    try:
        for location, values in connection_options.items():
            for value in values:
                output = subprocess.run([
                    'nmcli', 'connection', 'modify', connection_name, location,
                    value
                ],
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE)
                output.check_returncode()

        return True

    except subprocess.CalledProcessError:
        error = utils.format_std_string(output.stderr)
        logger.error("Could not add options to the connection: %s" % error)
        return False

    except Exception as ex:
        logger.error(ex)
        return False
コード例 #4
0
import os
from nordnm import utils

DIR_USERHOME = os.path.expanduser('~' + utils.get_current_user())
DIR_ROOT = os.path.join(DIR_USERHOME, '.nordnm/')
DIR_OVPN = os.path.join(DIR_ROOT, 'configs/')

SETTINGS = os.path.join(DIR_ROOT, 'settings.conf')
ACTIVE_SERVERS = os.path.join(DIR_ROOT, '.active_servers')
CREDENTIALS = os.path.join(DIR_ROOT, 'credentials.conf')
KILLSWITCH = os.path.join(DIR_ROOT, '.killswitch')