Updated `main()` for `delete.py` and `upload.py`

Also added information on whitelists to `README.md`
This commit is contained in:
slashtechno 2023-08-06 18:25:55 -04:00
parent 7cad46cf8a
commit 3a18a4536c
Signed by: slashtechno
GPG Key ID: 8EC1D9D9286C2B17
4 changed files with 21 additions and 12 deletions

View File

@ -34,6 +34,8 @@ The following command line flags can be used to set the Cloudflare credentials:
* Cloudflare Token: `--token` / `-t` * Cloudflare Token: `--token` / `-t`
#### Passing blocklists #### 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. 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 #### Uploading blocklists and creating a firewall policy
To upload the blocklists to Cloudflare and create a firewall policy, use the `upload` subcommand. To upload the blocklists to Cloudflare and create a firewall policy, use the `upload` subcommand.
For example: For example:

View File

@ -1,5 +1,6 @@
# To run from the root project directory, run the following command: # To run from the root project directory, run the following command:
# python -m src.__main__ # python -m src.__main__
# python -m src # also works because __main__ is the default module
import argparse import argparse
import os import os
from pathlib import Path from pathlib import Path
@ -71,13 +72,13 @@ def main():
"--blocklists", "--blocklists",
"-b", "-b",
help="Either a blocklist hosts file or a directory containing blocklist hosts files", # noqa E501 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( upload_parser.add_argument(
"--whitelists", "--whitelists",
"-w", "-w",
help="Either a whitelist hosts file or a directory containing whitelist hosts files", # noqa E501 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 # Add subcommand: delete
delete_parser = subparsers.add_parser( delete_parser = subparsers.add_parser(

View File

@ -44,11 +44,14 @@ def delete_adblock_policy(policies: dict, account_id: str, token: str):
def main(): def main():
rules = utils.get_gateway_rules() account_id = input("Enter your Cloudflare account ID: ")
delete_adblock_policy(rules) token = input("Enter your Cloudflare API token: ")
lists = utils.get_lists()
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) lists = utils.filter_adblock_lists(lists)
delete_adblock_list(lists) delete_adblock_list(lists, account_id, token)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -5,7 +5,7 @@ import requests
from . import utils from . import utils
def get_blocklists(hosts_path: str = None): def get_blocklists(hosts_path: str = 'blocklists'):
blocklists = [] blocklists = []
hosts_path = pathlib.Path(hosts_path) hosts_path = pathlib.Path(hosts_path)
if hosts_path.is_file(): if hosts_path.is_file():
@ -18,7 +18,7 @@ def get_blocklists(hosts_path: str = None):
return blocklists 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 file, convert it to a list.
# If whitelist is a directory, convert all files in it to a list and combine them. # 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 # 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(): def main():
account_id = input("Enter your Cloudflare account ID: ")
token = input("Enter your Cloudflare API token: ")
blocklists = get_blocklists() blocklists = get_blocklists()
blocklists = apply_whitelists(blocklists) blocklists = apply_whitelists(blocklists)
lists = split_list(blocklists) lists = split_list(blocklists)
upload_to_cloudflare(lists) upload_to_cloudflare(lists, account_id, token)
cloud_lists = utils.get_lists() cloud_lists = utils.get_lists(account_id, token)
cloud_lists = utils.filter_adblock_lists(cloud_lists) cloud_lists = utils.filter_adblock_lists(cloud_lists)
create_dns_policy(cloud_lists) create_dns_policy(cloud_lists, account_id, token)
if __name__ == "__main__": if __name__ == "__main__":
main() main()