proxy/proxy.py

55 lines
1.6 KiB
Python

#!/usr/bin/env python
import asyncio
import websockets
import os
async def loop(ws, p):
print("connected")
writeflag = False
authed = False
async for message in ws:
if not writeflag:
split = message.split(' ')
if split[0] == 'get' and authed:
try:
fd = open('tree/' + split[1], 'r')
except FileNotFoundError:
await ws.send("text/formatted")
await ws.send("#file not found!")
await ws.send("oh no")
continue
await ws.send("text/formatted")
for i in fd.readlines():
await ws.send(i.rstrip())
fd.close()
elif split[0] == 'put' and authed:
os.makedirs(os.path.dirname(split[1]), exist_ok=True)
fd = open('tree/' + split[1], 'w+')
writeflag = True;
elif split[0] == 'auth':
accts = open('login', 'r')
linedata = [i.rstrip().split(' ') for i in accts.readlines()]
passwdmatch = False
for (username, password) in linedata:
if split[1] == username and split[2] == password:
passwdmatch = True
if passwdmatch: authed = True
await ws.send("auth succeed")
else:
fd.write(message);
fd.close();
writeflag = False;
await ws.send("hi there!")
async def main():
async with websockets.serve(loop, "192.168.1.30", 8765):
await asyncio.Future()
asyncio.run(main())