From 6ffb044f8e08d4cb8798b827ba46167e2023ba36 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sat, 22 Sep 2012 20:47:32 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + app.fcgi | 7 +++ app.py | 123 +++++++++++++++++++++++++++++++++++++++++++ lighttpd.conf | 22 ++++++++ run.py | 8 +++ static/style.css | 104 ++++++++++++++++++++++++++++++++++++ templates/base.tpl | 37 +++++++++++++ templates/home.tpl | 4 ++ templates/result.tpl | 62 ++++++++++++++++++++++ 9 files changed, 368 insertions(+) create mode 100644 .gitignore create mode 100755 app.fcgi create mode 100644 app.py create mode 100644 lighttpd.conf create mode 100644 run.py create mode 100644 static/style.css create mode 100644 templates/base.tpl create mode 100644 templates/home.tpl create mode 100644 templates/result.tpl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/app.fcgi b/app.fcgi new file mode 100755 index 0000000..a276ed7 --- /dev/null +++ b/app.fcgi @@ -0,0 +1,7 @@ +#!/usr/local/bin/python +from flup.server.fcgi import WSGIServer +from app import app +from werkzeug.contrib.fixers import LighttpdCGIRootFix + +if __name__ == '__main__': + WSGIServer(LighttpdCGIRootFix(app)).run() diff --git a/app.py b/app.py new file mode 100644 index 0000000..681f243 --- /dev/null +++ b/app.py @@ -0,0 +1,123 @@ +import subprocess, datetime, time + +try: + import json +except ImportError: + import simplejson as json + +try: + import pythonwhois +except ImportError: + print "pythonwhois is not installed!" + exit(1) + +try: + import pymongo +except ImportError: + print "pymongo is not installed!" + exit(1) + +from flask import Flask, request, render_template, flash, redirect, url_for +app = Flask(__name__) +app.secret_key = "er4988n439h981359n5n9n5954b0fsdfkjglqrekjt0314njn" +app.debug=True +db = pymongo.Connection()['crytowhois'] + +def format_date(dates): + if dates is None: + return None + else: + return dates[0].isoformat() + +def get_first(options): + if options is None: + return None + else: + return options[0] + +def whois(domain): + result = pythonwhois.whois(domain) + + if result['creation_date'] is None and result['expiration_date'] is None and result['registrar'] is None and result['name_servers'] is None: + return None + else: + creation_date = format_date(result['creation_date']) + expiration_date = format_date(result['expiration_date']) + updated_date = format_date(result['updated_date']) + registrar = get_first(result['registrar']) + whois_server = get_first(result['whois_server']) + emails = result['emails'] + nameservers = result['name_servers'] + + return { + 'creation_date': creation_date, + 'expiration_date': expiration_date, + 'updated_date': updated_date, + 'registrar': registrar, + 'whois_server': whois_server, + 'emails': emails, + 'nameservers': nameservers + } + +@app.route('/', methods=["GET"]) +def home(): + return render_template("home.tpl") + +def find_whois(domain): + db_results = db['responses'].find_one({'domain': domain}) + + if db_results is not None: + return (db_results['timestamp'], db_results['response']) + else: + result = whois(domain) + + if result is not None: + db['responses'].insert({ + 'domain': domain, + 'response': result, + 'timestamp': time.time() + }) + + return (time.time(), result) + else: + return (time.time(), None) + +@app.route('/query', methods=["POST"]) +def query(): + try: + domain = request.form['domain'] + + if domain == "": + flash("You did not enter a domain.") + return render_template("home.tpl") + else: + return redirect(url_for('query_html', domain=domain)) + except KeyError, e: + flash("You did not specify a domain.") + return render_template("home.tpl") + +@app.route('/query/html/', methods=["GET"]) +def query_html(domain): + retrieval_date, result = find_whois(domain) + + if result is None: + flash("The specified domain does not exist.") + return render_template("home.tpl"), 404 + else: + retrieval_timestamp = datetime.datetime.fromtimestamp(int(retrieval_date)) + return render_template("result.tpl", domain=domain, retrieval_date=retrieval_timestamp.isoformat(), **result) + + +@app.route('/query/json/', methods=["GET"]) +def query_json(domain): + if domain is not None: + retrieval_date, result = find_whois(domain) + + result['retrieval_date'] = int(retrieval_date) + + return json.dumps(result) + else: + return json.dumps(None) + +if __name__ == '__main__': + app.run(host="0.0.0.0", port=1234, debug=True) diff --git a/lighttpd.conf b/lighttpd.conf new file mode 100644 index 0000000..61837b1 --- /dev/null +++ b/lighttpd.conf @@ -0,0 +1,22 @@ +server.event-handler = "poll" + +$HTTP["host"] =~ "whois.cryto.net" { + server.document-root = "/var/apps/whois" + + fastcgi.server = ("/" => + (( + "socket" => "/tmp/whoisapp-fcgi.sock", + "bin-path" => "/var/apps/whois/app.fcgi", + "check-local" => "disable", + "max-procs" => 1 + )) + ) + + alias.url = ( + "/static" => "/var/apps/whois/static" + ) + + url.rewrite-once = ( + "^(/static($|/.*))$" => "$1" + ) +} diff --git a/run.py b/run.py new file mode 100644 index 0000000..a218893 --- /dev/null +++ b/run.py @@ -0,0 +1,8 @@ +from tornado.wsgi import WSGIContainer +from tornado.httpserver import HTTPServer +from tornado.ioloop import IOLoop +from app import app + +http_server = HTTPServer(WSGIContainer(app)) +http_server.listen(1234) +IOLoop.instance().start() diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..eb83dc2 --- /dev/null +++ b/static/style.css @@ -0,0 +1,104 @@ +body +{ + background-color: #FBFBFB; + padding: 0px; + margin: 0px; + font-family: 'Orienta', 'Open Sans', sans-serif; +} + +label, input, button +{ + font-family: 'Source Sans Pro', 'Open Sans', sans-serif; +} + +#header +{ + padding: 11px 18px; + background-color: #505050; + color: white; +} + +#contents +{ + padding: 13px 18px; +} + +h1 +{ + margin: 0px; +} + +#whois_form +{ + padding: 6px 18px; + background-color: #E3E3E3; + border: 2px solid #252525; + border-radius: 8px; +} + +#whois_form label, input, button +{ + font-size: 22px; + color: #2D2D2D; +} + +#whois_form label +{ + margin-right: 10px; + font-weight: bold; +} + +#whois_form button +{ + padding: 2px 12px; + margin-left: 10px; +} + +#whois_form input +{ + padding: 3px 7px; + width: 400px; +} + +h2 +{ + margin-bottom: 4px; +} + +table th, td +{ + text-align: left; + border: 1px solid #636363; + padding: 4px 9px; +} + +table +{ + border-collapse: collapse; + border: 2px solid black; +} + +#flashes +{ + list-style: none; + margin: 18px 13px 6px 18px; + border: 1px solid red; + padding: 6px 8px; + background-color: #FFBFBF; +} + +#footer +{ + margin: 9px 18px; + background-color: #2F2F2F; + color: white; + padding: 8px 12px; + border: 1px solid black; + border-radius: 7px; + font-size: 13px; +} + +#footer a +{ + color: #FFFB85; +} diff --git a/templates/base.tpl b/templates/base.tpl new file mode 100644 index 0000000..172a9ee --- /dev/null +++ b/templates/base.tpl @@ -0,0 +1,37 @@ + + + + Cryto WHOIS + + + + + + + {% with messages = get_flashed_messages() %} + {% if messages %} +
    + {% for message in messages %} +
  • {{ message }}
  • + {% endfor %} +
+ {% endif %} + {% endwith %} +
+
+ + + +
+ {% block body %} + It would seem the page you requested was not found. + {% endblock %} +
+ + + diff --git a/templates/home.tpl b/templates/home.tpl new file mode 100644 index 0000000..1cf4a5c --- /dev/null +++ b/templates/home.tpl @@ -0,0 +1,4 @@ +{% extends "base.tpl" %} +{% block body %} + +{% endblock %} diff --git a/templates/result.tpl b/templates/result.tpl new file mode 100644 index 0000000..b913d13 --- /dev/null +++ b/templates/result.tpl @@ -0,0 +1,62 @@ +{% extends "base.tpl" %} +{% block body %} +
+

WHOIS results

+ + + + + + {% if registrar != None %} + + + + + {% endif %} + {% if whois_server != None %} + + + + + {% endif %} + {% if creation_date != None %} + + + + + {% endif %} + {% if expiration_date != None %} + + + + + {% endif %} + {% if updated_date != None %} + + + + + {% endif %} + {% if nameservers != None %} + {% for nameserver in nameservers %} + + + + + {% endfor %} + {% endif %} + {% if emails != None %} + {% for email in emails %} + + + + + {% endfor %} + {% endif %} + +
WHOIS record retrieval date:{{ retrieval_date }}
Registrar:{{ registrar }}
WHOIS server:{{ whois_server }}
Creation date:{{ creation_date }}
Expiration date:{{ expiration_date }}
Record/database last updated:{{ updated_date }}
Nameserver:{{ nameserver }}
Contact e-mail:{{ email }}
+

+ Need JSON? Try http://whois.cryto.net/query/json/{{ domain }} +

+
+{% endblock %}