def _remove_servers(name=DEFAULT_SUBSCRIBE, all_subs=False): all_servers = utils.read_as_json(SERVER_CONFIG) if all_subs: all_servers = dict() else: all_servers.pop(name, None) utils.write_to_json(all_servers, SERVER_CONFIG)
def _remove_subscribe(name=DEFAULT_SUBSCRIBE, all_subs=False): all_subscribes = utils.read_as_json(SUBSCRIBE_CONFIG) if all_subs: all_subscribes = dict() else: all_subscribes.pop(name, None) utils.write_to_json(all_subscribes, SUBSCRIBE_CONFIG)
def list_servers(name=DEFAULT_SUBSCRIBE, all_subs=False): all_servers = utils.read_as_json(SERVER_CONFIG) if not all_servers: click.echo("No servers found, please add and update subscribe first!") sys.exit(1) if all_subs: for name, nodes in all_servers.items(): _list_server(name, nodes) else: _list_server(name, all_servers[name])
def update_subscribe(name=DEFAULT_SUBSCRIBE, all_subs=False): all_subscribe = utils.read_as_json(SUBSCRIBE_CONFIG) if all_subs: _update_all_subscribe(all_subscribe) else: try: url = all_subscribe[name] except KeyError: click.echo("No subscribe named %s found!" % name) sys.exit(1) parser_subscribe(url, name=name) list_servers(name=name, all_subs=all_subs)
def parser_subscribe(url, name=DEFAULT_SUBSCRIBE): try: resp = requests.get(url) except requests.exceptions.ConnectionError as err: click.echo("Can't parse the url, please check your network or make" " sure you entered the correct URL!") sys.exit(1) server_links = base64.b64decode(resp.content).splitlines() servers = [] for index, server_link in enumerate(server_links): server_link = utils.byte2str(server_link).replace("vmess://", "") server_node = json.loads(base64.b64decode(server_link)) servers.append(server_node) all_servers = utils.read_as_json(SERVER_CONFIG) all_servers.update({name: servers}) utils.write_to_json(all_servers, SERVER_CONFIG)
def get_node(index, name=DEFAULT_SUBSCRIBE): all_servers = utils.read_as_json(SERVER_CONFIG) try: servers = all_servers[name] except KeyError: click.echo("No subscribe named %s found!" % name) sys.exit(1) try: utils.check_index(index) node = servers[index - 1] click.echo("switch to node:") utils.ping(name=name, index=index, all_servers=all_servers) except IndexError: click.echo("Invalid index: %s, please check it." % index) sys.exit(1) return node
def list_subscribe(): subscribe = utils.read_as_json(SUBSCRIBE_CONFIG) click.echo("Subscribes:") for name, url in subscribe.items(): click.echo("%s: %s" % (name, url))
def add_subscribe(url, name=DEFAULT_SUBSCRIBE): subscribe = utils.read_as_json(SUBSCRIBE_CONFIG) subscribe.update({name: url}) utils.write_to_json(subscribe, SUBSCRIBE_CONFIG) click.echo("Added subscribe: %s" % url)