Refactor domain availability checks for improved readability and consistency in logging
Browse files
app.py
CHANGED
@@ -64,19 +64,31 @@ def check_domain(domain: str):
|
|
64 |
def check_domain_availability(domain, logs_append: Callable[[str], None]):
|
65 |
"""Check domain availability using multiple methods."""
|
66 |
# First try DNS resolution
|
67 |
-
is_available, availability_method, _continue = dns_is_available(
|
|
|
68 |
if not _continue:
|
69 |
-
return {
|
|
|
|
|
|
|
70 |
|
71 |
# Try RDAP
|
72 |
-
is_available, availability_method, _continue = rdap_is_available(
|
|
|
73 |
if not _continue:
|
74 |
-
return {
|
|
|
|
|
|
|
75 |
|
76 |
# Fall back to WHOIS
|
77 |
-
is_available, availability_method, _continue = whois_is_available(
|
|
|
78 |
if not _continue:
|
79 |
-
return {
|
|
|
|
|
|
|
80 |
|
81 |
def dns_is_available(domain, logs_append: Callable[[str], None]):
|
82 |
"""Check if domain exists in DNS by looking for common record types."""
|
@@ -90,9 +102,13 @@ def dns_is_available(domain, logs_append: Callable[[str], None]):
|
|
90 |
resolver.resolve(domain, record_type)
|
91 |
return False, record_type, False
|
92 |
except Exception as e:
|
93 |
-
logs_append(
|
|
|
|
|
94 |
except Exception as e:
|
95 |
-
logs_append(
|
|
|
|
|
96 |
return True, None, True
|
97 |
|
98 |
def get_dns_resolver():
|
@@ -145,18 +161,24 @@ def whois_is_available(domain, logs_append: Callable[[str], None]) -> bool:
|
|
145 |
'status: free',
|
146 |
'domain not found'
|
147 |
]
|
148 |
-
is_available_callback = lambda output: any(
|
149 |
-
|
|
|
|
|
150 |
if is_available:
|
151 |
return True, availability_method, False
|
152 |
-
is_available, availability_method = terminal_whois_is_available(
|
|
|
153 |
if is_available:
|
154 |
return True, availability_method, False
|
155 |
except Exception as e:
|
156 |
logs_append(f"{whois_is_available.__name__}:Exception:{str(e)}")
|
157 |
return False, None, True
|
158 |
|
159 |
-
def socket_whois_is_available(
|
|
|
|
|
|
|
160 |
try:
|
161 |
whois_server = get_whois_server(domain, logs_append)
|
162 |
|
@@ -170,11 +192,16 @@ def socket_whois_is_available(domain, is_available_callback: Callable[[str], boo
|
|
170 |
response_lower = response.lower()
|
171 |
return is_available_callback(response_lower), whois_server
|
172 |
except Exception as e:
|
173 |
-
logs_append(
|
174 |
-
|
|
|
|
|
175 |
return False, None
|
176 |
|
177 |
-
def terminal_whois_is_available(
|
|
|
|
|
|
|
178 |
try:
|
179 |
# Check if OS is Linux
|
180 |
if platform.system().lower() == 'linux':
|
@@ -187,17 +214,27 @@ def terminal_whois_is_available(domain, is_available_callback: Callable[[str], b
|
|
187 |
try:
|
188 |
stdout, stderr = process.communicate(timeout=10)
|
189 |
output = stdout.decode('utf-8', errors='ignore').lower()
|
190 |
-
logs_append(
|
|
|
|
|
191 |
return is_available_callback(output), "system whois"
|
192 |
except subprocess.TimeoutExpired as timeout_e:
|
193 |
-
logs_append(
|
|
|
|
|
194 |
process.kill()
|
195 |
else:
|
196 |
-
logs_append(
|
|
|
|
|
|
|
197 |
else:
|
198 |
-
logs_append(
|
|
|
|
|
199 |
except Exception as e:
|
200 |
-
logs_append(
|
|
|
201 |
return False, None
|
202 |
|
203 |
def get_whois_server(domain, logs_append: Callable[[str], None]):
|
|
|
64 |
def check_domain_availability(domain, logs_append: Callable[[str], None]):
|
65 |
"""Check domain availability using multiple methods."""
|
66 |
# First try DNS resolution
|
67 |
+
is_available, availability_method, _continue = dns_is_available(
|
68 |
+
domain, logs_append)
|
69 |
if not _continue:
|
70 |
+
return {
|
71 |
+
"available": is_available,
|
72 |
+
"method": f"DNS:{availability_method}"
|
73 |
+
}
|
74 |
|
75 |
# Try RDAP
|
76 |
+
is_available, availability_method, _continue = rdap_is_available(
|
77 |
+
domain, logs_append)
|
78 |
if not _continue:
|
79 |
+
return {
|
80 |
+
"available": is_available,
|
81 |
+
"method": f"RDAP:{availability_method}"
|
82 |
+
}
|
83 |
|
84 |
# Fall back to WHOIS
|
85 |
+
is_available, availability_method, _continue = whois_is_available(
|
86 |
+
domain, logs_append)
|
87 |
if not _continue:
|
88 |
+
return {
|
89 |
+
"available": is_available,
|
90 |
+
"method": f"WHOIS:{availability_method}"
|
91 |
+
}
|
92 |
|
93 |
def dns_is_available(domain, logs_append: Callable[[str], None]):
|
94 |
"""Check if domain exists in DNS by looking for common record types."""
|
|
|
102 |
resolver.resolve(domain, record_type)
|
103 |
return False, record_type, False
|
104 |
except Exception as e:
|
105 |
+
logs_append(
|
106 |
+
(f"{dns_is_available.__name__}:{record_type}:Exception"
|
107 |
+
f":{'|'.join(resolver_nameservers)}:{str(e)}"))
|
108 |
except Exception as e:
|
109 |
+
logs_append(
|
110 |
+
(f"{dns_is_available.__name__}"
|
111 |
+
f":Exception:{'|'.join(resolver_nameservers)}:{str(e)}"))
|
112 |
return True, None, True
|
113 |
|
114 |
def get_dns_resolver():
|
|
|
161 |
'status: free',
|
162 |
'domain not found'
|
163 |
]
|
164 |
+
is_available_callback = lambda output: any(
|
165 |
+
pattern in output for pattern in available_patterns)
|
166 |
+
is_available, availability_method = socket_whois_is_available(
|
167 |
+
domain, is_available_callback, logs_append)
|
168 |
if is_available:
|
169 |
return True, availability_method, False
|
170 |
+
is_available, availability_method = terminal_whois_is_available(
|
171 |
+
domain, is_available_callback, logs_append)
|
172 |
if is_available:
|
173 |
return True, availability_method, False
|
174 |
except Exception as e:
|
175 |
logs_append(f"{whois_is_available.__name__}:Exception:{str(e)}")
|
176 |
return False, None, True
|
177 |
|
178 |
+
def socket_whois_is_available(
|
179 |
+
domain: str,
|
180 |
+
is_available_callback: Callable[[str], bool],
|
181 |
+
logs_append: Callable[[str], None]):
|
182 |
try:
|
183 |
whois_server = get_whois_server(domain, logs_append)
|
184 |
|
|
|
192 |
response_lower = response.lower()
|
193 |
return is_available_callback(response_lower), whois_server
|
194 |
except Exception as e:
|
195 |
+
logs_append(
|
196 |
+
f"{socket_whois_is_available.__name__}:whois_server:{whois_server}")
|
197 |
+
logs_append(
|
198 |
+
f"{socket_whois_is_available.__name__}:Exception:{str(e)}")
|
199 |
return False, None
|
200 |
|
201 |
+
def terminal_whois_is_available(
|
202 |
+
domain: str,
|
203 |
+
is_available_callback: Callable[[str], bool],
|
204 |
+
logs_append: Callable[[str], None]):
|
205 |
try:
|
206 |
# Check if OS is Linux
|
207 |
if platform.system().lower() == 'linux':
|
|
|
214 |
try:
|
215 |
stdout, stderr = process.communicate(timeout=10)
|
216 |
output = stdout.decode('utf-8', errors='ignore').lower()
|
217 |
+
logs_append(
|
218 |
+
(f"{terminal_whois_is_available.__name__}"
|
219 |
+
f":stderr:{str(stderr.decode(encoding='utf-8'))}"))
|
220 |
return is_available_callback(output), "system whois"
|
221 |
except subprocess.TimeoutExpired as timeout_e:
|
222 |
+
logs_append(
|
223 |
+
(f"{terminal_whois_is_available.__name__}"
|
224 |
+
f":TimeoutExpired:{str(timeout_e)}"))
|
225 |
process.kill()
|
226 |
else:
|
227 |
+
logs_append(
|
228 |
+
(f"{terminal_whois_is_available.__name__}"
|
229 |
+
":Exception:WHOIS not installed. "
|
230 |
+
"Install with: sudo apt-get install whois"))
|
231 |
else:
|
232 |
+
logs_append(
|
233 |
+
(f"{terminal_whois_is_available.__name__}"
|
234 |
+
":Exception:System WHOIS check only available on Linux"))
|
235 |
except Exception as e:
|
236 |
+
logs_append(
|
237 |
+
f"{terminal_whois_is_available.__name__}:Exception:{str(e)}")
|
238 |
return False, None
|
239 |
|
240 |
def get_whois_server(domain, logs_append: Callable[[str], None]):
|