resolv/app/main.py

128 lines
2.8 KiB
Python
Executable file

#!/usr/bin/python3
import dns.resolver
import dns.reversename
import whois
import os
import requests
import json
from flask import Flask
# from flask import jsonify
app = Flask(__name__)
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']
HOSTIO_ENABLED = os.environ['HOSTIO_ENABLED']
# def get_ptr(ip):
# ip_rev = dns.reversename.from_address(ip)
# ptr_rq = dns.resolver.resolve(ip_rev, 'PTR')
# ptr = ptr_rq[0].to_text()
# 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):
rq_url = "http://ip-api.com/json/" + ip + "?fields=17006105"
rq = requests.get(rq_url)
response = rq.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
@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'])
def ipinfo(input):
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__':
app.run(host="0.0.0.0")