From d92976993d7f61e544bf7156ff61dd58aeea078d Mon Sep 17 00:00:00 2001 From: Dillan Mills Date: Fri, 27 Jan 2023 15:08:10 -0700 Subject: [PATCH] Add load balancer slupport --- cloudflare-ddns.py | 21 +++++++++++++++++++++ config-example.json | 15 ++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index a99238b..601a4b7 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -165,6 +165,26 @@ def commitRecord(ip): return True +def updateLoadBalancer(ip): + + for option in config["load_balancer"]: + pools = cf_api('user/load_balancers/pools', 'GET', option) + + if pools: + idxr = dict((p['id'], i) for i, p in enumerate(pools['result'])) + idx = idxr.get(option['pool_id']) + + origins = pools['result'][idx]['origins'] + + idxr = dict((o['name'], i) for i, o in enumerate(origins)) + idx = idxr.get(option['origin']) + + origins[idx]['address'] = ip['ip'] + data = {'origins': origins} + + response = cf_api(f'user/load_balancers/pools/{option["pool_id"]}', 'PATCH', option, {}, data) + + def cf_api(endpoint, method, config, headers={}, data=False): api_token = config['authentication']['api_token'] if api_token != '' and api_token != 'api_token_here': @@ -201,6 +221,7 @@ def cf_api(endpoint, method, config, headers={}, data=False): def updateIPs(ips): for ip in ips.values(): commitRecord(ip) + updateLoadBalancer(ip) if __name__ == '__main__': diff --git a/config-example.json b/config-example.json index 38f1097..e2ead1f 100755 --- a/config-example.json +++ b/config-example.json @@ -21,8 +21,21 @@ ] } ], + "load_balancer": [ + { + "authentication": { + "api_token": "api_token_here", + "api_key": { + "api_key": "api_key_here", + "account_email": "your_email_here" + } + }, + "pool_id": "your_pool_id_here", + "origin": "your_origin_name_here" + } + ], "a": true, "aaaa": true, "purgeUnknownRecords": false, "ttl": 300 -} +} \ No newline at end of file