-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
44 lines (36 loc) · 1.26 KB
/
Copy pathapp.py
File metadata and controls
44 lines (36 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from flask import Flask, request
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import threading
import requests
import json
import logging
# Limits, do not spam this endpoint
app = Flask(__name__)
limiter = Limiter(
get_remote_address,
app=app,
default_limits=["5000 per minute"]
)
# Turn off logging in output logs (no HTTP post prints)
log = logging.getLogger('abcd')
log.disabled = True
# Limiter 2, dictionary so that different servers can't spam the same endpoint
internalLimiter = {}
def removeFromLimiter(key):
if key in internalLimiter:
internalLimiter.pop(key) # Remove debounce
# Webhook routing API
@app.route("/api/webhooks/<id>/<str>", methods=["POST"])
@limiter.limit("5000 per minute")
def proxy(id, str):
if (id + "/" + str) in internalLimiter:
return "Rate limit exceeded", 429
internalLimiter[(id + "/" + str)] = True # Debounce
threading.Timer(2.0, removeFromLimiter, [(id + "/" + str)]).start() # Function is called after 2 seconds asynchronously
data = request.get_json(force=True)
response = requests.post(f"https://discord.com/api/webhooks/{id}/{str}", json=data)
return "", response.status_code
# Run application
if __name__ == "__main__":
app.run()