def get_gateways(kls, eipconfig, providerconfig): """ Return the selected gateways for a given provider, looking at the EIP config file. :param eipconfig: eip configuration object :type eipconfig: EIPConfig :param providerconfig: provider specific configuration :type providerconfig: ProviderConfig :rtype: list """ gateways = [] settings = Settings() domain = providerconfig.get_domain() gateway_conf = settings.get_selected_gateway(domain) gateway_selector = VPNGatewaySelector(eipconfig) if gateway_conf == GATEWAY_AUTOMATIC: gateways = gateway_selector.get_gateways() else: gateways = [gateway_conf] if not gateways: logger.error('No gateway was found!') raise VPNLauncherException('No gateway was found!') logger.debug("Using gateways ips: {0}".format(', '.join(gateways))) return gateways
def test_get_gateway_with_no_locations(self): gateway_selector = VPNGatewaySelector(self.eipconfig) self.eipconfig.get_gateways = Mock( return_value=sample_gateways_no_location) self.eipconfig.get_locations = Mock(return_value=[]) gateways = gateway_selector.get_gateways() gateways_default_order = [ sample_gateways[0]['ip_address'], sample_gateways[1]['ip_address'], sample_gateways[2]['ip_address'] ] self.assertEqual(gateways, gateways_default_order)
def get_gateways(kls, eipconfig, providerconfig): """ Return a list with the selected gateways for a given provider, looking at the EIP config file. Each item of the list is a tuple containing (gateway, port). :param eipconfig: eip configuration object :type eipconfig: EIPConfig :param providerconfig: provider specific configuration :type providerconfig: ProviderConfig :rtype: list """ gateways = [] settings = Settings() domain = providerconfig.get_domain() gateway_conf = settings.get_selected_gateway(domain) gateway_selector = VPNGatewaySelector(eipconfig) if gateway_conf == GATEWAY_AUTOMATIC: gws = gateway_selector.get_gateways() else: gws = [gateway_conf] if not gws: logger.error('No gateway was found!') raise VPNLauncherException('No gateway was found!') for idx, gw in enumerate(gws): ports = eipconfig.get_gateway_ports(idx) the_port = "1194" # default port # pick the port preferring this order: for port in kls.PREFERRED_PORTS: if port in ports: the_port = port break else: continue gateways.append((gw, the_port)) logger.debug("Using gateways (ip, port): {0!r}".format(gateways)) return gateways
def get_gateways(kls, eipconfig, providerconfig): """ Return the selected gateways for a given provider, looking at the EIP config file. :param eipconfig: eip configuration object :type eipconfig: EIPConfig :param providerconfig: provider specific configuration :type providerconfig: ProviderConfig :rtype: list """ gateways = [] leap_settings = LeapSettings() domain = providerconfig.get_domain() gateway_conf = leap_settings.get_selected_gateway(domain) gateway_selector = VPNGatewaySelector(eipconfig) if gateway_conf == leap_settings.GATEWAY_AUTOMATIC: gateways = gateway_selector.get_gateways() else: gateways = [gateway_conf] if not gateways: logger.error('No gateway was found!') raise VPNLauncherException('No gateway was found!') # this only works for selecting the first gateway, as we're # currently doing. ccodes = gateway_selector.get_gateways_country_code() gateway_ccode = ccodes[gateways[0]] flags.CURRENT_VPN_COUNTRY = gateway_ccode logger.debug("Using gateways ips: {0}".format(', '.join(gateways))) return gateways
def test_get_no_gateways(self): gateway_selector = VPNGatewaySelector(self.eipconfig) self.eipconfig.get_gateways = Mock(return_value=[]) gateways = gateway_selector.get_gateways() self.assertEqual(gateways, [])
def test_correct_order_gmt_plus_14(self): gateway_selector = VPNGatewaySelector(self.eipconfig, 14) gateways = gateway_selector.get_gateways() self.assertEqual(gateways, [ips[4], ips[2], ips[3], ips[1]])
def get_vpn_command( self, eipconfig=None, providerconfig=None, socket_host=None, socket_port="9876", openvpn_verb=1 ): """ Returns the platform dependant vpn launching command. It will look for openvpn in the regular paths and algo in path_prefix/apps/eip/ (in case standalone is set) Might raise VPNException. :param eipconfig: eip configuration object :type eipconfig: EIPConfig :param providerconfig: provider specific configuration :type providerconfig: ProviderConfig :param socket_host: either socket path (unix) or socket IP :type socket_host: str :param socket_port: either string "unix" if it's a unix socket, or port otherwise :type socket_port: str :param openvpn_verb: the openvpn verbosity wanted :type openvpn_verb: int :return: A VPN command ready to be launched :rtype: list """ leap_assert(eipconfig, "We need an eip config") leap_assert_type(eipconfig, EIPConfig) leap_assert(providerconfig, "We need a provider config") leap_assert_type(providerconfig, ProviderConfig) leap_assert(socket_host, "We need a socket host!") leap_assert(socket_port, "We need a socket port!") leap_assert(socket_port != "unix", "We cannot use unix sockets in windows!") openvpn_possibilities = which( self.OPENVPN_BIN, path_extension=os.path.join(providerconfig.get_path_prefix(), "..", "apps", "eip") ) if len(openvpn_possibilities) == 0: raise OpenVPNNotFoundException() openvpn = first(openvpn_possibilities) args = [] args += ["--setenv", "LEAPOPENVPN", "1"] if openvpn_verb is not None: args += ["--verb", "%d" % (openvpn_verb,)] gateways = [] leap_settings = LeapSettings(ProviderConfig.standalone) domain = providerconfig.get_domain() gateway_conf = leap_settings.get_selected_gateway(domain) if gateway_conf == leap_settings.GATEWAY_AUTOMATIC: gateway_selector = VPNGatewaySelector(eipconfig) gateways = gateway_selector.get_gateways() else: gateways = [gateway_conf] if not gateways: logger.error("No gateway was found!") raise VPNLauncherException(self.tr("No gateway was found!")) logger.debug("Using gateways ips: {0}".format(", ".join(gateways))) for gw in gateways: args += ["--remote", gw, "1194", "udp"] args += [ "--client", "--dev", "tun", ############################################################## # persist-tun makes ping-restart fail because it leaves a # broken routing table ############################################################## # '--persist-tun', "--persist-key", "--tls-client", # We make it log to a file because we cannot attach to the # openvpn process' stdout since it's a process with more # privileges than we are "--log-append", "eip.log", "--remote-cert-tls", "server", ] openvpn_configuration = eipconfig.get_openvpn_configuration() for key, value in openvpn_configuration.items(): args += ["--%s" % (key,), value] ############################################################## # The down-root plugin fails in some situations, so we don't # drop privs for the time being ############################################################## # args += [ # '--user', getpass.getuser(), # #'--group', grp.getgrgid(os.getgroups()[-1]).gr_name # ] args += ["--management-signal", "--management", socket_host, socket_port, "--script-security", "2"] args += [ "--cert", eipconfig.get_client_cert_path(providerconfig), "--key", eipconfig.get_client_cert_path(providerconfig), "--ca", providerconfig.get_ca_cert_path(), ] logger.debug("Running VPN with command:") logger.debug("%s %s" % (openvpn, " ".join(args))) return [openvpn] + args
def get_vpn_command( self, eipconfig=None, providerconfig=None, socket_host=None, socket_port="unix", openvpn_verb=1 ): """ Returns the platform dependant vpn launching command Might raise VPNException. :param eipconfig: eip configuration object :type eipconfig: EIPConfig :param providerconfig: provider specific configuration :type providerconfig: ProviderConfig :param socket_host: either socket path (unix) or socket IP :type socket_host: str :param socket_port: either string "unix" if it's a unix socket, or port otherwise :type socket_port: str :param openvpn_verb: openvpn verbosity wanted :type openvpn_verb: int :return: A VPN command ready to be launched :rtype: list """ leap_assert(eipconfig, "We need an eip config") leap_assert_type(eipconfig, EIPConfig) leap_assert(providerconfig, "We need a provider config") leap_assert_type(providerconfig, ProviderConfig) leap_assert(socket_host, "We need a socket host!") leap_assert(socket_port, "We need a socket port!") if not self.maybe_kextloaded(): raise EIPNoTunKextLoaded kwargs = {} if ProviderConfig.standalone: kwargs["path_extension"] = os.path.join(providerconfig.get_path_prefix(), "..", "apps", "eip") openvpn_possibilities = which(self.OPENVPN_BIN, **kwargs) if len(openvpn_possibilities) == 0: raise OpenVPNNotFoundException() openvpn = first(openvpn_possibilities) args = [openvpn] args += ["--setenv", "LEAPOPENVPN", "1"] if openvpn_verb is not None: args += ["--verb", "%d" % (openvpn_verb,)] gateways = [] leap_settings = LeapSettings(ProviderConfig.standalone) domain = providerconfig.get_domain() gateway_conf = leap_settings.get_selected_gateway(domain) if gateway_conf == leap_settings.GATEWAY_AUTOMATIC: gateway_selector = VPNGatewaySelector(eipconfig) gateways = gateway_selector.get_gateways() else: gateways = [gateway_conf] if not gateways: logger.error("No gateway was found!") raise VPNLauncherException(self.tr("No gateway was found!")) logger.debug("Using gateways ips: {0}".format(", ".join(gateways))) for gw in gateways: args += ["--remote", gw, "1194", "udp"] args += [ "--client", "--dev", "tun", ############################################################## # persist-tun makes ping-restart fail because it leaves a # broken routing table ############################################################## # '--persist-tun', "--persist-key", "--tls-client", "--remote-cert-tls", "server", ] openvpn_configuration = eipconfig.get_openvpn_configuration() for key, value in openvpn_configuration.items(): args += ["--%s" % (key,), value] user = getpass.getuser() ############################################################## # The down-root plugin fails in some situations, so we don't # drop privs for the time being ############################################################## # args += [ # '--user', user, # '--group', grp.getgrgid(os.getgroups()[-1]).gr_name # ] if socket_port == "unix": args += ["--management-client-user", user] args += ["--management-signal", "--management", socket_host, socket_port, "--script-security", "2"] if _has_updown_scripts(self.UP_SCRIPT): args += ["--up", '"%s"' % (self.UP_SCRIPT,)] if _has_updown_scripts(self.DOWN_SCRIPT): args += ["--down", '"%s"' % (self.DOWN_SCRIPT,)] # should have the down script too if _has_updown_scripts(self.OPENVPN_DOWN_PLUGIN): args += [ ########################################################### # For the time being we are disabling the usage of the # down-root plugin, because it doesn't quite work as # expected (i.e. it doesn't run route -del as root # when finishing, so it fails to properly # restart/quit) ########################################################### # '--plugin', self.OPENVPN_DOWN_PLUGIN, # '\'%s\'' % self.DOWN_SCRIPT ] # we set user to be passed to the up/down scripts args += ["--setenv", "LEAPUSER", "%s" % (user,)] args += [ "--cert", eipconfig.get_client_cert_path(providerconfig), "--key", eipconfig.get_client_cert_path(providerconfig), "--ca", providerconfig.get_ca_cert_path(), ] command, cargs = self.get_cocoasudo_ovpn_cmd() cmd_args = cargs + args logger.debug("Running VPN with command:") logger.debug("%s %s" % (command, " ".join(cmd_args))) return [command] + cmd_args
def get_vpn_command( self, eipconfig=None, providerconfig=None, socket_host=None, socket_port="unix", openvpn_verb=1 ): """ Returns the platform dependant vpn launching command. It will look for openvpn in the regular paths and algo in path_prefix/apps/eip/ (in case standalone is set) Might raise: VPNLauncherException, OpenVPNNotFoundException. :param eipconfig: eip configuration object :type eipconfig: EIPConfig :param providerconfig: provider specific configuration :type providerconfig: ProviderConfig :param socket_host: either socket path (unix) or socket IP :type socket_host: str :param socket_port: either string "unix" if it's a unix socket, or port otherwise :type socket_port: str :param openvpn_verb: openvpn verbosity wanted :type openvpn_verb: int :return: A VPN command ready to be launched :rtype: list """ leap_assert(eipconfig, "We need an eip config") leap_assert_type(eipconfig, EIPConfig) leap_assert(providerconfig, "We need a provider config") leap_assert_type(providerconfig, ProviderConfig) leap_assert(socket_host, "We need a socket host!") leap_assert(socket_port, "We need a socket port!") kwargs = {} if ProviderConfig.standalone: kwargs["path_extension"] = os.path.join(providerconfig.get_path_prefix(), "..", "apps", "eip") openvpn_possibilities = which(self.OPENVPN_BIN, **kwargs) if len(openvpn_possibilities) == 0: raise OpenVPNNotFoundException() openvpn = first(openvpn_possibilities) args = [] pkexec = self.maybe_pkexec() if pkexec: args.append(openvpn) openvpn = first(pkexec) args += ["--setenv", "LEAPOPENVPN", "1"] if openvpn_verb is not None: args += ["--verb", "%d" % (openvpn_verb,)] gateways = [] leap_settings = LeapSettings(ProviderConfig.standalone) domain = providerconfig.get_domain() gateway_conf = leap_settings.get_selected_gateway(domain) if gateway_conf == leap_settings.GATEWAY_AUTOMATIC: gateway_selector = VPNGatewaySelector(eipconfig) gateways = gateway_selector.get_gateways() else: gateways = [gateway_conf] if not gateways: logger.error("No gateway was found!") raise VPNLauncherException(self.tr("No gateway was found!")) logger.debug("Using gateways ips: {0}".format(", ".join(gateways))) for gw in gateways: args += ["--remote", gw, "1194", "udp"] args += [ "--client", "--dev", "tun", ############################################################## # persist-tun makes ping-restart fail because it leaves a # broken routing table ############################################################## # '--persist-tun', "--persist-key", "--tls-client", "--remote-cert-tls", "server", ] openvpn_configuration = eipconfig.get_openvpn_configuration() for key, value in openvpn_configuration.items(): args += ["--%s" % (key,), value] ############################################################## # The down-root plugin fails in some situations, so we don't # drop privs for the time being ############################################################## # args += [ # '--user', getpass.getuser(), # '--group', grp.getgrgid(os.getgroups()[-1]).gr_name # ] if socket_port == "unix": # that's always the case for linux args += ["--management-client-user", getpass.getuser()] args += ["--management-signal", "--management", socket_host, socket_port, "--script-security", "2"] plugin_path = self.maybe_down_plugin() # If we do not have the down plugin neither in the bundle # nor in the system, we do not do updown scripts. The alternative # is leaving the user without the ability to restore dns and routes # to its original state. if plugin_path and _has_updown_scripts(self.UP_DOWN_PATH): args += [ "--up", self.UP_DOWN_PATH, "--down", self.UP_DOWN_PATH, ############################################################## # For the time being we are disabling the usage of the # down-root plugin, because it doesn't quite work as # expected (i.e. it doesn't run route -del as root # when finishing, so it fails to properly # restart/quit) ############################################################## # '--plugin', plugin_path, # '\'script_type=down %s\'' % self.UP_DOWN_PATH ] args += [ "--cert", eipconfig.get_client_cert_path(providerconfig), "--key", eipconfig.get_client_cert_path(providerconfig), "--ca", providerconfig.get_ca_cert_path(), ] logger.debug("Running VPN with command:") logger.debug("%s %s" % (openvpn, " ".join(args))) return [openvpn] + args
def test_correct_order_gmt(self): gateway_selector = VPNGatewaySelector(self.eipconfig, 0) gateways = gateway_selector.get_gateways() self.assertEqual(gateways, [ips[1], ips[3], ips[2], ips[4]])
def get_vpn_command(kls, eipconfig, providerconfig, socket_host, socket_port, openvpn_verb=1): """ Returns the platform dependant vpn launching command Might raise: OpenVPNNotFoundException, VPNLauncherException. :param eipconfig: eip configuration object :type eipconfig: EIPConfig :param providerconfig: provider specific configuration :type providerconfig: ProviderConfig :param socket_host: either socket path (unix) or socket IP :type socket_host: str :param socket_port: either string "unix" if it's a unix socket, or port otherwise :type socket_port: str :param openvpn_verb: the openvpn verbosity wanted :type openvpn_verb: int :return: A VPN command ready to be launched. :rtype: list """ leap_assert_type(eipconfig, EIPConfig) leap_assert_type(providerconfig, ProviderConfig) kwargs = {} if flags.STANDALONE: kwargs['path_extension'] = os.path.join( get_path_prefix(), "..", "apps", "eip") openvpn_possibilities = which(kls.OPENVPN_BIN, **kwargs) if len(openvpn_possibilities) == 0: raise OpenVPNNotFoundException() openvpn = first(openvpn_possibilities) args = [] args += [ '--setenv', "LEAPOPENVPN", "1", '--nobind' ] if openvpn_verb is not None: args += ['--verb', '%d' % (openvpn_verb,)] gateways = [] leap_settings = LeapSettings() domain = providerconfig.get_domain() gateway_conf = leap_settings.get_selected_gateway(domain) if gateway_conf == leap_settings.GATEWAY_AUTOMATIC: gateway_selector = VPNGatewaySelector(eipconfig) gateways = gateway_selector.get_gateways() else: gateways = [gateway_conf] if not gateways: logger.error('No gateway was found!') raise VPNLauncherException('No gateway was found!') logger.debug("Using gateways ips: {0}".format(', '.join(gateways))) for gw in gateways: args += ['--remote', gw, '1194', 'udp'] args += [ '--client', '--dev', 'tun', ############################################################## # persist-tun makes ping-restart fail because it leaves a # broken routing table ############################################################## # '--persist-tun', '--persist-key', '--tls-client', '--remote-cert-tls', 'server' ] openvpn_configuration = eipconfig.get_openvpn_configuration() for key, value in openvpn_configuration.items(): args += ['--%s' % (key,), value] user = getpass.getuser() ############################################################## # The down-root plugin fails in some situations, so we don't # drop privs for the time being ############################################################## # args += [ # '--user', user, # '--group', grp.getgrgid(os.getgroups()[-1]).gr_name # ] if socket_port == "unix": # that's always the case for linux args += [ '--management-client-user', user ] args += [ '--management-signal', '--management', socket_host, socket_port, '--script-security', '2' ] if kls.UP_SCRIPT is not None: if _has_updown_scripts(kls.UP_SCRIPT): args += [ '--up', '\"%s\"' % (kls.UP_SCRIPT,), ] if kls.DOWN_SCRIPT is not None: if _has_updown_scripts(kls.DOWN_SCRIPT): args += [ '--down', '\"%s\"' % (kls.DOWN_SCRIPT,) ] ########################################################### # For the time being we are disabling the usage of the # down-root plugin, because it doesn't quite work as # expected (i.e. it doesn't run route -del as root # when finishing, so it fails to properly # restart/quit) ########################################################### # if _has_updown_scripts(kls.OPENVPN_DOWN_PLUGIN): # args += [ # '--plugin', kls.OPENVPN_DOWN_ROOT, # '\'%s\'' % kls.DOWN_SCRIPT # for OSX # '\'script_type=down %s\'' % kls.DOWN_SCRIPT # for Linux # ] args += [ '--cert', eipconfig.get_client_cert_path(providerconfig), '--key', eipconfig.get_client_cert_path(providerconfig), '--ca', providerconfig.get_ca_cert_path() ] args += [ '--ping', '10', '--ping-restart', '30'] command_and_args = [openvpn] + args return command_and_args