-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·138 lines (115 loc) · 3.76 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
132
133
134
135
136
137
138
#!/usr/bin/env python
import os, sys, argparse, logging, json, base64
import requests, redis
from functools import lru_cache
from bottle import route, request, response, redirect, default_app, view, template, static_file
from cymruwhois import Client
from classes.address import Address
def enable_cors(fn):
def _enable_cors(*args, **kwargs):
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
if request.method != 'OPTIONS':
return fn(*args, **kwargs)
return _enable_cors
def return_error(status=404, message=''):
log.error(message)
output = {
'success': False,
'message': message
}
response.status = status
return json.dumps(output)
@route('/static/<filepath:path>')
def static(filepath):
return static_file(filepath, root='views/static')
@route('/<ip>', method=('OPTIONS', 'GET'))
@route('/<ip>/<contentType>', method=('OPTIONS', 'GET'))
@enable_cors
def process(ip, contentType='html'):
if ip in ["favicon.ico", "robots.txt"]:
return ""
output = {
"success": True,
"results": [],
"results_info": {
"count": 0,
"cached": 0
}
}
uri = ip
lookups = []
for ip in ip.split(','):
try:
Address(ip)
if r.get(ip):
output['results'].append(json.loads(r.get(ip)))
output['results_info']['cached'] = output['results_info']['cached'] + 1
else:
lookups.append(ip)
except AttributeError:
return return_error(400, "{} is not a valid IP address".format(ip))
for address in Client().lookupmany(lookups):
data = {
"ip": address.ip,
"asn": address.asn,
"prefix": address.prefix,
"owner": address.owner,
"cc": address.cc
}
# Now we push it to redis
r.set(address.ip, json.dumps(data), ex=84600)
output['results'].append(json.loads(r.get(address.ip)))
output['results_info']['count'] = len(output['results'])
if contentType in ['json']:
response.headers['Content-Type'] = 'application/json'
return json.dumps(output)
else:
response.headers['Content-Type'] = 'text/html'
return template("rec", uri=uri, data=output['results'])
@route('/cache')
def cache():
try:
output = {}
for key in r.scan_iter('*'):
output[key.decode("utf-8")] = json.loads(r.get(key))
response.headers['Content-Type'] = 'application/json'
return json.dumps(output)
except:
return return_error(403, "Unable to load keys from redis for display. Please try again later.")
@route('/ping')
def ping():
response.content_type = "text/plain"
return "pong"
@route('/', ('GET', 'POST'))
def index():
if request.method == "POST":
if request.forms.get('address', '') != '':
return redirect("/{}".format(request.forms.get('address')))
return template("home")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# Server settings
parser.add_argument("-i", "--host", default=os.getenv('HOST', '127.0.0.1'), help="server ip")
parser.add_argument("-p", "--port", default=os.getenv('PORT', 5000), help="server port")
# Redis settings
parser.add_argument("--redis", default=os.getenv('REDIS', 'redis://localhost:6379/0'), help="redis connection string")
# Verbose mode
parser.add_argument("--verbose", "-v", help="increase output verbosity", action="store_true")
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
try:
if args.redis:
r = redis.from_url(args.redis)
except:
log.fatal("Unable to connect to redis: {}".format(args.redis))
try:
app = default_app()
app.run(host=args.host, port=args.port, server='tornado')
except:
log.error("Unable to start server on {}:{}".format(args.host, args.port))