From 0591ec584f9eb14377b97b40169f1f2410394e56 Mon Sep 17 00:00:00 2001 From: Dung Nguyen Date: Mon, 20 Nov 2023 21:46:30 +0700 Subject: [PATCH] feat: support custom public ip service --- README.md | 26 +++++++++++++++++++++++--- cf-dns-update.py | 27 ++++++++++++++++++--------- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b6b70b0..30ba3e8 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,11 @@ Dynamic DNS record update utility for CloudFlare DNS service. Python implement ### Download & Setup #### Download -Download code from Release page. +Download code from Release page. Link here: [https://github.com/nhymxu/cf-dns-update-python/releases/latest](https://github.com/nhymxu/cf-dns-update-python/releases/latest) -Or +Or clone repo using git: @@ -51,7 +51,7 @@ CF_API_TOKEN = token_key_here 2. Copy `Zone ID` 3. Edit like sample -Notes: +Notes: - `@` will be same as `base_domain` - To proxy a record, include it in both `records` and `proxied_records` @@ -72,3 +72,23 @@ And add this line to end of file ``` This script will run each 15 minutes. + +### Service get public IP + +If you want custom what service to get public IP, you can config in `config.ini` file + +Sample + +```ini +[common] +CHECK_IP_SERVICE = xxx +``` + +where xxx is service name listed below + +| Name | Endpoint | +| ---- | -------- | +| amazonaws | https://checkip.amazonaws.com | +| ifconfig.me | https://ifconfig.me/ip | +| icanhazip | https://icanhazip.com/ | +| ipecho | https://ipecho.net/plain | diff --git a/cf-dns-update.py b/cf-dns-update.py index 6fe7bb2..179f7c4 100644 --- a/cf-dns-update.py +++ b/cf-dns-update.py @@ -16,6 +16,7 @@ CF_API_TOKEN = '' IS_DRYRUN = False +PUBLIC_IP_SERVICE = 'amazonaws' def make_request(method="GET", url="", request_body=None): @@ -57,14 +58,17 @@ def get_local_ip(): :return: string """ - ''' - Other domain can using to get IP: - ifconfig.me - icanhazip.com - ipecho.net/plain - ifconfig.co - ''' - endpoint = "https://checkip.amazonaws.com/" + svc_list = { + 'amazonaws': 'https://checkip.amazonaws.com/', + 'ifconfig.me': 'https://ifconfig.me/ip', + 'icanhazip.com': 'https://icanhazip.com/', + 'ipecho': 'https://ipecho.net/plain', + } + + if PUBLIC_IP_SERVICE not in svc_list: + raise ValueError("Unknown service") + + endpoint = svc_list[PUBLIC_IP_SERVICE] return make_request(url=endpoint).strip().decode('utf-8') @@ -171,6 +175,7 @@ def get_config(config_path='config.ini'): :return: """ global CF_API_TOKEN + global PUBLIC_IP_SERVICE if not path.exists(config_path): raise RuntimeError("config file not found") @@ -186,6 +191,9 @@ def get_config(config_path='config.ini'): CF_API_TOKEN = config['common']['CF_API_TOKEN'] + if config['common'].get('CHECK_IP_SERVICE'): + PUBLIC_IP_SERVICE = config['common']['CHECK_IP_SERVICE'] + config_sections = config.sections() config_sections.remove("common") @@ -235,7 +243,8 @@ def main(args): public_ip = get_local_ip() print("") - print("--- Public IP: {}".format(public_ip)) + print("--- [{}] Public IP: {}".format(PUBLIC_IP_SERVICE, public_ip)) + if not IS_DRYRUN and public_ip == get_old_ip(): print("Skip update") exit()