diff --git a/.SRCINFO b/.SRCINFO new file mode 100644 index 0000000..c23b3cb --- /dev/null +++ b/.SRCINFO @@ -0,0 +1,20 @@ +pkgbase = porkbun-dynamic-dns-python-git + pkgdesc = Our minimalist dynamic DNS client written in Python + pkgver = r14.e02d543 + pkgrel = 1 + url = https://github.com/Ta180m/porkbun-dynamic-dns-python + arch = any + license = MIT + depends = python + depends = python-requests + backup = etc/porkbun-ddns.json + source = porkbun-ddns.py + source = config.json.example + source = systemd.service + source = systemd.timer + sha256sums = SKIP + sha256sums = SKIP + sha256sums = SKIP + sha256sums = SKIP + +pkgname = porkbun-dynamic-dns-python-git diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 0000000..7da3ed9 --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,30 @@ +# Maintainer: Anthony Wang +_pkgname=porkbun-dynamic-dns-python +pkgname=${_pkgname}-git +pkgver=r14.e02d543 +pkgrel=1 +pkgdesc="Our minimalist dynamic DNS client written in Python" +arch=(any) +url="https://github.com/Ta180m/porkbun-dynamic-dns-python" +license=('MIT') +depends=('python' 'python-requests') +backup=('etc/porkbun-ddns.json') +source=("porkbun-ddns.py" + "config.json.example" + "systemd.service" + "systemd.timer") +sha256sums=(SKIP + SKIP + SKIP + SKIP) + +pkgver() { + printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)" +} + +package() { + install -vDm755 porkbun-ddns.py "$pkgdir/usr/bin/porkbun-ddns" + install -vDm600 config.json.example "$pkgdir/etc/porkbun-ddns.json" + install -vDm644 systemd.service "$pkgdir/usr/lib/systemd/system/porkbun-ddns.service" + install -vDm644 systemd.timer "$pkgdir/usr/lib/systemd/system/porkbun-ddns.timer" +} diff --git a/config.json.example b/config.json.example index 2936205..cde7977 100644 --- a/config.json.example +++ b/config.json.example @@ -1,4 +1,5 @@ -{ "endpoint":"https://api-ipv4.porkbun.com/api/json/v3", +{ + "domain": "", "apikey": "", "secretapikey": "" -} \ No newline at end of file +} diff --git a/porkbun-ddns.py b/porkbun-ddns.py index 1401bd5..9db484f 100755 --- a/porkbun-ddns.py +++ b/porkbun-ddns.py @@ -61,7 +61,7 @@ def main(args): description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter ) parser.add_argument("config", nargs=1, help="path to config file") - parser.add_argument("domain", nargs=1, help="domain to be updated") + parser.add_argument("domain", nargs="?", default="", help="domain to be updated") parser.add_argument("subdomain", nargs="?", default="", help="optional subdomain") parser.add_argument( "-i", @@ -70,19 +70,30 @@ def main(args): ) args = parser.parse_args() - args.domain, args.config = args.domain[0], args.config[0] - args.fqdn = "{}.{}".format(args.subdomain, args.domain).strip(".") + args.config = args.config[0] try: with sys.stdin if args.config == "-" else open(args.config) as file_: args.cfg = json.load(file_) except Exception as e: err(e) + + # Load domain from JSON config file if not specified as an arg + if args.domain == "": + args.domain = args.cfg["domain"] + if args.subdomain == "" and "subdomain" in args.cfg: + args.subdomain = args.cfg["subdomain"] + + args.fqdn = "{}.{}".format(args.subdomain, args.domain).strip(".") + required = ["secretapikey", "apikey"] if any(x not in args.cfg for x in required) or not isinstance(args.cfg, dict): err("all of the following are required in '{}': {}", args.config, required) args.cfg.setdefault("endpoint", "https://porkbun.com/api/json/v3/") + if not args.public_ip and "public_ip" in args.cfg: + args.public_ip = args.cfg["public_ip"] + if not args.public_ip: args.public_ip = api(args, "/ping/")["yourIp"] args.public_ip = ipaddress.ip_address(args.public_ip) diff --git a/systemd.service b/systemd.service new file mode 100644 index 0000000..d4f2d3f --- /dev/null +++ b/systemd.service @@ -0,0 +1,5 @@ +[Unit] +Description=Detect external IP address and create/update a corresponding DNS record + +[Service] +ExecStart=/usr/bin/porkbun-ddns /etc/porkbun-ddns.json diff --git a/systemd.timer b/systemd.timer new file mode 100644 index 0000000..c73aee3 --- /dev/null +++ b/systemd.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Detect external IP address and create/update a corresponding DNS record every 15 minutes + +[Timer] +OnBootSec=15min +OnUnitActiveSec=15min + +[Install] +WantedBy=timers.target