forked from ReetBarik/argo-shim-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
79 lines (57 loc) · 2.06 KB
/
Copy pathmain.py
File metadata and controls
79 lines (57 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import aiohttp
import aiohttp.web
LISTEN_PORT = 8083
TARGET_HOST = "apps.inside.anl.gov"
TUNNEL_HOST = "127.0.0.1"
TARGET_PORT = 8082
async def proxy_request(request):
session = request.app["session"]
url = f"https://{TUNNEL_HOST}:{TARGET_PORT}{request.path_qs}"
headers = dict(request.headers)
headers["Host"] = TARGET_HOST
headers.pop("Content-Length", None)
body = await request.read()
try:
async with session.request(
method=request.method,
url=url,
headers=headers,
data=body if body else None,
ssl=False,
allow_redirects=False,
) as resp:
# Preserve streaming + encoding
filtered_headers = {
k: v
for k, v in resp.headers.items()
if k.lower() not in ("content-length")
}
# HEAD → no body
if request.method == "HEAD":
return aiohttp.web.Response(
status=resp.status,
headers=filtered_headers,
)
response = aiohttp.web.StreamResponse(
status=resp.status,
headers=filtered_headers,
)
await response.prepare(request)
async for chunk in resp.content.iter_any():
await response.write(chunk)
await response.write_eof()
return response
except Exception as e:
return aiohttp.web.Response(status=500, text=str(e))
async def on_startup(app):
timeout = aiohttp.ClientTimeout(total=None)
app["session"] = aiohttp.ClientSession(auto_decompress=False, timeout=timeout)
async def on_cleanup(app):
await app["session"].close()
app = aiohttp.web.Application()
app.router.add_route("*", "/{path_info:.*}", proxy_request)
app.on_startup.append(on_startup)
app.on_cleanup.append(on_cleanup)
if __name__ == "__main__":
print(f"Argo proxy listening on http://127.0.0.1:{LISTEN_PORT}")
aiohttp.web.run_app(app, host="127.0.0.1", port=LISTEN_PORT, print=None)