Exemple #1
0
def process_blocklists(db_file):
    """ prompt for and process blocklists """
    source = inquirer.ask_blocklist()

    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 = inquirer.ask_import_file()
        import_file = open(fname)
        import_list = utils.process_lines(import_file, f"File: {fname}")

    if source == constants.PASTE:
        import_list = inquirer.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 inquirer.confirm(
            f"Add {len(import_list)} block lists to {db_file}?"):
        utils.warn("Nothing changed. Bye!")
        sys.exit(0)

    conn = sqlite3.connect(db_file)
    sqldb = conn.cursor()
    added = 0
    exists = 0
    for item in import_list:
        sqldb.execute("SELECT COUNT(*) FROM adlist WHERE address = ?",
                      (item["url"], ))

        cnt = sqldb.fetchone()

        if cnt[0] > 0:
            exists += 1
        else:
            added += 1
            vals = (item["url"], item["comment"])
            sqldb.execute(
                "INSERT OR IGNORE INTO adlist (address, comment) VALUES (?,?)",
                vals)
            conn.commit()

    sqldb.close()
    conn.close()

    utils.success(f"{added} block lists added! {exists} already existed.")
Exemple #2
0
def process_whitelists(db_file):
    """ prompt for and process blacklists """
    source = inquirer.ask_whitelist()

    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_WHITELIST:
            resp = requests.get(ANUDEEP_WHITELIST)
            import_list += utils.process_lines(resp.text, url_source['comment'], False)

    if source == constants.FILE:
        fname = inquirer.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 = inquirer.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 inquirer.confirm(f'Add {len(import_list)} white lists to {db_file}?'):
        utils.warn('Nothing changed. Bye!')
        sys.exit(0)

    conn = sqlite3.connect(db_file)
    sqldb = conn.cursor()
    added = 0
    exists = 0
    for item in import_list:
        sqldb.execute(
            "SELECT COUNT(*) FROM domainlist WHERE domain = ?",
            (item['url'],))

        cnt = sqldb.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'])
            sqldb.execute(
                'INSERT OR IGNORE INTO domainlist (domain, type, comment) VALUES (?,?,?)', vals)
            conn.commit()
            added += 1

    sqldb.close()
    conn.close()

    utils.success(f'{added} whitelists added! {exists} already existed.')