def jwt_init(username, password): """ initialize through the user name and password, write jwt to the local configuration file, the expiration time is about 12 hours, so it is recommended to initialize through the api key. :param username: str, login zoomeye account :param password: str, login zoomeye account password :return: """ file.check_exist(zoomeye_dir) try: zoom = ZoomEye(username=username, password=password) access_token = zoom.login() except Exception: return jwt_file = zoomeye_dir + "/jwt" if access_token: file.write_file(jwt_file, access_token) show.printf("successfully initialized", color="green") # change the permission of the configuration file to read-only os.chmod(jwt_file, 0o600) # display the remaining resources of the current account user_data = zoom.resources_info() show.printf("Role: {}".format(user_data["plan"])) show.printf("Quota: {}".format(user_data["resources"].get("search"))) else: show.printf("failed initialized!", color="red")
def save(self, data): # folder is exists? if not os.path.isdir(self.cache_folder): try: # create folder when folder does not exists os.makedirs(self.cache_folder) except OSError: raise Exception("init dirs failed {}".format(self.cache_folder)) # write to local file.write_file(self.filename, data)
def cache_data(self, history_data): """ save ip history data to local :param history_data: dict, ip history data """ try: # write data # if file path not exists file.write_file(self.cache_path, json.dumps(history_data)) except FileNotFoundError: # create fold # retry write to local file os.makedirs(os.path.expanduser(config.ZOOMEYE_CACHE_PATH)) file.write_file(self.cache_path, json.dumps(history_data)) except Exception as e: show.printf("unknown error: {}".format(e)) exit(0)
def save(self, fields): """ save the data to a local json file, you cannot specify the save path, but you can specify the save data :param fields: str, filter fields, ex: app=xxxx :return: """ # -save default, data format ex: # {"total":xxx, "matches":[{...}, {...}, {...}...], "facets":{{...}, {...}...}} # filter special characters in file names name = re.findall(r"[a-zA-Z0-9_\u4e00-\u9fa5]+", self.dork) re_name = '_'.join(name) if fields == 'all': filename = "{}_{}_{}.json".format(re_name, self.num, int(time.time())) data = { 'total': self.total, 'matches': self.dork_data[:self.num], 'facets': self.facet_data } file.write_file(filename, json.dumps(data)) show.printf("save file to {}/{} successful!".format( os.getcwd(), filename), color='green') # -save xx=xxxx, save the filtered data. data format ex: # {app:"xxx", "app":"httpd",.....} else: key, value = self.cli_filter(fields, save=True) filename = "{}_{}_{}.json".format(re_name, len(value), int(time.time())) # parser data for v in value: dict_save = {} for index in range(len(key.split(','))): dict_key = key.split(',')[index] dict_value = v[index] dict_save[dict_key] = dict_value # write to local file file.add_to_file(filename, str(dict_save)) show.printf("save file to {}/{} successful!".format( os.getcwd(), filename), color='green')
def key_init(key): """ initialize through the api key, write the api key to the local configuration file, theoretically it will never expire unless you remake the api key :param key: user input API key :return: """ file.check_exist(zoomeye_dir) key = key.strip() try: zoom = ZoomEye(api_key=key) except Exception: return # api key save path key_file = zoomeye_dir + "/apikey" # save api key file.write_file(key_file, key) show.printf("successfully initialized", color="green") # change the permission of the configuration file to read-only os.chmod(key_file, 0o600) # display the remaining resources of the current account user_data = zoom.resources_info() show.printf("Role: {}".format(user_data["plan"])) show.printf("Quota: {}".format(user_data["resources"].get("search")))