This commit is contained in:
0
dyndns/__init__.py
Normal file
0
dyndns/__init__.py
Normal file
42
dyndns/aws.py
Executable file
42
dyndns/aws.py
Executable file
@@ -0,0 +1,42 @@
|
||||
import json
|
||||
import argparse
|
||||
from datetime import datetime
|
||||
from subprocess import check_call
|
||||
from .utils import get_public_ip
|
||||
|
||||
def run():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-z', "--hosted-zone", metavar='HOSTED_ZONE_ID', required=True,
|
||||
help="Specify the hosted zone id")
|
||||
parser.add_argument('-d', "--domain", metavar='DOMAIN_NAME', required=True,
|
||||
help="Specify domain name")
|
||||
parser.add_argument('-t', "--type", metavar='DNS_TYPE', default='A',
|
||||
help="Specify DNS type")
|
||||
parser.add_argument('-l', "--ttl", metavar='TTL_S', type=int, default=300,
|
||||
help="Specify time-to-live in seconds")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||
public_ip = get_public_ip()
|
||||
params = {
|
||||
'Comment': f'Auto update by awsdyndns on {date}',
|
||||
'Changes': [
|
||||
{
|
||||
'Action': 'UPSERT',
|
||||
'ResourceRecordSet': {
|
||||
'ResourceRecords': [{'Value': public_ip}],
|
||||
'Name': args.domain,
|
||||
'Type': args.type,
|
||||
'TTL': args.ttl
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
check_call(['aws', 'route53', 'change-resource-record-sets', '--hosted-zone-id', args.hosted_zone, '--change-batch', json.dumps(params)])
|
||||
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
|
||||
|
||||
|
82
dyndns/cloudflare.py
Normal file
82
dyndns/cloudflare.py
Normal file
@@ -0,0 +1,82 @@
|
||||
import requests
|
||||
import json
|
||||
import argparse
|
||||
from logging import getLogger
|
||||
from logging.config import dictConfig
|
||||
from os import environ
|
||||
from .utils import get_public_ip, parse_log_config
|
||||
|
||||
dictConfig(parse_log_config())
|
||||
|
||||
# Define your variables
|
||||
cloudflare_api_key = environ['CLOUDFLARE_API_KEY']
|
||||
|
||||
log = getLogger(__name__)
|
||||
|
||||
def update_dns_record(
|
||||
zone_id: str,
|
||||
dns_record_id: str,
|
||||
ip_address: str,
|
||||
domain_name: str,
|
||||
ttl: int,
|
||||
record_type : str,
|
||||
comment : str = None
|
||||
):
|
||||
# Set the URL
|
||||
url = f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{dns_record_id}'
|
||||
|
||||
# Set the headers
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': f'Bearer {cloudflare_api_key}'
|
||||
}
|
||||
|
||||
# Define the data to be sent in the PATCH request
|
||||
data = {
|
||||
"comment": comment,
|
||||
"content": ip_address,
|
||||
"name": domain_name,
|
||||
"proxied": True,
|
||||
"ttl": ttl,
|
||||
"type": record_type
|
||||
}
|
||||
|
||||
# Make the PATCH request
|
||||
response = requests.patch(url, headers=headers, data=json.dumps(data))
|
||||
|
||||
# Check the response
|
||||
if response.status_code == 200:
|
||||
log.info('DNS record updated successfully:', response.json())
|
||||
else:
|
||||
msg = f'Failed to update DNS record: {response.status_code} {response.text}'
|
||||
raise RuntimeError(msg)
|
||||
|
||||
|
||||
def run():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-z', "--hosted-zone", metavar='HOSTED_ZONE_ID', required=True,
|
||||
help="Specify the hosted zone id")
|
||||
parser.add_argument('-d', "--domain", metavar='DOMAIN_NAME', required=True,
|
||||
help="Specify domain name")
|
||||
parser.add_argument('-t', "--type", metavar='DNS_TYPE', default='A',
|
||||
help="Specify DNS type")
|
||||
parser.add_argument('-l', "--ttl", metavar='TTL_S', type=int, default=300,
|
||||
help="Specify time-to-live in seconds")
|
||||
parser.add_argument('-r', "--record-id", metavar='RECORD_ID',
|
||||
help="Cloudflare DNS record id")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
public_ip = get_public_ip()
|
||||
|
||||
update_dns_record(
|
||||
zone_id=args.hosted_zone,
|
||||
domain_name=args.domain,
|
||||
record_type=args.type,
|
||||
ip_address=public_ip,
|
||||
ttl=args.ttl,
|
||||
dns_record_id=args.record_id
|
||||
)
|
||||
|
||||
|
||||
|
7
dyndns/utils.py
Normal file
7
dyndns/utils.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from urllib.request import urlopen, Request
|
||||
|
||||
def get_public_ip() -> str:
|
||||
url = "http://v4.ipv6-test.com/api/myip.php"
|
||||
request = Request(url)
|
||||
with urlopen(request) as request:
|
||||
return request.read().decode()
|
Reference in New Issue
Block a user