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|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
sys.path.insert(0, os.path.abspath('backend'))
|
| 4 |
+
|
| 5 |
+
from bs4 import BeautifulSoup
|
| 6 |
+
from celery import Celery
|
| 7 |
+
from celery.schedules import crontab
|
| 8 |
+
from collections import defaultdict
|
| 9 |
+
from scanners.base_scanner import (
|
| 10 |
+
active_scan_logs, add_log, get_scan_logs, parse_domain,
|
| 11 |
+
cleanup_scan_logs, schedule_log_cleanup, emit_scan_progress
|
| 12 |
+
)
|
| 13 |
+
from scanners import get_pipeline, get_phases, build_scanner, apply_scan_options
|
| 14 |
+
try:
|
| 15 |
+
from backend.utils.fuzzer_engine import ContextAwareFuzzer
|
| 16 |
+
except ImportError:
|
| 17 |
+
from utils.fuzzer_engine import ContextAwareFuzzer
|
| 18 |
+
from cryptography import x509
|
| 19 |
+
from cryptography.hazmat.backends import default_backend
|
| 20 |
+
from datetime import datetime, timezone
|
| 21 |
+
from datetime import datetime, timezone, timedelta
|
| 22 |
+
from datetime import datetime, timezone, timezone
|
| 23 |
+
from dotenv import load_dotenv
|
| 24 |
+
load_dotenv()
|
| 25 |
+
|
| 26 |
+
import stripe
|
| 27 |
+
from flask import Blueprint, request, jsonify, current_app, send_from_directory
|
| 28 |
+
from werkzeug.utils import secure_filename
|
| 29 |
+
from flask import Blueprint, send_file, jsonify, request
|
| 30 |
+
from flask import Flask
|
| 31 |
+
from flask import jsonify
|
| 32 |
+
from flask import render_template
|
| 33 |
+
from flask import request, abort, g, Response, make_response
|
| 34 |
+
from flask_cors import CORS
|
| 35 |
+
from flask_limiter import Limiter
|
| 36 |
+
from flask_limiter.util import get_remote_address
|
| 37 |
+
from flask_socketio import SocketIO, emit, join_room, leave_room
|
| 38 |
+
from flask_sqlalchemy import SQLAlchemy
|
| 39 |
+
from flask_caching import Cache
|
| 40 |
+
from functools import wraps
|
| 41 |
+
from markupsafe import escape # always available with Flask
|
| 42 |
+
from reportlab.lib import colors
|
| 43 |
+
from reportlab.lib.pagesizes import letter
|
| 44 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 45 |
+
from reportlab.pdfgen import canvas
|
| 46 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak, Image, Flowable
|
| 47 |
+
from reportlab.graphics.shapes import Drawing
|
| 48 |
+
from reportlab.graphics.charts.barcharts import VerticalBarChart
|
| 49 |
+
from sqlalchemy import event
|
| 50 |
+
from sqlalchemy import func
|
| 51 |
+
from sqlalchemy import inspect, text
|
| 52 |
+
from sqlalchemy import text
|
| 53 |
+
from sqlalchemy.engine import Engine
|
| 54 |
+
from typing import Any
|
| 55 |
+
from typing import Any, Callable
|
| 56 |
+
from typing import Callable
|
| 57 |
+
from typing import Literal
|
| 58 |
+
from urllib.parse import urljoin, urlparse
|
| 59 |
+
from urllib.parse import urlparse
|
| 60 |
+
import base64
|
| 61 |
+
import bcrypt
|
| 62 |
+
import concurrent.futures
|
| 63 |
+
from backend.utils.email_service import (
|
| 64 |
+
send_welcome_email,
|
| 65 |
+
send_scan_started,
|
| 66 |
+
send_scan_completed,
|
| 67 |
+
send_scan_failed,
|
| 68 |
+
send_critical_alert
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
import hashlib
|
| 72 |
+
import html
|
| 73 |
+
import io
|
| 74 |
+
import itertools
|
| 75 |
+
import json
|
| 76 |
+
import jwt
|
| 77 |
+
import math
|
| 78 |
+
import os
|
| 79 |
+
import re
|
| 80 |
+
import re, time, ipaddress, os, hashlib, threading
|
| 81 |
+
import requests
|
| 82 |
+
import socket
|
| 83 |
+
import sqlite3
|
| 84 |
+
import ssl
|
| 85 |
+
import statistics
|
| 86 |
+
import threading
|
| 87 |
+
import time
|
| 88 |
+
import traceback
|
| 89 |
+
import urllib.error
|
| 90 |
+
import urllib.parse
|
| 91 |
+
import urllib.request
|
| 92 |
+
import urllib3
|
| 93 |
+
import uuid
|
| 94 |
+
import ipaddress
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# --- From database.py ---
|
| 101 |
+
|
| 102 |
+
db = SQLAlchemy()
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# ── SQLite thread safety + WAL mode ──────────────────────────────────────────
|
| 106 |
+
# Allows SQLAlchemy sessions to be used across scanner threads safely.
|
| 107 |
+
@event.listens_for(Engine, "connect")
|
| 108 |
+
def set_sqlite_pragma(dbapi_connection, connection_record):
|
| 109 |
+
if isinstance(dbapi_connection, sqlite3.Connection):
|
| 110 |
+
cursor = dbapi_connection.cursor()
|
| 111 |
+
# WAL mode: concurrent reads don't block writes and vice-versa
|
| 112 |
+
cursor.execute("PRAGMA journal_mode=WAL")
|
| 113 |
+
# Relax fsync for scanner workloads (safe for dev/prod on local disk)
|
| 114 |
+
cursor.execute("PRAGMA synchronous=NORMAL")
|
| 115 |
+
cursor.close()
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# --- From celery_app.py ---
|
| 119 |
+
|
| 120 |
+
celery = Celery(
|
| 121 |
+
"sentinel_tasks",
|
| 122 |
+
broker="redis://localhost:6379/0",
|
| 123 |
+
backend="redis://localhost:6379/0"
|
| 124 |
+
)
|
| 125 |
+
_celery = celery
|
| 126 |
+
|
| 127 |
+
celery.conf.update(
|
| 128 |
+
task_serializer='json',
|
| 129 |
+
accept_content=['json'],
|
| 130 |
+
result_serializer='json',
|
| 131 |
+
timezone='UTC',
|
| 132 |
+
enable_utc=True,
|
| 133 |
+
task_track_started=True,
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# Enterprise Feature: Celery Beat Schedule
|
| 137 |
+
|
| 138 |
+
celery.conf.beat_schedule = {
|
| 139 |
+
'process-scheduled-scans-every-hour': {
|
| 140 |
+
'task': 'process_scheduled_scans',
|
| 141 |
+
'schedule': crontab(minute=0), # Run top of every hour
|
| 142 |
+
},
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
# --- From extensions.py ---
|
| 148 |
+
|
| 149 |
+
limiter = Limiter(
|
| 150 |
+
key_func=get_remote_address,
|
| 151 |
+
default_limits=["10000 per day", "2000 per hour", "100 per minute"]
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
socketio = SocketIO()
|
| 155 |
+
cache = Cache()
|
| 156 |
+
from flask_migrate import Migrate
|
| 157 |
+
migrate = Migrate()
|
| 158 |
+
|