Refactor domain check endpoint to accept JSON input and enhance domain validation
Browse files- app.py +20 -6
- index.html +12 -2
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import json5
|
|
| 2 |
import os
|
| 3 |
import random
|
| 4 |
from typing import Callable, Literal
|
| 5 |
-
from flask import Flask, send_from_directory
|
| 6 |
from urllib.parse import urlparse
|
| 7 |
import dns.resolver
|
| 8 |
import socket
|
|
@@ -10,6 +10,7 @@ import requests
|
|
| 10 |
import platform
|
| 11 |
import subprocess
|
| 12 |
from shutil import which
|
|
|
|
| 13 |
|
| 14 |
app = Flask(__name__)
|
| 15 |
|
|
@@ -20,16 +21,16 @@ def index():
|
|
| 20 |
return send_from_directory('.', 'index.html')
|
| 21 |
except Exception as e:
|
| 22 |
return str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
@app.route('/check/<domain>', methods=['POST'])
|
| 25 |
def check_domain(domain: str):
|
| 26 |
"""Check domain availability"""
|
| 27 |
logs: list[str] = []
|
| 28 |
try:
|
| 29 |
-
domain =
|
| 30 |
-
if '://' in domain:
|
| 31 |
-
domain = urlparse(domain).netloc
|
| 32 |
-
|
| 33 |
result = check_domain_availability(domain, logs.append)
|
| 34 |
if result:
|
| 35 |
return {
|
|
@@ -43,6 +44,19 @@ def check_domain(domain: str):
|
|
| 43 |
logs.append(f"{check_domain.__name__}:Exception:{str(e)}")
|
| 44 |
return default_error(domain, logs)
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def default_error(domain: str, logs: list[str]):
|
| 47 |
cannot_confirm = "Cannot confirm if doimain is available"
|
| 48 |
try:
|
|
|
|
| 2 |
import os
|
| 3 |
import random
|
| 4 |
from typing import Callable, Literal
|
| 5 |
+
from flask import Flask, send_from_directory, request
|
| 6 |
from urllib.parse import urlparse
|
| 7 |
import dns.resolver
|
| 8 |
import socket
|
|
|
|
| 10 |
import platform
|
| 11 |
import subprocess
|
| 12 |
from shutil import which
|
| 13 |
+
import re
|
| 14 |
|
| 15 |
app = Flask(__name__)
|
| 16 |
|
|
|
|
| 21 |
return send_from_directory('.', 'index.html')
|
| 22 |
except Exception as e:
|
| 23 |
return str(e)
|
| 24 |
+
|
| 25 |
+
@app.route('/check', methods=['POST'])
|
| 26 |
+
def check():
|
| 27 |
+
return check_domain(request.get_json().get('domain', ''))
|
| 28 |
|
|
|
|
| 29 |
def check_domain(domain: str):
|
| 30 |
"""Check domain availability"""
|
| 31 |
logs: list[str] = []
|
| 32 |
try:
|
| 33 |
+
domain = validate_and_correct_domain(domain)
|
|
|
|
|
|
|
|
|
|
| 34 |
result = check_domain_availability(domain, logs.append)
|
| 35 |
if result:
|
| 36 |
return {
|
|
|
|
| 44 |
logs.append(f"{check_domain.__name__}:Exception:{str(e)}")
|
| 45 |
return default_error(domain, logs)
|
| 46 |
|
| 47 |
+
def validate_and_correct_domain(domain: str):
|
| 48 |
+
# remove leding and trailing "/"
|
| 49 |
+
domain = domain.lower().strip('/').strip()
|
| 50 |
+
# extract domain
|
| 51 |
+
domain = urlparse(domain).netloc.strip() if '://' in domain else domain
|
| 52 |
+
# remove www.
|
| 53 |
+
domain = domain.lstrip("www.").strip()
|
| 54 |
+
# remove inner spaces
|
| 55 |
+
domain = re.sub(r'[\n\s]+', '', domain).strip()
|
| 56 |
+
# replace unwanted characters with hyphens
|
| 57 |
+
domain = re.sub(r'[^a-zA-Z0-9\.]', '-', domain).strip('-').strip('.').strip()
|
| 58 |
+
return domain
|
| 59 |
+
|
| 60 |
def default_error(domain: str, logs: list[str]):
|
| 61 |
cannot_confirm = "Cannot confirm if doimain is available"
|
| 62 |
try:
|
index.html
CHANGED
|
@@ -21,7 +21,7 @@
|
|
| 21 |
</p>
|
| 22 |
<form id="searchForm" class="mb-3 mt-4">
|
| 23 |
<div class="input-group">
|
| 24 |
-
<input type="text" id="domain" class="form-control" placeholder="Enter domain name... eg: example.com">
|
| 25 |
<button class="btn btn-primary" type="submit">Check</button>
|
| 26 |
</div>
|
| 27 |
</form>
|
|
@@ -46,6 +46,10 @@
|
|
| 46 |
document.getElementById('searchForm').onsubmit = async (e) => {
|
| 47 |
e.preventDefault();
|
| 48 |
const domain = document.getElementById('domain').value;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
const result = document.getElementById('result');
|
| 50 |
const spinner = document.getElementById('spinner');
|
| 51 |
const resultText = document.getElementById('resultText');
|
|
@@ -55,7 +59,13 @@
|
|
| 55 |
resultText.innerHTML = '';
|
| 56 |
|
| 57 |
try {
|
| 58 |
-
const response = await fetch(`/check
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
const data = await response.json();
|
| 60 |
|
| 61 |
resultText.innerHTML = `
|
|
|
|
| 21 |
</p>
|
| 22 |
<form id="searchForm" class="mb-3 mt-4">
|
| 23 |
<div class="input-group">
|
| 24 |
+
<input type="text" id="domain" class="form-control" placeholder="Enter domain name... eg: example.com" required/>
|
| 25 |
<button class="btn btn-primary" type="submit">Check</button>
|
| 26 |
</div>
|
| 27 |
</form>
|
|
|
|
| 46 |
document.getElementById('searchForm').onsubmit = async (e) => {
|
| 47 |
e.preventDefault();
|
| 48 |
const domain = document.getElementById('domain').value;
|
| 49 |
+
if ((domain || '').trim().length == 0){
|
| 50 |
+
return;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
const result = document.getElementById('result');
|
| 54 |
const spinner = document.getElementById('spinner');
|
| 55 |
const resultText = document.getElementById('resultText');
|
|
|
|
| 59 |
resultText.innerHTML = '';
|
| 60 |
|
| 61 |
try {
|
| 62 |
+
const response = await fetch(`/check`, {
|
| 63 |
+
method: 'POST',
|
| 64 |
+
headers: {
|
| 65 |
+
'Content-Type': 'application/json'
|
| 66 |
+
},
|
| 67 |
+
body: JSON.stringify({ domain })
|
| 68 |
+
});
|
| 69 |
const data = await response.json();
|
| 70 |
|
| 71 |
resultText.innerHTML = `
|