Skip to content

Commit 1ecb9ed

Browse files
committed
Introduction of extra rules for parked domains.
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
1 parent e5e4552 commit 1ecb9ed

File tree

9 files changed

+509
-131
lines changed

9 files changed

+509
-131
lines changed

PyFunceble/checker/availability/base.py

+36-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@
6262
import PyFunceble.facility
6363
import PyFunceble.factory
6464
import PyFunceble.storage
65-
from PyFunceble.checker.availability.extra_rules import ExtraRulesHandler
65+
from PyFunceble.checker.availability.extras.base import ExtraRuleHandlerBase
66+
from PyFunceble.checker.availability.extras.parked import ParkedRulesHandler
67+
from PyFunceble.checker.availability.extras.rules import ExtraRulesHandler
6668
from PyFunceble.checker.availability.params import AvailabilityCheckerParams
6769
from PyFunceble.checker.availability.status import AvailabilityCheckerStatus
6870
from PyFunceble.checker.base import CheckerBase
@@ -126,7 +128,7 @@ class AvailabilityCheckerBase(CheckerBase):
126128
domain_syntax_checker: Optional[DomainSyntaxChecker] = None
127129
ip_syntax_checker: Optional[IPSyntaxChecker] = None
128130
url_syntax_checker: Optional[URLSyntaxChecker] = None
129-
extra_rules_handler: Optional[ExtraRulesHandler] = None
131+
extra_rules_handlers: Optional[List[ExtraRuleHandlerBase]] = None
130132

131133
_use_extra_rules: bool = False
132134
_use_whois_lookup: bool = False
@@ -162,7 +164,8 @@ def __init__(
162164
self.domain_syntax_checker = DomainSyntaxChecker()
163165
self.ip_syntax_checker = IPSyntaxChecker()
164166
self.url_syntax_checker = URLSyntaxChecker()
165-
self.extra_rules_handler = ExtraRulesHandler()
167+
# WARNING: Put the aggressive one first!
168+
self.extra_rules_handlers = [ParkedRulesHandler(), ExtraRulesHandler()]
166169
self.db_session = db_session
167170

168171
self.params = AvailabilityCheckerParams()
@@ -1016,6 +1019,36 @@ def try_to_query_status_from_collection(self) -> "AvailabilityCheckerBase":
10161019
self.status.idna_subject,
10171020
)
10181021

1022+
return self
1023+
1024+
def try_to_query_status_from_extra_rules(self) -> "AvailabilityCheckerBase":
1025+
"""
1026+
Tries to query the status from the extra rules.
1027+
"""
1028+
1029+
PyFunceble.facility.Logger.info(
1030+
"Started to try to query the status of %r from: Extra Rules Lookup",
1031+
self.status.idna_subject,
1032+
)
1033+
1034+
if self.use_extra_rules:
1035+
for rule_handler in self.extra_rules_handlers:
1036+
rule_handler.set_status(self.status).start()
1037+
1038+
if self.status.status_after_extra_rules:
1039+
PyFunceble.facility.Logger.info(
1040+
"Could define the status of %r from: Extra Rules Lookup",
1041+
self.status.idna_subject,
1042+
)
1043+
break
1044+
1045+
PyFunceble.facility.Logger.info(
1046+
"Finished to try to query the status of %r from: Extra Rules Lookup",
1047+
self.status.idna_subject,
1048+
)
1049+
1050+
return self
1051+
10191052
@CheckerBase.ensure_subject_is_given
10201053
@CheckerBase.update_status_date_after_query
10211054
def query_status(self) -> "AvailabilityCheckerBase":

PyFunceble/checker/availability/domain.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def query_status(
178178
)
179179

180180
if self.use_extra_rules:
181-
self.extra_rules_handler.set_status(self.status).start()
181+
self.try_to_query_status_from_extra_rules()
182182

183183
return self
184184

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
The tool to check the availability or syntax of domain, IP or URL.
3+
4+
::
5+
6+
7+
██████╗ ██╗ ██╗███████╗██╗ ██╗███╗ ██╗ ██████╗███████╗██████╗ ██╗ ███████╗
8+
██╔══██╗╚██╗ ██╔╝██╔════╝██║ ██║████╗ ██║██╔════╝██╔════╝██╔══██╗██║ ██╔════╝
9+
██████╔╝ ╚████╔╝ █████╗ ██║ ██║██╔██╗ ██║██║ █████╗ ██████╔╝██║ █████╗
10+
██╔═══╝ ╚██╔╝ ██╔══╝ ██║ ██║██║╚██╗██║██║ ██╔══╝ ██╔══██╗██║ ██╔══╝
11+
██║ ██║ ██║ ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
12+
╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝
13+
14+
Provides everything related to the extras of the availablity checker.
15+
16+
Author:
17+
Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom
18+
19+
Special thanks:
20+
https://pyfunceble.github.io/#/special-thanks
21+
22+
Contributors:
23+
https://pyfunceble.github.io/#/contributors
24+
25+
Project link:
26+
https://github.com/funilrys/PyFunceble
27+
28+
Project documentation:
29+
https://pyfunceble.readthedocs.io/en/dev/
30+
31+
Project homepage:
32+
https://pyfunceble.github.io/
33+
34+
License:
35+
::
36+
37+
38+
Copyright 2017, 2018, 2019, 2020, 2022 Nissar Chababy
39+
40+
Licensed under the Apache License, Version 2.0 (the "License");
41+
you may not use this file except in compliance with the License.
42+
You may obtain a copy of the License at
43+
44+
http://www.apache.org/licenses/LICENSE-2.0
45+
46+
Unless required by applicable law or agreed to in writing, software
47+
distributed under the License is distributed on an "AS IS" BASIS,
48+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
49+
See the License for the specific language governing permissions and
50+
limitations under the License.
51+
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
"""
2+
The tool to check the availability or syntax of domain, IP or URL.
3+
4+
::
5+
6+
7+
██████╗ ██╗ ██╗███████╗██╗ ██╗███╗ ██╗ ██████╗███████╗██████╗ ██╗ ███████╗
8+
██╔══██╗╚██╗ ██╔╝██╔════╝██║ ██║████╗ ██║██╔════╝██╔════╝██╔══██╗██║ ██╔════╝
9+
██████╔╝ ╚████╔╝ █████╗ ██║ ██║██╔██╗ ██║██║ █████╗ ██████╔╝██║ █████╗
10+
██╔═══╝ ╚██╔╝ ██╔══╝ ██║ ██║██║╚██╗██║██║ ██╔══╝ ██╔══██╗██║ ██╔══╝
11+
██║ ██║ ██║ ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
12+
╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝
13+
14+
Provides the base of all extra handlers.
15+
16+
Author:
17+
Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom
18+
19+
Special thanks:
20+
https://pyfunceble.github.io/#/special-thanks
21+
22+
Contributors:
23+
https://pyfunceble.github.io/#/contributors
24+
25+
Project link:
26+
https://github.com/funilrys/PyFunceble
27+
28+
Project documentation:
29+
https://pyfunceble.readthedocs.io/en/dev/
30+
31+
Project homepage:
32+
https://pyfunceble.github.io/
33+
34+
License:
35+
::
36+
37+
38+
Copyright 2017, 2018, 2019, 2020, 2022 Nissar Chababy
39+
40+
Licensed under the Apache License, Version 2.0 (the "License");
41+
you may not use this file except in compliance with the License.
42+
You may obtain a copy of the License at
43+
44+
http://www.apache.org/licenses/LICENSE-2.0
45+
46+
Unless required by applicable law or agreed to in writing, software
47+
distributed under the License is distributed on an "AS IS" BASIS,
48+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
49+
See the License for the specific language governing permissions and
50+
limitations under the License.
51+
"""
52+
53+
import functools
54+
from typing import Callable, Optional
55+
56+
import PyFunceble.factory
57+
from PyFunceble.checker.availability.status import AvailabilityCheckerStatus
58+
59+
60+
class ExtraRuleHandlerBase:
61+
"""
62+
Provides the base of all extra rules handler.
63+
64+
:param statatus:
65+
The previously gathered status.
66+
:type status:
67+
:class:`~PyFunceble.checker.availability.status.AvailabilityCheckerStatus`
68+
"""
69+
70+
_status: Optional[AvailabilityCheckerStatus] = None
71+
72+
def __init__(self, status: Optional[AvailabilityCheckerStatus] = None) -> None:
73+
if status is not None:
74+
self.status = status
75+
76+
# Be sure that all settings are loaded proprely!!
77+
PyFunceble.factory.Requester.guess_all_settings()
78+
79+
def ensure_status_is_given(
80+
func: Callable[..., "ExtraRuleHandlerBase"]
81+
): # pylint: disable=no-self-argument
82+
"""
83+
Ensures that the status is given before running the decorated method.
84+
85+
:raise TypeError:
86+
If the subject is not a string.
87+
"""
88+
89+
@functools.wraps(func)
90+
def wrapper(self, *args, **kwargs): # pragma: no cover ## Safety!
91+
if not self.status:
92+
raise TypeError(
93+
f"<self.status> should be {AvailabilityCheckerStatus}, "
94+
f"{type(self.status)} given."
95+
)
96+
97+
return func(self, *args, **kwargs) # pylint: disable=not-callable
98+
99+
return wrapper
100+
101+
def setup_status_before(
102+
func: Callable[..., "ExtraRuleHandlerBase"]
103+
): # pylint: disable=no-self-argument
104+
"""
105+
Ensures that the status is given before running the decorated method.
106+
107+
:raise TypeError:
108+
If the subject is not a string.
109+
"""
110+
111+
@functools.wraps(func)
112+
def wrapper(self, *args, **kwargs): # pragma: no cover ## Safety!
113+
self.status.status_before_extra_rules = self.status.status
114+
self.status.status_source_before_extra_rules = self.status.status_source
115+
116+
return func(self, *args, **kwargs) # pylint: disable=not-callable
117+
118+
return wrapper
119+
120+
def setup_status_after(
121+
func: Callable[..., "ExtraRuleHandlerBase"]
122+
): # pylint: disable=no-self-argument
123+
"""
124+
Ensures that the status is given before running the decorated method.
125+
126+
:raise TypeError:
127+
If the subject is not a string.
128+
"""
129+
130+
@functools.wraps(func)
131+
def wrapper(self, *args, **kwargs): # pragma: no cover ## Safety!
132+
result = func(self, *args, **kwargs) # pylint: disable=not-callable
133+
134+
if self.status.status_after_extra_rules:
135+
self.status.status = self.status.status_after_extra_rules
136+
self.status.status_source = self.status.status_source_after_extra_rules
137+
138+
PyFunceble.facility.Logger.info(
139+
"Could define the status of %r from our own set of rules.",
140+
self.status.idna_subject,
141+
)
142+
else:
143+
self.status.status_before_extra_rules = None
144+
self.status.status_source_before_extra_rules = None
145+
self.status.status_after_extra_rules = None
146+
self.status.status_source_after_extra_rules = None
147+
148+
return result
149+
150+
return wrapper
151+
152+
@property
153+
def status(self) -> Optional[AvailabilityCheckerStatus]:
154+
"""
155+
Provides the current state of the :code:`_status` attribute.
156+
"""
157+
158+
return self._status
159+
160+
@status.setter
161+
def status(self, value: AvailabilityCheckerStatus) -> None:
162+
"""
163+
Sets the status to work with.
164+
165+
:param value:
166+
The status to work with.
167+
168+
:raise TypeError:
169+
When the given :code:`value` is not a
170+
:class:`~PyFunceble.checker.availability.status.AvailabilityCheckerStatus`.
171+
"""
172+
173+
if not isinstance(value, AvailabilityCheckerStatus):
174+
raise TypeError(
175+
f"<value> should be {AvailabilityCheckerStatus}, {type(value)} given."
176+
)
177+
178+
self._status = value
179+
180+
def set_status(self, value: AvailabilityCheckerStatus) -> "ExtraRuleHandlerBase":
181+
"""
182+
Sets the status to work with.
183+
184+
:param value:
185+
The status to work with.
186+
"""
187+
188+
self.status = value
189+
190+
return self
191+
192+
def start(self) -> "ExtraRuleHandlerBase":
193+
"""
194+
Starts the gathering process.
195+
"""
196+
197+
raise NotImplementedError()
198+
199+
def switch_to_down(self) -> "ExtraRuleHandlerBase":
200+
"""
201+
Switches the status to inactive.
202+
"""
203+
204+
self.status.status_after_extra_rules = PyFunceble.storage.STATUS.down
205+
self.status.status_source_after_extra_rules = "SPECIAL"
206+
207+
return self
208+
209+
def switch_to_up(self) -> "ExtraRuleHandlerBase":
210+
"""
211+
Switches the status to active.
212+
"""
213+
214+
self.status.status_after_extra_rules = PyFunceble.storage.STATUS.up
215+
self.status.status_source_after_extra_rules = "SPECIAL"

0 commit comments

Comments
 (0)