mostly working, visualization isn't that great and might be more useful if it shows bandwidth

This commit is contained in:
Tanishq Dubey 2024-03-10 18:23:25 -04:00
parent 8e3a0dc41d
commit ae0a9ea6fd
2 changed files with 47 additions and 5 deletions

47
main.py
View File

@ -6,6 +6,8 @@ from dataclasses import dataclass
from flask import Flask, render_template, request, jsonify
import uuid
@dataclass
class ChainRule:
packets: str
@ -19,8 +21,11 @@ class ChainRule:
destination: str
extra: str
raw: str
id: Optional[str] = None
def name(self):
if self.id is None:
self.id = str(uuid.uuid4())
return f"{self.protocol}\n{self.inp}->{self.out}>{self.target}{'(' + self.extra + ')' if self.extra else ''}"
@dataclass
@ -64,12 +69,50 @@ def build_chain_tree(chain: Chain):
def build_full_chain_tree(chains: List[Chain]):
ret = [[], [], []]
for chain in chains:
pprint.pp(len(chains))
for chain in chains.copy():
if not chain.referenced:
tree = build_chain_tree(chain)
ret[0] = ret[0] + tree[0]
ret[1] = ret[1] + tree[1]
ret[2] = ret[2] + tree[2]
chains.remove(chain)
pprint.pp(len(chains))
# Merge all ends
merged = []
for val in ret[-1]:
t = find_id_position(merged, val["id"])
if t is None:
merged.append(val)
else:
merged[t]["parents"] = merged[t]["parents"] + val["parents"]
ret[-1] = merged
while len(chains) > 0:
next = chains.pop(0)
next_tree = build_chain_tree(next)
# weird rebuild
next_id = find_id_position(ret[-1], next.name)
next_pop_id = ret[-1].pop(next_id)
append_ret = ret[-1]
next_tree[-1] = append_ret + next_tree[-1]
ret[-1] = [next_pop_id]
next_tree.pop(0)
ret = ret + next_tree
merged = []
for val in ret[-1]:
t = find_id_position(merged, val["id"])
if t is None:
merged.append(val)
else:
merged[t]["parents"] = merged[t]["parents"] + val["parents"]
ret[-1] = merged
pprint.pp(ret)
return ret
@ -88,14 +131,12 @@ def manual_parse_chain():
# First line of a chain is the chain metadata, such as name
chain_meta_raw = rules_raw[0].strip().split(" ")
policy = None
pprint.pp(chain_meta_raw)
if not ('references' in rules_raw[0]):
policy = chain_meta_raw[2]
chain = Chain(chain_meta_raw[0], policy, [])
if not ('policy' in rules_raw[0]):
chain.referenced = True
pprint.pp(chain)
# Second line is headers for the table, so drop

View File

@ -32,11 +32,12 @@
<form id="parse-form" method="post" class="pure-form pure-form-stacke" style="height: 100%;">
<div style="display: flex; flex-direction: column; height: 100%; gap: .25em;">
<div>
<label for="parsedata">Input</label>
<h3>IP Tables Visualizer</h3>
</div>
<div style="flex-grow: 2;">
<textarea placeholder="Paste the output of 'sudo iptables -L -v' here" id="parsedata" name="parsedata"
class=" pure-input" style="width: 100%; resize: none; height: 100%;"></textarea>
class=" pure-input"
style=" font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace; white-space: pre; overflow-x: scroll; width: 100%; resize: none; height: 100%;"></textarea>
</div>
<div style="width 100%">
<input class="pure-button pure-button-primary" type="submit" value="Render" style="width: 100%;">