52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
import asyncio
|
|
import hashlib
|
|
import json
|
|
import aiofiles
|
|
from os.path import isfile
|
|
|
|
from pddnsc.base import BaseFilterProvider
|
|
|
|
|
|
class StateHashFilter(BaseFilterProvider):
|
|
async def check_imp(self, source_provider, addr_v4, addr_v6):
|
|
if not isfile(self.config["filepath"]):
|
|
return True
|
|
|
|
new_state_str = (addr_v4 or "") + (addr_v6 or "")
|
|
new_sha = hashlib.sha256(new_state_str.encode(encoding="utf-8"))
|
|
async with aiofiles.open(
|
|
self.config["filepath"], mode="r", encoding="utf-8"
|
|
) as f:
|
|
old_state_hash = await f.read()
|
|
|
|
return old_state_hash != new_sha.hexdigest()
|
|
|
|
|
|
class StateFileFilter(BaseFilterProvider):
|
|
async def check_imp(self, source_provider, addr_v4, addr_v6):
|
|
if not isfile(self.config["filepath"]):
|
|
return True
|
|
|
|
new_state = {
|
|
"ipv4": addr_v4 or "",
|
|
"ipv6": addr_v6 or "",
|
|
}
|
|
|
|
async with aiofiles.open(
|
|
self.config["filepath"], mode="r", encoding="utf-8"
|
|
) as f:
|
|
old_state = json.loads(await f.read())
|
|
|
|
result = True
|
|
|
|
if "check_ipv4" not in self.config and "check_ipv4" not in self.config:
|
|
return new_state != old_state
|
|
|
|
if self.config.get("check_ipv4", False):
|
|
result = result and new_state["ipv4"] != old_state["ipv4"]
|
|
|
|
if self.config.get("check_ipv6", False):
|
|
result = result and new_state["ipv6"] != old_state["ipv6"]
|
|
|
|
return result
|