mirror of
https://github.com/KevinMidboe/cloudflare-ddns.git
synced 2025-10-29 09:30:17 +00:00
Send SMS to notify when IP cycles
This commit is contained in:
@@ -8,3 +8,5 @@ metadata:
|
||||
data:
|
||||
DDNS_ZONE: ${DDNS_ZONE}
|
||||
API_KEY: ${API_KEY}
|
||||
SMS_API_KEY: ${SMS_API_KEY}
|
||||
SMS_RECIPIENT: ${SMS_RECIPIENT}
|
||||
|
||||
12
main.py
12
main.py
@@ -1,12 +1,14 @@
|
||||
import os
|
||||
import requests
|
||||
from bulk_dns_update import updateAllZones, setAPIKey, getDDNSAddresszoneId
|
||||
from notify import notify
|
||||
from dotenv import load_dotenv
|
||||
from logger import logger
|
||||
|
||||
load_dotenv()
|
||||
|
||||
currentIP = None
|
||||
recordedIP = None
|
||||
DDNS_ZONE = os.getenv('DDNS_ZONE')
|
||||
|
||||
|
||||
@@ -20,6 +22,7 @@ def publicAddress():
|
||||
|
||||
|
||||
def cloudflareDDNS():
|
||||
global recordedIP
|
||||
logger.info('Checking IP recorded in Cloudflare...')
|
||||
ddnsRecord = getDDNSAddresszoneId(DDNS_ZONE)
|
||||
recordedIP = ddnsRecord['content']
|
||||
@@ -27,9 +30,10 @@ def cloudflareDDNS():
|
||||
|
||||
if currentIP != recordedIP:
|
||||
logger.info('Public IP has changed, updating all A records.')
|
||||
updateAllZones(recordedIP, currentIP)
|
||||
return True
|
||||
else:
|
||||
logger.info('is same, exiting')
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
@@ -42,7 +46,11 @@ def main():
|
||||
setAPIKey(apiKey)
|
||||
|
||||
publicAddress()
|
||||
cloudflareDDNS()
|
||||
changed = cloudflareDDNS()
|
||||
|
||||
if changed:
|
||||
notify("IP changed to: {}. Updating all cloudflare zones!".format(currentIP))
|
||||
updateAllZones(recordedIP, currentIP)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
42
notify.py
Normal file
42
notify.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import base64
|
||||
import requests
|
||||
from os import getenv
|
||||
from json import dumps
|
||||
from logger import logger
|
||||
|
||||
|
||||
def notify(message):
|
||||
SMS_API_KEY = getenv('SMS_API_KEY')
|
||||
SMS_RECIPIENT = getenv('SMS_RECIPIENT')
|
||||
|
||||
if not SMS_API_KEY:
|
||||
logger.info("No SMS API key found, not notifying")
|
||||
return
|
||||
|
||||
if not SMS_RECIPIENT:
|
||||
logger.info("No SMS recipient found, not notifying")
|
||||
return
|
||||
|
||||
recipient = "47{}".format(SMS_RECIPIENT)
|
||||
apiKey = base64.b64encode(SMS_API_KEY.encode("utf-8")).decode("utf-8")
|
||||
|
||||
logger.info("Notifying of IP change over SMS")
|
||||
url = "https://gatewayapi.com/rest/mtsms"
|
||||
payload = {
|
||||
"sender": "Dynamic DNS",
|
||||
"message": message,
|
||||
"recipients": [{"msisdn": recipient}]
|
||||
}
|
||||
headers = {
|
||||
"Host": "gatewayapi.com",
|
||||
"Authorization": "Basic " + apiKey,
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
r = requests.post(url, data=dumps(payload), headers=headers)
|
||||
response = r.json()
|
||||
|
||||
logger.info("Response from SMS api")
|
||||
logger.info("Status: {} {}".format(str(r.status_code), str(r.reason)))
|
||||
logger.info(response)
|
||||
Reference in New Issue
Block a user