Added flag

This commit is contained in:
slashtechno 2023-08-19 15:05:41 -04:00
parent fd5f951898
commit 5fd2ce94ab
Signed by: slashtechno
GPG Key ID: 8EC1D9D9286C2B17
5 changed files with 26 additions and 21 deletions

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "cloudflare-gateway-adblocking" name = "cloudflare-gateway-adblocking"
version = "0.1.2" version = "0.1.3"
description = "Serverless adblocking via Cloudflare Zero Trust Gateway" description = "Serverless adblocking via Cloudflare Zero Trust Gateway"
authors = ["slastechno <77907286+slashtechno@users.noreply.github.com>"] authors = ["slastechno <77907286+slashtechno@users.noreply.github.com>"]
license = "MIT" license = "MIT"

View File

@ -35,7 +35,12 @@ def main():
argparser.add_argument( argparser.add_argument(
"--log-level", "-l", help="Log level", default="INFO" "--log-level", "-l", help="Log level", default="INFO"
) # noqa E501 ) # noqa E501
argparser.add_argument(
"--timeout",
help="Timeout for requests",
default=None,
type = int
)
# Credential arguments # Credential arguments
credential_args.add_argument( credential_args.add_argument(
"--account-id", "--account-id",
@ -121,19 +126,19 @@ def upload_to_cloudflare(args):
blocklists = upload.get_blocklists(args.blocklists) blocklists = upload.get_blocklists(args.blocklists)
blocklists = upload.apply_whitelists(blocklists, args.whitelists) blocklists = upload.apply_whitelists(blocklists, args.whitelists)
lists = upload.split_list(blocklists) lists = upload.split_list(blocklists)
asyncio.run(upload.upload_to_cloudflare(lists, ACCOUNT_ID, TOKEN)) asyncio.run(upload.upload_to_cloudflare(lists, ACCOUNT_ID, TOKEN, args.timeout))
cloud_lists = utils.get_lists(ACCOUNT_ID, TOKEN) cloud_lists = utils.get_lists(ACCOUNT_ID, TOKEN, args.timeout)
cloud_lists = utils.filter_adblock_lists(cloud_lists) cloud_lists = utils.filter_adblock_lists(cloud_lists)
upload.create_dns_policy(cloud_lists, ACCOUNT_ID, TOKEN) upload.create_dns_policy(cloud_lists, ACCOUNT_ID, TOKEN, args.timeout)
def delete_from_cloudflare(args): def delete_from_cloudflare(args):
logger.info("Deleting from Cloudflare") logger.info("Deleting from Cloudflare")
rules = utils.get_gateway_rules(ACCOUNT_ID, TOKEN) rules = utils.get_gateway_rules(ACCOUNT_ID, TOKEN, timeout = args.timeout)
delete.delete_adblock_policy(rules, ACCOUNT_ID, TOKEN) delete.delete_adblock_policy(rules, ACCOUNT_ID, TOKEN, args.timeout)
lists = utils.get_lists(ACCOUNT_ID, TOKEN) lists = utils.get_lists(ACCOUNT_ID, TOKEN, args.timeout)
lists = utils.filter_adblock_lists(lists) lists = utils.filter_adblock_lists(lists)
asyncio.run(delete.delete_adblock_list(lists, ACCOUNT_ID, TOKEN)) asyncio.run(delete.delete_adblock_list(lists, ACCOUNT_ID, TOKEN, args.timeout))
def set_primary_logger(log_level): def set_primary_logger(log_level):

View File

@ -6,7 +6,7 @@ import requests
from . import utils from . import utils
async def delete_adblock_list(lists: dict, account_id: str, token: str): async def delete_adblock_list(lists: dict, account_id: str, token: str, timeout:int = 10):
try: try:
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
for lst in lists: for lst in lists:
@ -15,7 +15,7 @@ async def delete_adblock_list(lists: dict, account_id: str, token: str):
"Authorization": f"Bearer {token}", "Authorization": f"Bearer {token}",
"Content-Type": "application/json", "Content-Type": "application/json",
} }
response = await client.delete(url, headers=headers, timeout=10) response = await client.delete(url, headers=headers, timeout=timeout)
if response.status_code != 200: if response.status_code != 200:
print(f"Error deleting list: {response.text}") print(f"Error deleting list: {response.text}")
else: else:
@ -27,7 +27,7 @@ async def delete_adblock_list(lists: dict, account_id: str, token: str):
raise e raise e
def delete_adblock_policy(policies: dict, account_id: str, token: str): def delete_adblock_policy(policies: dict, account_id: str, token: str, timeout:int = 10):
for policy in policies: for policy in policies:
if policy["name"] == "Block Ads": if policy["name"] == "Block Ads":
url = f'https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/rules/{policy["id"]}' url = f'https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/rules/{policy["id"]}'
@ -35,7 +35,7 @@ def delete_adblock_policy(policies: dict, account_id: str, token: str):
"Authorization": f"Bearer {token}", "Authorization": f"Bearer {token}",
"Content-Type": "application/json", "Content-Type": "application/json",
} }
response = requests.delete(url, headers=headers, timeout=10) response = requests.delete(url, headers=headers, timeout=timeout)
if response.status_code != 200: if response.status_code != 200:
print(f"Error deleting policy: {response.text}") print(f"Error deleting policy: {response.text}")
else: else:

View File

@ -48,7 +48,7 @@ def split_list(blocklists):
return lists return lists
async def upload_to_cloudflare(lists, account_id: str, token: str) -> None: async def upload_to_cloudflare(lists, account_id: str, token: str, timeout:int = 10) -> None:
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
for i, lst in enumerate(lists): for i, lst in enumerate(lists):
list_name = f"adblock-list-{i + 1}" list_name = f"adblock-list-{i + 1}"
@ -70,14 +70,14 @@ async def upload_to_cloudflare(lists, account_id: str, token: str) -> None:
for x in lst for x in lst
], ],
} }
response = await client.post(url, headers=headers, json=data) response = await client.post(url, headers=headers, json=data, timeout=timeout)
print(f"Uploaded {list_name} to Cloudflare") print(f"Uploaded {list_name} to Cloudflare")
if response.status_code != 200: if response.status_code != 200:
print(f"Error uploading {list_name}: {response.text}") print(f"Error uploading {list_name}: {response.text}")
exit(1) exit(1)
def create_dns_policy(lists, account_id: str, token: str) -> None: def create_dns_policy(lists, account_id: str, token: str, timeout = 10) -> None:
url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/rules" url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/rules"
headers = { headers = {
"Authorization": f"Bearer {token}", "Authorization": f"Bearer {token}",
@ -98,7 +98,7 @@ def create_dns_policy(lists, account_id: str, token: str) -> None:
"traffic": traffic, "traffic": traffic,
"enabled": True, "enabled": True,
} }
response = requests.post(url, headers=headers, json=data, timeout=10) response = requests.post(url, headers=headers, json=data, timeout=timeout)
if response.status_code != 200: if response.status_code != 200:
print(f"Error creating DNS policy: {response.text}") print(f"Error creating DNS policy: {response.text}")

View File

@ -85,13 +85,13 @@ def convert_to_list(file: pathlib.Path) -> list:
# General Utils # General Utils
def get_lists(account_id, token) -> dict: def get_lists(account_id, token, timeout = 10) -> dict:
url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/lists" url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/lists"
headers = { headers = {
"Authorization": f"Bearer {token}", "Authorization": f"Bearer {token}",
"Content-Type": "application/json", "Content-Type": "application/json",
} }
response = requests.get(url, headers=headers, timeout=10) response = requests.get(url, headers=headers, timeout=timeout)
if response.status_code != 200: if response.status_code != 200:
print(f"Error getting lists: {response.text}") print(f"Error getting lists: {response.text}")
return response.json()["result"] return response.json()["result"]
@ -111,13 +111,13 @@ def filter_adblock_lists(lists: dict) -> dict:
return adblock_lists return adblock_lists
def get_gateway_rules(account_id, token) -> dict: def get_gateway_rules(account_id, token, timeout = 10) -> dict:
url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/rules" url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/rules"
headers = { headers = {
"Authorization": f"Bearer {token}", "Authorization": f"Bearer {token}",
"Content-Type": "application/json", "Content-Type": "application/json",
} }
response = requests.get(url, headers=headers, timeout=10) response = requests.get(url, headers=headers, timeout=timeout)
if response.status_code != 200: if response.status_code != 200:
print(f"Error getting lists: {response.text}") print(f"Error getting lists: {response.text}")
return response.json()["result"] return response.json()["result"]