Skip to content

Commit 0fd2975

Browse files
author
Pedro Crespo
committed
Merge remote-tracking branch 'upstream/master'
2 parents d423dcc + 8d7d44f commit 0fd2975

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import asyncio
2+
import logging
3+
import pprint
4+
5+
import aiohttp
6+
from aiohttp import client, web
7+
8+
logging.basicConfig(level=logging.INFO)
9+
logger = logging.getLogger(__name__)
10+
11+
12+
baseUrl = 'http://0.0.0.0:8888'
13+
mountPoint = '/fakeUuid'
14+
15+
16+
async def handler(req):
17+
proxyPath = req.match_info.get(
18+
'proxyPath', 'no proxyPath placeholder defined')
19+
reqH = req.headers.copy()
20+
if reqH['connection'] == 'Upgrade' and reqH['upgrade'] == 'websocket' and req.method == 'GET':
21+
22+
ws_server = web.WebSocketResponse()
23+
await ws_server.prepare(req)
24+
logger.info('##### WS_SERVER %s', pprint.pformat(ws_server))
25+
26+
client_session = aiohttp.ClientSession(cookies=req.cookies)
27+
async with client_session.ws_connect(
28+
baseUrl+req.path_qs,
29+
) as ws_client:
30+
logger.info('##### WS_CLIENT %s', pprint.pformat(ws_client))
31+
32+
async def ws_forward(ws_from, ws_to):
33+
async for msg in ws_from:
34+
logger.info('>>> msg: %s', pprint.pformat(msg))
35+
mt = msg.type
36+
md = msg.data
37+
if mt == aiohttp.WSMsgType.TEXT:
38+
await ws_to.send_str(md)
39+
elif mt == aiohttp.WSMsgType.BINARY:
40+
await ws_to.send_bytes(md)
41+
elif mt == aiohttp.WSMsgType.PING:
42+
await ws_to.ping()
43+
elif mt == aiohttp.WSMsgType.PONG:
44+
await ws_to.pong()
45+
elif ws_to.closed:
46+
await ws_to.close(code=ws_to.close_code, message=msg.extra)
47+
else:
48+
raise ValueError(
49+
'unexpected message type: %s' % pprint.pformat(msg))
50+
51+
await asyncio.wait([ws_forward(ws_server, ws_client), ws_forward(ws_client, ws_server)], return_when=asyncio.FIRST_COMPLETED)
52+
53+
return ws_server
54+
else:
55+
async with client.request(
56+
req.method, baseUrl+mountPoint+proxyPath,
57+
headers=reqH,
58+
allow_redirects=False,
59+
data=await req.read()
60+
) as res:
61+
headers = res.headers.copy()
62+
body = await res.read()
63+
return web.Response(
64+
headers=headers,
65+
status=res.status,
66+
body=body
67+
)
68+
return ws_server
69+
70+
app = web.Application()
71+
app.router.add_route('*', mountPoint + '{proxyPath:.*}', handler)
72+
web.run_app(app, port=3984)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import asyncio
2+
import logging
3+
import pprint
4+
5+
import aiohttp
6+
from aiohttp import client, web
7+
8+
logging.basicConfig(level=logging.INFO)
9+
logger = logging.getLogger(__name__)
10+
11+
12+
baseUrl = 'http://0.0.0.0:8080'
13+
mountPoint = '/fakeUuid'
14+
15+
16+
async def handler(req):
17+
proxyPath = req.match_info.get(
18+
'proxyPath', 'no proxyPath placeholder defined')
19+
reqH = req.headers.copy()
20+
if reqH['connection'] == 'Upgrade' and reqH['upgrade'] == 'websocket' and req.method == 'GET':
21+
22+
ws_server = web.WebSocketResponse()
23+
await ws_server.prepare(req)
24+
logger.info('##### WS_SERVER %s', pprint.pformat(ws_server))
25+
26+
client_session = aiohttp.ClientSession(cookies=req.cookies)
27+
async with client_session.ws_connect(
28+
baseUrl+proxyPath,
29+
) as ws_client:
30+
logger.info('##### WS_CLIENT %s', pprint.pformat(ws_client))
31+
32+
async def ws_forward(ws_from, ws_to):
33+
async for msg in ws_from:
34+
#logger.info('>>> msg: %s',pprint.pformat(msg))
35+
mt = msg.type
36+
md = msg.data
37+
if mt == aiohttp.WSMsgType.TEXT:
38+
await ws_to.send_str(md)
39+
elif mt == aiohttp.WSMsgType.BINARY:
40+
await ws_to.send_bytes(md)
41+
elif mt == aiohttp.WSMsgType.PING:
42+
await ws_to.ping()
43+
elif mt == aiohttp.WSMsgType.PONG:
44+
await ws_to.pong()
45+
elif ws_to.closed:
46+
await ws_to.close(code=ws_to.close_code, message=msg.extra)
47+
else:
48+
raise ValueError(
49+
'unexpected message type: %s' % pprint.pformat(msg))
50+
51+
await asyncio.wait([ws_forward(ws_server, ws_client), ws_forward(ws_client, ws_server)], return_when=asyncio.FIRST_COMPLETED)
52+
53+
return ws_server
54+
else:
55+
async with client.request(
56+
req.method, baseUrl+proxyPath,
57+
headers=reqH,
58+
allow_redirects=False,
59+
data=await req.read()
60+
) as res:
61+
headers = res.headers.copy()
62+
del headers['content-length']
63+
body = await res.read()
64+
if proxyPath == '/Visualizer.js':
65+
body = body.replace(b'"/ws"', b'"%s/ws"' %
66+
mountPoint.encode(), 1)
67+
body = body.replace(
68+
b'"/paraview/"', b'"%s/paraview/"' % mountPoint.encode(), 1)
69+
logger.info("fixed Visualizer.js paths on the fly")
70+
return web.Response(
71+
headers=headers,
72+
status=res.status,
73+
body=body
74+
)
75+
return ws_server
76+
77+
app = web.Application()
78+
app.router.add_route('*', mountPoint + '{proxyPath:.*}', handler)
79+
web.run_app(app, port=3985)

0 commit comments

Comments
 (0)