aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md12
-rwxr-xr-xhe-dyndns28
-rw-r--r--tests/test_he_dyndns.py44
3 files changed, 84 insertions, 0 deletions
diff --git a/README.md b/README.md
index 3e1ffe8..05fc30e 100644
--- a/README.md
+++ b/README.md
@@ -62,6 +62,7 @@ Other configuration sections will be ignored.
```
usage: he-dyndns [-h] [--v4 | --v6] [--addr4 ADDR4 | --addr6 ADDR6]
[--check-url CHECK_URL] [--state-file STATE_FILE]
+ [--initialize-state]
[--keyfile KEYFILE] [--key KEY] [--timeout TIMEOUT] [--debug]
record
@@ -78,6 +79,8 @@ options:
--addr6 ADDR6 Use this IPv6 address; bypass discovery/state comparison
--check-url URL Public address service (default https://ip.mfamily.org/)
--state-file FILE Local comparison state (default /var/lib/he-dyndns/state.json)
+ --initialize-state Discover and store the current IPv4 and IPv6 addresses,
+ then exit without updating HE
--keyfile KEYFILE Key config file (default /etc/he-dyndns.conf)
--key KEY HE DDNS key for record; overrides --keyfile
--timeout SECONDS Network timeout in seconds (default 10)
@@ -89,6 +92,15 @@ options:
Returns 0 on success (successful change or no change) or 1 on failure.
Failures also include a single line error message.
+To seed both address families without reading an HE key or attempting an HE
+update:
+
+```
+he-dyndns --initialize-state home.example.com
+```
+
+Both discoveries must succeed before the state file is changed.
+
For example, two cron entries can check both families while sharing the state
file:
diff --git a/he-dyndns b/he-dyndns
index e4bfd39..18a5932 100755
--- a/he-dyndns
+++ b/he-dyndns
@@ -216,6 +216,11 @@ def make_parser():
help="local comparison state (default: %(default)s)",
)
parser.add_argument(
+ "--initialize-state",
+ action="store_true",
+ help="record the current IPv4 and IPv6 addresses, then exit without updating HE",
+ )
+ parser.add_argument(
"--keyfile", default="/etc/he-dyndns.conf",
help="key config file (default: %(default)s)",
)
@@ -226,6 +231,29 @@ def make_parser():
def run(args, parser):
+ if args.initialize_state:
+ if args.v4 or args.v6 or args.addr4 or args.addr6:
+ parser.error(
+ "--initialize-state cannot be combined with address-family or explicit-address options"
+ )
+ with state_lock(args.state_file):
+ addresses = {
+ str(version): str(
+ discover_address(args.check_url, version, timeout=args.timeout)
+ )
+ for version in (4, 6)
+ }
+ state = read_state(args.state_file)
+ state[args.record] = addresses
+ write_state(args.state_file, state)
+ log.debug(
+ "initialized state for %s: IPv4 %s, IPv6 %s",
+ args.record,
+ addresses["4"],
+ addresses["6"],
+ )
+ return
+
version = selected_version(args, parser)
explicit_value = args.addr4 if version == 4 else args.addr6
key = args.key if args.key is not None else get_key(args.keyfile, args.record)
diff --git a/tests/test_he_dyndns.py b/tests/test_he_dyndns.py
index 0fbe343..f7ea0e7 100644
--- a/tests/test_he_dyndns.py
+++ b/tests/test_he_dyndns.py
@@ -94,6 +94,50 @@ class StateTests(unittest.TestCase):
@mock.patch.object(he_dyndns, "update_he")
@mock.patch.object(he_dyndns, "discover_address")
+ def test_initialize_state_discovers_both_families_without_he(self, discover, update):
+ discover.side_effect = [
+ ipaddress.ip_address("192.0.2.60"),
+ ipaddress.ip_address("2001:db8::60"),
+ ]
+ args = self.parser.parse_args([
+ "home.example", "--initialize-state", "--state-file", self.state_path
+ ])
+
+ he_dyndns.run(args, self.parser)
+
+ self.assertEqual(
+ [call.args[1] for call in discover.call_args_list],
+ [4, 6],
+ )
+ update.assert_not_called()
+ with open(self.state_path, encoding="utf-8") as state_file:
+ self.assertEqual(
+ json.load(state_file)["home.example"],
+ {"4": "192.0.2.60", "6": "2001:db8::60"},
+ )
+
+ @mock.patch.object(he_dyndns, "update_he")
+ @mock.patch.object(he_dyndns, "discover_address")
+ def test_initialize_failure_does_not_change_existing_state(self, discover, update):
+ existing = {"home.example": {"4": "192.0.2.10", "6": "2001:db8::10"}}
+ he_dyndns.write_state(self.state_path, existing)
+ discover.side_effect = [
+ ipaddress.ip_address("192.0.2.60"),
+ RuntimeError("IPv6 unavailable"),
+ ]
+ args = self.parser.parse_args([
+ "home.example", "--initialize-state", "--state-file", self.state_path
+ ])
+
+ with self.assertRaisesRegex(RuntimeError, "IPv6 unavailable"):
+ he_dyndns.run(args, self.parser)
+
+ update.assert_not_called()
+ with open(self.state_path, encoding="utf-8") as state_file:
+ self.assertEqual(json.load(state_file), existing)
+
+ @mock.patch.object(he_dyndns, "update_he")
+ @mock.patch.object(he_dyndns, "discover_address")
def test_unchanged_discovered_address_skips_update(self, discover, update):
address = ipaddress.ip_address("192.0.2.44")
discover.return_value = address