nya-proxy 0.3.4__tar.gz → 0.3.6__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.
Files changed (47) hide show
  1. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/PKG-INFO +1 -1
  2. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/__init__.py +2 -2
  3. nya_proxy-0.3.6/nya/_version.py +1 -0
  4. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/common/exceptions.py +41 -19
  5. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/common/models.py +4 -3
  6. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/config/manager.py +14 -13
  7. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/config.yaml +20 -12
  8. {nya_proxy-0.3.4/nya/services → nya_proxy-0.3.6/nya/core}/control.py +120 -96
  9. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/core/handler.py +31 -21
  10. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/core/proxy.py +13 -65
  11. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/core/queue.py +136 -105
  12. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/core/request.py +1 -2
  13. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/schema.json +12 -14
  14. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/server/app.py +20 -44
  15. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/services/limit.py +18 -0
  16. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya_proxy.egg-info/PKG-INFO +1 -1
  17. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya_proxy.egg-info/SOURCES.txt +2 -2
  18. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/pyproject.toml +1 -1
  19. nya_proxy-0.3.4/nya/_version.py +0 -1
  20. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/LICENSE +0 -0
  21. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/README.md +0 -0
  22. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/common/__init__.py +0 -0
  23. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/common/constants.py +0 -0
  24. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/config/__init__.py +0 -0
  25. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/core/__init__.py +0 -0
  26. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/core/streaming.py +0 -0
  27. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/dashboard/__init__.py +0 -0
  28. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/dashboard/api.py +0 -0
  29. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/html/favicon.ico +0 -0
  30. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/html/index.html +0 -0
  31. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/html/login.html +0 -0
  32. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/html/static/css/styles.css +0 -0
  33. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/html/static/js/dashboard.js +0 -0
  34. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/html/static/logo.svg +0 -0
  35. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/server/__init__.py +0 -0
  36. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/server/auth.py +0 -0
  37. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/services/__init__.py +0 -0
  38. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/services/lb.py +0 -0
  39. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/services/metrics.py +0 -0
  40. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/utils/__init__.py +0 -0
  41. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/utils/header.py +0 -0
  42. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya/utils/helper.py +0 -0
  43. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya_proxy.egg-info/dependency_links.txt +0 -0
  44. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya_proxy.egg-info/entry_points.txt +0 -0
  45. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya_proxy.egg-info/requires.txt +0 -0
  46. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/nya_proxy.egg-info/top_level.txt +0 -0
  47. {nya_proxy-0.3.4 → nya_proxy-0.3.6}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nya-proxy
3
- Version: 0.3.4
3
+ Version: 0.3.6
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
@@ -9,7 +9,7 @@ from .common.exceptions import (
9
9
  ConnectionError,
10
10
  EndpointRateLimitExceededError,
11
11
  NoAvailableAPIKeyError,
12
- NyaProxyError,
12
+ NyaProxyStatus,
13
13
  QueueFullError,
14
14
  RequestExpiredError,
15
15
  TimeoutError,
@@ -42,7 +42,7 @@ __all__ = [
42
42
  # Utilities
43
43
  "format_elapsed_time",
44
44
  # Exceptions
45
- "NyaProxyError",
45
+ "NyaProxyStatus",
46
46
  "ConfigurationError",
47
47
  "VariablesConfigurationError",
48
48
  "EndpointRateLimitExceededError",
@@ -0,0 +1 @@
1
+ __version__ = "0.3.6"
@@ -3,25 +3,25 @@ Improved custom exceptions for NyaProxy.
3
3
  """
4
4
 
5
5
 
6
- class NyaProxyError(Exception):
6
+ class NyaProxyStatus(Exception):
7
7
  """
8
- Base exception class for all NyaProxy errors.
8
+ Base exception class for all NyaProxy status.
9
9
  """
10
10
 
11
11
  def __init__(self, message: str = None):
12
12
  """
13
- Initialize NyaProxy error.
13
+ Initialize NyaProxy status.
14
14
 
15
15
  Args:
16
- message: Error message
16
+ message: Status message
17
17
  """
18
18
  super().__init__()
19
- self.message = message or "An error occurred in NyaProxy"
19
+ self.message = message or "An event occurred in NyaProxy"
20
20
 
21
21
  pass
22
22
 
23
23
 
24
- class ConfigurationError(NyaProxyError):
24
+ class ConfigurationError(NyaProxyStatus):
25
25
  """
26
26
  Exception raised for configuration errors.
27
27
  """
@@ -55,7 +55,7 @@ class VariablesConfigurationError(ConfigurationError):
55
55
  self.message = message
56
56
 
57
57
 
58
- class EndpointRateLimitExceededError(NyaProxyError):
58
+ class EndpointRateLimitExceededError(NyaProxyStatus):
59
59
  """
60
60
  Exception raised when rate limits are exceeded.
61
61
  """
@@ -78,7 +78,7 @@ class EndpointRateLimitExceededError(NyaProxyError):
78
78
  )
79
79
 
80
80
 
81
- class QueueFullError(NyaProxyError):
81
+ class QueueFullError(NyaProxyStatus):
82
82
  """
83
83
  Exception raised when a request queue is full.
84
84
  """
@@ -98,7 +98,7 @@ class QueueFullError(NyaProxyError):
98
98
  )
99
99
 
100
100
 
101
- class RequestExpiredError(NyaProxyError):
101
+ class RequestExpiredError(NyaProxyStatus):
102
102
  """
103
103
  Exception raised when a queued request expires.
104
104
  """
@@ -118,7 +118,7 @@ class RequestExpiredError(NyaProxyError):
118
118
  )
119
119
 
120
120
 
121
- class NoAvailableAPIKeyError(NyaProxyError):
121
+ class NoAvailableAPIKeyError(NyaProxyStatus):
122
122
  """
123
123
  Exception raised when no API keys are available (all key are rate limited).
124
124
  """
@@ -137,7 +137,7 @@ class NoAvailableAPIKeyError(NyaProxyError):
137
137
  )
138
138
 
139
139
 
140
- class APIKeyNotConfiguredError(NyaProxyError):
140
+ class APIKeyNotConfiguredError(NyaProxyStatus):
141
141
  """
142
142
  Exception raised when there is no API key found in the configuration.
143
143
  """
@@ -153,7 +153,7 @@ class APIKeyNotConfiguredError(NyaProxyError):
153
153
  super().__init__(f"NyaProxy: No API key found for {api_name}")
154
154
 
155
155
 
156
- class MissingAPIKeyError(NyaProxyError):
156
+ class MissingAPIKeyError(NyaProxyStatus):
157
157
  """
158
158
  Exception raised when an API key is missing in the configuration.
159
159
  """
@@ -169,7 +169,7 @@ class MissingAPIKeyError(NyaProxyError):
169
169
  super().__init__(f"NyaProxy: Missing API key for {api_name}")
170
170
 
171
171
 
172
- class APIConfigError(NyaProxyError):
172
+ class APIConfigError(NyaProxyStatus):
173
173
  """
174
174
  Exception raised for API configuration errors.
175
175
  """
@@ -177,7 +177,7 @@ class APIConfigError(NyaProxyError):
177
177
  pass
178
178
 
179
179
 
180
- class UnknownAPIError(NyaProxyError):
180
+ class UnknownAPIError(NyaProxyStatus):
181
181
  """
182
182
  Exception raised when requesting an unknown API.
183
183
  """
@@ -193,7 +193,7 @@ class UnknownAPIError(NyaProxyError):
193
193
  super().__init__(f"NyaProxy: Unknown API endpoint for path: {path}")
194
194
 
195
195
 
196
- class ConnectionError(NyaProxyError):
196
+ class ConnectionError(NyaProxyStatus):
197
197
  """
198
198
  Exception raised for connection errors to target APIs.
199
199
  """
@@ -214,7 +214,7 @@ class ConnectionError(NyaProxyError):
214
214
  )
215
215
 
216
216
 
217
- class TimeoutError(NyaProxyError):
217
+ class TimeoutError(NyaProxyStatus):
218
218
  """
219
219
  Exception raised for request timeouts.
220
220
  """
@@ -234,7 +234,7 @@ class TimeoutError(NyaProxyError):
234
234
  )
235
235
 
236
236
 
237
- class RequestRateLimited(NyaProxyError):
237
+ class RequestRateLimited(NyaProxyStatus):
238
238
  """
239
239
  Exception raised when a request is rate limited.
240
240
  """
@@ -254,7 +254,7 @@ class RequestRateLimited(NyaProxyError):
254
254
  )
255
255
 
256
256
 
257
- class EncounterRetryStatusCodeError(NyaProxyError):
257
+ class EncounterUserDefinedRetry(NyaProxyStatus):
258
258
  """
259
259
  Exception raised when encountering a retry status code defined in the configuration.
260
260
  """
@@ -274,7 +274,7 @@ class EncounterRetryStatusCodeError(NyaProxyError):
274
274
  )
275
275
 
276
276
 
277
- class ReachedMaxRetriesError(NyaProxyError):
277
+ class ReachedMaxRetriesError(NyaProxyStatus):
278
278
  """
279
279
  Exception raised when the maximum number of retries is reached.
280
280
  """
@@ -292,3 +292,25 @@ class ReachedMaxRetriesError(NyaProxyError):
292
292
  super().__init__(
293
293
  f"NyaProxy: Reached maximum retries ({max_retries}) for {api_name}"
294
294
  )
295
+
296
+
297
+ class ReachedDailyQuotaError(NyaProxyStatus):
298
+ """
299
+ Exception raised when the daily quota for an API is reached.
300
+ """
301
+
302
+ def __init__(self, api_name: str, wait_time: float = None):
303
+ """
304
+ Initialize reached daily quota error.
305
+
306
+ Args:
307
+ api_name: Name of the API
308
+ wait_time: Optional time to wait before retrying (in seconds)
309
+ """
310
+ self.api_name = api_name
311
+ self.wait_time = wait_time
312
+ super().__init__(
313
+ f"NyaProxy: Max quota is reached for {api_name}, please try again in {wait_time:.1f}s"
314
+ if wait_time
315
+ else f"NyaProxy: Max quota is reached for {api_name}"
316
+ )
@@ -4,7 +4,7 @@ Data models for request handling in NyaProxy.
4
4
 
5
5
  import asyncio
6
6
  import time
7
- from typing import TYPE_CHECKING, Optional
7
+ from typing import TYPE_CHECKING, Optional, Self
8
8
 
9
9
  from httpx import Headers
10
10
 
@@ -46,6 +46,8 @@ class ProxyRequest:
46
46
  # API Related metadata
47
47
  self.api_name: str = "unknow"
48
48
  self.api_key: Optional[str] = None
49
+
50
+ self.user: Optional[str] = None
49
51
  self.ip = ip
50
52
 
51
53
  # Number of attempts made for this request
@@ -56,7 +58,6 @@ class ProxyRequest:
56
58
  self._rate_limited = False
57
59
 
58
60
  self.future: Optional[asyncio.Future] = None
59
- self._original_future: Optional[asyncio.Future] = None
60
61
 
61
62
  @classmethod
62
63
  async def from_request(cls, request: "Request") -> "ProxyRequest":
@@ -72,7 +73,7 @@ class ProxyRequest:
72
73
  ip=request.client.host,
73
74
  )
74
75
 
75
- def __lt__(self, other):
76
+ def __lt__(self, other: Self):
76
77
  """
77
78
  Compare for heap ordering (priority first, then timestamp).
78
79
  """
@@ -111,7 +111,8 @@ class ConfigManager:
111
111
  logger.info(f"Using local configuration file: {self.config_path}")
112
112
  storage = FileStorageBackend(config_path=self.config_path, logger=logger)
113
113
 
114
- storage.set_change_callback(self.callback)
114
+ if self.callback:
115
+ storage.set_change_callback(self.callback)
115
116
 
116
117
  if not storage:
117
118
  raise ConfigurationError(
@@ -157,18 +158,6 @@ class ConfigManager:
157
158
 
158
159
  return server
159
160
 
160
- def get_port(self) -> int:
161
- """
162
- Get the port for the proxy server.
163
- """
164
- return self.config.get_int("server.port", 8080)
165
-
166
- def get_host(self) -> str:
167
- """
168
- Get the host for the proxy server.
169
- """
170
- return self.config.get_str("server.host", "0.0.0.0")
171
-
172
161
  def get_debug_level(self) -> str:
173
162
  """
174
163
  Get the debug level for logging.
@@ -366,6 +355,12 @@ class ConfigManager:
366
355
  """
367
356
  return self.get_api_setting(api_name, "key_concurrency", "bool")
368
357
 
358
+ def get_api_random_delay(self, api_name: str) -> float:
359
+ """
360
+ Get randomness setting for API key selection.
361
+ """
362
+ return self.get_api_setting(api_name, "randomness", "float")
363
+
369
364
  def get_api_custom_headers(self, api_name: str) -> Dict[str, Any]:
370
365
  """
371
366
  Get custom headers.
@@ -420,6 +415,12 @@ class ConfigManager:
420
415
  """
421
416
  return self.get_api_setting(api_name, "rate_limit.ip_rate_limit", "str")
422
417
 
418
+ def get_api_user_rate_limit(self, api_name: str) -> str:
419
+ """
420
+ Get user rate limit.
421
+ """
422
+ return self.get_api_setting(api_name, "rate_limit.user_rate_limit", "str")
423
+
423
424
  def get_api_retry_enabled(self, api_name: str) -> bool:
424
425
  """
425
426
  Get retry enabled status.
@@ -1,6 +1,7 @@
1
+ # NyaProxy Configuration File
2
+ # This file contains server settings and API endpoint configurations
3
+
1
4
  server:
2
- host: 0.0.0.0
3
- port: 8080
4
5
  api_key:
5
6
  logging:
6
7
  enabled: true
@@ -12,24 +13,27 @@ server:
12
13
  dashboard:
13
14
  enabled: true
14
15
  cors:
15
- allow_origins: ["*"] # Set to "*" to allow all origins, however, when allow_credentials is true, this must be set to a specific origin
16
+ # Allow all origins with "*", but specify exact origins when allow_credentials is true for security
17
+ allow_origins: ["*"]
16
18
  allow_credentials: true
17
19
  allow_methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"]
18
20
  allow_headers: ["*"]
19
21
 
20
- # Default settings for the all apis if not specified
22
+ # Default configuration applied to all API endpoints unless overridden
21
23
  default_settings:
22
24
  key_variable: keys
23
- key_concurrency: true # Whether or not each key can handle concurrent requests
25
+ key_concurrency: true # Enable concurrent requests per API key for better performance
26
+ randomness: 0.0 # Random delay of (0.0-x)s to introduce variability in request timing and avoid detection
24
27
  load_balancing_strategy: round_robin
25
28
  queue:
26
29
  max_size: 200
27
30
  expiry_seconds: 300
28
31
  rate_limit:
29
32
  enabled: true
30
- endpoint_rate_limit: 10/s # Default endpoint rate limit, can be overridden by each API
31
- key_rate_limit: 10/m # Default key rate limit, can be overridden by each API
32
- ip_rate_limit: 200/d # You can also set a global IP rate limit, this is useful for protecting against abuse
33
+ endpoint_rate_limit: 10/s # Default endpoint rate limit - can be overridden per API
34
+ key_rate_limit: 10/m # Default key rate limit - can be overridden per API
35
+ ip_rate_limit: 1000/d # IP-based rate limit to protect against abuse and key redistribution
36
+ user_rate_limit: 1000/d # User-based rate limit per proxy API key defined in server section
33
37
  rate_limit_paths:
34
38
  - "*"
35
39
  retry:
@@ -61,8 +65,9 @@ default_settings:
61
65
  operator: "exists"
62
66
  apis:
63
67
  gemini:
64
- # Any OpenAI-Compatible API
68
+ # OpenAI-compatible API endpoint
65
69
  name: Google Gemini API
70
+ # Supported endpoints:
66
71
  # Gemini: https://generativelanguage.googleapis.com/v1beta/openai
67
72
  # OpenAI: https://api.openai.com/v1
68
73
  # Anthropic: https://api.anthropic.com/v1
@@ -84,11 +89,11 @@ apis:
84
89
  load_balancing_strategy: least_requests
85
90
  rate_limit:
86
91
  enabled: true
87
- # For Gemini, the rate limits (gemini-2.5-pro-exp-03-25) for each key are 5 RPM and 25 RPD
88
- # Ideally, the endpoint rate limit should be n x Per-Key-RPD, where n is the number of keys
92
+ # For Gemini-2.5-pro-exp-03-25, rate limits per key are 5 RPM and 25 RPD
93
+ # Endpoint rate limit should be n × Per-Key-RPD, where n is the number of keys
89
94
  endpoint_rate_limit: 75/d
90
95
  key_rate_limit: 5/m
91
- # Rate limit paths are optional, but you can configure which paths to apply the rate limits to (regex supported), default is all paths "*"
96
+ # Paths to apply rate limits (regex supported) - defaults to all paths "*"
92
97
  rate_limit_paths:
93
98
  - "/chat/*"
94
99
  - "/images/*"
@@ -97,6 +102,7 @@ apis:
97
102
  name: Test API
98
103
  endpoint: http://127.0.0.1:8082
99
104
  key_variable: keys
105
+ randomness: 5
100
106
  headers:
101
107
  Authorization: 'Bearer ${{keys}}'
102
108
  User-Agent: ${{agents}}
@@ -114,5 +120,7 @@ apis:
114
120
  enabled: true
115
121
  endpoint_rate_limit: 20/m
116
122
  key_rate_limit: 5/m
123
+ ip_rate_limit: 1000/d
124
+ user_rate_limit: 1000/d
117
125
  rate_limit_paths:
118
126
  - "/v1/*"