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-01 06:06:22 +01:00
|
|
|
from loguru import logger
|
|
|
|
# Import the utils package
|
2023-08-01 16:43:03 +01:00
|
|
|
from . import utils
|
2023-08-01 16:58:52 +01:00
|
|
|
# import utils
|
2023-08-01 06:06:22 +01:00
|
|
|
|
2023-08-01 04:39:12 +01:00
|
|
|
import argparse
|
|
|
|
import os
|
2023-08-01 06:06:22 +01:00
|
|
|
import dotenv
|
|
|
|
from sys import exit, stderr
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
TOKEN = None
|
|
|
|
ACCOUNT_ID = None
|
2023-08-01 04:39:12 +01:00
|
|
|
|
2023-08-01 16:43:03 +01:00
|
|
|
def main():
|
2023-08-01 06:06:22 +01:00
|
|
|
# Setup logging
|
|
|
|
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>" # pylint: disable=line-too-long
|
|
|
|
logger.add(stderr, format=logger_format, colorize=True, level="DEBUG")
|
|
|
|
|
|
|
|
|
|
|
|
# Load .env if it exists
|
|
|
|
# This must precede the argparse setup as os.environ is used in the default arguments
|
|
|
|
if Path(".env").is_file():
|
|
|
|
dotenv.load_dotenv()
|
|
|
|
logger.info("Loaded .env file")
|
|
|
|
else:
|
|
|
|
logger.info("No .env file found")
|
|
|
|
|
|
|
|
# Parse arguments
|
|
|
|
|
2023-08-01 04:39:12 +01:00
|
|
|
# Set up argparse
|
2023-08-01 06:06:22 +01:00
|
|
|
argparser = argparse.ArgumentParser(
|
|
|
|
prog='Cloudflare Gateway Adblocking',
|
|
|
|
description='Serverless adblocking via Cloudflare Zero Trust Gateway',
|
|
|
|
epilog=':)'
|
|
|
|
)
|
|
|
|
|
|
|
|
# Add argument groups
|
|
|
|
credential_args = argparser.add_argument_group('Cloudflare Credentials')
|
|
|
|
|
2023-08-01 04:39:12 +01:00
|
|
|
# Add 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",
|
|
|
|
default=os.environ.get("CLOUDFLARE_ACCOUNT_ID")
|
|
|
|
)
|
2023-08-01 06:06:22 +01:00
|
|
|
credential_args.add_argument('--token',
|
2023-08-01 04:39:12 +01:00
|
|
|
'-t',
|
|
|
|
help='Cloudflare API token - environment variable: CLOUDFLARE_TOKEN',
|
|
|
|
default=os.environ.get("CLOUDFLARE_TOKEN")
|
|
|
|
)
|
2023-08-01 06:06:22 +01:00
|
|
|
|
|
|
|
# Add subcommands
|
|
|
|
subparsers = argparser.add_subparsers(
|
|
|
|
title="subcommands",
|
|
|
|
description="",
|
|
|
|
help="Subcommands to preform operations",
|
|
|
|
dest="subcommand"
|
|
|
|
)
|
|
|
|
# Add subcommand: upload
|
|
|
|
upload_parser = subparsers.add_parser(
|
|
|
|
"upload",
|
|
|
|
help="Upload adblock lists to Cloudflare"
|
|
|
|
)
|
|
|
|
upload_parser.set_defaults(func=upload_to_cloudflare)
|
|
|
|
upload_parser.add_argument(
|
|
|
|
"--blocklists",
|
|
|
|
"-b",
|
|
|
|
help="Either a blocklist hosts file or a directory containing blocklist hosts files",
|
|
|
|
default="blocklists"
|
|
|
|
)
|
|
|
|
upload_parser.add_argument(
|
|
|
|
"--whitelists",
|
|
|
|
"-w",
|
|
|
|
# help="Either a whitelist hosts file or a directory containing whitelist hosts files"
|
|
|
|
help="Whitelist hosts file or directory",
|
|
|
|
default="whitelist.txt" # Need to change this so it's optional
|
|
|
|
)
|
|
|
|
# Add subcommand: delete
|
|
|
|
delete_parser = subparsers.add_parser(
|
|
|
|
"delete",
|
|
|
|
help="Delete adblock lists from Cloudflare"
|
|
|
|
)
|
|
|
|
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-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
|
|
|
|
# Check if variables are set
|
|
|
|
if TOKEN is None or ACCOUNT_ID is None:
|
|
|
|
logger.error("No environment variables found. Please create a .env file or .envrc file") # noqa E501
|
2023-08-01 06:06:22 +01:00
|
|
|
exit(1)
|
2023-08-01 16:43:03 +01:00
|
|
|
try :
|
|
|
|
args.func(args)
|
|
|
|
except AttributeError:
|
|
|
|
argparser.print_help()
|
2023-08-01 16:58:52 +01:00
|
|
|
pass
|
2023-08-01 04:39:12 +01:00
|
|
|
|
2023-08-01 06:06:22 +01:00
|
|
|
def upload_to_cloudflare(args):
|
|
|
|
logger.info("Uploading to Cloudflare")
|
2023-08-01 16:58:52 +01:00
|
|
|
blocklists = utils.upload.get_blocklists(args.blocklists)
|
2023-08-01 06:08:55 +01:00
|
|
|
blocklists = utils.upload.apply_whitelists(blocklists, args.whitelists)
|
|
|
|
lists = utils.upload.split_list(blocklists)
|
|
|
|
utils.upload.upload_to_cloudflare(lists, ACCOUNT_ID, TOKEN)
|
2023-08-01 06:06:22 +01:00
|
|
|
cloud_lists = utils.utils.get_lists(ACCOUNT_ID, TOKEN)
|
|
|
|
cloud_lists = utils.utils.filter_adblock_lists(cloud_lists)
|
2023-08-01 06:08:55 +01:00
|
|
|
utils.upload.create_dns_policy(cloud_lists, ACCOUNT_ID, TOKEN)
|
2023-08-01 06:06:22 +01:00
|
|
|
def delete_from_cloudflare(args):
|
|
|
|
logger.info("Deleting from Cloudflare")
|
|
|
|
rules = utils.utils.get_gateway_rules(ACCOUNT_ID, TOKEN)
|
2023-08-01 16:43:03 +01:00
|
|
|
utils.delete.delete_adblock_policy(rules, ACCOUNT_ID, TOKEN)
|
2023-08-01 06:06:22 +01:00
|
|
|
lists = utils.utils.get_lists(ACCOUNT_ID, TOKEN)
|
|
|
|
lists = utils.utils.filter_adblock_lists(lists)
|
2023-08-01 16:43:03 +01:00
|
|
|
utils.delete.delete_adblock_list(lists, ACCOUNT_ID, TOKEN)
|
2023-08-01 04:39:12 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-08-01 16:43:03 +01:00
|
|
|
main()
|