venv + install requirement auto
This commit is contained in:
301
gui_firewall.py
Normal file
301
gui_firewall.py
Normal file
@@ -0,0 +1,301 @@
|
||||
import subprocess
|
||||
import os
|
||||
import sys
|
||||
import tkinter as tk
|
||||
from tkinter import ttk, filedialog, messagebox
|
||||
from datetime import datetime
|
||||
from threading import Thread
|
||||
|
||||
class ToolTip:
|
||||
def __init__(self, widget, text):
|
||||
self.widget = widget
|
||||
self.text = text
|
||||
self.tipwindow = None
|
||||
|
||||
widget.bind("<Enter>", self.show)
|
||||
widget.bind("<Leave>", self.hide)
|
||||
|
||||
def show(self, event=None):
|
||||
if self.tipwindow or not self.text:
|
||||
return
|
||||
|
||||
x = self.widget.winfo_rootx() + 20
|
||||
y = self.widget.winfo_rooty() + 20
|
||||
|
||||
self.tipwindow = tw = tk.Toplevel(self.widget)
|
||||
tw.wm_overrideredirect(True)
|
||||
tw.wm_geometry(f"+{x}+{y}")
|
||||
|
||||
label = tk.Label(
|
||||
tw,
|
||||
text=self.text,
|
||||
justify="left",
|
||||
background="#ffffe0",
|
||||
relief="solid",
|
||||
borderwidth=1,
|
||||
font=("Arial", 9),
|
||||
padx=8,
|
||||
pady=4
|
||||
)
|
||||
label.pack()
|
||||
|
||||
def hide(self, event=None):
|
||||
if self.tipwindow:
|
||||
self.tipwindow.destroy()
|
||||
self.tipwindow = None
|
||||
|
||||
# Contenu fenêtre Analyse Firewall
|
||||
def open_firewall_gui(root, BASE_DIR):
|
||||
FIREWALL_DIR = os.path.join(
|
||||
BASE_DIR,
|
||||
"Parseurs_config_Firewall"
|
||||
)
|
||||
|
||||
FIREWALL_MAIN = os.path.join(
|
||||
FIREWALL_DIR,
|
||||
"src",
|
||||
"main.py"
|
||||
)
|
||||
|
||||
HELP_FILE_FW = os.path.join(BASE_DIR, "help_Firewall.md")
|
||||
|
||||
OUTPUT_DIR = os.path.join(FIREWALL_DIR, "src", "output")
|
||||
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||||
|
||||
app = tk.Toplevel(root)
|
||||
app.title("Analyse Configuration Firewall")
|
||||
app.geometry("800x400")
|
||||
app.resizable(False, False)
|
||||
|
||||
firewall_var = tk.StringVar()
|
||||
input_var = tk.StringVar()
|
||||
output_var = tk.StringVar()
|
||||
|
||||
matrice_flux = tk.BooleanVar()
|
||||
matrice_routage = tk.BooleanVar()
|
||||
|
||||
def browse_input():
|
||||
fw_type = firewall_var.get()
|
||||
if fw_type == "":
|
||||
messagebox.showerror("Erreur", "Veuillez d'abord sélectionner un type de firewall.")
|
||||
return
|
||||
|
||||
if fw_type.lower() == "stormshield":
|
||||
path = filedialog.askdirectory(title="Sélectionnez le dossier d'entrée", parent=app)
|
||||
else:
|
||||
path = filedialog.askopenfilename(title="Sélectionnez le fichier d'entrée", parent=app)
|
||||
|
||||
if path:
|
||||
input_var.set(path)
|
||||
|
||||
def update_output_label(*args):
|
||||
if firewall_var.get() == "":
|
||||
output_label_var.set("Fichier de sortie :")
|
||||
return
|
||||
else :
|
||||
fw = firewall_var.get().lower()
|
||||
|
||||
if output_var.get():
|
||||
f_json = os.path.join(OUTPUT_DIR, f"{fw}_{output_var.get()}.json")
|
||||
f_flux = os.path.join(OUTPUT_DIR, f"matrice_flux_{fw}_{output_var.get()}.xlsx")
|
||||
f_routage = os.path.join(OUTPUT_DIR, f"matrice_routage_{fw}_{output_var.get()}.xlsx")
|
||||
else:
|
||||
dt = datetime.now().strftime("%Y%m%d")
|
||||
f_json = os.path.join(OUTPUT_DIR, f"{fw}_{dt}.json")
|
||||
f_flux = os.path.join(OUTPUT_DIR, f"matrice_flux_{fw}_{dt}.xlsx")
|
||||
f_routage = os.path.join(OUTPUT_DIR, f"matrice_routage_{fw}_{dt}.xlsx")
|
||||
|
||||
if not matrice_flux.get():
|
||||
if not matrice_routage.get():
|
||||
output_label_var.set("Fichier de sortie :\n" + f_json)
|
||||
else:
|
||||
output_label_var.set("Fichiers de sortie :\n" + f_json + "\n" + f_routage)
|
||||
else:
|
||||
if not matrice_routage.get():
|
||||
output_label_var.set("Fichiers de sortie :\n" + f_json + "\n" + f_flux)
|
||||
else:
|
||||
output_label_var.set("Fichiers de sortie :\n" + f_json + "\n" + f_flux + "\n" + f_routage)
|
||||
app.update_idletasks()
|
||||
|
||||
def open_output_folder():
|
||||
if sys.platform == "win32":
|
||||
os.startfile(OUTPUT_DIR)
|
||||
elif sys.platform == "darwin":
|
||||
subprocess.run(["open", OUTPUT_DIR])
|
||||
else:
|
||||
subprocess.run(["xdg-open", OUTPUT_DIR])
|
||||
|
||||
def open_help():
|
||||
if not os.path.exists(HELP_FILE_FW):
|
||||
messagebox.showerror("Erreur", "Le fichier d'aide est introuvable.")
|
||||
return
|
||||
|
||||
if sys.platform == "win32":
|
||||
os.startfile(HELP_FILE_FW)
|
||||
elif sys.platform == "darwin":
|
||||
subprocess.run(["open", HELP_FILE_FW])
|
||||
else:
|
||||
subprocess.run(["xdg-open", HELP_FILE_FW])
|
||||
|
||||
def run_parser():
|
||||
if not firewall_var.get():
|
||||
messagebox.showerror("Erreur", "Veuillez sélectionner un type de firewall.")
|
||||
return
|
||||
|
||||
if not input_var.get():
|
||||
messagebox.showerror("Erreur", "Veuillez sélectionner un fichier ou dossier d'entrée.")
|
||||
return
|
||||
|
||||
app.config(cursor="watch")
|
||||
progress_bar = ttk.Progressbar(app, mode="indeterminate")
|
||||
progress_bar.pack(pady=10, fill="x", padx=10)
|
||||
progress_bar.start(10)
|
||||
app.update_idletasks()
|
||||
def process():
|
||||
cmd = [
|
||||
sys.executable,
|
||||
FIREWALL_MAIN,
|
||||
firewall_var.get(),
|
||||
input_var.get()
|
||||
]
|
||||
|
||||
if output_var.get():
|
||||
cmd.extend(["-o", output_var.get()])
|
||||
|
||||
if matrice_flux.get():
|
||||
cmd.append("-f")
|
||||
|
||||
if matrice_routage.get():
|
||||
cmd.append("-r")
|
||||
|
||||
print("Commande exécutée :", " ".join(cmd))
|
||||
print("Dossier courant (cwd) :", FIREWALL_DIR)
|
||||
|
||||
try:
|
||||
subprocess.run(
|
||||
cmd,
|
||||
cwd=FIREWALL_DIR,
|
||||
check=True
|
||||
)
|
||||
|
||||
def on_success():
|
||||
progress_bar.stop()
|
||||
progress_bar.destroy()
|
||||
app.config(cursor="")
|
||||
messagebox.showinfo("Succès", "Traitement terminé avec succès.")
|
||||
open_output_folder()
|
||||
app.destroy()
|
||||
|
||||
app.after(0, on_success)
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
def on_error():
|
||||
progress_bar.stop()
|
||||
progress_bar.destroy()
|
||||
app.config(cursor="")
|
||||
messagebox.showerror(
|
||||
"Erreur",
|
||||
f"Erreur lors de l'exécution du traitement.\n\n{e}"
|
||||
)
|
||||
|
||||
app.after(0, on_error)
|
||||
|
||||
Thread(target=process, daemon=True).start()
|
||||
|
||||
header_frame = ttk.Frame(app)
|
||||
header_frame.pack(fill="x", padx=10, pady=5)
|
||||
|
||||
ttk.Button(
|
||||
header_frame,
|
||||
text="Help",
|
||||
width=6,
|
||||
command=open_help
|
||||
).pack(side="right")
|
||||
|
||||
ttk.Label(
|
||||
header_frame,
|
||||
text="Génération d'un fichier JSON au format normalisé YANG à partir des configurations d'un firewall",
|
||||
font=("Arial", 8, "bold")
|
||||
).pack(side="left")
|
||||
|
||||
ttk.Label(app, text="Type de firewall").pack(anchor="w", padx=10, pady=5)
|
||||
ttk.Combobox(
|
||||
app,
|
||||
values=["paloalto", "stormshield", "forcepoint"],
|
||||
textvariable=firewall_var,
|
||||
state="readonly"
|
||||
).pack(fill="x", padx=10)
|
||||
|
||||
|
||||
input_label_var = tk.StringVar()
|
||||
input_label_var.set("Fichier ou dossier de configuration source")
|
||||
input_label_frame = ttk.Frame(app)
|
||||
input_label_frame.pack(anchor="w", padx=5, pady=5)
|
||||
info_label = ttk.Label(
|
||||
input_label_frame,
|
||||
text="ⓘ",
|
||||
cursor="hand2"
|
||||
)
|
||||
info_label.pack(side="right")
|
||||
ttk.Label(
|
||||
input_label_frame,
|
||||
textvariable=input_label_var
|
||||
).pack(side="left", padx=(5, 0))
|
||||
ToolTip(
|
||||
info_label,
|
||||
"/!\\ Mettre le fichier/dossier de configurations\n"
|
||||
"dans le dossier `/Parseurs_config_Firewall/src/input/` /!\\"
|
||||
)
|
||||
|
||||
def update_input_label(*args):
|
||||
fw_type = firewall_var.get().lower()
|
||||
if fw_type == "stormshield":
|
||||
input_label_var.set("Dossier de configuration source")
|
||||
else:
|
||||
input_label_var.set("Fichier de configuration source")
|
||||
|
||||
firewall_var.trace_add("write", update_input_label)
|
||||
input_frame = ttk.Frame(app)
|
||||
input_frame.pack(fill="x", padx=10)
|
||||
|
||||
ttk.Entry(input_frame, textvariable=input_var).pack(side="left", fill="x", expand=True)
|
||||
ttk.Button(input_frame, text="Parcourir", command=browse_input).pack(side="right")
|
||||
|
||||
ttk.Label(app, text="Nom de sortie (optionnel)").pack(anchor="w", padx=10, pady=5)
|
||||
ttk.Entry(app, textvariable=output_var).pack(fill="x", padx=10)
|
||||
|
||||
ttk.Checkbutton(
|
||||
app,
|
||||
text="Générer la matrice de flux en Excel",
|
||||
variable=matrice_flux
|
||||
).pack(anchor="w", padx=10, pady=(10, 0))
|
||||
matrice_flux.set(True)
|
||||
|
||||
ttk.Checkbutton(
|
||||
app,
|
||||
text="Générer la matrice de routage en Excel (route statique uniquement)",
|
||||
variable=matrice_routage
|
||||
).pack(anchor="w", padx=10, pady=(0, 10))
|
||||
matrice_routage.set(True)
|
||||
|
||||
output_label_var = tk.StringVar()
|
||||
ttk.Label(app, textvariable=output_label_var).pack(anchor="w", padx=10)
|
||||
firewall_var.trace_add("write", update_output_label)
|
||||
output_var.trace_add("write", update_output_label)
|
||||
matrice_flux.trace_add("write", update_output_label)
|
||||
matrice_routage.trace_add("write", update_output_label)
|
||||
update_output_label()
|
||||
|
||||
ttk.Button(
|
||||
app,
|
||||
text="Ouvrir le dossier de sortie",
|
||||
command=open_output_folder
|
||||
).pack(anchor="w", padx=10)
|
||||
|
||||
ttk.Button(
|
||||
app,
|
||||
text="Lancer le traitement",
|
||||
command=run_parser
|
||||
).pack(pady=15)
|
||||
|
||||
app.mainloop()
|
||||
Reference in New Issue
Block a user