forked from viettools/checkdomain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
207 lines (176 loc) · 7.33 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# -*- coding: utf-8 -*-
# https://check.rs/
from whois import Whois
from cctld import WhOISccTLD
from parse_whois_socket import ParseWhoisSocket
from verified.registrar import VerifiedRegistrar
from fastapi import FastAPI, Request, Body
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from bs4 import BeautifulSoup
from tld import get_tld
import re, os, requests, json, time, pathlib
app = FastAPI(docs_url=None, redoc_url=None) # Remove 'docs_url=None, redoc_url=None' to check api
app.add_middleware(CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'])
current_path_dir = os.path.dirname(os.path.abspath(__file__))
app.mount('/static', StaticFiles(directory='{0}/static/src'.format(current_path_dir)), name='static')
templates = Jinja2Templates('{0}/templates'.format(current_path_dir))
@app.get('/', response_class=HTMLResponse)
def main(request: Request):
header_template = templates.TemplateResponse('header.html', {'request': request})
footer_template = templates.TemplateResponse('footer.html', {'request': request})
main_template = templates.TemplateResponse('index.html',
{
'request': request,
'header_template': header_template.body.decode('utf-8'),
'footer_template': footer_template.body.decode('utf-8'),
})
return main_template
@app.post('/api/v1/whois')
def whois_data(domain: str = Body(..., embed=True)):
result = {'status': False, 'result': False}
parse_data = {
'registrar': '',
'registrar_url': '',
'domain_status': [],
'nameservers': [],
'creation_date': '',
'updated_date': '',
'expiry_date': '',
'verified': False
}
result.update({'parse': parse_data})
if not domain:
return result
domain = BeautifulSoup(domain, features='html.parser').get_text()
# Special TLD
arr_special_tld = ['br.com', 'cn.com', 'de.com', 'eu.com', 'gb.net',
'gr.com', 'in.net', 'ru.com', 'sa.com', 'se.net',
'uk.com', 'uk.net', 'us.com', 'za.com', 'jpn.com',
'it.com', 'priv.at', 'co.ca', 'co.pl', 'ac.ru', 'com.ru',
'msk.ru', 'net.ru', 'nov.ru', 'org.ru', 'pp.ru', 'spb.ru',
'ac.uk', 'gov.uk', 'co.uz', 'com.uz', 'net.uz', 'org.uz',
'ac.za', 'co.za', 'net.za', 'org.za', 'web.za', 'gov.za']
# Web WHOIS
arr_web_tld = ['ao', 'az',
'ba', 'bb', 'bd', 'bt',
'cu', 'cv', 'cy',
'dz', 'dj',
'es', 'eg',
'gm', 'gr', 'gt', 'gw',
'hm', 'lk', 'tj', 'jo',
'sv', 'np', 'tt', 'pa',
'ph', 'vi', 'vn']
#google.com -> tld_domain = 'com'
tld_domain = False
try:
tld_domain = get_tld(domain, fix_protocol=True)
except:
spl = domain.split('.')
if spl:
tld_domain = spl[-1]
if not tld_domain:
return result
final_tld_domain = tld_domain
# sub domain ? 'com.ps' --> 'ps'
if tld_domain not in arr_special_tld and tld_domain.find('.') > -1:
spl_tld_domain = tld_domain.split('.')
if spl_tld_domain:
final_tld_domain = spl_tld_domain[-1]
whois_result = {}
wcc = WhOISccTLD()
if wcc.get_whois_server(final_tld_domain):
whois_server = wcc.get_whois_server(final_tld_domain)
# Socket
if whois_server:
whois_data = Whois(domain, final_tld_domain)
whois_data.set_hostname(whois_server)
whois_result = whois_data.get_data()
elif final_tld_domain in arr_web_tld:
USER_AGENT = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/117.0'
dynamic_import = __import__('webwhois.{0}'.format(final_tld_domain))
if hasattr(dynamic_import, '{0}'.format(final_tld_domain)):
whois_result = getattr(dynamic_import, '{0}'.format(final_tld_domain)).whois_via_web(USER_AGENT, domain, tld_domain)
else:
whois_result = {
'status': False,
'result': False
}
# I want to limit query via web
time.sleep(6)
else:
whois_iana_data = Whois(domain, final_tld_domain)
result_iana = whois_iana_data.get_data()
if result_iana['result']:
check = re.findall('whois:\s+(.+)', result_iana['result'], re.IGNORECASE)
# .AD no whois server --> [status: ACTIVE]
if check and check[0] and check[0].find('status') == -1:
next_query = check[0].strip()
whois_data = Whois(domain, final_tld_domain)
whois_data.set_hostname(next_query)
whois_result = whois_data.get_data()
else:
# Only whois data from whois.iana.org
whois_result.update({'status': True, 'result': result_iana['result']})
if whois_result:
result.update(whois_result)
if whois_result.get('result', False):
parse_obj = ParseWhoisSocket()
verified_obj = VerifiedRegistrar()
parse_raw = parse_obj.parse_socket_data(whois_result['result'], final_tld_domain)
verified_data = verified_obj.check_registrar(whois_result['result'])
parse_raw.update({'verified': verified_data})
result.update({'parse': parse_raw})
return result
@app.get('/api/v1/proxy/rdap')
def query_rdap_proxy(domain: str | None = None):
# Bypass: Response body is not available to scripts (Reason: CORS Missing Allow Origin)
result = {}
if not domain:
return result
domain = BeautifulSoup(domain, features='html.parser').get_text()
rdap_url = False
spl_domain = domain.split('.')
if spl_domain:
if spl_domain[-1] == 'de':
rdap_url = 'https://rdap.denic.de'
elif spl_domain[-1] == 've':
rdap_url = 'https://rdap.nic.ve/rdap'
elif spl_domain[-1] == 'tz':
rdap_url = 'https://whois.tznic.or.tz/rdap'
elif spl_domain[-1] == 'uz':
rdap_url = 'http://cctld.uz:9000'
elif spl_domain[-1] == 'kg':
rdap_url = 'https://rdap.cctld.kg/'
if rdap_url:
req = requests.Session()
req_get = False
try:
req_get = req.get('{0}/domain/{1}'.format(rdap_url, domain))
except:
pass
if req_get and req_get.status_code == 200:
result = json.loads(req_get.text)
return result
def is_docker():
if pathlib.Path('/.dockerenv').exists():
return True
path = '/proc/self/cgroup'
if not os.path.isfile(path): return False
with open(path) as f:
for line in f:
if re.match('\d+:[\w=]+:/docker(-[ce]e)?/\w+', line):
return True
return False
if __name__ == '__main__':
listen_host = '0.0.0.0'
if not is_docker():
listen_host= '127.0.0.1'
uvicorn.run('main:app', host=listen_host, port=6996, reload=True)