Skip to content

Commit

Permalink
Add DDos protection
Browse files Browse the repository at this point in the history
  • Loading branch information
223880 committed Jul 25, 2024
1 parent 51399da commit 00f7fa1
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
from datetime import datetime, timedelta

class CoinJoinManager:

Expand Down Expand Up @@ -106,7 +107,36 @@ async def _create_and_sign_coinjoin_transaction(self, wallet: Wallet):

return result is not None

# Dictionary to track request counts and time windows
request_counts = {}
time_window = timedelta(seconds=60) # Time window for rate limiting
request_limit = 10 # Maximum requests allowed per IP within the time window
blacklisted_ips = set() # Set to store blacklisted IP addresses

async def handle_client(reader, writer):
peername = writer.get_extra_info('peername')
ip = peername[0]

# Check if IP is blacklisted
if ip in blacklisted_ips:
writer.close()
await writer.wait_closed()
return

# Rate limiting
now = datetime.now()
if ip not in request_counts:
request_counts[ip] = []
request_counts[ip] = [timestamp for timestamp in request_counts[ip] if now - timestamp < time_window]

if len(request_counts[ip]) >= request_limit:
blacklisted_ips.add(ip)
writer.close()
await writer.wait_closed()
return

request_counts[ip].append(now)

data = await reader.read(1000)
message = data.decode()
request = json.loads(message)
Expand Down Expand Up @@ -137,4 +167,4 @@ async def run_server():
# Run the CoinJoin process
# wallet = ... # Get the wallet instance
# coinjoin_manager = CoinJoinManager()
# asyncio.run(coinjoin_manager.initiate_coinjoin(wallet, known_peers))
# asyncio.run(coinjoin_manager.initiate_coinjoin(wallet, known_peers))

0 comments on commit 00f7fa1

Please sign in to comment.