Fixed IPv6 access inside Docker container

This commit is contained in:
timothymiller 2020-08-13 18:26:43 -04:00
parent 1e55144bf2
commit 5ac69b8274
5 changed files with 30 additions and 32 deletions

View File

@ -9,13 +9,14 @@
".vscode": true, ".vscode": true,
"LICENSE": true, "LICENSE": true,
"requirements.txt": true, "requirements.txt": true,
"build-docker-image.sh": false, "build-docker-image.sh": true,
".gitignore": true, ".gitignore": true,
"Dockerfile": false, "Dockerfile": true,
"start-sync.sh": false, "start-sync.sh": true,
"venv": true "venv": true
}, },
"explorerExclude.backup": null, "explorerExclude.backup": null,
"python.linting.pylintEnabled": true, "python.linting.pylintEnabled": true,
"python.linting.enabled": true "python.linting.enabled": true,
"python.pythonPath": "venv/bin/python"
} }

View File

@ -11,7 +11,7 @@ RUN pip install -r requirements.txt
# #
# ---- Release ---- # ---- Release ----
FROM dependencies AS release FROM dependencies AS release
# copy project file(s) # copy project source file(s)
WORKDIR / WORKDIR /
COPY cloudflare-ddns.py . COPY cloudflare-ddns.py .
CMD ["python", "/cloudflare-ddns.py", "--repeat"] CMD ["python", "-u", "/cloudflare-ddns.py", "--repeat"]

View File

@ -1,5 +1,5 @@
import requests, json, sys, os import requests, json, sys, os
import time, traceback import time
PATH = os.getcwd() + "/" PATH = os.getcwd() + "/"
version = float(str(sys.version_info[0]) + "." + str(sys.version_info[1])) 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: with open(PATH + "config.json") as config_file:
config = json.loads(config_file.read()) config = json.loads(config_file.read())
def getIPs(): def getIPs():
a = requests.get("https://api.ipify.org?format=json").json().get("ip") a = requests.get("https://api.ipify.org?format=json").json().get("ip")
aaaa = requests.get("https://api6.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", "type": "AAAA",
"ip": aaaa "ip": aaaa
}) })
else:
print("Warning: IPv6 not detected.")
return ips return ips
@ -102,26 +103,21 @@ def cf_api(endpoint, method, config, headers={}, data=False):
return response.json() 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(): def updateIPs():
for ip in getIPs(): for ip in getIPs():
print("Checking " + ip["type"] + " records")
commitRecord(ip) commitRecord(ip)
if(len(sys.argv) > 1): if(len(sys.argv) > 1):
if(sys.argv[1] == "--repeat"): if(sys.argv[1] == "--repeat"):
import threading print("Updating A & AAAA records every 15 minutes")
threading.Thread(target=lambda: every(60*15, updateIPs)).start() updateIPs()
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()

View File

@ -1,13 +1,14 @@
version: "3.7" version: "3.7"
services: services:
cloudflare-ddns: cloudflare-ddns:
image: timothymiller/cloudflare-ddns:latest image: timothyjmiller/cloudflare-ddns:latest
container_name: cloudflare-ddns container_name: cloudflare-ddns
security_opt: security_opt:
- no-new-privileges:true - no-new-privileges:true
network_mode: "host"
environment: environment:
- PUID=1000 - PUID=1000
- PGID=1000 - PGID=1000
volumes: volumes:
- /EDIT/YOUR/PATH/HERE/config.json:/config.json - /YOUR/PATH/HERE/config.json:/config.json
restart: unless-stopped restart: unless-stopped