Skip to content

Commit

Permalink
use httpx to handel requests instead of aiohttp
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrtye committed Jan 5, 2025
1 parent f4ba60c commit 975fc80
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 49 deletions.
89 changes: 40 additions & 49 deletions shadowgate/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import aiohttp
import httpx
from aiohttp import web
from functools import partial
from xui import *
Expand Down Expand Up @@ -37,62 +37,53 @@ async def forward_to_proxy(request, port: str):


async def forward_request(request, target_url, upgrade_connection=False, timeout=None):
async with aiohttp.ClientSession() as session:
headers = {
key: value
for key, value in request.headers.items()
if key.lower() not in ["host", "connection", "upgrade"]
headers = {
key: value
for key, value in request.headers.items()
if key.lower() not in ["host", "connection", "upgrade"]
}
headers.update(
{
"Host": request.headers.get("Host", ""),
"X-Real-IP": request.remote,
"X-Forwarded-For": request.headers.get("X-Forwarded-For", request.remote),
}
headers.update(
{
"Host": request.headers.get("Host", ""),
"X-Real-IP": request.remote,
"X-Forwarded-For": request.headers.get(
"X-Forwarded-For", request.remote
),
}
)
if upgrade_connection:
headers["Connection"] = "upgrade"
headers["Upgrade"] = request.headers.get("Upgrade", "")
)
if upgrade_connection:
headers["Connection"] = "upgrade"
headers["Upgrade"] = request.headers.get("Upgrade", "")

try:
async with session.request(
try:
async with httpx.AsyncClient(timeout=timeout) as client:
response = await client.request(
method=request.method,
url=target_url,
headers=headers,
data=await request.read(),
timeout=timeout,
) as response:

resp_headers = {
key: value
for key, value in response.headers.items()
if key.lower() != "transfer-encoding"
}
resp_headers.update(
{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS, PUT, DELETE",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
}
)
body = await response.read()

print(f"Response from {target_url}: {response.status}")
print(f"Response headers: {resp_headers}")
print(f"Response body: {body[:500]}")

return web.Response(
status=response.status, headers=resp_headers, body=body
)

except Exception as e:
print(f"Error while forwarding request to {target_url}: {e}")
content=await request.read(),
)

resp_headers = {
key: value
for key, value in response.headers.items()
if key.lower() != "transfer-encoding"
}

body = response.content

print(f"Response from {target_url}: {response.status_code}")
print(f"Response headers: {resp_headers}")
print(f"Response body: {body[:500]}")

return web.Response(
status=500, text=f"Error while forwarding request: {str(e)}"
status=response.status_code, headers=resp_headers, body=body
)

except Exception as e:
print(f"Error while forwarding request to {target_url}: {e}")
return web.Response(
status=500, text=f"Error while forwarding request: {str(e)}"
)


async def start_proxy(inbounds: list[dict[str, str]]):
app = web.Application()
Expand Down
1 change: 1 addition & 0 deletions shadowgate/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
httpx
aiohttp
requests

0 comments on commit 975fc80

Please sign in to comment.