def connect_to_specific_server(domain, openvpn, daemon, protocol): """Connect to a specific server :param domain: list of domains which holds one value like gb111 :param openvpn: string of options to pass-through to openvpn :param daemon: True if the openvpn shall be run in daemon mode :param protocol: may be one of 'udp' or 'tcp' :returns: True if openvpn was run successfully """ server = servers.get_server_by_domain(domain[0]) printer = Printer() if categories.has_category(server, "Obfuscated Servers"): if not printer.yes_no( ( "WARNING: {} is an obfuscated server.\n" "This may fail if not configured properly.\n" "Are you sure you want to continue?" ).format(server["domain"]) ): return False return run_openvpn(server, openvpn, daemon, protocol)
def connect( domain, countries_, areas_, features_, categories_, netflix, load_, match, daemon, openvpn, protocol, ): """High-level function to connect to a openvpn server. Filters servers and tries to connect to an openvpn server 'max_retries' times or else raise an exception. :param domain: list of domains. defaults to one value 'best'. :param countries_: list of countries :param areas_: list of areas :param features_: list of features :param categories_: list of categories :param netflix: True to filter netflix optimized servers :param load_: depending on match, filter by max, min or equal load servers :param match: may be 'max', 'min' or 'equal' :param daemon: True if openvpn shall run in daemon mode :param config_: the path to the openvpn configuration file :param openvpn: options to pass-through to openvpn as string :param protocol: may be 'udp' or 'tcp' :returns: True if running openvpn was successful :raises: ConnectError """ if "best" not in domain: return connect_to_specific_server(domain, openvpn, daemon, protocol) servers_ = servers.get_servers() servers_ = filter_servers( servers_, netflix, countries_, areas_, features_, categories_, load_, match ) best_servers = filter_best_servers(servers_) max_retries = 3 printer = Printer() for i, server in enumerate(best_servers): if i == max_retries: raise ConnectError("Maximum retries reached.") if categories.has_category(server, "Obfuscated Servers"): if not printer.yes_no( ( "WARNING: {} is an obfuscated server.\n" "This may fail if not configured properly.\n" "Are you sure you want to continue?" ).format(server["domain"]) ): continue if server["ping"] != inf: printer.info( "Trying to connect to {}: {} ms".format( server["domain"], server["ping"] ) ) printer.print_map(server["location"]["lat"], server["location"]["long"]) if run_openvpn(server, openvpn, daemon, protocol): return True # else give the next server a try else: raise ConnectError("No server left with a valid ping.") raise ConnectError("No server found to establish a connection.")