-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathflareproxy.py
More file actions
53 lines (42 loc) · 1.79 KB
/
flareproxy.py
File metadata and controls
53 lines (42 loc) · 1.79 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
45
46
47
48
49
50
51
52
53
import json
import os
from http.server import BaseHTTPRequestHandler, HTTPServer
import requests
# Get FlareSolverr URL from environment variable or use default
FLARESOLVERR_URL = os.getenv("FLARESOLVERR_URL", "http://flaresolverr:8191/v1")
class ProxyHTTPRequestHandler(BaseHTTPRequestHandler):
def handle_request(self):
"""Handle the core logic for GET and CONNECT requests."""
try:
# Prepare the payload
headers = {"Content-Type": "application/json"}
data = {
"cmd": "request.get",
"url": self.path.replace("http", "https"),
"maxTimeout": 60000
}
# Send the POST request to FlareSolverr
response = requests.post(FLARESOLVERR_URL, headers=headers, json=data)
json_response = response.json()
# Forward the response back to the client
self.send_response(response.status_code)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.end_headers()
self.wfile.write(bytes(json_response.get("solution", {}).get("response", ""), "utf-8"))
except Exception as e:
self.send_response(500)
self.send_header("Content-Type", "application/json")
self.end_headers()
error_message = json.dumps({"error": str(e)})
self.wfile.write(error_message.encode("utf-8"))
def do_GET(self):
"""Handle GET requests."""
self.handle_request()
def do_CONNECT(self):
"""Handle CONNECT requests."""
self.handle_request()
if __name__ == "__main__":
server_address = ("", 8080)
httpd = HTTPServer(server_address, ProxyHTTPRequestHandler)
print("FlareProxy adapter running on port 8080")
httpd.serve_forever()