Skip to content

Commit

Permalink
extract ip address from netif
Browse files Browse the repository at this point in the history
Signed-off-by: Comic Chang <comicchang@gmail.com>
  • Loading branch information
comicchang committed Sep 27, 2022
1 parent b0a396b commit d9ba3de
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
34 changes: 24 additions & 10 deletions cloudflare-ddns.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import threading
import time
import requests
import netifaces as ni

CONFIG_PATH = os.environ.get('CONFIG_PATH', os.getcwd())

CONFIG_PATH = os.environ.get('CONFIG_PATH', os.path.dirname(os.path.realpath(__file__)) + '/')

class GracefulExit:
def __init__(self):
Expand Down Expand Up @@ -56,12 +56,17 @@ def getIPs():
global ipv4_enabled
global ipv6_enabled
global purgeUnknownRecords
global method
global interfaces
if ipv4_enabled:
try:
a = requests.get(
"https://1.1.1.1/cdn-cgi/trace").text.split("\n")
a.pop()
a = dict(s.split("=") for s in a)["ip"]
if method == 'http':
a = requests.get(
"https://1.1.1.1/cdn-cgi/trace").text.split("\n")
a.pop()
a = dict(s.split("=") for s in a)["ip"]
else:
a = ni.ifaddresses(interface)[ni.AF_INET][0]['addr']
except Exception:
global shown_ipv4_warning
if not shown_ipv4_warning:
Expand All @@ -71,10 +76,13 @@ def getIPs():
deleteEntries("A")
if ipv6_enabled:
try:
aaaa = requests.get(
"https://[2606:4700:4700::1111]/cdn-cgi/trace").text.split("\n")
aaaa.pop()
aaaa = dict(s.split("=") for s in aaaa)["ip"]
if method == 'http':
aaaa = requests.get(
"https://[2606:4700:4700::1111]/cdn-cgi/trace").text.split("\n")
aaaa.pop()
aaaa = dict(s.split("=") for s in aaaa)["ip"]
else:
aaaa = ni.ifaddresses(interface)[ni.AF_INET6][0]['addr']
except Exception:
global shown_ipv6_warning
if not shown_ipv6_warning:
Expand Down Expand Up @@ -208,6 +216,8 @@ def updateIPs(ips):
shown_ipv6_warning = False
ipv4_enabled = True
ipv6_enabled = True
method = 'http'
interface = ''
purgeUnknownRecords = False

if sys.version_info < (3, 5):
Expand All @@ -226,9 +236,13 @@ def updateIPs(ips):
try:
ipv4_enabled = config["a"]
ipv6_enabled = config["aaaa"]
method = config["method"]
interface = config["interface"]
except:
ipv4_enabled = True
ipv6_enabled = True
method = 'http'
interface = ''
print("⚙️ Individually disable IPv4 or IPv6 with new config.json options. Read more about it here: https://github.com/timothymiller/cloudflare-ddns/blob/master/README.md")
try:
purgeUnknownRecords = config["purgeUnknownRecords"]
Expand Down
2 changes: 2 additions & 0 deletions config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
],
"a": true,
"aaaa": true,
"method": "http",
"interface": "ppp0",
"purgeUnknownRecords": false,
"ttl": 300
}
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests==2.25.0
requests==2.25.0
netifaces==0.11.0

0 comments on commit d9ba3de

Please sign in to comment.