WebPass / wsgi.py
ag235772's picture
Fixed SQLite race condition with single gunicorn worker and forced db init
1b08971
import os
import sys
from collections import deque
from werkzeug.middleware.proxy_fix import ProxyFix
# Allow OAuth over HTTP for local testing
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
from webpass import create_app, socketio, db # <--- ADDED 'db' IMPORT
app = create_app()
# --- FORCE DATABASE INITIALIZATION (THE FIX) ---
# This physically creates fallback.db and all tables before Gunicorn accepts any traffic
with app.app_context():
try:
print(" [*] Verifying/Creating database tables...")
db.create_all()
print(" [+] Database initialization complete.")
except Exception as e:
print(f" [!] Database init error: {e}")
app.captured_packets = deque(maxlen=1000)
@socketio.on('connect')
def handle_connect():
print(" [+] Client connected to Live Feed")
# --- CLOUD TOGGLE (THE FORCE FIELD) ---
IS_CLOUD = os.environ.get('SPACE_ID') is not None
if IS_CLOUD:
# Tell Flask it is behind a secure Cloud Proxy (Forces HTTPS for Google OAuth)
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
# ONLY import Scapy and Network Monitor if running locally
if not IS_CLOUD:
try:
from webpass.network_monitor import start_packet_capture
from scapy.all import conf
def get_best_interface():
try:
iface = conf.iface
print(f" [*] Auto-detected best interface: {iface}")
return iface
except Exception as e:
print(f" [!] Error detecting interface: {e}")
return None
except ImportError:
print(" [!] Scapy not installed locally. Network Monitor disabled.")
if __name__ == '__main__':
print("--- WEBPASS SECURITY SERVER STARTING ---")
if not IS_CLOUD:
target_interface = get_best_interface()
if target_interface:
print(f" [+] Launching Packet Sniffer on: {target_interface}")
start_packet_capture(app, socketio, interface=target_interface)
else:
print(" [*] Running in Cloud Mode. Hardware packet sniffing is disabled.")
print(" [+] Server running on http://127.0.0.1:5000")
print("--------------------------------------------")
socketio.run(app, host='127.0.0.1', port=5000, debug=True, use_reloader=False)