in server/api/pages/search.py [0:0]
def run(API, environ, indata, session):
method = environ['REQUEST_METHOD']
# Searching? :)
if method == "POST":
found = {
'whitelist': [],
'banlist': [],
'iptables': [],
}
ip = indata['source']
docid = plugins.worker.make_sha1(ip)
#get whitelist and banlist, plus iptables rules
whitelist = plugins.worker.get_whitelist(session.DB)
banlist = plugins.worker.get_banlist(session.DB)
iptables = plugins.worker.get_iptables(session.DB)
me = plugins.worker.to_block(ip) # queried IP as IPNetwork object
# Find all whitelist entries that touch on this
for block in whitelist:
if me in block or block in me or me == block:
rule = find_rule(session.DB, 'whitelist', str(block))
if rule:
doc = rule['_source']
doc['rid'] = rule['_id']
found['whitelist'].append(doc)
# Find all banlist entries that touch on this
for block in banlist:
if me in block or block in me or me == block:
rule = find_rule(session.DB, 'ban', str(block))
if rule:
doc = rule['_source']
doc['rid'] = rule['_id']
if not 'ip' in doc:
doc['ip'] = doc['rid'].replace('_', '/')
found['banlist'].append(doc)
# Find any iptables rules that may have it as well
found_iptables = 0
anything = netaddr.IPNetwork("0.0.0.0/0")
for host in iptables:
for rule in host['rules']:
block = rule['ip']
if (me in block or block in me or me == block ) and (block != anything and me != anything):
rule['hostname'] = host['hostname']
rule['ip'] = str(rule['ip']) # stringify
found['iptables'].append(rule)
found_iptables += 1
yield json.dumps({"results": found}, indent = 2)
return
# Finally, if we hit a method we don't know, balk!
yield API.exception(400, "I don't know this request method!!")