cloudflare-gateway-adblocking/zerotrust_adblock/utils.py

86 lines
2.8 KiB
Python
Raw Normal View History

2023-03-26 23:16:26 +01:00
import os
import pathlib
2023-03-20 18:40:08 +00:00
import requests
from dotenv import load_dotenv
# List Utils
2023-03-26 23:16:26 +01:00
2023-03-20 18:40:08 +00:00
# Convert a hosts file to a simple hostname list
2023-03-26 23:16:26 +01:00
def convert_to_list(file: pathlib.Path) -> list:
with open(file, "r") as f:
2023-03-20 18:40:08 +00:00
# Don't read commented lines; strip whitespace; remove 127.0.0.1 from beginning of line; ignore lines with "localhost"; ignore lines with "::1"; ignore newlines blocklists.extend([i[10:].strip() for i in f.readlines() if not i.startswith('#') and 'localhost' not in i and '::1' not in i])
2023-03-26 23:16:26 +01:00
hosts = [
i[10:].strip()
for i in f.readlines()
if not i.startswith("#") and "localhost" not in i and "::1" not in i
]
2023-03-20 18:40:08 +00:00
# Equivalent to:
# 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 since for some reason, whitespace is stil in the list
2023-03-26 23:16:26 +01:00
hosts = [i for i in hosts if i != ""]
2023-03-20 18:40:08 +00:00
return hosts
2023-03-26 23:16:26 +01:00
2023-03-20 18:40:08 +00:00
# General Utils
2023-03-26 23:16:26 +01:00
2023-03-20 18:40:08 +00:00
# Load environment variables
def load_env() -> dict:
load_dotenv(".env")
2023-03-26 23:16:26 +01:00
if not os.environ.get("CLOUDFLARE_TOKEN") and not os.environ.get(
"CLOUDFLARE_ACCOUNT_ID"
):
2023-03-20 18:40:08 +00:00
load_dotenv(".envrc")
2023-03-26 23:16:26 +01:00
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"
)
2023-03-20 18:40:08 +00:00
exit()
2023-03-26 23:16:26 +01:00
else:
return {
"CLOUDFLARE_TOKEN": os.environ.get("CLOUDFLARE_TOKEN"),
"CLOUDFLARE_ACCOUNT_ID": os.environ.get("CLOUDFLARE_ACCOUNT_ID"),
}
2023-03-20 18:40:08 +00:00
def get_lists() -> dict:
2023-03-26 23:16:26 +01:00
url = f"https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/gateway/lists"
2023-03-20 18:40:08 +00:00
headers = {
2023-03-26 23:16:26 +01:00
"Authorization": f"Bearer {TOKEN}",
2023-03-20 18:40:08 +00:00
"Content-Type": "application/json",
}
2023-03-26 23:16:26 +01:00
response = requests.get(url, headers=headers, timeout=10)
2023-03-20 18:40:08 +00:00
if response.status_code != 200:
2023-03-26 23:16:26 +01:00
print(f"Error getting lists: {response.text}")
return response.json()["result"]
2023-03-20 18:40:08 +00:00
def filter_adblock_lists(lists: dict) -> dict:
adblock_lists = []
for lst in lists:
2023-03-26 23:16:26 +01:00
if lst["name"].startswith("adblock-list") and lst["type"] == "DOMAIN":
2023-03-20 18:40:08 +00:00
adblock_lists.append(lst)
return adblock_lists
2023-03-26 23:16:26 +01:00
def get_gateway_rules() -> 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)
if response.status_code != 200:
print(f"Error getting lists: {response.text}")
return response.json()["result"]
TOKEN = load_env()["CLOUDFLARE_TOKEN"]
ACCOUNT_ID = load_env()["CLOUDFLARE_ACCOUNT_ID"]