add rate limiting

This commit is contained in:
Micky 2024-12-26 13:01:44 +11:00
parent 65def1c617
commit 874cfb050a
5 changed files with 22 additions and 7 deletions

View File

@ -10,6 +10,7 @@ https://photonglass.dev
- **Multi Device Support**: Connect to multiple devices from one single interface.
- **Easy Deployment**: Extremely easy to deploy and scale with multiple devices.
- **Webhook Logging**: Log queries to a webhook channel (optional).
- **Rate Limiting**: Reduce service abuse by rate limiting users, 100 per day and 10 per minute.
## Setup (Docker)
1. Clone repository

View File

@ -1,12 +1,23 @@
import logging
from flask import Flask
import logging, os
from flask_limiter import Limiter
from app.functions.utils import get_client_ip
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
limiter = Limiter(
get_client_ip,
default_limits=["100 per day", "10 per minute"],
storage_uri="memory://"
)
def create_app():
app = Flask(__name__, instance_path="/instance")
limiter.init_app(app)
from app.views import main
app.register_blueprint(main.bp)
app.add_url_rule('/', endpoint='index')

View File

@ -1,6 +1,7 @@
import os, yaml, logging, requests
from functools import wraps
from netmiko import ConnectHandler, NetmikoTimeoutException, NetmikoAuthenticationException
from flask import request
logger = logging.getLogger(__name__)
@ -66,7 +67,7 @@ def send_webhook(webhook_url, text_data):
# Get client IP address
@exception_handler
def get_client_ip(request):
def get_client_ip():
if not request.headers.getlist("X-Forwarded-For"):
return request.remote_addr
return request.headers.getlist("X-Forwarded-For")[0]

View File

@ -4,3 +4,4 @@ netmiko==4.5.0
werkzeug==3.1.3
gunicorn==23.0.0
requests==2.32.3
flask-limiter==3.9.2

View File

@ -1,14 +1,15 @@
from flask import (
Blueprint, request, render_template
)
import logging
from flask import Blueprint, request, render_template
from app.functions.utils import exception_handler, load_yaml, send_webhook, get_client_ip, execute_command
logger = logging.getLogger(__name__)
bp = Blueprint('main', __name__)
# Route to render the main page
@bp.route('/')
@exception_handler
def index():