-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.py
84 lines (67 loc) · 2.45 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
__version__ = "0.1.0"
import os
import dns.resolver
from dns.exception import Timeout
from bottle import Bottle, request, redirect
from cors import CorsPlugin, enable_cors
from tld import exceptions
from utils import jsonify, validate_domain, resolve_domain, get_authoritative_nameserver
app = Bottle()
ORIGINS = os.getenv("ORIGINS", "*").split(",")
ENV = os.getenv("ENV", "development")
@app.get("/")
def redirect_to_app():
return redirect("https://dnscheck.now.sh")
@enable_cors
@app.post("/authoritative_nameserver")
def post_auth_ns():
domain = request.forms.get("domain")
if domain is None:
return jsonify(status=400, message="Param domain missing.")
try:
nameserver_list = get_authoritative_nameserver(domain)
except exceptions.TldDomainNotFound as e:
print("TldDomainNotFound", e)
return jsonify(status=400, message=str(e))
except exceptions.TldBadUrl as e:
print("TldBadUrl", e)
return jsonify(status=400, message=str(e))
return jsonify(status=200, message=nameserver_list)
@enable_cors
@app.post("/")
def index():
domain = request.forms.get("domain")
dns_server = request.forms.get("dns_server")
if domain is None:
return jsonify(status=400, message="Param domain missing.")
if dns_server is None:
return jsonify(status=400, message="Param dns_server missing.")
try:
domain = validate_domain(domain)
dns_results = resolve_domain(domain, dns_server=dns_server)
return jsonify(status=200, data=dns_results)
except exceptions.TldDomainNotFound as e:
print("TldDomainNotFound", e)
return jsonify(status=400, message=str(e))
except exceptions.TldBadUrl as e:
print("TldBadUrl", e)
return jsonify(status=400, message=str(e))
except dns.resolver.NXDOMAIN as e:
print("NXDOMAIN", e)
return jsonify(status=404, message=e.msg)
except dns.resolver.NoAnswer as e:
print("NoAnswer", e)
return jsonify(status=500, message=e.msg)
except dns.resolver.NoNameservers as e:
print("NoNameservers", e)
return jsonify(status=500, message=e.msg)
except Timeout as e:
print("Timeout", e)
return jsonify(
status=504,
message="{} A common reason for this is an invalid DNS server.".format(
e.msg
),
)
debug = True if ENV == "development" else False
app.install(CorsPlugin(origins=ORIGINS))