def empty(cur): """ remove all block lists""" utils.danger("\n\tThis will REMOVE ALL blocklists!\n") if prompts.confirm("Are you sure?", "n"): cur.execute("DELETE FROM adlist") return True return False
def empty(cur): """ remove all block lists""" utils.danger(""" This will REMOVE ALL manually added allowlists! You probably DO NOT want to do this! """) if prompts.confirm("Are you sure?", "n"): cur.execute("DELETE FROM domainlist WHERE type in (0,2)") return True return False
def remove(cur): """ remove lists we added """ utils.info(""" This will try to remove blocklists added by this tool. Removal is done based on the comment for each list. If you've never changed any comments or used other tools, this is 100% safe. """) if prompts.confirm("Are you sure?", "n"): cur.execute( "DELETE FROM domainlist WHERE comment LIKE '%AndeepND |%' OR comment LIKE '%[ph5lt]'" ) return True return False
def add(cur): """ prompt for and process blocklists """ source = prompts.ask_blocklist() utils.warn_long_running() import_list = [] if source in blockLists: url_source = blockLists[source] resp = requests.get(url_source["url"]) import_list = utils.process_lines(resp.text, url_source["comment"]) if source == constants.FILE: fname = prompts.ask_import_file() import_file = open(fname) import_list = utils.process_lines(import_file.read(), f"File: {fname}") if source == constants.PASTE: import_list = prompts.ask_paste() import_list = utils.process_lines(import_list, "Pasted content") if len(import_list) == 0: utils.die("No valid urls found, try again") if not prompts.confirm(f"Add {len(import_list)} block lists?"): return False added = 0 exists = 0 for item in import_list: cur.execute("SELECT COUNT(*) FROM adlist WHERE address = ?", (item["url"], )) cnt = cur.fetchone() if cnt[0] > 0: exists += 1 else: added += 1 vals = (item["url"], item["comment"] + " [ph5lt]") cur.execute( "INSERT OR IGNORE INTO adlist (address, comment) VALUES (?,?)", vals) utils.success(f"{added} block lists added! {exists} already existed.") return True
def reset(cur): """ reset block lists to pihole install default """ utils.info("\nThis will replace ALL blocklists with these defaults:") for url in DEFAULT_LISTS: utils.info(" - " + url) print() if prompts.confirm("Are you sure?", "n"): cur.execute("DELETE FROM adlist") for url in DEFAULT_LISTS: vals = (url, "Pi-hole defaults") cur.execute( "INSERT OR IGNORE INTO adlist (address, comment) VALUES (?,?)", vals) return True return False
def update_gravity(use_docker): """ various ways of updating the gravity db """ if prompts.confirm("Update Gravity for immediate effect?"): print() if use_docker: os.system('docker exec pihole bash "/usr/local/bin/pihole" "-g"') else: os.system("pihole -g") else: print() if use_docker: utils.info( "Update Gravity through the web interface or by running:\n\t" + '# docker exec pihole bash "/usr/local/bin/pihole" "-g"' ) else: utils.info( "Update Gravity through the web interface or by running:\n\t# pihole -g" )
def main(): """main method""" conn = None try: utils.clear() banner.display() use_docker = False docker = utils.find_docker() if docker[0] is True: utils.success(f"+ Found Running Docker config: {docker[1]}") use_docker = prompts.confirm("Use Docker-ized config?", "n") if use_docker: db_file = docker[1] if not use_docker: print() db_file = prompts.ask_db() # ask_db validates the db, pass this connection round for easy access & "global" mgmt conn = sqlite3.connect(db_file) cur = conn.cursor() default = constants.BLOCKLIST option = "" any_save = False while option != constants.EXIT: stats.stat_bar(cur) option = prompts.main_menu(default) save = False if option == constants.BLOCKLIST: save = blocklists.manage_blocklists(cur) if option == constants.ALLOWLIST: save = allowlists.manage_allowlists(cur) if option == constants.STATS: stats.header(cur) if save: any_save = True default = constants.EXIT conn.commit() if option == constants.ALLOWLIST: stats.allow_header(cur) if option == constants.BLOCKLIST: stats.block_header(cur) if prompts.confirm("Are you finished?"): break conn.close() if any_save: update_gravity(use_docker) utils.info("\n\tBye!\n") except (KeyboardInterrupt, KeyError): if conn: conn.close() sys.exit(0)
def add(cur): """ prompt for and process allowlists """ source = prompts.ask_allowlist() utils.warn_long_running() import_list = [] if source in whiteLists: url_source = whiteLists[source] resp = requests.get(url_source["url"]) import_list = utils.process_lines(resp.text, url_source["comment"], False) # This breaks if we add a new whitelist setup if source != ANUDEEP_ALLOWLIST: resp = requests.get(ANUDEEP_ALLOWLIST) import_list += utils.process_lines(resp.text, url_source["comment"], False) if source == constants.FILE: fname = prompts.ask_import_file() import_file = open(fname) import_list = utils.process_lines(import_file.read(), f"File: {fname}", False) if source == constants.PASTE: import_list = prompts.ask_paste() import_list = utils.process_lines(import_list, "Pasted content", utils.validate_host) if len(import_list) == 0: utils.die("No valid urls found, try again") if not prompts.confirm(f"Add {len(import_list)} white lists?"): return False added = 0 exists = 0 for item in import_list: cur.execute("SELECT COUNT(*) FROM domainlist WHERE domain = ?", (item["url"], )) cnt = cur.fetchone() if cnt[0] > 0: exists += 1 else: # 0 = exact whitelist # 2 = regex whitelist domain_type = 0 if item["type"] == constants.REGEX: domain_type = 2 vals = (item["url"], domain_type, item["comment"] + " [ph5lt]") cur.execute( "INSERT OR IGNORE INTO domainlist (domain, type, comment) VALUES (?,?,?)", vals, ) added += 1 utils.success(f"{added} allowlists added! {exists} already existed.") return True