Skip to content

Instantly share code, notes, and snippets.

@aont
Last active July 4, 2024 08:08
Show Gist options
  • Save aont/a94bdf4156f1fe964b9b0eb6deba2950 to your computer and use it in GitHub Desktop.
Save aont/a94bdf4156f1fe964b9b0eb6deba2950 to your computer and use it in GitHub Desktop.
import aiohttp
import aiohttp.web
import aiohttp_basicauth
headers = {
"sec-ch-ua": "\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Google Chrome\";v=\"126\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"Referrer-Policy": "origin",
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
}
session: aiohttp.ClientSession = None
async def handle(request: aiohttp.web.Request):
url = 'https:/' + request.path
original_host = request.path.split("/")[1]
headers_cur = { "Referer": "https://" + original_host }
headers_cur.update(headers)
response = await session.get(url, headers=headers)
content = await response.content.read()
content = content.replace(b"<script", b'<!-- <script').replace(b"</script>", b"</script> -->")
proxy_hostname = request.host
content = content.replace(b"https://" + original_host.encode(), b'http://' + proxy_hostname.encode() + b'/' + original_host.encode())
return aiohttp.web.Response(body=content, status=response.status, content_type=response.content_type)
if False:
import ssl
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ctx.load_cert_chain("server.crt", keyfile="server.key")
else:
ctx = None
async def startup_callback(app : aiohttp.web.Application):
global session
session = aiohttp.ClientSession()
async def session_close(app : aiohttp.web.Application):
return await session.close()
app.on_cleanup.append(session_close)
auth = aiohttp_basicauth.BasicAuthMiddleware(username="user", password="1234")
app = aiohttp.web.Application(middlewares=(auth,), )
app.router.add_route('GET', '/{tail:.*}', handle)
app.on_startup.append(startup_callback)
aiohttp.web.run_app(app, ssl_context=ctx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment