update_namecheap in python
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
{ pkgs, lib, user, host, ... }:
|
||||
{ config, pkgs, lib, user, host, ... }:
|
||||
lib.mkIf (host == "NxACE")
|
||||
{
|
||||
sops.secrets = {
|
||||
"nx2site/namecheap.pw" = { };
|
||||
};
|
||||
|
||||
systemd = {
|
||||
timers."namecheap-dynamic-dns" = {
|
||||
wantedBy = [ "timers.target" ];
|
||||
@@ -12,33 +16,63 @@ lib.mkIf (host == "NxACE")
|
||||
};
|
||||
services."namecheap-dynamic-dns" =
|
||||
let
|
||||
u = pkgs.writeScriptBin "update_namecheap" ''
|
||||
my_ip=$(${pkgs.curl}/bin/curl -s https://ipinfo.io/ip)
|
||||
dns_ip=$(${pkgs.dig}/bin/nslookup nx2.site | grep -E "Address: [0-9]" | cut -c 10-)
|
||||
fdc="/home/nx2/nx2site/domain/count.txt"
|
||||
u = let
|
||||
domain = "nx2.site";
|
||||
passord-file-path = config.sops.secrets."nx2site/namecheap.pw".path;
|
||||
log-file-path = "/var/log/update_namecheap.log";
|
||||
count-file-path = "/var/log/update_namecheap-count.txt";
|
||||
in
|
||||
pkgs.writers.writePython3Bin "update_namecheap" {
|
||||
libraries = with pkgs.python311Packages; [
|
||||
requests
|
||||
];
|
||||
flakeIgnore = [ "E501" "E305" "E701" "E704" "E302" "E114" "F841" ];
|
||||
} ''
|
||||
import requests
|
||||
import argparse
|
||||
import socket
|
||||
from datetime import datetime
|
||||
|
||||
force_update=false
|
||||
def get_public_ip(): return requests.get('https://ipinfo.io/ip').text.strip()
|
||||
|
||||
while getopts "f" opt; do
|
||||
case $opt in
|
||||
f)
|
||||
force_update=true
|
||||
;;
|
||||
\?)
|
||||
echo "Invalid option: -$OPTARG" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
def get_dns_ip(): return socket.gethostbyname_ex('${domain}')[2][0]
|
||||
|
||||
if [ "$force_update" == true ] || [ "$my_ip" != "$dns_ip" ]; then
|
||||
count=$(<$fdc)
|
||||
echo [ $count times ] $(date) from $dns_ip to $my_ip >> /home/nx2/nx2site/domain/updates.log
|
||||
pw=$(cat /home/nx2/nx2site/domain/pw.txt)
|
||||
${pkgs.curl}/bin/curl -s "https://dynamicdns.park-your-domain.com/update?host=@&domain=nx2.site&password=$pw&ip=$my_ip"
|
||||
${pkgs.curl}/bin/curl -s "https://dynamicdns.park-your-domain.com/update?host=*&domain=nx2.site&password=$pw&ip=$my_ip"
|
||||
echo 0 > $fdc
|
||||
fi
|
||||
def main(force_update):
|
||||
my_ip = get_public_ip()
|
||||
dns_ip = get_dns_ip()
|
||||
|
||||
with open("${count-file-path}", "r") as f:
|
||||
content = f.read()
|
||||
if content == "": count = 0
|
||||
else: count = int(content)
|
||||
count += 1
|
||||
with open("${count-file-path}", "w") as f:
|
||||
f.write(str(count))
|
||||
|
||||
if not (force_update or my_ip != dns_ip):
|
||||
print(f"Host IP and DNS response are both {my_ip} --> No Action")
|
||||
exit(0)
|
||||
else:
|
||||
with open("${passord-file-path}", 'r') as pw_file: pw = pw_file.read().strip()
|
||||
|
||||
# Perform DNS updates
|
||||
resp_base = requests.get(f"https://dynamicdns.park-your-domain.com/update?host=@&domain=${domain}&password={pw}&ip={my_ip}")
|
||||
resp_subd = requests.get(f"https://dynamicdns.park-your-domain.com/update?host=*&domain=${domain}&password={pw}&ip={my_ip}")
|
||||
|
||||
# Reset the count file
|
||||
with open("${count-file-path}", 'w') as f: f.write('0')
|
||||
|
||||
now_str = datetime.now().strftime('%Y/%m/%d-%R')
|
||||
log_entry = f"At {now_str} - from {dns_ip} to {my_ip} - {count} times - Response {resp_base.status_code}{' - (forced)' if force_update else ' '}\n"
|
||||
print(log_entry, end="")
|
||||
with open("${log-file-path}", 'a') as log_file: log_file.write(log_entry)
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-f', '--force', action='store_true', help='Force update')
|
||||
args = parser.parse_args()
|
||||
|
||||
main(args.force)
|
||||
'';
|
||||
in
|
||||
{
|
||||
@@ -48,7 +82,7 @@ lib.mkIf (host == "NxACE")
|
||||
'';
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
User = "nx2";
|
||||
# User = "nx2";
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user