diff --git a/.vscode/settings.json b/.vscode/settings.json index 856150f..41521cd 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,13 +9,14 @@ ".vscode": true, "LICENSE": true, "requirements.txt": true, - "build-docker-image.sh": false, + "build-docker-image.sh": true, ".gitignore": true, - "Dockerfile": false, - "start-sync.sh": false, + "Dockerfile": true, + "start-sync.sh": true, "venv": true }, "explorerExclude.backup": null, "python.linting.pylintEnabled": true, - "python.linting.enabled": true + "python.linting.enabled": true, + "python.pythonPath": "venv/bin/python" } \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 65ea052..c0b4891 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,7 @@ RUN pip install -r requirements.txt # # ---- Release ---- FROM dependencies AS release -# copy project file(s) +# copy project source file(s) WORKDIR / COPY cloudflare-ddns.py . -CMD ["python", "/cloudflare-ddns.py", "--repeat"] \ No newline at end of file +CMD ["python", "-u", "/cloudflare-ddns.py", "--repeat"] \ No newline at end of file diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index 5274e26..dc620bf 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -1,5 +1,5 @@ import requests, json, sys, os -import time, traceback +import time PATH = os.getcwd() + "/" version = float(str(sys.version_info[0]) + "." + str(sys.version_info[1])) @@ -10,7 +10,6 @@ if(version < 3.5): with open(PATH + "config.json") as config_file: config = json.loads(config_file.read()) - def getIPs(): a = requests.get("https://api.ipify.org?format=json").json().get("ip") aaaa = requests.get("https://api6.ipify.org?format=json").json().get("ip") @@ -27,6 +26,8 @@ def getIPs(): "type": "AAAA", "ip": aaaa }) + else: + print("Warning: IPv6 not detected.") return ips @@ -102,26 +103,21 @@ def cf_api(endpoint, method, config, headers={}, data=False): return response.json() -def every(delay, task): - next_time = time.time() + delay - while True: - time.sleep(max(0, next_time - time.time())) - try: - task() - except Exception: - traceback.print_exc() - # in production code you might want to have this instead of course: - # logger.exception("Problem while executing repetitive task.") - # skip tasks if we are behind schedule: - next_time += (time.time() - next_time) // delay * delay + delay - def updateIPs(): for ip in getIPs(): - print("Checking " + ip["type"] + " records") commitRecord(ip) if(len(sys.argv) > 1): if(sys.argv[1] == "--repeat"): - import threading - threading.Thread(target=lambda: every(60*15, updateIPs)).start() -updateIPs() + print("Updating A & AAAA records every 15 minutes") + updateIPs() + delay = 15*60 # 15 minutes + next_time = time.time() + delay + while True: + time.sleep(max(0, next_time - time.time())) + updateIPs() + next_time += (time.time() - next_time) // delay * delay + delay + else: + print("Unrecognized parameter '" + sys.argv[1] + "'. Stopping now.") +else: + updateIPs() \ No newline at end of file diff --git a/config-example.json b/config-example.json index 8b1dd38..3de729b 100755 --- a/config-example.json +++ b/config-example.json @@ -3,14 +3,14 @@ { "authentication": { "api_token": "api_token_here", - "api_key": { - "api_key": "api_key_here", - "account_email": "your_email_here" - } + "api_key": { + "api_key": "api_key_here", + "account_email": "your_email_here" + } }, "zone_id": "your_zone_id_here", "subdomains": [ - "", + "", "subdomain" ], "proxied": false diff --git a/docker-compose.yml b/docker-compose.yml index 07ec82a..3af0888 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,13 +1,14 @@ version: "3.7" services: cloudflare-ddns: - image: timothymiller/cloudflare-ddns:latest + image: timothyjmiller/cloudflare-ddns:latest container_name: cloudflare-ddns security_opt: - no-new-privileges:true + network_mode: "host" environment: - PUID=1000 - PGID=1000 volumes: - - /EDIT/YOUR/PATH/HERE/config.json:/config.json + - /YOUR/PATH/HERE/config.json:/config.json restart: unless-stopped \ No newline at end of file