cloudflare-gateway-adblocking/src/delete_adblock_zerotrust.py

53 lines
1.6 KiB
Python
Raw Normal View History

2023-03-20 18:40:08 +00:00
# This is a scriprt to undo the changes made by adblock-zerotrust.py
import requests
2023-07-31 19:07:55 +01:00
import utils.utils
2023-03-20 18:40:08 +00:00
# Load environment variables
2023-03-26 23:16:26 +01:00
TOKEN = utils.load_env()["CLOUDFLARE_TOKEN"]
ACCOUNT_ID = utils.load_env()["CLOUDFLARE_ACCOUNT_ID"]
2023-03-20 18:40:08 +00:00
def delete_adblock_list(lists: dict):
for lst in lists:
url = f'https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/gateway/lists/{lst["id"]}'
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.delete(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 deleting list: {response.text}")
2023-03-20 18:40:08 +00:00
else:
print(f'Deleted list {lst["name"]}')
2023-03-26 23:16:26 +01:00
def delete_adblock_policy(policies: dict):
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)
if response.status_code != 200:
print(f"Error deleting policy: {response.text}")
else:
print(f'Deleted policy {policy["name"]}')
break
else:
print("No policy found")
2023-03-20 18:40:08 +00:00
def main():
2023-03-26 23:16:26 +01:00
rules = utils.get_gateway_rules()
delete_adblock_policy(rules)
2023-03-20 18:40:08 +00:00
lists = utils.get_lists()
lists = utils.filter_adblock_lists(lists)
delete_adblock_list(lists)
2023-03-26 23:16:26 +01:00
if __name__ == "__main__":
main()