mirror of
https://github.com/pacnpal/simpleguardhome.git
synced 2025-12-19 20:11:14 -05:00
Update adguard.py
This commit is contained in:
@@ -75,6 +75,22 @@ def validate_domain(domain: str) -> bool:
|
||||
return False
|
||||
return bool(DOMAIN_PATTERN.match(domain))
|
||||
|
||||
def get_parent_domains(domain: str) -> List[str]:
|
||||
"""Get all parent domains for a given domain, excluding the TLD.
|
||||
|
||||
Example:
|
||||
Input: track.soclevercomm.jmsend.com
|
||||
Output: [
|
||||
'track.soclevercomm.jmsend.com',
|
||||
'soclevercomm.jmsend.com',
|
||||
'jmsend.com'
|
||||
]
|
||||
"""
|
||||
parts = domain.split('.')
|
||||
# Only return domains that have at least one subdomain (length >= 2)
|
||||
# This excludes TLDs like 'com', 'net', 'org', etc.
|
||||
return ['.'.join(parts[i:]) for i in range(len(parts)-1)]
|
||||
|
||||
def sanitize_rule(rule: str) -> str:
|
||||
"""Sanitize and validate rule format."""
|
||||
# Remove any whitespace and normalize
|
||||
@@ -166,7 +182,6 @@ class AdGuardClient:
|
||||
|
||||
await self._ensure_authenticated()
|
||||
url = f"{self.base_url}/filtering/check_host"
|
||||
params = {"name": domain}
|
||||
headers = {}
|
||||
|
||||
if self._session_cookie:
|
||||
@@ -174,6 +189,11 @@ class AdGuardClient:
|
||||
|
||||
try:
|
||||
logger.info(f"Checking domain: {domain}")
|
||||
# Get all parent domains to check (excluding TLD)
|
||||
domains_to_check = get_parent_domains(domain)
|
||||
|
||||
for check_domain in domains_to_check:
|
||||
params = {"name": check_domain}
|
||||
response = await self.client.get(url, params=params, headers=headers)
|
||||
|
||||
if response.status_code == 401:
|
||||
@@ -185,6 +205,17 @@ class AdGuardClient:
|
||||
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
|
||||
# If this domain is filtered, return the result
|
||||
if result.get("reason", "").startswith("Filtered"):
|
||||
logger.info(f"Domain {domain} is filtered due to parent domain {check_domain}")
|
||||
logger.info(f"Domain check result: {result}")
|
||||
return FilterCheckHostResponse(**result)
|
||||
|
||||
# If no parent domains are filtered, return the result for the original domain
|
||||
params = {"name": domain}
|
||||
response = await self.client.get(url, params=params, headers=headers)
|
||||
result = response.json()
|
||||
logger.info(f"Domain check result for {domain}: {result}")
|
||||
return FilterCheckHostResponse(**result)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user