diff --git a/main.py b/main.py index a35ec95..8df73ae 100644 --- a/main.py +++ b/main.py @@ -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 diff --git a/templates/index.html b/templates/index.html index bafd278..9e64fbb 100644 --- a/templates/index.html +++ b/templates/index.html @@ -32,11 +32,12 @@
- +

IP Tables Visualizer

+ 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%;">