File size: 1,278 Bytes
03c899c
f4cda89
 
 
 
 
10f0fe4
f4cda89
 
 
 
c392a75
f4cda89
 
 
 
10f0fe4
f4cda89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import requests
from flask import Flask, request, Response

app = Flask(__name__)

OR_BASE_URL = "https://openrouter.ai/api"


@app.route("/", methods=["GET"])
def home():
    return {"status": "ok", "message": "OpenRouter proxy is working :3"}


@app.route("/<path:endpoint>", methods=["GET", "POST", "PUT", "PATCH", "DELETE"])
def proxy(endpoint):
    url = f"{OR_BASE_URL}/{endpoint}"

    # Копируем все заголовки, кроме host и content-length
    headers = {k: v for k, v in request.headers if k.lower() not in ["host", "content-length"]}

    try:
        resp = requests.request(
            method=request.method,
            url=url,
            headers=headers,
            params=request.args,
            data=request.get_data(),
            allow_redirects=False,
            stream=True,
        )

        excluded_headers = ["content-encoding", "transfer-encoding", "connection"]
        response_headers = [(k, v) for k, v in resp.raw.headers.items() if k.lower() not in excluded_headers]

        return Response(resp.content, resp.status_code, response_headers)

    except Exception as e:
        return {"error": str(e)}, 500


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))