output refactor, whois add

This commit is contained in:
Matěj Divecký 2022-07-17 16:41:16 +02:00
parent 8b1cb6f23c
commit 47fb19b0e0
5 changed files with 95 additions and 14 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.env

View file

@ -1,9 +1,10 @@
#!/usr/bin/python3 #!/usr/bin/python3
# import dns.resolver import dns.resolver
# import dns.reversename import dns.reversename
# import sys import whois
# from whois import whois
import os
import requests import requests
import json import json
@ -16,12 +17,9 @@ from flask import Flask
app = Flask(__name__) app = Flask(__name__)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
# Host.io is limited do 1000 request/month. And it's only used to show domains on the same IP. It can be disabled while testing to prevent issues.
HOSTIO_TOKEN = os.environ['HOSTIO_TOKEN']
# def get_ip(domain): HOSTIO_ENABLED = False
# ip_rq = dns.resolver.resolve(domain, 'A')
# ip = ip_rq[0].to_text()
# return(ip)
@ -31,22 +29,100 @@ app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
# ptr = ptr_rq[0].to_text() # ptr = ptr_rq[0].to_text()
# return(ptr) # return(ptr)
def get_ip(domain):
ip_rq = dns.resolver.resolve(domain, 'A')
ip = ip_rq[0].to_text()
return(ip)
def get_mx(domain):
mx_rq = dns.resolver.resolve(domain, 'MX')
mx = []
for mx_res in mx_rq:
mx.append(mx_res.to_text())
return(mx)
def ip_api(ip): def ip_api(ip):
rq_url = "http://ip-api.com/json/" + ip + "?fields=17006105" rq_url = "http://ip-api.com/json/" + ip + "?fields=17006105"
rq = requests.get(rq_url) rq = requests.get(rq_url)
response = rq.json() response = rq.json()
#return Response(json.dumps(response), mimetype='application/json') return response
def hostio_api(ip, token):
if HOSTIO_ENABLED:
rq_url = "https://host.io/api/domains/ip/" + ip + "?token=" + token
rq = requests.get(rq_url)
response = rq.json()
return response
else:
return False
def get_whois(domain):
response = whois.query(domain)
return response return response
@app.route('/favicon.ico')
def ret404():
return("Sorry, favicon not here. This is a workaround to not resolve favicon.ico as domain...", 404)
@app.route('/')
def homepage():
return("This is homepage. Nothing to see here.", 200)
@app.route('/<string:input>', methods=['GET']) @app.route('/<string:input>', methods=['GET'])
def ipinfo(input): def ipinfo(input):
ip_info = ip_api(input) info = {}
return(ip_info) try:
ip = get_ip(input)
info['ip'] = ip
except:
print("DNS IP query failed, exiting.")
return("IP could not be resolved. Bye!", 400)
try:
mx = get_mx(input)
info['domain_mx'] = mx
except:
print("MX query failed")
pass
try:
whois_data = get_whois(input)
info['domain_registrant'] = whois_data.registrant
info['domain_nameservers'] = whois_data.name_servers
info['domain_registrar'] = whois_data.registrar
info['domain_creation'] = whois_data.creation_date
info['domain_expiration'] = whois_data.expiration_date
except:
print("Whois query failed")
pass
try:
ipapi = ip_api(ip)
info['ip_AS'] = ipapi['as']
info['ip_city'] = ipapi['city']
info['ip_country'] = ipapi['country']
info['ip_is_hosting'] = ipapi['hosting']
info['ip_is_mobile'] = ipapi['mobile']
info['ip_is_proxy'] = ipapi['proxy']
info['ip_reverse'] = ipapi['reverse']
info['ip_isp'] = ipapi['isp']
info['ip_org'] = ipapi['org']
except:
print("ip-api.org query failed")
pass
try:
hostio = hostio_api(ip, HOSTIO_TOKEN)
info['ip_domains'] = hostio['domains']
info['ip_domains_count'] = hostio['total']
except:
print("Host.io query failed")
pass
return(info)
if __name__ == '__main__': if __name__ == '__main__':
app.run(host="0.0.0.0") app.run(host="0.0.0.0")

View file

@ -1,3 +1,4 @@
dnspython dnspython
Flask Flask
requests requests
whois

View file

@ -7,4 +7,6 @@ services:
- "5000:5000" - "5000:5000"
volumes: volumes:
- ./app:/python-flask - ./app:/python-flask
env_file:
- .env
container_name: resolv container_name: resolv

1
env.example Normal file
View file

@ -0,0 +1 @@
HOSTIO_TOKEN=xyz