cutycat2000x commited on
Commit
fa5c81c
·
verified ·
1 Parent(s): 88abbff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -8
app.py CHANGED
@@ -1,36 +1,69 @@
1
  from flask import Flask, request, Response
2
  import requests
 
 
3
 
 
 
 
 
4
  app = Flask("DiscordRocks Instant Chat Proxy")
5
 
6
  DISCORD_ROCKS_HOST = 'chat.discord.rocks'
7
  CHAT_UI_HOST = 'cutycat2000x-instantchat.static.hf.space'
8
 
9
- @app.route('/',methods=['GET', 'POST'])
10
  @app.route("/<path:path>", methods=['GET', 'POST'])
11
  def handle_request(path=""):
12
  chat_ui_url = f'https://{DISCORD_ROCKS_HOST}/{path}'
13
-
14
- # Pass method, data, and params from the original request
15
  method = request.method
16
  data = request.data
17
  params = request.args
18
-
19
  headers = None
20
-
21
- # If the request is a POST request, set the Content-Type header to application/json
22
  if method == 'POST':
23
  headers = {'Content-Type': 'application/json'}
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- # Make a request to the new URL with the provided method, data, params, and headers
26
  response = requests.request(method, chat_ui_url, params=params, data=data, headers=headers)
27
 
28
- # Create a response with the content received from the new URL
29
  proxied_response = Response(response.content)
30
  proxied_response.status_code = response.status_code
31
  proxied_response.headers["Content-Type"] = "text/html"
 
 
32
 
33
  return proxied_response
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  if __name__ == "__main__":
36
  app.run(host="0.0.0.0", port=7860)
 
1
  from flask import Flask, request, Response
2
  import requests
3
+ import asyncio
4
+ import os
5
 
6
+ request_count = {}
7
+ ongoing_requests = {}
8
+ bypass = os.getenv("BYPASS")
9
+ MAX_REQUESTS_PER_MINUTE = 7
10
  app = Flask("DiscordRocks Instant Chat Proxy")
11
 
12
  DISCORD_ROCKS_HOST = 'chat.discord.rocks'
13
  CHAT_UI_HOST = 'cutycat2000x-instantchat.static.hf.space'
14
 
15
+ @app.route('/', methods=['GET', 'POST'])
16
  @app.route("/<path:path>", methods=['GET', 'POST'])
17
  def handle_request(path=""):
18
  chat_ui_url = f'https://{DISCORD_ROCKS_HOST}/{path}'
 
 
19
  method = request.method
20
  data = request.data
21
  params = request.args
 
22
  headers = None
23
+ didReply = False
 
24
  if method == 'POST':
25
  headers = {'Content-Type': 'application/json'}
26
+ if 'reply' in path:
27
+ if request_count.get(ip, 0) >= MAX_REQUESTS_PER_MINUTE:
28
+ return jsonify({"error": "Too Many Requests. Please try again later."}), 429
29
+ if ip in ongoing_requests:
30
+ return jsonify({"error": "Concurrent requests not allowed."}), 429
31
+ ongoing_requests[ip] = True
32
+ request_count[ip] = request_count.get(ip, 0) + 1
33
+ didReply = True
34
+ json_data = request.get_json()
35
+ json_data['bypass'] = bypass
36
+ data = json.dumps(json_data)
37
 
 
38
  response = requests.request(method, chat_ui_url, params=params, data=data, headers=headers)
39
 
 
40
  proxied_response = Response(response.content)
41
  proxied_response.status_code = response.status_code
42
  proxied_response.headers["Content-Type"] = "text/html"
43
+ if didReply:
44
+ decrement_request_count_task(ip)
45
 
46
  return proxied_response
47
 
48
+ def decrement_request_count_task(ip):
49
+ @copy_current_request_context
50
+ def decrement_count():
51
+ try:
52
+ loop = asyncio.new_event_loop()
53
+ asyncio.set_event_loop(loop)
54
+ loop.run_until_complete(decrement_request_count(ip))
55
+ except Exception as e:
56
+ print(e)
57
+
58
+ threading.Thread(target=decrement_count).start()
59
+
60
+ async def decrement_request_count(ip):
61
+ try:
62
+ await asyncio.sleep(60) # Ensure this runs asynchronously
63
+ request_count[ip] = request_count.get(ip, 0) - 1
64
+ print(request_count[ip])
65
+ except Exception as es:
66
+ print(es)
67
+
68
  if __name__ == "__main__":
69
  app.run(host="0.0.0.0", port=7860)