A Flask-based IP management dependency that helps manage IP addresses, monitor visitors, and protect websites from DDoS attacks.
Project description
IPMT - Flask IP Management Tool
IPMT is a Flask-based IP management library designed to help you manage IP addresses, monitor visitor activity, and protect your website from malicious traffic (e.g., DDoS attacks).
Features
- Request rate limiting (customizable thresholds).
- Whitelist, graylist, and blacklist IPs or networks.
- Monitor and log visitor activity.
- Protect your website from DDoS attacks with customizable policies.
- Automatic cleanup of expired graylist and blacklist entries.
Installation
You can install IPMT using pip:
pip install ipmt
Simple Usage
Here’s a minimal example of how to use IPMT in your Flask application:
from flask import Flask
from ipmt.manager import IPMT
app = Flask(__name__)
# Initialize IPMT with customized configurations
ip_manager = IPMT(app,
request_limit=60, # Max 60 requests
time_window=60, # Per 60 seconds
graylist_duration=24, # Graylist duration in hours
blacklist_duration=30 # Blacklist duration in days
)
# Add trusted IPs/networks to the whitelist
ip_manager.add_to_whitelist("10.0.0.0/24")
ip_manager.add_to_whitelist("192.168.1.100")
# Define a route for the home page
@app.route('/')
def home():
return "Welcome to the home page!"
# Run the app
if __name__ == '__main__':
app.run(debug=True)
How It Works:
- Rate Limiting: The library tracks requests from each IP address and enforces a limit based on the configured
request_limit
andtime_window
. - Whitelisting: Trusted IPs or networks are exempt from rate limiting and blacklisting/graylisting.
- Graylisting & Blacklisting: If an IP exceeds the request limit, it is temporarily added to the graylist. After a certain number of violations, the IP is moved to the blacklist.
Customization
You can customize the following settings in the IPMT
initialization:
request_limit
: Maximum number of requests allowed per IP within thetime_window
(default is 60).time_window
: Time window in seconds (default is 60).graylist_duration
: Duration (in hours) an IP remains in the graylist (default is 24 hours).blacklist_duration
: Duration (in days) an IP remains in the blacklist (default is 30 days).
Adding IPs to Whitelist
You can add single IPs or networks to the whitelist:
ip_manager.add_to_whitelist("10.0.0.0/24") # Add network to whitelist
ip_manager.add_to_whitelist("192.168.1.100") # Add single IP to whitelist
Getting the Status
You can check the current status of IP management, including the number of tracked IPs, graylisted/blacklisted IPs, and the whitelist:
status = ip_manager.get_status()
print(status)
# Get specific details for an IP address
ip_status = ip_manager.get_status("192.168.1.100")
print(ip_status)
Logging
IPMT automatically logs activities like adding IPs to the whitelist, graylist, and blacklist, as well as any violations and errors.
You can customize the logging format or change the logging level if needed. Logs are output to the console by default.
Example Error Responses
When an IP is rate-limited, the system returns a response like this:
{
"error": "Access denied",
"reason": "IP graylisted",
"expires": "2025-01-01T12:00:00",
"retry_after": 3600
}
This response tells the client that their IP is graylisted and includes the time when they can retry after the lock expires.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ipmt-0.1.3.tar.gz
.
File metadata
- Download URL: ipmt-0.1.3.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
70a1385e365ff92214df2e1149585b172673eb79fef523ee1eaac4b687cc3fae
|
|
MD5 |
ebb1cfb23277fb8d6ee6fa9f6ca43221
|
|
BLAKE2b-256 |
941f3dbd893e7d78152ff325c18628f583df96b92b79ec50a3f57f4f16a658a7
|
File details
Details for the file ipmt-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: ipmt-0.1.3-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
473b938ee2002ce95884de32262b086f4d4f74850215a7be668c9c1d8a3fa51d
|
|
MD5 |
6234e6cf1f2fe98ef1d75b87238397c8
|
|
BLAKE2b-256 |
c216c976947c5137fc2c247bdc618f9e3c3fb1b7d5b7033387e89736ecb2f677
|