From 32f1cf588e97850ec8000e220774063f5fee0bd5 Mon Sep 17 00:00:00 2001
From: slashtechno <77907286+slashtechno@users.noreply.github.com>
Date: Tue, 1 Aug 2023 12:16:31 -0500
Subject: [PATCH] Allow directory to be passed as `--whitelist` Improve error
handling
---
.gitignore | 3 +-
poetry.lock | 120 +++++++++++++++++++++++++++++++++++++++++++-
pyproject.toml | 4 ++
src/__main__.py | 56 +++++++++++----------
src/utils/delete.py | 26 ++++++----
src/utils/upload.py | 36 ++++++++-----
src/utils/utils.py | 66 ++++++++++--------------
whitelist.txt | 0
8 files changed, 221 insertions(+), 90 deletions(-)
delete mode 100644 whitelist.txt
diff --git a/.gitignore b/.gitignore
index 56cf5bb..646d043 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,4 +6,5 @@ blocklists/*
!blocklists/.gitkeep
tmp.py
.venv
-dist/
\ No newline at end of file
+dist/
+.ruff_cache/
diff --git a/poetry.lock b/poetry.lock
index 3d4cf19..0a165e4 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -1,5 +1,50 @@
# This file is automatically @generated by Poetry 1.5.1 and should not be changed by hand.
+[[package]]
+name = "black"
+version = "23.7.0"
+description = "The uncompromising code formatter."
+optional = false
+python-versions = ">=3.8"
+files = [
+ {file = "black-23.7.0-cp310-cp310-macosx_10_16_arm64.whl", hash = "sha256:5c4bc552ab52f6c1c506ccae05681fab58c3f72d59ae6e6639e8885e94fe2587"},
+ {file = "black-23.7.0-cp310-cp310-macosx_10_16_universal2.whl", hash = "sha256:552513d5cd5694590d7ef6f46e1767a4df9af168d449ff767b13b084c020e63f"},
+ {file = "black-23.7.0-cp310-cp310-macosx_10_16_x86_64.whl", hash = "sha256:86cee259349b4448adb4ef9b204bb4467aae74a386bce85d56ba4f5dc0da27be"},
+ {file = "black-23.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:501387a9edcb75d7ae8a4412bb8749900386eaef258f1aefab18adddea1936bc"},
+ {file = "black-23.7.0-cp310-cp310-win_amd64.whl", hash = "sha256:fb074d8b213749fa1d077d630db0d5f8cc3b2ae63587ad4116e8a436e9bbe995"},
+ {file = "black-23.7.0-cp311-cp311-macosx_10_16_arm64.whl", hash = "sha256:b5b0ee6d96b345a8b420100b7d71ebfdd19fab5e8301aff48ec270042cd40ac2"},
+ {file = "black-23.7.0-cp311-cp311-macosx_10_16_universal2.whl", hash = "sha256:893695a76b140881531062d48476ebe4a48f5d1e9388177e175d76234ca247cd"},
+ {file = "black-23.7.0-cp311-cp311-macosx_10_16_x86_64.whl", hash = "sha256:c333286dc3ddca6fdff74670b911cccedacb4ef0a60b34e491b8a67c833b343a"},
+ {file = "black-23.7.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:831d8f54c3a8c8cf55f64d0422ee875eecac26f5f649fb6c1df65316b67c8926"},
+ {file = "black-23.7.0-cp311-cp311-win_amd64.whl", hash = "sha256:7f3bf2dec7d541b4619b8ce526bda74a6b0bffc480a163fed32eb8b3c9aed8ad"},
+ {file = "black-23.7.0-cp38-cp38-macosx_10_16_arm64.whl", hash = "sha256:f9062af71c59c004cd519e2fb8f5d25d39e46d3af011b41ab43b9c74e27e236f"},
+ {file = "black-23.7.0-cp38-cp38-macosx_10_16_universal2.whl", hash = "sha256:01ede61aac8c154b55f35301fac3e730baf0c9cf8120f65a9cd61a81cfb4a0c3"},
+ {file = "black-23.7.0-cp38-cp38-macosx_10_16_x86_64.whl", hash = "sha256:327a8c2550ddc573b51e2c352adb88143464bb9d92c10416feb86b0f5aee5ff6"},
+ {file = "black-23.7.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6d1c6022b86f83b632d06f2b02774134def5d4d4f1dac8bef16d90cda18ba28a"},
+ {file = "black-23.7.0-cp38-cp38-win_amd64.whl", hash = "sha256:27eb7a0c71604d5de083757fbdb245b1a4fae60e9596514c6ec497eb63f95320"},
+ {file = "black-23.7.0-cp39-cp39-macosx_10_16_arm64.whl", hash = "sha256:8417dbd2f57b5701492cd46edcecc4f9208dc75529bcf76c514864e48da867d9"},
+ {file = "black-23.7.0-cp39-cp39-macosx_10_16_universal2.whl", hash = "sha256:47e56d83aad53ca140da0af87678fb38e44fd6bc0af71eebab2d1f59b1acf1d3"},
+ {file = "black-23.7.0-cp39-cp39-macosx_10_16_x86_64.whl", hash = "sha256:25cc308838fe71f7065df53aedd20327969d05671bac95b38fdf37ebe70ac087"},
+ {file = "black-23.7.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:642496b675095d423f9b8448243336f8ec71c9d4d57ec17bf795b67f08132a91"},
+ {file = "black-23.7.0-cp39-cp39-win_amd64.whl", hash = "sha256:ad0014efc7acf0bd745792bd0d8857413652979200ab924fbf239062adc12491"},
+ {file = "black-23.7.0-py3-none-any.whl", hash = "sha256:9fd59d418c60c0348505f2ddf9609c1e1de8e7493eab96198fc89d9f865e7a96"},
+ {file = "black-23.7.0.tar.gz", hash = "sha256:022a582720b0d9480ed82576c920a8c1dde97cc38ff11d8d8859b3bd6ca9eedb"},
+]
+
+[package.dependencies]
+click = ">=8.0.0"
+mypy-extensions = ">=0.4.3"
+packaging = ">=22.0"
+pathspec = ">=0.9.0"
+platformdirs = ">=2"
+tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""}
+
+[package.extras]
+colorama = ["colorama (>=0.4.3)"]
+d = ["aiohttp (>=3.7.4)"]
+jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"]
+uvloop = ["uvloop (>=0.15.2)"]
+
[[package]]
name = "certifi"
version = "2023.7.22"
@@ -95,6 +140,20 @@ files = [
{file = "charset_normalizer-3.2.0-py3-none-any.whl", hash = "sha256:8e098148dd37b4ce3baca71fb394c81dc5d9c7728c95df695d2dca218edf40e6"},
]
+[[package]]
+name = "click"
+version = "8.1.6"
+description = "Composable command line interface toolkit"
+optional = false
+python-versions = ">=3.7"
+files = [
+ {file = "click-8.1.6-py3-none-any.whl", hash = "sha256:fa244bb30b3b5ee2cae3da8f55c9e5e0c0e86093306301fb418eb9dc40fbded5"},
+ {file = "click-8.1.6.tar.gz", hash = "sha256:48ee849951919527a045bfe3bf7baa8a959c423134e1a5b98c05c20ba75a1cbd"},
+]
+
+[package.dependencies]
+colorama = {version = "*", markers = "platform_system == \"Windows\""}
+
[[package]]
name = "colorama"
version = "0.4.6"
@@ -135,6 +194,54 @@ win32-setctime = {version = ">=1.0.0", markers = "sys_platform == \"win32\""}
[package.extras]
dev = ["Sphinx (==5.3.0)", "colorama (==0.4.5)", "colorama (==0.4.6)", "freezegun (==1.1.0)", "freezegun (==1.2.2)", "mypy (==v0.910)", "mypy (==v0.971)", "mypy (==v0.990)", "pre-commit (==3.2.1)", "pytest (==6.1.2)", "pytest (==7.2.1)", "pytest-cov (==2.12.1)", "pytest-cov (==4.0.0)", "pytest-mypy-plugins (==1.10.1)", "pytest-mypy-plugins (==1.9.3)", "sphinx-autobuild (==2021.3.14)", "sphinx-rtd-theme (==1.2.0)", "tox (==3.27.1)", "tox (==4.4.6)"]
+[[package]]
+name = "mypy-extensions"
+version = "1.0.0"
+description = "Type system extensions for programs checked with the mypy type checker."
+optional = false
+python-versions = ">=3.5"
+files = [
+ {file = "mypy_extensions-1.0.0-py3-none-any.whl", hash = "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d"},
+ {file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"},
+]
+
+[[package]]
+name = "packaging"
+version = "23.1"
+description = "Core utilities for Python packages"
+optional = false
+python-versions = ">=3.7"
+files = [
+ {file = "packaging-23.1-py3-none-any.whl", hash = "sha256:994793af429502c4ea2ebf6bf664629d07c1a9fe974af92966e4b8d2df7edc61"},
+ {file = "packaging-23.1.tar.gz", hash = "sha256:a392980d2b6cffa644431898be54b0045151319d1e7ec34f0cfed48767dd334f"},
+]
+
+[[package]]
+name = "pathspec"
+version = "0.11.2"
+description = "Utility library for gitignore style pattern matching of file paths."
+optional = false
+python-versions = ">=3.7"
+files = [
+ {file = "pathspec-0.11.2-py3-none-any.whl", hash = "sha256:1d6ed233af05e679efb96b1851550ea95bbb64b7c490b0f5aa52996c11e92a20"},
+ {file = "pathspec-0.11.2.tar.gz", hash = "sha256:e0d8d0ac2f12da61956eb2306b69f9469b42f4deb0f3cb6ed47b9cce9996ced3"},
+]
+
+[[package]]
+name = "platformdirs"
+version = "3.10.0"
+description = "A small Python package for determining appropriate platform-specific dirs, e.g. a \"user data dir\"."
+optional = false
+python-versions = ">=3.7"
+files = [
+ {file = "platformdirs-3.10.0-py3-none-any.whl", hash = "sha256:d7c24979f292f916dc9cbf8648319032f551ea8c49a4c9bf2fb556a02070ec1d"},
+ {file = "platformdirs-3.10.0.tar.gz", hash = "sha256:b45696dab2d7cc691a3226759c0d3b00c47c8b6e293d96f6436f733303f77f6d"},
+]
+
+[package.extras]
+docs = ["furo (>=2023.7.26)", "proselint (>=0.13)", "sphinx (>=7.1.1)", "sphinx-autodoc-typehints (>=1.24)"]
+test = ["appdirs (==1.4.4)", "covdefaults (>=2.3)", "pytest (>=7.4)", "pytest-cov (>=4.1)", "pytest-mock (>=3.11.1)"]
+
[[package]]
name = "python-dotenv"
version = "1.0.0"
@@ -196,6 +303,17 @@ files = [
{file = "ruff-0.0.281.tar.gz", hash = "sha256:bab2cdfa78754315cccc2b4d46ad6181aabb29e89747a3b135a4b85e11baa025"},
]
+[[package]]
+name = "tomli"
+version = "2.0.1"
+description = "A lil' TOML parser"
+optional = false
+python-versions = ">=3.7"
+files = [
+ {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"},
+ {file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"},
+]
+
[[package]]
name = "urllib3"
version = "2.0.4"
@@ -230,4 +348,4 @@ dev = ["black (>=19.3b0)", "pytest (>=4.6.2)"]
[metadata]
lock-version = "2.0"
python-versions = "^3.10"
-content-hash = "8737765a4f30f43f2314296861d56c04682f5632f54ecd7b2046b4d9591ed101"
+content-hash = "d1e33ec5def7b19f6fd09ac80725a9a9f24da3274b075ee37eb53fe2b2f54219"
diff --git a/pyproject.toml b/pyproject.toml
index 168894c..c94c322 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -5,6 +5,9 @@ description = "Serverless adblocking via Cloudflare Zero Trust Gateway"
authors = ["slastechno <77907286+slashtechno@users.noreply.github.com>"]
license = "MIT"
readme = "README.md"
+repository = "https://github.com/slashtechno/cloudflare-gateway-adblocking"
+keywords = ["cloudflare", "dns", "adblocking", "serverless"]
+
packages = [{include = "src"}]
[tool.poetry.scripts]
@@ -19,6 +22,7 @@ python-dotenv = "^1.0.0"
[tool.poetry.group.dev.dependencies]
ruff = "^0.0.281"
+black = "^23.7.0"
[build-system]
requires = ["poetry-core"]
diff --git a/src/__main__.py b/src/__main__.py
index 1771e84..d768e57 100644
--- a/src/__main__.py
+++ b/src/__main__.py
@@ -1,6 +1,6 @@
# To run from the root project directory, run the following command:
# python -m src.__main__
-from loguru import logger
+from loguru import logger
from .utils import upload, delete, utils
import argparse
@@ -12,45 +12,46 @@ from pathlib import Path
TOKEN = None
ACCOUNT_ID = None
+
def main():
# Setup logging
logger.remove()
# ^10 is a formatting directive to center with a padding of 10
- logger_format = "{time:YYYY-MM-DD HH:mm:ss} |{level: ^10}| {message}" # pylint: disable=line-too-long
+ logger_format = "{time:YYYY-MM-DD HH:mm:ss} |{level: ^10}| {message}" # noqa E501
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
+ # This must precede the argparse setup as env variables are used as default values
if Path(".env").is_file():
dotenv.load_dotenv()
logger.info("Loaded .env file")
- else:
+ else:
logger.info("No .env file found")
# Parse arguments
# Set up argparse
argparser = argparse.ArgumentParser(
- prog='Cloudflare Gateway Adblocking',
- description='Serverless adblocking via Cloudflare Zero Trust Gateway',
- epilog=':)'
+ prog="Cloudflare Gateway Adblocking",
+ description="Serverless adblocking via Cloudflare Zero Trust Gateway",
+ epilog=":)",
)
# Add argument groups
- credential_args = argparser.add_argument_group('Cloudflare Credentials')
+ credential_args = argparser.add_argument_group("Cloudflare Credentials")
# Add arguments
credential_args.add_argument(
"--account-id",
"-a",
help="Cloudflare account ID - environment variable: CLOUDFLARE_ACCOUNT_ID",
- default=os.environ.get("CLOUDFLARE_ACCOUNT_ID")
+ default=os.environ.get("CLOUDFLARE_ACCOUNT_ID"),
)
- credential_args.add_argument('--token',
- '-t',
- help='Cloudflare API token - environment variable: CLOUDFLARE_TOKEN',
- default=os.environ.get("CLOUDFLARE_TOKEN")
+ credential_args.add_argument(
+ "--token",
+ "-t",
+ help="Cloudflare API token - environment variable: CLOUDFLARE_TOKEN",
+ default=os.environ.get("CLOUDFLARE_TOKEN"),
)
# Add subcommands
@@ -58,31 +59,28 @@ def main():
title="subcommands",
description="",
help="Subcommands to preform operations",
- dest="subcommand"
+ dest="subcommand",
)
# Add subcommand: upload
upload_parser = subparsers.add_parser(
- "upload",
- help="Upload adblock lists to Cloudflare"
+ "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"
+ help="Either a blocklist hosts file or a directory containing blocklist hosts files", # noqa E501
+ 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
+ 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
)
# Add subcommand: delete
delete_parser = subparsers.add_parser(
- "delete",
- help="Delete adblock lists from Cloudflare"
+ "delete", help="Delete adblock lists from Cloudflare"
)
delete_parser.set_defaults(func=delete_from_cloudflare)
@@ -97,14 +95,17 @@ def main():
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
+ logger.error(
+ "No environment variables found. Please create a .env file or .envrc file"
+ ) # noqa E501
exit(1)
- try :
+ try:
args.func(args)
except AttributeError as e:
logger.debug(e)
argparser.print_help()
+
def upload_to_cloudflare(args):
logger.info("Uploading to Cloudflare")
blocklists = upload.get_blocklists(args.blocklists)
@@ -114,6 +115,8 @@ def upload_to_cloudflare(args):
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)
+
+
def delete_from_cloudflare(args):
logger.info("Deleting from Cloudflare")
rules = utils.get_gateway_rules(ACCOUNT_ID, TOKEN)
@@ -122,5 +125,6 @@ def delete_from_cloudflare(args):
lists = utils.filter_adblock_lists(lists)
delete.delete_adblock_list(lists, ACCOUNT_ID, TOKEN)
+
if __name__ == "__main__":
main()
diff --git a/src/utils/delete.py b/src/utils/delete.py
index f87ce46..1662bc5 100644
--- a/src/utils/delete.py
+++ b/src/utils/delete.py
@@ -5,17 +5,23 @@ from . import utils
def delete_adblock_list(lists: dict, account_id: str, token: str):
- for lst in lists:
- url = f'https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/lists/{lst["id"]}'
- headers = {
- "Authorization": f"Bearer {token}",
- "Content-Type": "application/json",
- }
- response = requests.delete(url, headers=headers, timeout=10)
- if response.status_code != 200:
- print(f"Error deleting list: {response.text}")
+ try:
+ for lst in lists:
+ url = f'https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/lists/{lst["id"]}'
+ headers = {
+ "Authorization": f"Bearer {token}",
+ "Content-Type": "application/json",
+ }
+ response = requests.delete(url, headers=headers, timeout=10)
+ if response.status_code != 200:
+ print(f"Error deleting list: {response.text}")
+ else:
+ print(f'Deleted list {lst["name"]}')
+ except TypeError as e:
+ if str(e) == "'NoneType' object is not iterable":
+ print("No lists found")
else:
- print(f'Deleted list {lst["name"]}')
+ raise e
def delete_adblock_policy(policies: dict, account_id: str, token: str):
diff --git a/src/utils/upload.py b/src/utils/upload.py
index 51c184a..464836c 100644
--- a/src/utils/upload.py
+++ b/src/utils/upload.py
@@ -3,21 +3,32 @@ from . import utils
import pathlib
-def get_blocklists(path_to_blocklists: str = None):
- # __file__ is a special variable that is the path to the current file
- # list_directory = pathlib.Path(__file__).parent.parent.joinpath("blocklists")
-
- list_directory = pathlib.Path(path_to_blocklists)
- for file in list_directory.iterdir():
- blocklists = utils.convert_to_list(file)
+def get_blocklists(hosts_path: str = None):
+ blocklists = []
+ hosts_path = pathlib.Path(hosts_path)
+ if hosts_path.is_file():
+ blocklists = utils.convert_to_list(hosts_path)
+ elif hosts_path.is_dir():
+ for file in hosts_path.iterdir():
+ blocklists.extend(utils.convert_to_list(file))
+ else:
+ raise ValueError("Invalid hosts file or directory")
return blocklists
def apply_whitelists(blocklists, whitelist: str = None):
- whitelist = utils.convert_to_list(
- # pathlib.Path(__file__).parent.parent.joinpath("whitelist.txt")
- pathlib.Path(whitelist)
- )
+ # 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
+ whitelist_path = pathlib.Path(whitelist)
+ if whitelist_path.is_file():
+ whitelist = utils.convert_to_list(whitelist_path)
+ elif whitelist_path.is_dir():
+ whitelist = []
+ for file in whitelist_path.iterdir():
+ whitelist.extend(utils.convert_to_list(file))
+ else:
+ return blocklists
blocklists = [x for x in blocklists if x not in whitelist]
return blocklists
@@ -28,7 +39,7 @@ def split_list(blocklists):
[blocklists[i : i + 1000] for i in range(0, len(blocklists), 1000)]
) # This is the same as the for loop below
# for i in range(0, len(blocklists), 1000):
- # # This is appending a list of 1000 domains to the lists list. It is doing this by slicing the blocklists list to get the first 1000 domains, then the next 1000 domains, etc.
+ # This continues to append lists of 1000 items to the lists list via slicing
# lists.append(blocklists[i:i + 1000])
return lists
@@ -98,5 +109,6 @@ def main():
cloud_lists = utils.filter_adblock_lists(cloud_lists)
create_dns_policy(cloud_lists)
+
if __name__ == "__main__":
main()
diff --git a/src/utils/utils.py b/src/utils/utils.py
index 1e48770..de5b5dd 100644
--- a/src/utils/utils.py
+++ b/src/utils/utils.py
@@ -4,6 +4,7 @@ import pathlib
import requests
from dotenv import load_dotenv
+
class Config:
def __init__(self, token, account_id):
# Set token using setter
@@ -14,28 +15,31 @@ class Config:
@property
def token(self):
return self._token
+
@token.setter
def token(self, token):
if token is None:
raise ValueError("No token provided")
else:
- url = 'https://api.cloudflare.com/client/v4/user/tokens/verify'
+ url = "https://api.cloudflare.com/client/v4/user/tokens/verify"
headers = {
- 'Authorization': f'Bearer {token}',
- 'Content-Type': 'application/json'
+ "Authorization": f"Bearer {token}",
+ "Content-Type": "application/json",
}
response = requests.get(url, headers=headers, timeout=10)
- if response.json()['success'] is False:
- raise ValueError('Invalid token')
+ if response.json()["success"] is False:
+ raise ValueError("Invalid token")
else:
# Token needs the following scopes:
# Zero Trust: Read/Edit
# Account Firewall Access Rules: Read/Edit
- # Access Apps and Policies: Read/Edit
- self._token = token
+ # Access Apps and Policies: Read/Edit
+ self._token = token
+
@property
def account_id(self):
return self._account_id
+
@account_id.setter
def account_id(self, account_id):
if account_id is None:
@@ -44,10 +48,7 @@ class Config:
# Possibly make a request to lists to check if the account ID exists
self._account_id = account_id
-
-
-
-
+
# List Utils
@@ -55,8 +56,8 @@ class Config:
def convert_to_list(file: pathlib.Path) -> list:
with open(file, "r") as f:
# Don't read commented lines; strip whitespace;
- # remove 127.0.0.1 from beginning of line;
- # ignore lines with "localhost"; ignore lines with "::1";
+ # remove 127.0.0.1 from beginning of line;
+ # ignore lines with "localhost"; ignore lines with "::1";
# ignore newlines
hosts = [
i[10:].strip()
@@ -67,9 +68,9 @@ def convert_to_list(file: pathlib.Path) -> list:
# for x in f.readlines():
# if not x.startswith('#') and 'localhost' not in x and '::1' not in x:
# hosts.append(x[10:].strip())
-
- # If there are any empty strings in the list, remove them
- # For some reason, whitelist seems to still be present
+
+ # If there are any empty strings in the list, remove them
+ # For some reason, whitelist seems to still be present
hosts = [i for i in hosts if i != ""]
return hosts
@@ -77,27 +78,6 @@ def convert_to_list(file: pathlib.Path) -> list:
# General Utils
-# Load environment variables
-# def load_env() -> dict:
-# load_dotenv(".env")
-# if not os.environ.get("CLOUDFLARE_TOKEN") and not os.environ.get(
-# "CLOUDFLARE_ACCOUNT_ID"
-# ):
-# load_dotenv(".envrc")
-# if not os.environ.get("CLOUDFLARE_TOKEN") or not os.environ.get(
-# "CLOUDFLARE_ACCOUNT_ID"
-# ):
-# print(
-# "No environment variables found. Please create a .env file or .envrc file"
-# )
-# exit()
-# else:
-# return {
-# "CLOUDFLARE_TOKEN": os.environ.get("CLOUDFLARE_TOKEN"),
-# "CLOUDFLARE_ACCOUNT_ID": os.environ.get("CLOUDFLARE_ACCOUNT_ID"),
-# }
-
-
def get_lists(account_id, token) -> dict:
url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/lists"
headers = {
@@ -112,9 +92,15 @@ def get_lists(account_id, token) -> dict:
def filter_adblock_lists(lists: dict) -> dict:
adblock_lists = []
- for lst in lists:
- if lst["name"].startswith("adblock-list") and lst["type"] == "DOMAIN":
- adblock_lists.append(lst)
+ try:
+ for lst in lists:
+ if lst["name"].startswith("adblock-list") and lst["type"] == "DOMAIN":
+ adblock_lists.append(lst)
+ except TypeError as e:
+ if str(e) == "'NoneType' object is not iterable":
+ print("No lists found")
+ else:
+ raise e
return adblock_lists
diff --git a/whitelist.txt b/whitelist.txt
deleted file mode 100644
index e69de29..0000000