90 lines
3.4 KiB
Python
Executable File
90 lines
3.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Cloudflare API Helper
|
|
- Login et récupération API Key
|
|
- Gestion zones et records
|
|
"""
|
|
import requests
|
|
import json
|
|
import sys
|
|
|
|
CF_API_BASE = "https://api.cloudflare.com/client/v4"
|
|
|
|
def get_api_key_from_token(api_token):
|
|
"""Vérifier token API"""
|
|
headers = {"Authorization": f"Bearer {api_token}", "Content-Type": "application/json"}
|
|
r = requests.get(f"{CF_API_BASE}/user/tokens/verify", headers=headers)
|
|
return r.json()
|
|
|
|
def list_zones(api_key, email):
|
|
"""Lister les zones d'un compte"""
|
|
headers = {"X-Auth-Email": email, "X-Auth-Key": api_key, "Content-Type": "application/json"}
|
|
r = requests.get(f"{CF_API_BASE}/zones", headers=headers)
|
|
return r.json()
|
|
|
|
def add_zone(api_key, email, domain):
|
|
"""Ajouter un domaine"""
|
|
headers = {"X-Auth-Email": email, "X-Auth-Key": api_key, "Content-Type": "application/json"}
|
|
data = {"name": domain, "jump_start": True}
|
|
r = requests.post(f"{CF_API_BASE}/zones", headers=headers, json=data)
|
|
return r.json()
|
|
|
|
def add_dns_record(api_key, email, zone_id, record_type, name, content, ttl=1, proxied=False):
|
|
"""Ajouter un enregistrement DNS"""
|
|
headers = {"X-Auth-Email": email, "X-Auth-Key": api_key, "Content-Type": "application/json"}
|
|
data = {"type": record_type, "name": name, "content": content, "ttl": ttl, "proxied": proxied}
|
|
r = requests.post(f"{CF_API_BASE}/zones/{zone_id}/dns_records", headers=headers, json=data)
|
|
return r.json()
|
|
|
|
def setup_email_dns(api_key, email, zone_id, domain):
|
|
"""Configurer DNS pour email (MX, SPF, DKIM, DMARC)"""
|
|
results = []
|
|
|
|
# MX Records pour Office365
|
|
mx_records = [
|
|
{"priority": 0, "content": f"{domain.replace('.', '-')}.mail.protection.outlook.com"}
|
|
]
|
|
for mx in mx_records:
|
|
r = add_dns_record(api_key, email, zone_id, "MX", domain, mx['content'], 3600, False)
|
|
results.append({"type": "MX", "result": r})
|
|
|
|
# SPF
|
|
spf = "v=spf1 include:spf.protection.outlook.com -all"
|
|
r = add_dns_record(api_key, email, zone_id, "TXT", domain, spf, 3600, False)
|
|
results.append({"type": "SPF", "result": r})
|
|
|
|
# DMARC
|
|
dmarc = "v=DMARC1; p=none; rua=mailto:dmarc@" + domain
|
|
r = add_dns_record(api_key, email, zone_id, "TXT", "_dmarc." + domain, dmarc, 3600, False)
|
|
results.append({"type": "DMARC", "result": r})
|
|
|
|
# Autodiscover CNAME
|
|
r = add_dns_record(api_key, email, zone_id, "CNAME", "autodiscover", "autodiscover.outlook.com", 1, False)
|
|
results.append({"type": "Autodiscover", "result": r})
|
|
|
|
return results
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 3:
|
|
print("Usage: python3 cloudflare_api.py <action> <params>")
|
|
print("Actions: verify_token, list_zones, add_zone, setup_email")
|
|
sys.exit(1)
|
|
|
|
action = sys.argv[1]
|
|
|
|
if action == "verify_token":
|
|
token = sys.argv[2]
|
|
print(json.dumps(get_api_key_from_token(token), indent=2))
|
|
|
|
elif action == "list_zones":
|
|
api_key, email = sys.argv[2], sys.argv[3]
|
|
print(json.dumps(list_zones(api_key, email), indent=2))
|
|
|
|
elif action == "add_zone":
|
|
api_key, email, domain = sys.argv[2], sys.argv[3], sys.argv[4]
|
|
print(json.dumps(add_zone(api_key, email, domain), indent=2))
|
|
|
|
elif action == "setup_email":
|
|
api_key, email, zone_id, domain = sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]
|
|
print(json.dumps(setup_email_dns(api_key, email, zone_id, domain), indent=2))
|