From 3a18a4536cd98b12d960f6d850e2fe5014b4390d Mon Sep 17 00:00:00 2001 From: slashtechno <77907286+slashtechno@users.noreply.github.com> Date: Sun, 6 Aug 2023 18:25:55 -0400 Subject: [PATCH] Updated `main()` for `delete.py` and `upload.py` Also added information on whitelists to `README.md` --- README.md | 2 ++ src/__main__.py | 5 +++-- src/utils/delete.py | 11 +++++++---- src/utils/upload.py | 15 +++++++++------ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 811dbc3..69989aa 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,8 @@ The following command line flags can be used to set the Cloudflare credentials: * Cloudflare Token: `--token` / `-t` #### Passing blocklists Blocklists can be passed to the program via the command line flag `--blocklist` / `-b`. This flag can either point to a hosts file or a directory containing hosts files. If this flag is not passed, the program will look for a file or directory named `blocklists` in the current working directory. +# Passing whitelists +Whitelists can be passed to the program via the command line flag `--whitelist` / `-w`. This flag can either point to a hosts file or a directory containing hosts files. If this flag is not passed, then if a file or directory named `whitelists` exists in the current working directory, it will be used. Domains in this whitelist will be excluded from the blocklists. #### Uploading blocklists and creating a firewall policy To upload the blocklists to Cloudflare and create a firewall policy, use the `upload` subcommand. For example: diff --git a/src/__main__.py b/src/__main__.py index aaa789f..6f178db 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -1,5 +1,6 @@ # To run from the root project directory, run the following command: # python -m src.__main__ +# python -m src # also works because __main__ is the default module import argparse import os from pathlib import Path @@ -71,13 +72,13 @@ def main(): "--blocklists", "-b", help="Either a blocklist hosts file or a directory containing blocklist hosts files", # noqa E501 - default="blocklists", + default="blocklists", # Not really needed because the get_blocklists function will default to this # noqa: E501 ) upload_parser.add_argument( "--whitelists", "-w", help="Either a whitelist hosts file or a directory containing whitelist hosts files", # noqa E501 - default="whitelist.txt", # Need to change this so it's optional + default="whitelist.txt", # Not really needed because the apply_whitelists function will default to this # noqa: E501 ) # Add subcommand: delete delete_parser = subparsers.add_parser( diff --git a/src/utils/delete.py b/src/utils/delete.py index 159c8be..e1d2ec3 100644 --- a/src/utils/delete.py +++ b/src/utils/delete.py @@ -44,11 +44,14 @@ def delete_adblock_policy(policies: dict, account_id: str, token: str): def main(): - rules = utils.get_gateway_rules() - delete_adblock_policy(rules) - lists = utils.get_lists() + account_id = input("Enter your Cloudflare account ID: ") + token = input("Enter your Cloudflare API token: ") + + rules = utils.get_gateway_rules(account_id, token) + delete_adblock_policy(rules, account_id, token) + lists = utils.get_lists(account_id, token) lists = utils.filter_adblock_lists(lists) - delete_adblock_list(lists) + delete_adblock_list(lists, account_id, token) if __name__ == "__main__": diff --git a/src/utils/upload.py b/src/utils/upload.py index 2c73458..b5513a6 100644 --- a/src/utils/upload.py +++ b/src/utils/upload.py @@ -5,7 +5,7 @@ import requests from . import utils -def get_blocklists(hosts_path: str = None): +def get_blocklists(hosts_path: str = 'blocklists'): blocklists = [] hosts_path = pathlib.Path(hosts_path) if hosts_path.is_file(): @@ -18,7 +18,7 @@ def get_blocklists(hosts_path: str = None): return blocklists -def apply_whitelists(blocklists, whitelist: str = None): +def apply_whitelists(blocklists, whitelist: str = 'whitelists'): # If whitelist is a file, convert it to a list. # If whitelist is a directory, convert all files in it to a list and combine them. # If it does not exist, return the original blocklists @@ -102,14 +102,17 @@ def create_dns_policy(lists, account_id: str, token: str) -> None: def main(): + + account_id = input("Enter your Cloudflare account ID: ") + token = input("Enter your Cloudflare API token: ") + blocklists = get_blocklists() blocklists = apply_whitelists(blocklists) lists = split_list(blocklists) - upload_to_cloudflare(lists) - cloud_lists = utils.get_lists() + upload_to_cloudflare(lists, account_id, token) + cloud_lists = utils.get_lists(account_id, token) cloud_lists = utils.filter_adblock_lists(cloud_lists) - create_dns_policy(cloud_lists) - + create_dns_policy(cloud_lists, account_id, token) if __name__ == "__main__": main()