import os import sys import time import threading # Add the project root to sys.path so modules can import each other BASE_DIR = os.path.dirname(os.path.abspath(__file__)) sys.path.append(BASE_DIR) from network_sniffer.packet_sniffer import NetworkSniffer from api_server import app def start_api(sniffer): # Inject sniffer instance into Flask API app.sniffer_instance = sniffer app.start_socket_broadcasts() print("Starting Flask API server on http://localhost:5000") # Turn off reloader to avoid double initialization in threads app.socketio.run( app.app, host="0.0.0.0", port=5000, debug=False, use_reloader=False, allow_unsafe_werkzeug=True, ) def main(): print("=========================================") print(" NetWokie AI System Starter ") print("=========================================") # 1. Initialize Network Sniffer sniffer = NetworkSniffer() capture_iface = os.getenv("NETWOKIE_CAPTURE_IFACE") sniffer.start_sniffing(capture_iface) # 2. Start API Server in a separate thread api_thread = threading.Thread(target=start_api, args=(sniffer,), daemon=True) api_thread.start() print("\nNetWokie AI is now active!") print("Access the dashboard at: http://localhost:5000") print(f"Capture mode: {sniffer.get_status().get('mode', 'unknown')}") print("Press Ctrl+C to stop.") # Auto-open browser import webbrowser threading.Timer(1.5, lambda: webbrowser.open("http://localhost:5000")).start() try: while True: # Regularly clear stale flows sniffer.clear_stale_flows(timeout_seconds=60) time.sleep(10) except KeyboardInterrupt: print("\nStopping NetWokie AI...") finally: sniffer.stop_sniffing() print("Shutdown complete.") if __name__ == "__main__": main()