#!/usr/bin/env python3 import json import re import urllib.error import urllib.request import paramiko PASS = "Bbt9115xty9176!" CF = "https://api.cloudflare.com/client/v4" ACCOUNT = "2599c23bbb1255dbb73e8d34b4115fda" DOMAIN = "exposedgays.com" TUNNEL_CNAME = "03079a26-f14c-4622-b463-ba54a24f7472.cfargotunnel.com" TOKENS = [ ("pfsense", None), ("apply_mail", "cfut_zlUt5lAKVsu7qIcRCHJnyhAvpJVF5jx24HlrIEn9a8fcd6a1"), ("local_old", "cfut_vF51avCWJV9EnCM4lXS2v60jXo6TPQZ0yxESHuRn5b0f3515"), ("local_full", "cfut_IOm1DJW4pDkPCuQA6UoOjqO6Tcv2zU10y2PSw82m379f7db3f7"), ("local_trunc", "cfut_IOm1DJW4pDkPCuQA6UoOjqO6Tcv2zU10y2PSw82m379f7db3"), ] try: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("10.10.0.1", username="admin", password=PASS, timeout=15) _, o, e = c.exec_command("cat /usr/local/etc/wan-dns-failover.env", timeout=30) o.channel.recv_exit_status() text = (o.read() + e.read()).decode() c.close() TOKENS[0] = ("pfsense", re.search(r"CLOUDFLARE_API_TOKEN=([^\s\"']+)", text).group(1)) except Exception as ex: print("pfsense err", ex) def call(tok, path, method="GET", body=None): req = urllib.request.Request( CF + path, data=json.dumps(body).encode() if body else None, method=method, headers={"Authorization": f"Bearer {tok}", "Content-Type": "application/json"}, ) try: with urllib.request.urlopen(req, timeout=45) as r: return json.load(r) except urllib.error.HTTPError as e: return json.loads(e.read().decode()) def upsert_dns(tok, zid): records = call(tok, f"/zones/{zid}/dns_records?per_page=100").get("result", []) for name in [DOMAIN, f"www.{DOMAIN}"]: existing = next( (r for r in records if r["name"] == name and r["type"] in ("A", "CNAME", "AAAA")), None, ) body = {"type": "CNAME", "name": name, "content": TUNNEL_CNAME, "ttl": 1, "proxied": True} if existing: res = call(tok, f"/zones/{zid}/dns_records/{existing['id']}", "PATCH", body) action = "PATCH" else: res = call(tok, f"/zones/{zid}/dns_records", "POST", body) action = "POST" print(f" {action} {name}: success={res.get('success')} errors={res.get('errors')}") def main(): winner = None zone = None for label, tok in TOKENS: if not tok: continue v = call(tok, "/user/tokens/verify") print(f"\n[{label}] verify={v.get('success')} errors={v.get('errors')}") if not v.get("success"): continue z = call(tok, f"/zones?name={DOMAIN}") if z.get("result"): winner, zone, tok_label = tok, z["result"][0], label print(f"[{label}] FOUND existing zone {zone['id']} status={zone['status']}") break c = call( tok, "/zones", "POST", {"name": DOMAIN, "account": {"id": ACCOUNT}, "type": "full", "jump_start": False}, ) print(f"[{label}] create={c.get('success')} errors={c.get('errors')}") if c.get("success"): winner, zone, tok_label = tok, c["result"], label print(f"[{label}] CREATED zone NS={zone.get('name_servers')}") break if not zone: print("\nFAILED: no token could find or create zone") return print(f"\nUsing token [{tok_label}] zone {zone['id']} status={zone['status']}") print("Nameservers:") for ns in zone.get("name_servers", []): print(f" {ns}") upsert_dns(winner, zone["id"]) print("\nFinal records:") for r in call(winner, f"/zones/{zone['id']}/dns_records?per_page=100").get("result", []): if "exposedgays" in r["name"]: print(f" {r['type']} {r['name']} -> {r['content']} proxied={r.get('proxied')}") if __name__ == "__main__": main()