2023-08-01 16:58:52 +01:00
|
|
|
# To run from the root project directory, run the following command:
|
|
|
|
# python -m src.__main__
|
2023-08-06 23:25:55 +01:00
|
|
|
# python -m src # also works because __main__ is the default module
|
2023-08-01 04:39:12 +01:00
|
|
|
import argparse
|
|
|
|
import os
|
2023-08-01 06:06:22 +01:00
|
|
|
from pathlib import Path
|
2023-08-06 22:55:19 +01:00
|
|
|
from sys import exit, stderr
|
|
|
|
|
|
|
|
import dotenv
|
|
|
|
from loguru import logger
|
|
|
|
|
|
|
|
from .utils import delete, upload, utils
|
2023-08-01 06:06:22 +01:00
|
|
|
|
|
|
|
TOKEN = None
|
|
|
|
ACCOUNT_ID = None
|
2023-08-01 04:39:12 +01:00
|
|
|
|
2023-08-01 18:16:31 +01:00
|
|
|
|
2023-08-01 16:43:03 +01:00
|
|
|
def main():
|
2023-08-01 06:06:22 +01:00
|
|
|
# Parse arguments
|
|
|
|
|
2023-08-01 04:39:12 +01:00
|
|
|
# Set up argparse
|
2023-08-01 06:06:22 +01:00
|
|
|
argparser = argparse.ArgumentParser(
|
2023-08-01 18:16:31 +01:00
|
|
|
prog="Cloudflare Gateway Adblocking",
|
|
|
|
description="Serverless adblocking via Cloudflare Zero Trust Gateway",
|
|
|
|
epilog=":)",
|
2023-08-01 06:06:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Add argument groups
|
2023-08-01 18:16:31 +01:00
|
|
|
credential_args = argparser.add_argument_group("Cloudflare Credentials")
|
2023-08-01 06:06:22 +01:00
|
|
|
|
2023-08-01 04:39:12 +01:00
|
|
|
# Add arguments
|
2023-08-07 13:25:55 +01:00
|
|
|
|
|
|
|
# General arguments
|
2023-08-08 15:43:09 +01:00
|
|
|
argparser.add_argument(
|
|
|
|
"--log-level", "-l", help="Log level", default="INFO"
|
|
|
|
) # noqa E501
|
2023-08-07 13:25:55 +01:00
|
|
|
|
|
|
|
# Credential arguments
|
2023-08-01 06:06:22 +01:00
|
|
|
credential_args.add_argument(
|
2023-08-01 04:39:12 +01:00
|
|
|
"--account-id",
|
|
|
|
"-a",
|
|
|
|
help="Cloudflare account ID - environment variable: CLOUDFLARE_ACCOUNT_ID",
|
|
|
|
)
|
2023-08-01 18:16:31 +01:00
|
|
|
credential_args.add_argument(
|
|
|
|
"--token",
|
|
|
|
"-t",
|
|
|
|
help="Cloudflare API token - environment variable: CLOUDFLARE_TOKEN",
|
2023-08-01 04:39:12 +01:00
|
|
|
)
|
2023-08-01 06:06:22 +01:00
|
|
|
|
|
|
|
# Add subcommands
|
|
|
|
subparsers = argparser.add_subparsers(
|
|
|
|
title="subcommands",
|
|
|
|
description="",
|
|
|
|
help="Subcommands to preform operations",
|
2023-08-01 18:16:31 +01:00
|
|
|
dest="subcommand",
|
2023-08-01 06:06:22 +01:00
|
|
|
)
|
|
|
|
# Add subcommand: upload
|
|
|
|
upload_parser = subparsers.add_parser(
|
2023-08-01 18:16:31 +01:00
|
|
|
"upload", help="Upload adblock lists to Cloudflare"
|
2023-08-01 06:06:22 +01:00
|
|
|
)
|
|
|
|
upload_parser.set_defaults(func=upload_to_cloudflare)
|
|
|
|
upload_parser.add_argument(
|
|
|
|
"--blocklists",
|
|
|
|
"-b",
|
2023-08-01 18:16:31 +01:00
|
|
|
help="Either a blocklist hosts file or a directory containing blocklist hosts files", # noqa E501
|
2023-08-06 23:26:16 +01:00
|
|
|
default="blocklists", # Not really needed because the get_blocklists function will default to this # noqa: E501
|
2023-08-01 06:06:22 +01:00
|
|
|
)
|
|
|
|
upload_parser.add_argument(
|
|
|
|
"--whitelists",
|
|
|
|
"-w",
|
2023-08-01 18:20:18 +01:00
|
|
|
help="Either a whitelist hosts file or a directory containing whitelist hosts files", # noqa E501
|
2023-08-06 23:25:55 +01:00
|
|
|
default="whitelist.txt", # Not really needed because the apply_whitelists function will default to this # noqa: E501
|
2023-08-01 06:06:22 +01:00
|
|
|
)
|
|
|
|
# Add subcommand: delete
|
|
|
|
delete_parser = subparsers.add_parser(
|
2023-08-01 18:16:31 +01:00
|
|
|
"delete", help="Delete adblock lists from Cloudflare"
|
2023-08-01 06:06:22 +01:00
|
|
|
)
|
|
|
|
delete_parser.set_defaults(func=delete_from_cloudflare)
|
|
|
|
|
2023-08-01 04:39:12 +01:00
|
|
|
args = argparser.parse_args()
|
2023-08-01 06:06:22 +01:00
|
|
|
|
2023-08-07 13:25:55 +01:00
|
|
|
# Set up logging
|
|
|
|
set_primary_logger(args.log_level)
|
2023-08-01 04:39:12 +01:00
|
|
|
logger.debug(args)
|
|
|
|
|
|
|
|
# Load variables
|
2023-08-01 06:06:22 +01:00
|
|
|
global TOKEN
|
|
|
|
global ACCOUNT_ID
|
2023-08-01 04:39:12 +01:00
|
|
|
TOKEN = args.token
|
|
|
|
ACCOUNT_ID = args.account_id
|
2023-08-07 13:25:55 +01:00
|
|
|
|
|
|
|
# Check if credentials are set, if they are not, attempt to load from environment variables and .env # noqa E501
|
2023-08-01 04:39:12 +01:00
|
|
|
if TOKEN is None or ACCOUNT_ID is None:
|
2023-08-07 13:25:55 +01:00
|
|
|
logger.info("No credentials provided with flags")
|
|
|
|
if Path(".env").is_file():
|
|
|
|
logger.debug("Loading .env")
|
|
|
|
dotenv.load_dotenv()
|
|
|
|
else:
|
|
|
|
logger.debug("No .env file found")
|
|
|
|
try:
|
|
|
|
TOKEN = os.environ["CLOUDFLARE_TOKEN"]
|
|
|
|
ACCOUNT_ID = os.environ["CLOUDFLARE_ACCOUNT_ID"]
|
|
|
|
logger.info("Loaded credentials from environment variables")
|
|
|
|
except KeyError:
|
|
|
|
logger.error("No credentials provided")
|
|
|
|
argparser.print_help()
|
|
|
|
exit(1)
|
|
|
|
|
2023-08-01 18:16:31 +01:00
|
|
|
try:
|
2023-08-01 16:43:03 +01:00
|
|
|
args.func(args)
|
2023-08-07 00:02:40 +01:00
|
|
|
except AttributeError:
|
2023-08-06 21:29:07 +01:00
|
|
|
logger.error("No subcommand specified")
|
2023-08-01 16:43:03 +01:00
|
|
|
argparser.print_help()
|
2023-08-06 21:29:07 +01:00
|
|
|
exit(1)
|
2023-08-01 04:39:12 +01:00
|
|
|
|
2023-08-01 18:16:31 +01:00
|
|
|
|
2023-08-01 06:06:22 +01:00
|
|
|
def upload_to_cloudflare(args):
|
|
|
|
logger.info("Uploading to Cloudflare")
|
2023-08-01 17:18:24 +01:00
|
|
|
blocklists = upload.get_blocklists(args.blocklists)
|
|
|
|
blocklists = upload.apply_whitelists(blocklists, args.whitelists)
|
|
|
|
lists = upload.split_list(blocklists)
|
|
|
|
upload.upload_to_cloudflare(lists, ACCOUNT_ID, TOKEN)
|
|
|
|
cloud_lists = utils.get_lists(ACCOUNT_ID, TOKEN)
|
|
|
|
cloud_lists = utils.filter_adblock_lists(cloud_lists)
|
|
|
|
upload.create_dns_policy(cloud_lists, ACCOUNT_ID, TOKEN)
|
2023-08-01 18:16:31 +01:00
|
|
|
|
|
|
|
|
2023-08-01 06:06:22 +01:00
|
|
|
def delete_from_cloudflare(args):
|
|
|
|
logger.info("Deleting from Cloudflare")
|
2023-08-01 17:18:24 +01:00
|
|
|
rules = utils.get_gateway_rules(ACCOUNT_ID, TOKEN)
|
|
|
|
delete.delete_adblock_policy(rules, ACCOUNT_ID, TOKEN)
|
|
|
|
lists = utils.get_lists(ACCOUNT_ID, TOKEN)
|
|
|
|
lists = utils.filter_adblock_lists(lists)
|
|
|
|
delete.delete_adblock_list(lists, ACCOUNT_ID, TOKEN)
|
2023-08-01 04:39:12 +01:00
|
|
|
|
2023-08-08 15:43:09 +01:00
|
|
|
|
2023-08-07 13:25:55 +01:00
|
|
|
def set_primary_logger(log_level):
|
|
|
|
logger.remove()
|
|
|
|
# ^10 is a formatting directive to center with a padding of 10
|
|
|
|
logger_format = "<green>{time:YYYY-MM-DD HH:mm:ss}</green> |<level>{level: ^10}</level>| <level>{message}</level>" # noqa E501
|
|
|
|
logger.add(stderr, format=logger_format, colorize=True, level=log_level)
|
|
|
|
|
2023-08-01 18:16:31 +01:00
|
|
|
|
2023-08-01 04:39:12 +01:00
|
|
|
if __name__ == "__main__":
|
2023-08-01 16:43:03 +01:00
|
|
|
main()
|