bugfix
This commit is contained in:
parent
82dacf619e
commit
1d874946a3
@ -6,7 +6,7 @@ import codecs
|
||||
from urllib.parse import quote
|
||||
import json
|
||||
import asyncio
|
||||
from traceback import fromat_exc
|
||||
from traceback import format_exc
|
||||
|
||||
import random
|
||||
import time
|
||||
|
@ -1,5 +1,6 @@
|
||||
import time
|
||||
import uuid
|
||||
from traceback import print_exc
|
||||
from aiohttp_auth import auth
|
||||
from aiohttp_auth.auth.ticket_auth import TktAuthentication
|
||||
from aiohttp_session.redis_storage import RedisStorage
|
||||
@ -150,6 +151,8 @@ class AuthAPI:
|
||||
except Exception as e:
|
||||
t3 = time.time()
|
||||
info(f'Exception=client({ip}) {user} access {path} cost {t3-t1}, ({t2-t1}), except={e}')
|
||||
print(f'Exception=client({ip}) {user} access {path} cost {t3-t1}, ({t2-t1}), except={e}')
|
||||
print_exc()
|
||||
raise e
|
||||
|
||||
if user is None:
|
||||
|
@ -6,6 +6,7 @@ import codecs
|
||||
from urllib.parse import quote
|
||||
import json
|
||||
import asyncio
|
||||
from traceback import fromat_exc
|
||||
|
||||
import random
|
||||
import time
|
||||
@ -62,6 +63,10 @@ async def save_file(str_or_bytes, filename):
|
||||
r = await fs.save(filename, str_or_bytes)
|
||||
return r
|
||||
|
||||
def realpath(path):
|
||||
fs = FileStorage()
|
||||
return fs.realPath(path)
|
||||
|
||||
class FileOutZone(Exception):
|
||||
def __init__(self,fp,*args,**kwargs):
|
||||
super(FileOutZone,self).__init__(*args,**kwargs)
|
||||
@ -199,6 +204,8 @@ def initEnv():
|
||||
g.async_sleep = asyncio.sleep
|
||||
g.quotedstr = quotedstr
|
||||
g.save_file = save_file
|
||||
g.realpath = realpath
|
||||
g.format_exc = format_exc
|
||||
|
||||
def set_builtins():
|
||||
all_builtins = [ i for i in dir(builtins) if not i.startswith('_')]
|
||||
|
@ -7,6 +7,8 @@ from aiohttp import web
|
||||
import aiohttp_cors
|
||||
from traceback import print_exc
|
||||
from appPublic.sshx import SSHNode
|
||||
from appPublic.dictObject import DictObject
|
||||
from appPublic.log import info, debug, warning, error, exception, critical
|
||||
from .baseProcessor import BaseProcessor, PythonScriptProcessor
|
||||
|
||||
class XtermProcessor(PythonScriptProcessor):
|
||||
@ -81,8 +83,7 @@ class XtermProcessor(PythonScriptProcessor):
|
||||
self.p_obj.close()
|
||||
|
||||
async def ws_send(ws:web.WebSocketResponse, data):
|
||||
if not isinstance(data, str):
|
||||
data = json.dumps(data)
|
||||
info(f'data={data} {ws=}')
|
||||
d = {
|
||||
"type":1,
|
||||
"data":data
|
||||
@ -91,54 +92,121 @@ async def ws_send(ws:web.WebSocketResponse, data):
|
||||
try:
|
||||
return await ws.send_str(d)
|
||||
except Exception as e:
|
||||
print('ws.send_str() error:', e)
|
||||
exception(f'ws.send_str() error: {e=}')
|
||||
print_exc()
|
||||
return False
|
||||
|
||||
class WsSession:
|
||||
def __init__(self, session):
|
||||
self.session = session
|
||||
self.nodes = {}
|
||||
|
||||
def join(node):
|
||||
self.nodes[node.id] = node
|
||||
|
||||
def leave(node):
|
||||
self.nodes = {k:v for k,v in self.nodes.items() if k != node.id}
|
||||
|
||||
class WsData:
|
||||
def __init__(self):
|
||||
self.nodes = {}
|
||||
self.sessions = {}
|
||||
|
||||
def add_node(self, node):
|
||||
self.nodes[node.id] = node
|
||||
|
||||
def del_node(self, node):
|
||||
self.nodes = {k:v for k,v in self.nodes.items() if k!=node.id}
|
||||
|
||||
def get_nodes(self):
|
||||
return self.nodes
|
||||
|
||||
def get_node(self, id):
|
||||
return self.nodes.get(id)
|
||||
|
||||
def add_session(self, session):
|
||||
self.sessions[session.sessionid] = session
|
||||
|
||||
def del_session(self, session):
|
||||
self.sessions = {k:v for k,v in self.sessions.items() if k != session.sessionid}
|
||||
|
||||
def get_session(self, id):
|
||||
return self.sessions.get(id)
|
||||
|
||||
class WsPool:
|
||||
def __init__(self, ws, ws_path, app):
|
||||
def __init__(self, ws, ip, ws_path, app):
|
||||
self.app = app
|
||||
self.ip = ip
|
||||
self.id = None
|
||||
self.ws = ws
|
||||
print(f'-----------{self.id=}-----------')
|
||||
self.ws_path = ws_path
|
||||
|
||||
def get_data(self):
|
||||
return self.app.get_data(self.ws_path)
|
||||
r = self.app.get_data(self.ws_path)
|
||||
if r is None:
|
||||
r = WsData()
|
||||
self.set_data(r)
|
||||
return r
|
||||
|
||||
def set_data(self, data):
|
||||
self.app.set_data(self.ws_path, data)
|
||||
|
||||
def is_online(self, userid):
|
||||
data = self.get_data()
|
||||
ws = data.get(userid)
|
||||
if ws is None:
|
||||
node = data.get_node(userid)
|
||||
if node is None:
|
||||
return False
|
||||
return True
|
||||
|
||||
def register(self, id):
|
||||
self.id = id
|
||||
self.add_mine()
|
||||
iddata = DictObject()
|
||||
iddata.id = id
|
||||
self.add_me(iddata)
|
||||
|
||||
def add_mine(self):
|
||||
def add_me(self, iddata):
|
||||
data = self.get_data()
|
||||
if data is None:
|
||||
data = {}
|
||||
data.update({self.id:self.ws})
|
||||
iddata.ws = self.ws
|
||||
iddata.ip = self.ip
|
||||
self.id = iddata.id
|
||||
data.add_node(ddata)
|
||||
self.set_data(data)
|
||||
|
||||
def delete_mine(self):
|
||||
def delete_id(self, id):
|
||||
data = self.get_data()
|
||||
if not data.get(self.id):
|
||||
return
|
||||
data = {k:v for k,v in self.data.items() if k != self.id }
|
||||
node = data.get_node(id)
|
||||
if node:
|
||||
data.del_node(node)
|
||||
self.set_data(data)
|
||||
|
||||
def delete_me(self):
|
||||
self.delete_id(self.id)
|
||||
|
||||
def add_session(self, session):
|
||||
data = self.get_data()
|
||||
data.add_session(session)
|
||||
self.set_data(data)
|
||||
|
||||
def del_session(self, session):
|
||||
data = self.get_data()
|
||||
data.del_session(session)
|
||||
self.set_data(data)
|
||||
|
||||
def get_session(self, sessionid):
|
||||
data = self.get_data()
|
||||
return data.get_session(sessionid)
|
||||
|
||||
async def sendto(self, data, id=None):
|
||||
if id is None:
|
||||
return await ws_send(self.ws, data)
|
||||
d = self.get_data()
|
||||
ws = d.get(id)
|
||||
iddata = d.get_node(id)
|
||||
ws = iddata.ws
|
||||
try:
|
||||
return await ws_send(ws, data)
|
||||
except:
|
||||
self.delete_id(id)
|
||||
|
||||
class WebsocketProcessor(PythonScriptProcessor):
|
||||
@classmethod
|
||||
@ -151,32 +219,38 @@ class WebsocketProcessor(PythonScriptProcessor):
|
||||
lenv.update(params)
|
||||
params_kw = lenv.params_kw
|
||||
userid = lenv.params_kw.userid or await lenv.get_user()
|
||||
debug(f'========== debug ===========')
|
||||
del lenv['request']
|
||||
txt = await self.loadScript(self.real_path)
|
||||
ws = web.WebSocketResponse()
|
||||
debug(f'========== debug ===========')
|
||||
try:
|
||||
await ws.prepare(request)
|
||||
except Exception as e:
|
||||
print('--------except:', e, type(e))
|
||||
exception(f'--------except: {e}')
|
||||
print_exc()
|
||||
raise e
|
||||
ws_pool = WsPool(ws, request.path, request.app)
|
||||
debug(f'========== debug ===========')
|
||||
ws_pool = WsPool(ws, request['client_ip'], request.path, request.app)
|
||||
debug(f'========== debug ===========')
|
||||
async for msg in ws:
|
||||
if msg.type == aiohttp.WSMsgType.TEXT:
|
||||
debug(f'========== {msg=} ===========')
|
||||
if msg.data == 'exit':
|
||||
break
|
||||
debug(f'WS:msg from client={msg}')
|
||||
lenv['ws_data'] = msg.data
|
||||
lenv['ws_pool'] = ws_pool
|
||||
exec(txt,lenv,lenv)
|
||||
func = lenv['myfunc']
|
||||
resp = await func(request,**lenv)
|
||||
# await self.ws_sendstr(ws, resp)
|
||||
elif msg.type == aiohttp.WSMsgType.ERROR:
|
||||
# print('ws connection closed with exception %s' % ws.exception())
|
||||
error('ws connection closed with exception %s' % ws.exception())
|
||||
break
|
||||
else:
|
||||
print('datatype error', msg.type)
|
||||
ws_pool.delete_mine()
|
||||
info('datatype error', msg.type)
|
||||
debug(f'========== ws connection end ===========')
|
||||
ws_pool.delete_me()
|
||||
self.retResponse = ws
|
||||
await ws.close()
|
||||
return ws
|
||||
|
Loading…
Reference in New Issue
Block a user