/
ihtiandr9
/
httpserver
Обзор
Документация
Войти
/
ihtiandr9
/
httpserver
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
class-stylesheet
cpp/ssl/proxy.py
68 строк
2 KB
lomaster
ssl proxy
08 фев 2026, 15:22
08 фев 2026, 15:22
5770c7c
Код
Авторство
О чём код?
# dev_ssl_proxy.py - Простой HTTP/HTTPS мост import asyncio import aiohttp from aiohttp import web import ssl async def handle(request): """Проксируем запрос на HTTP сервер""" target_url = f"http://localhost:8080{request.path_qs}" async with aiohttp.ClientSession() as session: # Копируем метод и заголовки async with session.request( method=request.method, url=target_url, headers=request.headers, data=await request.read() if request.can_read_body else None ) as resp: # Возвращаем ответ response = web.StreamResponse( status=resp.status, reason=resp.reason ) for name, value in resp.headers.items(): if name.lower() not in ('transfer-encoding', 'connection'): response.headers[name] = value await response.prepare(request) async for chunk in resp.content.iter_chunked(8192): await response.write(chunk) await response.write_eof() return response async def main(): app = web.Application() app.router.add_route('*', '/{path:.*}', handle) # Создаем SSL контекст ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) ssl_context.load_cert_chain('cert.pem', 'key.pem') runner = web.AppRunner(app) await runner.setup() site = web.TCPSite(runner, 'localhost', 8443, ssl_context=ssl_context) await site.start() print(f"HTTPS прокси запущен: https://localhost:8443 -> http://localhost:8080") print("Нажмите Ctrl+C для остановки") # Бесконечное ожидание await asyncio.Event().wait() if __name__ == '__main__': import os # Создаем сертификат если нужно if not os.path.exists('cert.pem'): print("Создаю сертификат...") os.system('openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost"') try: asyncio.run(main()) except KeyboardInterrupt: print("\nСервер остановлен")