-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·131 lines (101 loc) · 3.61 KB
/
app.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
#!/usr/bin/env python
from flask import Flask, request, jsonify, render_template, flash, g, redirect, url_for
import json, socket, geoip2.database, geoip2.errors, ipaddr, os
from flask_bootstrap import Bootstrap
from mongokit import Connection, Document
import config
app = Flask(__name__)
bootstrap = Bootstrap(app)
app.config.from_object(config.Config)
# GeoIP DB Reader
reader = geoip2.database.Reader('GeoLite2-City.mmdb')
#DB Query Model
class Domain_Lookup(Document):
structure = {
'IP Address': str,
'City': str,
'Subdivision': str,
'Country': str,
'Country Code': str,
'Latitude': str,
'Longitude': str,
'Domain Name': str,
}
use_dot_notation = True
def __repr__(self):
return '<Domain Lookup %r>' % (self['Domain Name'])
# register the User document with our current connection
connection = Connection(app.config['MONGOLAB_URI'])
connection.register([Domain_Lookup])
collection = connection[app.config['MONGOLAB_DB']].lookups
def get_traits(ip_addr):
try:
ipaddr.IPAddress(ip_addr)
domain_name = socket.gethostbyaddr(ip_addr)[0]
except (ValueError, ipaddr.AddressValueError) as e:
domain_name = ip_addr
ip_addr = socket.gethostbyname(domain_name)
traits = reader.city(ip_addr)
lookup = collection.Domain_Lookup()
lookup['IP Address'] = str(ip_addr)
lookup['City'] = str(traits.city.name)
lookup['Subdivision'] = str(traits.subdivisions.most_specific.name)
lookup['Country'] = str(traits.country.name)
lookup['Country Code'] = str(traits.country.iso_code)
lookup['Latitude'] = str(traits.location.latitude)
lookup['Longitude'] = str(traits.location.longitude)
lookup['Domain Name'] = str(domain_name)
# store query for history
lookup.save()
details = {
'IP Address': lookup['IP Address'],
'City': lookup['City'],
'Subdivision': lookup['Subdivision'],
'Country': lookup['Country'],
'Country Code': lookup['Country Code'],
'Latitude': lookup['Latitude'],
'Longitude': lookup['Longitude'],
'Domain Name': lookup['Domain Name'],
}
return details
@app.route('/')
def home():
return render_template('home.html')
@app.route('/dest/<destination>')
def home_dest(destination):
try:
ip_traits = get_traits(destination)
except ValueError as e:
return jsonify({'Error': e.message}), 400
try:
return render_template('home.html', destination=get_traits(destination), orig_query=destination)
except ValueError as e:
flash(e.message, 'danger')
return render_template('home.html')
@app.route('/feed/')
def feed():
try:
limit = int(request.args.get('limit')) or 20
except TypeError:
limit = 20
points = [x for x in collection.Domain_Lookup.find()] or [None]
return render_template('tail.html', points=points, limit=limit)
@app.route('/findme')
def findme():
return render_template('findme.html')
@app.route('/features')
def features():
return render_template('features.html')
@app.route('/geoip/<ip_addr>', methods=['GET', 'POST'])
def geoip_lookup(ip_addr):
try:
details = get_traits(ip_addr)
except Exception as e:
return jsonify({"Error": "'" + ip_addr + "' is not a valid domain name or IP address."}), 400
return jsonify(details), 200
@app.errorhandler(404)
def page_not_found(e):
flash('The page you requested does not exist', 'warning')
return redirect(url_for('home'))
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=5000)