nya-proxy 0.2.2__tar.gz → 0.2.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {nya_proxy-0.2.2/nya_proxy.egg-info → nya_proxy-0.2.3}/PKG-INFO +1 -1
- nya_proxy-0.2.3/nya/_version.py +1 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/common/models.py +2 -1
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/core/response.py +10 -7
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/core/streaming.py +6 -4
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/server/app.py +21 -3
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/server/auth.py +5 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/utils/header.py +21 -18
- {nya_proxy-0.2.2 → nya_proxy-0.2.3/nya_proxy.egg-info}/PKG-INFO +1 -1
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/pyproject.toml +1 -1
- nya_proxy-0.2.2/nya/_version.py +0 -1
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/LICENSE +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/README.md +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/__init__.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/common/__init__.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/common/constants.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/common/exceptions.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/common/logger.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/config/__init__.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/config/manager.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/config.yaml +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/core/__init__.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/core/factory.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/core/handler.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/core/proxy.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/core/request.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/dashboard/__init__.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/dashboard/api.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/html/favicon.ico +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/html/index.html +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/html/login.html +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/html/static/css/styles.css +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/html/static/js/dashboard.js +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/html/static/logo.svg +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/integrations/__init__.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/integrations/openai.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/schema.json +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/server/__init__.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/services/__init__.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/services/key.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/services/lb.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/services/limit.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/services/metrics.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/services/queue.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/utils/__init__.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya/utils/helper.py +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya_proxy.egg-info/SOURCES.txt +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya_proxy.egg-info/dependency_links.txt +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya_proxy.egg-info/entry_points.txt +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya_proxy.egg-info/requires.txt +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/nya_proxy.egg-info/top_level.txt +0 -0
- {nya_proxy-0.2.2 → nya_proxy-0.2.3}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: nya-proxy
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.3
|
|
4
4
|
Summary: A lightweight, flexible API proxy with dynamic token rotation, load balancing, and rate limiting capabilities.
|
|
5
5
|
Author-email: "Nya Foundation Team (k3scat)" <k3scat@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.3"
|
|
@@ -81,10 +81,11 @@ class NyaRequest:
|
|
|
81
81
|
"""
|
|
82
82
|
Create a NyaRequest instance from a FastAPI Request object.
|
|
83
83
|
"""
|
|
84
|
+
|
|
84
85
|
return NyaRequest(
|
|
85
86
|
method=request.method,
|
|
86
87
|
_url=request.url,
|
|
87
|
-
headers=request.headers,
|
|
88
|
+
headers=dict(request.headers),
|
|
88
89
|
content=await request.body(),
|
|
89
90
|
_raw=request,
|
|
90
91
|
_added_at=time.time(),
|
|
@@ -121,19 +121,21 @@ class ResponseProcessor:
|
|
|
121
121
|
# Record metrics for successful responses
|
|
122
122
|
self.record_response_metrics(r, httpx_response, start_time)
|
|
123
123
|
|
|
124
|
-
#
|
|
125
|
-
headers =
|
|
124
|
+
# Use Headers to preserve case but allow case-insensitive operations
|
|
125
|
+
headers = httpx.Headers(httpx_response.headers)
|
|
126
126
|
headers_to_remove = ["server", "date", "transfer-encoding", "content-length"]
|
|
127
127
|
|
|
128
128
|
for header in headers_to_remove:
|
|
129
|
-
if header in headers:
|
|
130
|
-
del headers[header]
|
|
129
|
+
if header.lower() in headers:
|
|
130
|
+
del headers[header.lower()]
|
|
131
131
|
|
|
132
132
|
# Determine the response content type
|
|
133
133
|
content_type = httpx_response.headers.get("content-type", "application/json")
|
|
134
134
|
|
|
135
135
|
self.logger.debug(f"Response status code: {httpx_response.status_code}")
|
|
136
|
-
self.logger.debug(
|
|
136
|
+
self.logger.debug(
|
|
137
|
+
f"Response Headers\n: {json_safe_dumps(dict(headers.items()))}"
|
|
138
|
+
)
|
|
137
139
|
|
|
138
140
|
# Check if it's streaming based on headers
|
|
139
141
|
is_streaming = self.streaming_handler.detect_streaming_content(
|
|
@@ -173,7 +175,8 @@ class ResponseProcessor:
|
|
|
173
175
|
raw_content = decode_content(raw_content, content_encoding)
|
|
174
176
|
|
|
175
177
|
# Remove content-encoding header if present
|
|
176
|
-
|
|
178
|
+
if "content-encoding" in headers:
|
|
179
|
+
del headers["content-encoding"]
|
|
177
180
|
|
|
178
181
|
# HTML specific handling, rarely used (some user might want this)
|
|
179
182
|
if "text/html" in content_type:
|
|
@@ -187,7 +190,7 @@ class ResponseProcessor:
|
|
|
187
190
|
content=raw_content,
|
|
188
191
|
status_code=httpx_response.status_code,
|
|
189
192
|
media_type=content_type,
|
|
190
|
-
headers=headers,
|
|
193
|
+
headers=dict(headers.items()),
|
|
191
194
|
)
|
|
192
195
|
|
|
193
196
|
# Add base tag to HTML content for relative links
|
|
@@ -46,7 +46,7 @@ class StreamingHandler:
|
|
|
46
46
|
self.logger.debug(
|
|
47
47
|
f"Handling streaming response with status {httpx_response.status_code}"
|
|
48
48
|
)
|
|
49
|
-
headers =
|
|
49
|
+
headers = httpx.Headers(httpx_response.headers)
|
|
50
50
|
status_code = httpx_response.status_code
|
|
51
51
|
content_type = httpx_response.headers.get("content-type", "").lower()
|
|
52
52
|
|
|
@@ -89,7 +89,7 @@ class StreamingHandler:
|
|
|
89
89
|
Returns:
|
|
90
90
|
StreamingResponse for FastAPI
|
|
91
91
|
"""
|
|
92
|
-
headers =
|
|
92
|
+
headers = httpx.Headers(httpx_response.headers)
|
|
93
93
|
status_code = httpx_response.status_code
|
|
94
94
|
|
|
95
95
|
# Store the full content for simulated streaming
|
|
@@ -192,6 +192,8 @@ class StreamingHandler:
|
|
|
192
192
|
Returns:
|
|
193
193
|
Processed headers for streaming
|
|
194
194
|
"""
|
|
195
|
+
if not isinstance(headers, httpx.Headers):
|
|
196
|
+
headers = httpx.Headers(headers)
|
|
195
197
|
# Headers to remove for streaming responses
|
|
196
198
|
headers_to_remove = [
|
|
197
199
|
"content-encoding",
|
|
@@ -200,8 +202,8 @@ class StreamingHandler:
|
|
|
200
202
|
]
|
|
201
203
|
|
|
202
204
|
for header in headers_to_remove:
|
|
203
|
-
if header in headers:
|
|
204
|
-
del headers[header]
|
|
205
|
+
if header.lower() in headers:
|
|
206
|
+
del headers[header.lower()]
|
|
205
207
|
|
|
206
208
|
# Set SSE-specific headers according to standards
|
|
207
209
|
headers["cache-control"] = "no-cache, no-transform"
|
|
@@ -6,10 +6,9 @@ import argparse
|
|
|
6
6
|
import contextlib
|
|
7
7
|
import logging
|
|
8
8
|
import os
|
|
9
|
-
import time
|
|
10
9
|
|
|
11
10
|
import uvicorn
|
|
12
|
-
from fastapi import FastAPI, Request
|
|
11
|
+
from fastapi import FastAPI, Request, Response
|
|
13
12
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
14
13
|
from starlette.middleware.cors import CORSMiddleware
|
|
15
14
|
from starlette.responses import JSONResponse
|
|
@@ -340,7 +339,26 @@ class NyaProxyApp:
|
|
|
340
339
|
|
|
341
340
|
@self.app.options("/api/{path:path}", name="proxy_options")
|
|
342
341
|
async def proxy_options_request(request: Request):
|
|
343
|
-
|
|
342
|
+
# hijack the options request to handle CORS preflight requests
|
|
343
|
+
origin = request.headers.get("origin", "*")
|
|
344
|
+
acr_headers = request.headers.get(
|
|
345
|
+
"access-control-request-headers", "Content-Type, Authorization"
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
self.logger.debug(
|
|
349
|
+
f"Handling CORS preflight request from {origin} with headers {request.headers}"
|
|
350
|
+
)
|
|
351
|
+
|
|
352
|
+
return Response(
|
|
353
|
+
content="",
|
|
354
|
+
status_code=204,
|
|
355
|
+
headers={
|
|
356
|
+
"Access-Control-Allow-Origin": origin,
|
|
357
|
+
"Access-Control-Allow-Headers": acr_headers,
|
|
358
|
+
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS",
|
|
359
|
+
"Access-Control-Max-Age": "86400",
|
|
360
|
+
},
|
|
361
|
+
)
|
|
344
362
|
|
|
345
363
|
async def shutdown(self):
|
|
346
364
|
"""Clean up resources on shutdown."""
|
|
@@ -176,6 +176,11 @@ class AuthMiddleware(BaseHTTPMiddleware):
|
|
|
176
176
|
self.logger = logger or logging.getLogger(__name__)
|
|
177
177
|
|
|
178
178
|
async def dispatch(self, request: Request, call_next):
|
|
179
|
+
|
|
180
|
+
# skip OPTIONS requests
|
|
181
|
+
if request.method == "OPTIONS":
|
|
182
|
+
return await call_next(request)
|
|
183
|
+
|
|
179
184
|
# Skip auth for specific paths if needed
|
|
180
185
|
excluded_paths = [
|
|
181
186
|
"/",
|
|
@@ -3,7 +3,9 @@ Header processing utilities for NyaProxy.
|
|
|
3
3
|
"""
|
|
4
4
|
|
|
5
5
|
import re
|
|
6
|
-
from typing import Any, Dict, Optional, Set
|
|
6
|
+
from typing import Any, Dict, Optional, Set, Union
|
|
7
|
+
|
|
8
|
+
from httpx import Headers
|
|
7
9
|
|
|
8
10
|
from ..common.constants import EXCLUDED_REQUEST_HEADERS
|
|
9
11
|
from ..common.logger import getLogger
|
|
@@ -48,7 +50,7 @@ class HeaderUtils:
|
|
|
48
50
|
header_templates: Dict[str, Any],
|
|
49
51
|
variable_values: Dict[str, Any],
|
|
50
52
|
original_headers: Optional[Dict[str, str]] = None,
|
|
51
|
-
) -> Dict[str, str]:
|
|
53
|
+
) -> Union[Dict[str, str], Headers]:
|
|
52
54
|
"""
|
|
53
55
|
Process headers with variable substitution.
|
|
54
56
|
|
|
@@ -61,14 +63,14 @@ class HeaderUtils:
|
|
|
61
63
|
Processed headers with variables substituted
|
|
62
64
|
"""
|
|
63
65
|
|
|
64
|
-
#
|
|
65
|
-
final_headers =
|
|
66
|
+
# Use CaseInsensitiveDict to maintain case-insensitive lookups but preserve original case
|
|
67
|
+
final_headers = Headers()
|
|
68
|
+
|
|
69
|
+
# Start with original headers, filtering out excluded ones
|
|
66
70
|
if original_headers:
|
|
67
|
-
|
|
68
|
-
k.lower():
|
|
69
|
-
|
|
70
|
-
if k.lower() not in HeaderUtils._EXCLUDED_HEADERS
|
|
71
|
-
}
|
|
71
|
+
for k, v in original_headers.items():
|
|
72
|
+
if k.lower() not in HeaderUtils._EXCLUDED_HEADERS:
|
|
73
|
+
final_headers[k] = v # This preserves the original case
|
|
72
74
|
|
|
73
75
|
# Process each header template
|
|
74
76
|
for header_name, template in header_templates.items():
|
|
@@ -83,14 +85,14 @@ class HeaderUtils:
|
|
|
83
85
|
template_str, variable_values
|
|
84
86
|
)
|
|
85
87
|
|
|
86
|
-
#
|
|
87
|
-
final_headers[header_name
|
|
88
|
+
# Set header with original casing preserved
|
|
89
|
+
final_headers[header_name] = header_value
|
|
88
90
|
|
|
89
91
|
# patch accept-encoding header to avoid issues with httpx
|
|
90
92
|
if header_name.lower() == "accept-encoding":
|
|
91
|
-
final_headers[header_name
|
|
93
|
+
final_headers[header_name] = "identity"
|
|
92
94
|
|
|
93
|
-
return final_headers
|
|
95
|
+
return dict(final_headers.items())
|
|
94
96
|
|
|
95
97
|
@staticmethod
|
|
96
98
|
def _substitute_variables(
|
|
@@ -163,15 +165,16 @@ class HeaderUtils:
|
|
|
163
165
|
Returns:
|
|
164
166
|
Merged headers dictionary
|
|
165
167
|
"""
|
|
166
|
-
#
|
|
167
|
-
result =
|
|
168
|
+
# Use CaseInsensitiveDict to handle case-insensitive lookups but preserve casing
|
|
169
|
+
result = Headers(base_headers)
|
|
168
170
|
|
|
169
171
|
# Merge in override headers
|
|
170
172
|
for key, value in override_headers.items():
|
|
171
|
-
result[key
|
|
173
|
+
result[key] = value # Will override case-insensitively but preserve case
|
|
172
174
|
|
|
173
175
|
# Remove excluded headers
|
|
174
176
|
for header in EXCLUDED_REQUEST_HEADERS:
|
|
175
|
-
|
|
177
|
+
if header.lower() in result: # Case-insensitive check
|
|
178
|
+
del result[header.lower()]
|
|
176
179
|
|
|
177
|
-
return result
|
|
180
|
+
return dict(result) # Convert back to regular dict for compatibility
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: nya-proxy
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.3
|
|
4
4
|
Summary: A lightweight, flexible API proxy with dynamic token rotation, load balancing, and rate limiting capabilities.
|
|
5
5
|
Author-email: "Nya Foundation Team (k3scat)" <k3scat@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "nya-proxy"
|
|
7
|
-
version = "0.2.
|
|
7
|
+
version = "0.2.3"
|
|
8
8
|
description = "A lightweight, flexible API proxy with dynamic token rotation, load balancing, and rate limiting capabilities."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
authors = [
|
nya_proxy-0.2.2/nya/_version.py
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.2.2"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|