mckabue commited on
Commit
1ef0e69
·
1 Parent(s): d5db4e7

Improve error logging in domain availability checks and enhance exception handling

Browse files
Files changed (1) hide show
  1. app.py +24 -22
app.py CHANGED
@@ -1,5 +1,5 @@
1
  from typing import Callable
2
- from flask import Flask, request, send_from_directory
3
  import dns.resolver
4
  import socket
5
  import requests
@@ -53,7 +53,7 @@ def check_domain(domain: str):
53
  "logs": logs
54
  }
55
  except Exception as e:
56
- logs.append(f"{check_domain.__name__}:{str(e)}")
57
  return {
58
  'domain': domain,
59
  "available": False,
@@ -86,7 +86,7 @@ def dns_is_available(domain, logs_append: Callable[[str], None]):
86
  dns.resolver.resolve(domain, record_type)
87
  return False, record_type, False
88
  except Exception as e:
89
- logs_append(f"{dns_is_available.__name__}:{str(e)}")
90
  continue
91
  return True, None, True
92
 
@@ -106,7 +106,7 @@ def rdap_is_available(domain, logs_append: Callable[[str], None]):
106
  elif response.status_code == 200:
107
  return False, rdap_base_url, False
108
  except Exception as e:
109
- logs_append(f"{rdap_is_available.__name__}:{str(e)}")
110
  return False, None, True
111
 
112
  def whois_is_available(domain, logs_append: Callable[[str], None]) -> bool:
@@ -129,7 +129,7 @@ def whois_is_available(domain, logs_append: Callable[[str], None]) -> bool:
129
  if is_available:
130
  return True, availability_method, False
131
  except Exception as e:
132
- logs_append(f"{whois_is_available.__name__}:{str(e)}")
133
  return False, None, True
134
 
135
  def socket_whois_is_available(domain, is_available_callback: Callable[[str], bool], logs_append: Callable[[str], None]):
@@ -147,29 +147,31 @@ def socket_whois_is_available(domain, is_available_callback: Callable[[str], boo
147
  response_lower = response.lower()
148
  return is_available_callback(response_lower), whois_server
149
  except Exception as e:
150
- logs_append(f"{socket_whois_is_available.__name__}:{str(e)}")
151
  return False, None
152
 
153
  def terminal_whois_is_available(domain, is_available_callback: Callable[[str], bool], logs_append: Callable[[str], None]):
154
  try:
155
  # Check if OS is Linux
156
  if platform.system().lower() == 'linux':
157
- if which('whois') is not None:
158
- # Run whois command with timeout
159
- process = subprocess.Popen(
160
- ['whois', domain],
161
- stdout=subprocess.PIPE,
162
- stderr=subprocess.PIPE)
163
- try:
164
- stdout, stderr = process.communicate(timeout=60)
165
- output = stdout.decode('utf-8', errors='ignore').lower()
166
- logs_append(f"{terminal_whois_is_available.__name__}:stderr:{str(stderr.decode(encoding='utf-8'))}")
167
- return is_available_callback(output), "system whois"
168
- except subprocess.TimeoutExpired as timeout_e:
169
- logs_append(f"{terminal_whois_is_available.__name__}:{str(timeout_e)}")
170
- process.kill()
 
 
171
  except Exception as e:
172
- logs_append(f"{terminal_whois_is_available.__name__}:{str(e)}")
173
  return False, None
174
 
175
  def get_whois_server(domain, logs_append: Callable[[str], None]):
@@ -181,5 +183,5 @@ def get_whois_server(domain, logs_append: Callable[[str], None]):
181
  if 'whois:' in line.lower():
182
  return line.split(':')[1].strip()
183
  except Exception as e:
184
- logs_append(f"{get_whois_server.__name__}:{str(e)}")
185
  return None
 
1
  from typing import Callable
2
+ from flask import Flask, send_from_directory
3
  import dns.resolver
4
  import socket
5
  import requests
 
53
  "logs": logs
54
  }
55
  except Exception as e:
56
+ logs.append(f"{check_domain.__name__}:Exception:{str(e)}")
57
  return {
58
  'domain': domain,
59
  "available": False,
 
86
  dns.resolver.resolve(domain, record_type)
87
  return False, record_type, False
88
  except Exception as e:
89
+ logs_append(f"{dns_is_available.__name__}:Exception:{str(e)}")
90
  continue
91
  return True, None, True
92
 
 
106
  elif response.status_code == 200:
107
  return False, rdap_base_url, False
108
  except Exception as e:
109
+ logs_append(f"{rdap_is_available.__name__}:Exception:{str(e)}")
110
  return False, None, True
111
 
112
  def whois_is_available(domain, logs_append: Callable[[str], None]) -> bool:
 
129
  if is_available:
130
  return True, availability_method, False
131
  except Exception as e:
132
+ logs_append(f"{whois_is_available.__name__}:Exception:{str(e)}")
133
  return False, None, True
134
 
135
  def socket_whois_is_available(domain, is_available_callback: Callable[[str], bool], logs_append: Callable[[str], None]):
 
147
  response_lower = response.lower()
148
  return is_available_callback(response_lower), whois_server
149
  except Exception as e:
150
+ logs_append(f"{socket_whois_is_available.__name__}:Exception:{str(e)}")
151
  return False, None
152
 
153
  def terminal_whois_is_available(domain, is_available_callback: Callable[[str], bool], logs_append: Callable[[str], None]):
154
  try:
155
  # Check if OS is Linux
156
  if platform.system().lower() == 'linux':
157
+ logs_append(f"{terminal_whois_is_available.__name__}:Exception:System WHOIS check only available on Linux")
158
+ if which('whois') is not None:
159
+ logs_append(f"{terminal_whois_is_available.__name__}:Exception:WHOIS not installed. Install with: sudo apt-get install whois")
160
+ # Run whois command with timeout
161
+ process = subprocess.Popen(
162
+ ['whois', domain],
163
+ stdout=subprocess.PIPE,
164
+ stderr=subprocess.PIPE)
165
+ try:
166
+ stdout, stderr = process.communicate(timeout=60)
167
+ output = stdout.decode('utf-8', errors='ignore').lower()
168
+ logs_append(f"{terminal_whois_is_available.__name__}:stderr:{str(stderr.decode(encoding='utf-8'))}")
169
+ return is_available_callback(output), "system whois"
170
+ except subprocess.TimeoutExpired as timeout_e:
171
+ logs_append(f"{terminal_whois_is_available.__name__}:TimeoutExpired:{str(timeout_e)}")
172
+ process.kill()
173
  except Exception as e:
174
+ logs_append(f"{terminal_whois_is_available.__name__}:Exception:{str(e)}")
175
  return False, None
176
 
177
  def get_whois_server(domain, logs_append: Callable[[str], None]):
 
183
  if 'whois:' in line.lower():
184
  return line.split(':')[1].strip()
185
  except Exception as e:
186
+ logs_append(f"{get_whois_server.__name__}:Exception:{str(e)}")
187
  return None