-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
34 lines (24 loc) · 803 Bytes
/
__init__.py
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
import asyncio
from sanic import Sanic
__version__ = "1.1.0"
app = Sanic("webtelnet")
app.config["THOST"] = "telehack.com"
app.config["TPORT"] = 23
app.static("/", "webtelnet/index.html")
async def websocket_fwd2client(ws, rsreader):
while True:
tosend = await rsreader.read(1024)
await ws.send(tosend)
async def websocket_fwd2serv(ws, rswriter):
while True:
recvd = await ws.recv()
rswriter.write(recvd.encode())
await rswriter.drain()
@app.websocket("/ws")
async def websocket(req, ws):
rsreader, rswriter = await asyncio.open_connection(
app.config["THOST"], app.config["TPORT"]
)
asyncio.create_task(websocket_fwd2client(ws, rsreader))
asyncio.create_task(websocket_fwd2serv(ws, rswriter))
await asyncio.Future()