Skip to content

Commit

Permalink
Introduction of extra rules for parked domains.
Browse files Browse the repository at this point in the history
This patch touches and fixes #255.

Indeed, before this patch, we didn't bother to try to find parked
domains. This patch introduces a new layout of SPECIAL rules that
tries to check if the tested subject is a parked one.

Warning:
  This patch is at an early stage. It is enabled when anyone
  allow the usage of special rules but it doesn't yet cover
  all possible parked subjects.

Also:
  * Improvment of the extra rules layout by changing structure.

Contributors:
  * @keczuppp
  • Loading branch information
funilrys committed Oct 7, 2022
1 parent e5e4552 commit 1ecb9ed
Show file tree
Hide file tree
Showing 9 changed files with 509 additions and 131 deletions.
39 changes: 36 additions & 3 deletions PyFunceble/checker/availability/base.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
import PyFunceble.facility import PyFunceble.facility
import PyFunceble.factory import PyFunceble.factory
import PyFunceble.storage import PyFunceble.storage
from PyFunceble.checker.availability.extra_rules import ExtraRulesHandler from PyFunceble.checker.availability.extras.base import ExtraRuleHandlerBase
from PyFunceble.checker.availability.extras.parked import ParkedRulesHandler
from PyFunceble.checker.availability.extras.rules import ExtraRulesHandler
from PyFunceble.checker.availability.params import AvailabilityCheckerParams from PyFunceble.checker.availability.params import AvailabilityCheckerParams
from PyFunceble.checker.availability.status import AvailabilityCheckerStatus from PyFunceble.checker.availability.status import AvailabilityCheckerStatus
from PyFunceble.checker.base import CheckerBase from PyFunceble.checker.base import CheckerBase
Expand Down Expand Up @@ -126,7 +128,7 @@ class AvailabilityCheckerBase(CheckerBase):
domain_syntax_checker: Optional[DomainSyntaxChecker] = None domain_syntax_checker: Optional[DomainSyntaxChecker] = None
ip_syntax_checker: Optional[IPSyntaxChecker] = None ip_syntax_checker: Optional[IPSyntaxChecker] = None
url_syntax_checker: Optional[URLSyntaxChecker] = None url_syntax_checker: Optional[URLSyntaxChecker] = None
extra_rules_handler: Optional[ExtraRulesHandler] = None extra_rules_handlers: Optional[List[ExtraRuleHandlerBase]] = None


_use_extra_rules: bool = False _use_extra_rules: bool = False
_use_whois_lookup: bool = False _use_whois_lookup: bool = False
Expand Down Expand Up @@ -162,7 +164,8 @@ def __init__(
self.domain_syntax_checker = DomainSyntaxChecker() self.domain_syntax_checker = DomainSyntaxChecker()
self.ip_syntax_checker = IPSyntaxChecker() self.ip_syntax_checker = IPSyntaxChecker()
self.url_syntax_checker = URLSyntaxChecker() self.url_syntax_checker = URLSyntaxChecker()
self.extra_rules_handler = ExtraRulesHandler() # WARNING: Put the aggressive one first!
self.extra_rules_handlers = [ParkedRulesHandler(), ExtraRulesHandler()]
self.db_session = db_session self.db_session = db_session


self.params = AvailabilityCheckerParams() self.params = AvailabilityCheckerParams()
Expand Down Expand Up @@ -1016,6 +1019,36 @@ def try_to_query_status_from_collection(self) -> "AvailabilityCheckerBase":
self.status.idna_subject, self.status.idna_subject,
) )


return self

def try_to_query_status_from_extra_rules(self) -> "AvailabilityCheckerBase":
"""
Tries to query the status from the extra rules.
"""

PyFunceble.facility.Logger.info(
"Started to try to query the status of %r from: Extra Rules Lookup",
self.status.idna_subject,
)

if self.use_extra_rules:
for rule_handler in self.extra_rules_handlers:
rule_handler.set_status(self.status).start()

if self.status.status_after_extra_rules:
PyFunceble.facility.Logger.info(
"Could define the status of %r from: Extra Rules Lookup",
self.status.idna_subject,
)
break

PyFunceble.facility.Logger.info(
"Finished to try to query the status of %r from: Extra Rules Lookup",
self.status.idna_subject,
)

return self

@CheckerBase.ensure_subject_is_given @CheckerBase.ensure_subject_is_given
@CheckerBase.update_status_date_after_query @CheckerBase.update_status_date_after_query
def query_status(self) -> "AvailabilityCheckerBase": def query_status(self) -> "AvailabilityCheckerBase":
Expand Down
2 changes: 1 addition & 1 deletion PyFunceble/checker/availability/domain.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def query_status(
) )


if self.use_extra_rules: if self.use_extra_rules:
self.extra_rules_handler.set_status(self.status).start() self.try_to_query_status_from_extra_rules()


return self return self


Expand Down
51 changes: 51 additions & 0 deletions PyFunceble/checker/availability/extras/__init__.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
The tool to check the availability or syntax of domain, IP or URL.
::
██████╗ ██╗ ██╗███████╗██╗ ██╗███╗ ██╗ ██████╗███████╗██████╗ ██╗ ███████╗
██╔══██╗╚██╗ ██╔╝██╔════╝██║ ██║████╗ ██║██╔════╝██╔════╝██╔══██╗██║ ██╔════╝
██████╔╝ ╚████╔╝ █████╗ ██║ ██║██╔██╗ ██║██║ █████╗ ██████╔╝██║ █████╗
██╔═══╝ ╚██╔╝ ██╔══╝ ██║ ██║██║╚██╗██║██║ ██╔══╝ ██╔══██╗██║ ██╔══╝
██║ ██║ ██║ ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝
Provides everything related to the extras of the availablity checker.
Author:
Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom
Special thanks:
https://pyfunceble.github.io/#/special-thanks
Contributors:
https://pyfunceble.github.io/#/contributors
Project link:
https://github.com/funilrys/PyFunceble
Project documentation:
https://pyfunceble.readthedocs.io/en/dev/
Project homepage:
https://pyfunceble.github.io/
License:
::
Copyright 2017, 2018, 2019, 2020, 2022 Nissar Chababy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
215 changes: 215 additions & 0 deletions PyFunceble/checker/availability/extras/base.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,215 @@
"""
The tool to check the availability or syntax of domain, IP or URL.
::
██████╗ ██╗ ██╗███████╗██╗ ██╗███╗ ██╗ ██████╗███████╗██████╗ ██╗ ███████╗
██╔══██╗╚██╗ ██╔╝██╔════╝██║ ██║████╗ ██║██╔════╝██╔════╝██╔══██╗██║ ██╔════╝
██████╔╝ ╚████╔╝ █████╗ ██║ ██║██╔██╗ ██║██║ █████╗ ██████╔╝██║ █████╗
██╔═══╝ ╚██╔╝ ██╔══╝ ██║ ██║██║╚██╗██║██║ ██╔══╝ ██╔══██╗██║ ██╔══╝
██║ ██║ ██║ ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝
Provides the base of all extra handlers.
Author:
Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom
Special thanks:
https://pyfunceble.github.io/#/special-thanks
Contributors:
https://pyfunceble.github.io/#/contributors
Project link:
https://github.com/funilrys/PyFunceble
Project documentation:
https://pyfunceble.readthedocs.io/en/dev/
Project homepage:
https://pyfunceble.github.io/
License:
::
Copyright 2017, 2018, 2019, 2020, 2022 Nissar Chababy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import functools
from typing import Callable, Optional

import PyFunceble.factory
from PyFunceble.checker.availability.status import AvailabilityCheckerStatus


class ExtraRuleHandlerBase:
"""
Provides the base of all extra rules handler.
:param statatus:
The previously gathered status.
:type status:
:class:`~PyFunceble.checker.availability.status.AvailabilityCheckerStatus`
"""

_status: Optional[AvailabilityCheckerStatus] = None

def __init__(self, status: Optional[AvailabilityCheckerStatus] = None) -> None:
if status is not None:
self.status = status

# Be sure that all settings are loaded proprely!!
PyFunceble.factory.Requester.guess_all_settings()

def ensure_status_is_given(
func: Callable[..., "ExtraRuleHandlerBase"]
): # pylint: disable=no-self-argument
"""
Ensures that the status is given before running the decorated method.
:raise TypeError:
If the subject is not a string.
"""

@functools.wraps(func)
def wrapper(self, *args, **kwargs): # pragma: no cover ## Safety!
if not self.status:
raise TypeError(
f"<self.status> should be {AvailabilityCheckerStatus}, "
f"{type(self.status)} given."
)

return func(self, *args, **kwargs) # pylint: disable=not-callable

return wrapper

def setup_status_before(
func: Callable[..., "ExtraRuleHandlerBase"]
): # pylint: disable=no-self-argument
"""
Ensures that the status is given before running the decorated method.
:raise TypeError:
If the subject is not a string.
"""

@functools.wraps(func)
def wrapper(self, *args, **kwargs): # pragma: no cover ## Safety!
self.status.status_before_extra_rules = self.status.status
self.status.status_source_before_extra_rules = self.status.status_source

return func(self, *args, **kwargs) # pylint: disable=not-callable

return wrapper

def setup_status_after(
func: Callable[..., "ExtraRuleHandlerBase"]
): # pylint: disable=no-self-argument
"""
Ensures that the status is given before running the decorated method.
:raise TypeError:
If the subject is not a string.
"""

@functools.wraps(func)
def wrapper(self, *args, **kwargs): # pragma: no cover ## Safety!
result = func(self, *args, **kwargs) # pylint: disable=not-callable

if self.status.status_after_extra_rules:
self.status.status = self.status.status_after_extra_rules
self.status.status_source = self.status.status_source_after_extra_rules

PyFunceble.facility.Logger.info(
"Could define the status of %r from our own set of rules.",
self.status.idna_subject,
)
else:
self.status.status_before_extra_rules = None
self.status.status_source_before_extra_rules = None
self.status.status_after_extra_rules = None
self.status.status_source_after_extra_rules = None

return result

return wrapper

@property
def status(self) -> Optional[AvailabilityCheckerStatus]:
"""
Provides the current state of the :code:`_status` attribute.
"""

return self._status

@status.setter
def status(self, value: AvailabilityCheckerStatus) -> None:
"""
Sets the status to work with.
:param value:
The status to work with.
:raise TypeError:
When the given :code:`value` is not a
:class:`~PyFunceble.checker.availability.status.AvailabilityCheckerStatus`.
"""

if not isinstance(value, AvailabilityCheckerStatus):
raise TypeError(
f"<value> should be {AvailabilityCheckerStatus}, {type(value)} given."
)

self._status = value

def set_status(self, value: AvailabilityCheckerStatus) -> "ExtraRuleHandlerBase":
"""
Sets the status to work with.
:param value:
The status to work with.
"""

self.status = value

return self

def start(self) -> "ExtraRuleHandlerBase":
"""
Starts the gathering process.
"""

raise NotImplementedError()

def switch_to_down(self) -> "ExtraRuleHandlerBase":
"""
Switches the status to inactive.
"""

self.status.status_after_extra_rules = PyFunceble.storage.STATUS.down
self.status.status_source_after_extra_rules = "SPECIAL"

return self

def switch_to_up(self) -> "ExtraRuleHandlerBase":
"""
Switches the status to active.
"""

self.status.status_after_extra_rules = PyFunceble.storage.STATUS.up
self.status.status_source_after_extra_rules = "SPECIAL"
Loading

0 comments on commit 1ecb9ed

Please sign in to comment.