Skip to content

Commit

Permalink
Added flag
Browse files Browse the repository at this point in the history
  • Loading branch information
slashtechno committed Aug 19, 2023
1 parent fd5f951 commit 5fd2ce9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cloudflare-gateway-adblocking"
version = "0.1.2"
version = "0.1.3"
description = "Serverless adblocking via Cloudflare Zero Trust Gateway"
authors = ["slastechno <[email protected]>"]
license = "MIT"
Expand Down
21 changes: 13 additions & 8 deletions src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ def main():
argparser.add_argument(
"--log-level", "-l", help="Log level", default="INFO"
) # noqa E501

argparser.add_argument(
"--timeout",
help="Timeout for requests",
default=None,
type = int
)
# Credential arguments
credential_args.add_argument(
"--account-id",
Expand Down Expand Up @@ -121,19 +126,19 @@ def upload_to_cloudflare(args):
blocklists = upload.get_blocklists(args.blocklists)
blocklists = upload.apply_whitelists(blocklists, args.whitelists)
lists = upload.split_list(blocklists)
asyncio.run(upload.upload_to_cloudflare(lists, ACCOUNT_ID, TOKEN))
cloud_lists = utils.get_lists(ACCOUNT_ID, TOKEN)
asyncio.run(upload.upload_to_cloudflare(lists, ACCOUNT_ID, TOKEN, args.timeout))
cloud_lists = utils.get_lists(ACCOUNT_ID, TOKEN, args.timeout)
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):
logger.info("Deleting from Cloudflare")
rules = utils.get_gateway_rules(ACCOUNT_ID, TOKEN)
delete.delete_adblock_policy(rules, ACCOUNT_ID, TOKEN)
lists = utils.get_lists(ACCOUNT_ID, TOKEN)
rules = utils.get_gateway_rules(ACCOUNT_ID, TOKEN, timeout = args.timeout)
delete.delete_adblock_policy(rules, ACCOUNT_ID, TOKEN, args.timeout)
lists = utils.get_lists(ACCOUNT_ID, TOKEN, args.timeout)
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):
Expand Down
8 changes: 4 additions & 4 deletions src/utils/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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:
async with httpx.AsyncClient() as client:
for lst in lists:
Expand All @@ -15,7 +15,7 @@ async def delete_adblock_list(lists: dict, account_id: str, token: str):
"Authorization": f"Bearer {token}",
"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:
print(f"Error deleting list: {response.text}")
else:
Expand All @@ -27,15 +27,15 @@ async def delete_adblock_list(lists: dict, account_id: str, token: str):
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:
if policy["name"] == "Block Ads":
url = f'https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/rules/{policy["id"]}'
headers = {
"Authorization": f"Bearer {token}",
"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:
print(f"Error deleting policy: {response.text}")
else:
Expand Down
8 changes: 4 additions & 4 deletions src/utils/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def split_list(blocklists):
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:
for i, lst in enumerate(lists):
list_name = f"adblock-list-{i + 1}"
Expand All @@ -70,14 +70,14 @@ async def upload_to_cloudflare(lists, account_id: str, token: str) -> None:
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")
if response.status_code != 200:
print(f"Error uploading {list_name}: {response.text}")
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"
headers = {
"Authorization": f"Bearer {token}",
Expand All @@ -98,7 +98,7 @@ def create_dns_policy(lists, account_id: str, token: str) -> None:
"traffic": traffic,
"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:
print(f"Error creating DNS policy: {response.text}")

Expand Down
8 changes: 4 additions & 4 deletions src/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ def convert_to_list(file: pathlib.Path) -> list:
# 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"
headers = {
"Authorization": f"Bearer {token}",
"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:
print(f"Error getting lists: {response.text}")
return response.json()["result"]
Expand All @@ -111,13 +111,13 @@ def filter_adblock_lists(lists: dict) -> dict:
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"
headers = {
"Authorization": f"Bearer {token}",
"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:
print(f"Error getting lists: {response.text}")
return response.json()["result"]

0 comments on commit 5fd2ce9

Please sign in to comment.