diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/app/main.py b/app/main.py index d4d438f..ef5dfa5 100755 --- a/app/main.py +++ b/app/main.py @@ -1,9 +1,10 @@ #!/usr/bin/python3 -# import dns.resolver -# import dns.reversename -# import sys -# from whois import whois +import dns.resolver +import dns.reversename +import whois + +import os import requests import json @@ -16,12 +17,9 @@ from flask import Flask app = Flask(__name__) app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True - - -# def get_ip(domain): -# ip_rq = dns.resolver.resolve(domain, 'A') -# ip = ip_rq[0].to_text() -# return(ip) +# 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 = False @@ -31,22 +29,100 @@ app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True # 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(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 +@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('/', methods=['GET']) def ipinfo(input): - ip_info = ip_api(input) - return(ip_info) + 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") diff --git a/app/requirements.txt b/app/requirements.txt index dbd3af9..3c98130 100644 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -1,3 +1,4 @@ dnspython Flask requests +whois \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 42a306f..e3290e1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,4 +7,6 @@ services: - "5000:5000" volumes: - ./app:/python-flask + env_file: + - .env container_name: resolv \ No newline at end of file diff --git a/env.example b/env.example new file mode 100644 index 0000000..456d89f --- /dev/null +++ b/env.example @@ -0,0 +1 @@ +HOSTIO_TOKEN=xyz \ No newline at end of file