From a9170e9d4238ed30688f1d1cf7fc38391cc36767 Mon Sep 17 00:00:00 2001 From: Leon Kohli <98176333+LeonKohli@users.noreply.github.com> Date: Wed, 31 Jan 2024 12:41:09 +0100 Subject: [PATCH 1/2] =?UTF-8?q?f=C3=BCr=20janneck?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ip-atlas/filter.py | 25 ++++++------- ip-atlas/routes/atlas.py | 66 +++++++++++++++++++++++---------- ip-atlas/templates/ip/list.html | 12 +++--- 3 files changed, 64 insertions(+), 39 deletions(-) diff --git a/ip-atlas/filter.py b/ip-atlas/filter.py index d42d3bc..89d693d 100644 --- a/ip-atlas/filter.py +++ b/ip-atlas/filter.py @@ -45,10 +45,9 @@ 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 result = {"hosts": []} @@ -64,8 +63,10 @@ def filterByIp(octed1, octed2, octed3, octed4, data): 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"] @@ -74,20 +75,16 @@ def filterByName(search, data): return result # filter by ports + + def filterByPort(search, data): - result = {"hosts" : []} + result = {"hosts": []} # for host in data: if not search == "": 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 @@ -97,7 +94,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") @@ -110,6 +107,8 @@ def filterByTags(search, data): return result # applies all filters + + def filterAll(ip, name, port, tag, data): result = data if ip != "": @@ -124,6 +123,7 @@ def filterAll(ip, name, port, tag, data): return result +# print(filterAll("*.*.*.*", "", "", "", loadJson())) #! tests # print(filterByIp("*", "*", "*", "47", loadJson())) # print(filterByName("40", loadJson())) @@ -133,8 +133,7 @@ def filterAll(ip, name, port, tag, 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()))) diff --git a/ip-atlas/routes/atlas.py b/ip-atlas/routes/atlas.py index 2c4b0bd..2bee049 100644 --- a/ip-atlas/routes/atlas.py +++ b/ip-atlas/routes/atlas.py @@ -15,28 +15,29 @@ def index(): @bp_atlas.route('/ip/list', methods=['GET', 'POST']) 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) + 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) + return render_template('ip/list.html', data=filtered_data) @bp_atlas.route('/ip/ping/') @@ -141,3 +142,28 @@ 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={'hosts': filtered_data}) diff --git a/ip-atlas/templates/ip/list.html b/ip-atlas/templates/ip/list.html index b41617b..b39f58b 100644 --- a/ip-atlas/templates/ip/list.html +++ b/ip-atlas/templates/ip/list.html @@ -15,7 +15,7 @@

IP Addresses

Filter