-
i make a proxy that connects to an other proxy but I can't send data to user and read raw req data from user import asyncio
from hola_proxy import get_proxy
import aiohttp
import base64
from aiohttp import web
@web.middleware
async def handle(request: web.Request, handler):
print(request.method)
print(request.url)
print(request.headers)
if request.method != "CONNECT":
async with aiohttp.ClientSession() as s:
async with s.request(request.method, request.url, headers=request.headers) as r:
rp = web.StreamResponse(status=r.status, headers=r.headers)
await rp.prepare(request)
async for data in r.content.iter_any():
await rp.write(data)
await rp.write_eof()
return rp
else:
proxylist = await get_proxy()
proxy = proxylist[0]
reader, writer = await asyncio.open_connection(proxy.host, proxy.port)
headers_proxy = [f"{k}: {v}" for k, v in request.headers.items()]
auth = base64.urlsafe_b64encode(f"{proxy.user}:{proxy.password}".encode()).decode()
headers_proxy.append(f"Proxy-Authorization: Basic "+auth)
headers_proxy_str = "\r\n".join(headers_proxy)
headers = ""
if headers_proxy_str:
headers = "\r\n"+headers_proxy_str
data = f'CONNECT {request.url.host}:{request.url.port} HTTP/1.1{headers}\r\n\r\n'.encode()
print(data)
writer.write(data)
await writer.drain()
reader_US = asyncio.StreamReader(loop=request._loop)
reader_US.set_transport(request._protocol.transport)
ison = [1]
async def server_to_user():
while ison:
read = await reader.read(1024)
print(b"server_to_user: "+read)
if read and ison:
await request.writer.write(read)
await request.writer.drain()
else:
ison.clear()
async def user_to_server():
while ison:
read = await reader_US.read(1024)
print(b"user_to_server: "+read)
if read and ison:
writer.write(read)
else:
ison.clear()
await asyncio.gather(user_to_server(), server_to_user())
app = web.Application()
app.middlewares.append(handle)
web.run_app(app) |
Beta Was this translation helpful? Give feedback.
Answered by
thedtvn
Jul 12, 2023
Replies: 2 comments 1 reply
-
I honestly have no idea. It looks like you're trying to build a proxy server inside of an endpoint... |
Beta Was this translation helpful? Give feedback.
1 reply
-
this is how to foword tcp req import asyncio
from hola_proxy import get_proxy
import aiohttp
import base64
from aiohttp import web
class TCPReader(asyncio.StreamReader):
def feed_data(self, data: bytes):
super().feed_data(data)
return (False, 1)
@web.middleware
async def handle(request: web.Request, handler):
print(request.method)
print(request.url)
print(request.headers)
proxylist = await get_proxy()
proxy = proxylist[0]
print(proxy)
reader, writer = await asyncio.open_connection(proxy.host, proxy.port)
headers_proxy = [f"{k}: {v}" for k, v in request.headers.items()]
auth = base64.urlsafe_b64encode(f"{proxy.user}:{proxy.password}".encode()).decode()
headers_proxy.append(f"Proxy-Authorization: Basic "+auth)
headers_proxy_str = "\r\n".join(headers_proxy)
headers = ""
if headers_proxy_str:
headers = "\r\n"+headers_proxy_str
url = request.url if request.url.scheme else f"{request.url.host}:{request.url.port}"
data = f'{request.method} {url} HTTP/1.1{headers}\r\n\r\n'.encode()+await request.content.read()
writer.write(data)
await writer.drain()
reader_US = TCPReader(loop=request._loop)
reader_US.set_transport(request._protocol.transport)
writer_user = aiohttp.http.StreamWriter(request._protocol, request._loop)
request.protocol.set_parser(reader_US)
ison = [1]
request.protocol.keep_alive(False)
async def server_to_user():
while ison:
read = await reader.read(1024)
if read and ison:
writer_user._write(read)
await writer_user.drain()
else:
ison.clear()
async def user_to_server():
while ison:
read = await reader_US.read(1024)
if read and ison:
writer.write(read)
else:
ison.clear()
await asyncio.gather(user_to_server(), server_to_user())
request.protocol.close()
writer.close()
app = web.Application()
app.middlewares.append(handle)
web.run_app(app) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
thedtvn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is how to foword tcp req