This commit is contained in:
nsde 2023-10-02 20:06:38 +02:00
parent 577cdc0d0b
commit 007050e9fe
3 changed files with 10 additions and 29 deletions

View file

@ -14,10 +14,6 @@ from fastapi.middleware.cors import CORSMiddleware
from slowapi.util import get_remote_address from slowapi.util import get_remote_address
from slowapi import Limiter, _rate_limit_exceeded_handler from slowapi import Limiter, _rate_limit_exceeded_handler
from helpers import network
#
import core import core
import handler import handler

View file

@ -37,18 +37,20 @@ class Proxy:
url = url.split('://')[1] url = url.split('://')[1]
if '@' in url: if '@' in url:
username = url.split('@')[1].split(':')[0] username = url.split('@')[0].split(':')[0]
password = url.split('@')[1].split(':')[1] password = url.split('@')[0].split(':')[1]
host_or_ip = url.split(':')[0] host_or_ip = url.split('@')[-1].split(':')[0]
port = url.split(':')[1] port = int(url.split('@')[-1].split(':')[1])
self.proxy_type = proxy_type self.proxy_type = proxy_type
self.host_or_ip = host_or_ip self.host_or_ip = host_or_ip
try: try:
self.ip_address = socket.gethostbyname(self.host_or_ip) # get ip address from host self.ip_address = socket.gethostbyname(self.host_or_ip) # get ip address from host
except socket.gaierror: except socket.gaierror:
self.ip_address = self.host_or_ip self.ip_address = self.host_or_ip
self.host = self.host_or_ip self.host = self.host_or_ip
self.port = port self.port = port
self.username = username self.username = username
@ -64,17 +66,6 @@ class Proxy:
self.urls_httpx = {k + '://' :v for k, v in self.urls.items()} self.urls_httpx = {k + '://' :v for k, v in self.urls.items()}
self.proxies = self.url self.proxies = self.url
print({
'proxy_type': self.proxy_type,
'host_or_ip': self.host_or_ip,
'ip_address': self.ip_address,
'host': self.host,
'port': self.port,
'username': self.username,
'password': self.password,
'url': self.url
})
@property @property
def connector(self): def connector(self):
""" """
@ -128,8 +119,6 @@ def get_proxy() -> Proxy:
The proxy is either from the proxy list or from the environment variables. The proxy is either from the proxy list or from the environment variables.
""" """
print('URL:\t' + ProxyLists().get_random.url)
if USE_PROXY_LIST: if USE_PROXY_LIST:
return ProxyLists().get_random return ProxyLists().get_random
@ -143,4 +132,3 @@ def get_proxy() -> Proxy:
if __name__ == '__main__': if __name__ == '__main__':
print(get_proxy().url) print(get_proxy().url)
print(get_proxy().connector)

View file

@ -46,7 +46,7 @@ async def respond(
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
for _ in range(20): for _ in range(10):
# Load balancing: randomly selecting a suitable provider # Load balancing: randomly selecting a suitable provider
# If the request is a chat completion, then we need to load balance between chat providers # If the request is a chat completion, then we need to load balance between chat providers
# If the request is an organic request, then we need to load balance between organic providers # If the request is an organic request, then we need to load balance between organic providers
@ -86,7 +86,7 @@ async def respond(
cookies=target_request.get('cookies'), cookies=target_request.get('cookies'),
ssl=False, ssl=False,
timeout=aiohttp.ClientTimeout( timeout=aiohttp.ClientTimeout(
connect=0.3, connect=1.0,
total=float(os.getenv('TRANSFER_TIMEOUT', '500')) total=float(os.getenv('TRANSFER_TIMEOUT', '500'))
), ),
) as response: ) as response:
@ -110,7 +110,6 @@ async def respond(
case 'invalid_api_key': case 'invalid_api_key':
await key_validation.log_rated_key(key) await key_validation.log_rated_key(key)
print('[!] invalid key', key) print('[!] invalid key', key)
pass
case _: case _:
print('[!] unknown error with key: ', key, error) print('[!] unknown error with key: ', key, error)
@ -127,6 +126,7 @@ async def respond(
json_response = data json_response = data
else: else:
print('[!] error', data)
continue continue
@ -140,6 +140,7 @@ async def respond(
async for chunk in response.content.iter_any(): async for chunk in response.content.iter_any():
chunk = chunk.decode('utf8').strip() chunk = chunk.decode('utf8').strip()
print(1)
yield chunk + '\n\n' yield chunk + '\n\n'
break break
@ -148,11 +149,7 @@ async def respond(
print('[!] exception', exc) print('[!] exception', exc)
continue continue
if (not json_response) and is_chat:
print('[!] chat response is empty')
continue
else: else:
print('[!] no response')
yield await errors.yield_error(500, 'Sorry, our API seems to have issues connecting to our provider(s).', 'This most likely isn\'t your fault. Please try again later.') yield await errors.yield_error(500, 'Sorry, our API seems to have issues connecting to our provider(s).', 'This most likely isn\'t your fault. Please try again later.')
return return