Skip to content

Commit

Permalink
Merge pull request #11 from ZIT-P22/feature-filter-für-janneck
Browse files Browse the repository at this point in the history
Feature filter für janneck
  • Loading branch information
LeonKohli authored Feb 1, 2024
2 parents 3a3b28c + fb5da01 commit a5e2c88
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 43 deletions.
39 changes: 20 additions & 19 deletions ip-atlas/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,29 @@ def filter_data(search, key, data):
return filtered_data



# this function filter the json document for any ip address which maches the pattern
# example filter could be *.168.*.2 (* means it can be any number)
#? inputs have to be strings
# ? inputs have to be strings
def filterByIp(octed1, octed2, octed3, octed4, data):
# convert all octeds to int
goThrough = ["*",""]
result = {"hosts": []}
for i in range(len(data["hosts"])):
ip = devideIp(data["hosts"][i]["ip"])
if (
(octed1 == "*" or ip[0] == octed1)
and (octed2 == "*" or ip[1] == octed2)
and (octed3 == "*" or ip[2] == octed3)
and (octed4 == "*" or ip[3] == octed4)
(octed1 in goThrough or ip[0] == octed1)
and (octed2 in goThrough or ip[1] == octed2)
and (octed3 in goThrough or ip[2] == octed3)
and (octed4 in goThrough or ip[3] == octed4)
):
result["hosts"].append(data["hosts"][i])
return result

# filter by name/name section


def filterByName(search, data):
result = {"hosts" : []}
result = {"hosts": []}
# for name in data["hosts"]:
for i in range(len(data["hosts"])):
dataName = data["hosts"][i]["name"]
Expand All @@ -74,20 +76,17 @@ def filterByName(search, data):
return result

# filter by ports


def filterByPort(search, data):
result = {"hosts" : []}
goThrough = ["*",""]
result = {"hosts": []}
# for host in data:
if not search == "":
if not search in goThrough:
for i in range(len(data["hosts"])):
# dataPorts = host.get("ports")
dataPorts = data["hosts"][i]["ports"]
for port in dataPorts:
# for port in dataPorts[0]:
# print(port)
# print(search)
# print(dataPorts)
if search in port:
# result.append(host)
result["hosts"].append(data["hosts"][i])
break
return result
Expand All @@ -97,7 +96,7 @@ def filterByPort(search, data):

# filter by tags
def filterByTags(search, data):
result = {"hosts" : []}
result = {"hosts": []}
# for host in data:
for i in range(len(data["hosts"])):
# dataTags = host.get("tags")
Expand All @@ -110,6 +109,8 @@ def filterByTags(search, data):
return result

# applies all filters


def filterAll(ip, name, port, tag, data):
result = data
if ip != "":
Expand All @@ -124,17 +125,17 @@ def filterAll(ip, name, port, tag, data):
return result


# print(filterAll("*.*.*.*", "", "", "", loadJson()))
#! tests
# print(filterByIp("*", "*", "*", "47", loadJson()))
# print(filterByIp("*", "", "", "4", loadJson()))
# print(filterByName("40", loadJson()))
# data = filterByPort(99, loadJson())
# print(data)
# print(len(data))
# tags = filterByTags("tea", loadJson())
# print(tags)
# print(len(tags))
# print(filterAll("*.*.*.*", "", "", ""))
print(filterByIp("*", "*", "*", "*", filterByName("", filterByPort("", filterByTags("tea", loadJson())))))
# print(filterByIp("*", "*", "*", "*", filterByName("", filterByPort("", filterByTags("tea", loadJson())))))
# print(filterByIp("*", "*", "*", "*", filterByPort("99", loadJson())))
# printJson()
# print(filterByTags("test", filterByIp("*", "*", "*", "49", loadJson())))
51 changes: 28 additions & 23 deletions ip-atlas/routes/atlas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,11 @@ def index():
return render_template('ip/list.html', data=data)


@bp_atlas.route('/ip/list', methods=['GET', 'POST'])
@bp_atlas.route('/ip/list')
def list():

ipocted1 = request.form.get('ipocted1', '*')
ipocted2 = request.form.get('ipocted2', '*')
ipocted3 = request.form.get('ipocted3', '*')
ipocted4 = request.form.get('ipocted4', '*')
name = request.form.get('name', '')
port = request.form.get('port', '')
tag = request.form.get('tag', '')

createJson()
# printJson()
data = loadJson()

# Apply filters if any filter is provided
if any([ip, name, port, tag]):
filtered_data = filterAll(ip, name, port, tag)
else:
filtered_data = data
if any([ipocted1, ipocted2, ipocted3, ipocted4,nameFilter,portFilter,tagFilter]):
filtered_data = filterAll(ipocted1, ipocted2, ipocted3, ipocted4,nameFilter,portFilter,tagFilter)

return render_template('ip/list.html', data=filtered_data)
createJson()
data = loadJson()
return render_template('ip/list.html', data=data)


@bp_atlas.route('/ip/ping/<ip_address>')
Expand Down Expand Up @@ -141,3 +122,27 @@ def search():

# Render the template with the filtered data
return render_template('ip/list.html', data={'hosts': filtered_data})


@bp_atlas.route('/filter', methods=['GET'])
def filter():
data = loadJson()
# Retrieve query parameters
name = request.args.get('name', '')
ipocted1 = request.args.get('ipocted1', '')
ipocted2 = request.args.get('ipocted2', '')
ipocted3 = request.args.get('ipocted3', '')
ipocted4 = request.args.get('ipocted4', '')
tags = request.args.get('tags', '')
ports = request.args.get('ports', '')
print("Filter: ", name, ipocted1, ipocted2, ipocted3, ipocted4, tags, ports)
# Combine IP octets into a single string
ip = f"{ipocted1}.{ipocted2}.{ipocted3}.{ipocted4}"
print("IP: ", ip)

# Call the filterAll function with the collected parameters
filtered_data = filterAll(ip, name, ports, tags, data)
print("Filtered data: ", filtered_data)

# Render the template with the filtered data
return render_template('ip/list.html', data=filtered_data)
2 changes: 1 addition & 1 deletion ip-atlas/templates/ip/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h1 class="text-lg font-semibold md:text-2xl">IP Addresses</h1>
<span>Filter</span>
</button>
<div id="filterDropdown" class="absolute z-50 hidden w-64 p-4 mt-2 space-y-3 text-white bg-gray-800 divide-y divide-gray-600 rounded-lg shadow-lg top-full sm:w-80">
<form action="{{ url_for('atlas.list') }}" method="POST" class="space-y-3">
<form action="{{ url_for('atlas.filter') }}" method="GET" class="space-y-3">
<div class="space-y-1">
<label for="nameInput" class="block text-sm font-semibold">Name</label>
<input type="text" id="nameInput" name="name" value="{{ request.args.get('name', '') }}"
Expand Down

0 comments on commit a5e2c88

Please sign in to comment.