-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyrdap_server.py
190 lines (172 loc) · 6.37 KB
/
pyrdap_server.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from flask import Flask,jsonify,session
import ipwhois
from ipwhois import IPWhois
import socket
import socks
import requests
import json
import elastastore
import sys
sys.path.append("/opt/pyrdap/whois")
import whois
from elasticsearch import Elasticsearch
app = Flask(__name__)
app.config["JSON_SORT_KEYS"] = False
@app.route('/rdap/ip/<ip>', methods=['GET'])
def get_rdap_ip(ip):
es = Elasticsearch()
does_exist = es.exists(index='rdap', doc_type='ipaddr', id = ip)
print does_exist
if does_exist is True:
status = 200
print "Found it!"
get_record = es.get(index='rdap',doc_type='ipaddr', id = ip)
results = jsonify(get_record['_source'])
else:
try:
obj = IPWhois(ip)
results_raw = obj.lookup_rdap(depth=1)
status = 200
results = jsonify(results_raw)
es.index(index='rdap', doc_type='ipaddr', id=ip, body=json.dumps(results_raw))
except Exception as e:
print e
results = jsonify({'status': "not_found"})
status = 404
results = jsonify({'status': "not_found"})
return results,status
@app.route('/rdap/asn/<asn>', methods=['GET'])
def get_rdap_asn(asn):
es = Elasticsearch()
does_exist = es.exists(index='whois', doc_type='asn_rdap', id = asn)
print does_exist
if does_exist is True:
status = 200
print "Found it!"
get_record = es.get(index='rdap',doc_type='asn', id = asn)
results = jsonify(get_record['_source'])
else:
try:
url = 'http://hailey.opendnsbl.net:8080/rdapbootstrap/autnum/%s' % asn
r = requests.get(url)
status = 200
b = r.json()
#c = json.loads(b)
#d = c['entities']
#print d
#e = json.dumps(c)
#es.index(index='rwhois', doc_type='asn', id=asn, body=json.dumps(b))
results = jsonify(b)
except Exception as e:
print e
results_raw = jsonify({'status': "not_found"})
status = 404
results = jsonify({'status': "not_found"})
return results,status
@app.route('/whois/ip/<ip>', methods=['GET'])
@app.route('/whois/ip/<ip>/refresh/<refresh>', methods=['GET'])
def get_whois_ip(ip,refresh=None):
es = Elasticsearch()
print repr(ip)
id_num = str(ip).replace(".","0")
does_exist = es.exists(index='rwhois2', doc_type='ipaddr', id = id_num)
print does_exist
if does_exist is True and refresh is None:
status = 200
print "Found it!"
get_record = es.get(index='rwhois2',doc_type='ipaddr', id = id_num)
results = jsonify(get_record['_source'])
elif does_exist is True and refresh is not None:
status = 200
print "Forcing refresh!"
es.delete(index='rwhois2', doc_type='ipaddr', id = id_num)
try:
ipwhois.net.socks.setdefaultproxy(ipwhois.net.socks.SOCKS5,"localhost")
obj = IPWhois(ip)
try:
results_raw = obj.lookup_whois(get_referral=True,inc_nir=True)
except:
results_raw = obj.lookup_whois()
status = 200
results = jsonify(results_raw)
es.index(index='rwhois2', doc_type='ipaddr', id=id_num, body=results_raw)
except Exception as e:
print e
results = jsonify({'status': "not_found"})
status = 404
else:
try:
obj = IPWhois(ip)
try:
results_raw = obj.lookup_whois(get_referral=True)
except:
results_raw = obj.lookup_whois()
status = 200
results = jsonify(results_raw)
id_num = str(ip).replace(".","0")
print results
try:
es.index(index='rwhois2', doc_type='ipaddr', id=id_num, body=results_raw)
except Exception as e:
print "Elasticsearch encountered a problem ", e
pass
except Exception as e:
#print results_raw
print e
results_raw = jsonify({'status': "not_found"})
status = 404
results = jsonify({'status': "not_found"})
return results,status
@app.route('/whois/search/<search>', methods=['GET'])
def get_search(search):
es = Elasticsearch()
a = str(search)
try:
b = es.search(index='rwhois2',size='10000', body={"query": {"filtered": {"query":{ "query_string": { "query": a}}}}})
status = 200
results = jsonify(b)
except Exception as e:
print e
results_raw = jsonify({'status': "not_found"})
status = 404
results = jsonify({'status': "not_found"})
return results,status
@app.route('/whois/domain/<domain>', methods=['GET'])
@app.route('/whois/domain/<domain>/refresh/<refresh>', methods=['GET'])
def get_whois_domain(domain,refresh=None):
es = Elasticsearch()
id_num = domain
does_exist = es.exists(index='domain', doc_type='domain', id = domain)
print does_exist
if does_exist is True and refresh is None:
status = 200
print "Found it!"
get_record = es.get(index='domain',doc_type='domain', id = domain)
results = jsonify(get_record['_source'])
elif does_exist is True and refresh is not None:
status = 200
print "Forcing refresh!"
es.delete(index='domain', doc_type='domain', id = domain)
try:
obj = whois.whois(domain)
status = 200
results = jsonify(obj)
es.index(index='domain', doc_type='domain', id=domain, body=obj)
except Exception as e:
print e
results_raw = jsonify({'status': "not_found"})
status = 404
else:
try:
obj = whois.whois(domain)
status = 200
results = jsonify(obj)
es.index(index='domain', doc_type='domain', id=domain, body=obj)
except Exception as e:
print e
results_raw = jsonify({'status': "not_found"})
status = 404
results = jsonify({'status': "not_found"})
return results,status
if __name__ == '__main__':
app.run(host='0.0.0.0',port=8006,debug=True,threaded=True)