Compare commits
No commits in common. "49e7cd13b66f3319e662ac593be8e423b061b351" and "a443cf9b51c6a3c111ec58dfcb4e08ec17328eed" have entirely different histories.
49e7cd13b6
...
a443cf9b51
4
.gitignore
vendored
4
.gitignore
vendored
@ -168,6 +168,4 @@ cython_debug/
|
|||||||
|
|
||||||
|
|
||||||
# Project setup
|
# Project setup
|
||||||
config.json
|
config.json
|
||||||
response-example*
|
|
||||||
dns-export*
|
|
||||||
56
apicalls.py
56
apicalls.py
@ -1,56 +0,0 @@
|
|||||||
import requests
|
|
||||||
|
|
||||||
def getHeaders(credentials):
|
|
||||||
headers = {'Authorization': f'Bearer {credentials["api_key"]}', 'X-Auth-Email': f'{credentials["email"]}',
|
|
||||||
'Content-Type': 'application/json'}
|
|
||||||
return headers
|
|
||||||
|
|
||||||
def errorChecker(r):
|
|
||||||
if r.status_code == 200:
|
|
||||||
return False
|
|
||||||
elif r.status_code == 429:
|
|
||||||
print(f'\n\n\n{"="*80}It looks like you have reached your rate limit to access CloudFlare APIs\nPlease try after 5 minutes.{"="*80}')
|
|
||||||
else:
|
|
||||||
print(f'Error {r.status_code}.\nDetails:\n{r.json()}')
|
|
||||||
assert False
|
|
||||||
|
|
||||||
def verifyCredentials(credentials):
|
|
||||||
headers = getHeaders(credentials)
|
|
||||||
r = requests.get('https://api.cloudflare.com/client/v4/user/tokens/verify', headers=headers)
|
|
||||||
errorChecker(r)
|
|
||||||
return r
|
|
||||||
|
|
||||||
|
|
||||||
def listAllZones(credentials):
|
|
||||||
zones = requests.get(
|
|
||||||
'https://api.cloudflare.com/client/v4/zones',
|
|
||||||
headers= getHeaders(credentials)
|
|
||||||
)
|
|
||||||
errorChecker(zones)
|
|
||||||
return zones
|
|
||||||
|
|
||||||
|
|
||||||
def getAllDNS(credentials, id):
|
|
||||||
dns_records = requests.get(
|
|
||||||
f'https://api.cloudflare.com/client/v4/zones/{id}/dns_records',
|
|
||||||
headers=getHeaders(credentials)
|
|
||||||
)
|
|
||||||
errorChecker(dns_records)
|
|
||||||
return dns_records
|
|
||||||
|
|
||||||
def exportBindDNS(credentials, id):
|
|
||||||
export_response = requests.get(
|
|
||||||
f'https://api.cloudflare.com/client/v4/zones/{id}/dns_records/export',
|
|
||||||
headers=getHeaders(credentials)
|
|
||||||
)
|
|
||||||
errorChecker(export_response)
|
|
||||||
return export_response
|
|
||||||
|
|
||||||
def deleteARecord(credentials, zone_id, dns_id):
|
|
||||||
delete_response = requests.delete(
|
|
||||||
f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{dns_id}',
|
|
||||||
headers=getHeaders(credentials)
|
|
||||||
)
|
|
||||||
errorChecker(delete_response)
|
|
||||||
return delete_response
|
|
||||||
|
|
||||||
76
app.py
76
app.py
@ -1,76 +0,0 @@
|
|||||||
import json
|
|
||||||
import uuid
|
|
||||||
import datetime
|
|
||||||
from fileinput import filename
|
|
||||||
from sys import argv
|
|
||||||
from pathlib import Path
|
|
||||||
from apicalls import *
|
|
||||||
|
|
||||||
|
|
||||||
# Get API key and Email address
|
|
||||||
def getCredentials():
|
|
||||||
api_key = input("Enter the Cloudflare API key: ")
|
|
||||||
email_address = input("Enter the Cloudflare Email address: ")
|
|
||||||
return {"api_key": api_key, "email": email_address}
|
|
||||||
|
|
||||||
|
|
||||||
# Get credentials
|
|
||||||
|
|
||||||
if len(argv) > 1 and 'loadconfig' in argv:
|
|
||||||
if Path("config.json").exists():
|
|
||||||
configfile = open("config.json")
|
|
||||||
config = json.load(configfile)
|
|
||||||
credentials = {'api_key': config['API_Key'], 'email': config['Email']}
|
|
||||||
else:
|
|
||||||
print('config.json was not found. Please enter the credentials manually')
|
|
||||||
credentials = getCredentials()
|
|
||||||
else:
|
|
||||||
credentials = getCredentials()
|
|
||||||
|
|
||||||
|
|
||||||
zones = listAllZones(credentials).json()["result"]
|
|
||||||
|
|
||||||
|
|
||||||
print('Here are the domains in your account')
|
|
||||||
zone_index = 0
|
|
||||||
zone_list = {}
|
|
||||||
for zone in zones:
|
|
||||||
print(f'{zone_index + 1}: {zone["name"]}')
|
|
||||||
zone_list[zone['name']] = {'id': zone['id'], 'name': zone['name'], 'owner': zone['owner']['email'], 'account_of': zone['account']['name']}
|
|
||||||
|
|
||||||
domainToDelete = input('Please enter the domain name to delete the DNS records from:\n')
|
|
||||||
|
|
||||||
if domainToDelete in zone_list:
|
|
||||||
zone_id = zone_list[domainToDelete]['id']
|
|
||||||
zone_owner = zone_list[domainToDelete]['owner']
|
|
||||||
confirmation = input(f'Are you sure to delete all the DNS records from the domain {domainToDelete}, owned by {zone_owner}?\nEnter YES to continue: ')
|
|
||||||
if confirmation == 'YES':
|
|
||||||
# Get Bind DNS
|
|
||||||
bind_DNS = exportBindDNS(credentials, zone_id).text
|
|
||||||
# Write to a file
|
|
||||||
file_name = f'dns-export-{str(uuid.uuid4())[0:8]}-{domainToDelete}-{datetime.datetime.now().strftime("%d-%b-%Y-%H-%M-%S")}.txt'
|
|
||||||
print(f'Backing up DNS records to {file_name}')
|
|
||||||
bind_export_file = open(file_name, 'w')
|
|
||||||
bind_export_file.write(bind_DNS)
|
|
||||||
bind_export_file.close()
|
|
||||||
print('Successfully backed up DNS records in BIND format.')
|
|
||||||
# get DNS Records
|
|
||||||
dns_records = getAllDNS(credentials, zone_id).json()["result"]
|
|
||||||
# Delete DNS Records
|
|
||||||
print('Starting Deletion of DNS records')
|
|
||||||
for record in dns_records:
|
|
||||||
dns_id = record['id']
|
|
||||||
dns_type = record['type']
|
|
||||||
dns_name = record['name']
|
|
||||||
dns_content = record['content']
|
|
||||||
print(f'Deleting:\n{dns_type}\t{dns_name}\t{dns_content}...')
|
|
||||||
delete_response = deleteARecord(credentials, zone_id, dns_id)
|
|
||||||
if delete_response.json()['result']['id'] == dns_id:
|
|
||||||
print(f'Successfully Deleted {dns_id}')
|
|
||||||
else:
|
|
||||||
print('Something is wrong, abroading...')
|
|
||||||
assert False
|
|
||||||
else:
|
|
||||||
print('Aborted, bye...')
|
|
||||||
else:
|
|
||||||
print('We don\'t have access to the domain name you have entered. Please make sure the API key you have provided has access to the domain name you have entereed.')
|
|
||||||
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"API_Key": "your api key goes here",
|
"saveCredentials": true,
|
||||||
|
"API_KEY": "your api key goes here",
|
||||||
"Email": "your cloudflare email address"
|
"Email": "your cloudflare email address"
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user