Compare commits
22 Commits
692b300f9b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36bbedd3cf | ||
|
|
0e3703b1f4 | ||
|
|
52696707dd | ||
|
|
0a812e496f | ||
|
|
de1ba08d7e | ||
|
|
3f14cbd6c1 | ||
|
|
c51565199c | ||
|
|
8d29a26d7d | ||
|
|
decf24020a | ||
|
|
3dc219adc2 | ||
|
|
40d1341685 | ||
|
|
0d606fb171 | ||
|
|
e1f32ec980 | ||
|
|
ad8d7d069c | ||
|
|
687b316db6 | ||
|
|
60248d5c51 | ||
|
|
befec5543c | ||
|
|
af24c51a2f | ||
|
|
fa91696aae | ||
|
|
e698c50a4b | ||
|
|
75795dd853 | ||
|
|
5b8180b48e |
@@ -39,16 +39,19 @@ class ASEngineClient:
|
|||||||
# 连接事件
|
# 连接事件
|
||||||
@self.sio.on('connect', namespace=self.namespace)
|
@self.sio.on('connect', namespace=self.namespace)
|
||||||
def on_connect():
|
def on_connect():
|
||||||
|
print("---------------ASEngineClient on_connect----------------")
|
||||||
|
self.sid = self.sio.sid
|
||||||
|
print(f"Connected to server with sid: {self.sid}")
|
||||||
|
|
||||||
self.connected = True
|
self.connected = True
|
||||||
self.sid = self.sio.get_sid(namespace=self.namespace)
|
self.sio.emit('join', {'id': self.id}, namespace=self.namespace)
|
||||||
print(f"Connected to server. SID: {self.sid}")
|
|
||||||
if self._on_connect_callback is not None:
|
if self._on_connect_callback is not None:
|
||||||
self._on_connect_callback(
|
self._on_connect_callback(
|
||||||
self,
|
self,
|
||||||
self._on_connect_callback_entry,
|
self._on_connect_callback_entry,
|
||||||
init_data=self._on_connect_callback_initData
|
init_data=self._on_connect_callback_initData
|
||||||
)
|
)
|
||||||
|
|
||||||
# 断开连接事件
|
# 断开连接事件
|
||||||
@self.sio.on('disconnect', namespace=self.namespace)
|
@self.sio.on('disconnect', namespace=self.namespace)
|
||||||
def on_disconnect():
|
def on_disconnect():
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ from typing import Optional
|
|||||||
mongo = PyMongo()
|
mongo = PyMongo()
|
||||||
|
|
||||||
|
|
||||||
socketio = SocketIO(cors_allowed_origins="*",max_payload_size=52428800, async_mode="eventlet") # 不直接传 app
|
socketio = SocketIO(cors_allowed_origins="*",max_payload_size=52428800, async_mode="eventlet",always_connect=False) # 不直接传 app
|
||||||
cors = CORS()
|
cors = CORS()
|
||||||
# ===== 你的全局对象 =====
|
# ===== 你的全局对象 =====
|
||||||
# Backboard
|
# Backboard
|
||||||
@@ -38,7 +38,7 @@ def init_extensions(app):
|
|||||||
cors_allowed_origins='*',#app.config["VSCODE_WEB_URL"],
|
cors_allowed_origins='*',#app.config["VSCODE_WEB_URL"],
|
||||||
ping_timeout=app.config["SOCKETIO_PING_TIMEOUT"],
|
ping_timeout=app.config["SOCKETIO_PING_TIMEOUT"],
|
||||||
ping_interval=app.config["SOCKETIO_PING_INTERVAL"],
|
ping_interval=app.config["SOCKETIO_PING_INTERVAL"],
|
||||||
max_payload_size=50000000, async_mode="eventlet"
|
max_payload_size=50000000, async_mode="eventlet",always_connect=False
|
||||||
)
|
)
|
||||||
cors.init_app(
|
cors.init_app(
|
||||||
app,
|
app,
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
import requests
|
import requests
|
||||||
from flask_socketio import Namespace
|
|
||||||
|
|
||||||
|
|
||||||
def clear_user_session(asengine_url, asengine_token, user_id):
|
def clear_user_session(asengine_url, asengine_token, user_id):
|
||||||
'''
|
'''
|
||||||
@@ -28,9 +26,12 @@ def on_connect_to_ase(client, entry, init_data, **kwargs):
|
|||||||
'''
|
'''
|
||||||
此时刚刚建立好aseclient连接,并向前端发送打开code-server-like指令。
|
此时刚刚建立好aseclient连接,并向前端发送打开code-server-like指令。
|
||||||
'''
|
'''
|
||||||
namespace_entry, ase_client = entry
|
namespace_entry, ase_client, logger = entry
|
||||||
|
logger.info(f"on_connect_to_ase with init_data: {init_data}")
|
||||||
namespace_entry.emit('open-iframe',"", room=init_data['room'], namespace='/agent')
|
namespace_entry.emit('open-iframe',"", room=init_data['room'], namespace='/agent')
|
||||||
namespace_entry.emit('message', "Agents服务已连接, 加载教案中", room=init_data['room'], namespace='/agent')
|
namespace_entry.emit('message', "Agents服务已连接, 加载教案中", room=init_data['room'], namespace='/agent')
|
||||||
|
namespace_entry.emit('message', f'{init_data}', room=init_data['room'], namespace='/agent')
|
||||||
|
|
||||||
|
|
||||||
restart = init_data.get("restart", False)
|
restart = init_data.get("restart", False)
|
||||||
# 不再需要手动next_chapter,因为已经在on_login中根据mongo进度恢复了正确的章节
|
# 不再需要手动next_chapter,因为已经在on_login中根据mongo进度恢复了正确的章节
|
||||||
|
|||||||
@@ -93,7 +93,6 @@ def get_multiagents_dialog_list(user_id: str) -> List[Dict]:
|
|||||||
|
|
||||||
# 检查响应
|
# 检查响应
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
current_app.logger.info(f"获取MultiAgents对话列表成功: user_id={user_id}, json={response.json()}")
|
|
||||||
return response.json()
|
return response.json()
|
||||||
else:
|
else:
|
||||||
current_app.logger.error(f"获取MultiAgents对话列表失败: HTTP {response.status_code}")
|
current_app.logger.error(f"获取MultiAgents对话列表失败: HTTP {response.status_code}")
|
||||||
|
|||||||
@@ -63,6 +63,12 @@ class VSCodeNamespace(Namespace):
|
|||||||
|
|
||||||
|
|
||||||
class AgentNamespace(Namespace):
|
class AgentNamespace(Namespace):
|
||||||
|
def __init__(self, namespace):
|
||||||
|
super().__init__(namespace)
|
||||||
|
# 使用字典存储每个用户的chatmanager和ase_client,避免多用户冲突
|
||||||
|
self.user_chatmanagers = {}
|
||||||
|
self.user_ase_clients = {}
|
||||||
|
|
||||||
def _load_learning_progress(self, chatmanager, user_id, course_id, chapter_name, lesson_name, continue_learn):
|
def _load_learning_progress(self, chatmanager, user_id, course_id, chapter_name, lesson_name, continue_learn):
|
||||||
"""加载并恢复用户学习进度
|
"""加载并恢复用户学习进度
|
||||||
|
|
||||||
@@ -120,8 +126,8 @@ class AgentNamespace(Namespace):
|
|||||||
chapter_name = data['chapter_name']
|
chapter_name = data['chapter_name']
|
||||||
user_id = uuid2username[user_uuid]
|
user_id = uuid2username[user_uuid]
|
||||||
session['user_uuid'] = user_uuid
|
session['user_uuid'] = user_uuid
|
||||||
join_room(user_uuid, namespace='/agent') # 将该用户加入以user_id为名的room
|
|
||||||
print(f'User connected with session user_uuid: {user_uuid}')
|
print(f'User connected with session user_uuid: {user_uuid}')
|
||||||
|
join_room(user_uuid)
|
||||||
|
|
||||||
user_uuid2chatmanager = ex["user_uuid2chatmanager"]
|
user_uuid2chatmanager = ex["user_uuid2chatmanager"]
|
||||||
user_uuid2ase_client = ex["user_uuid2ase_client"]
|
user_uuid2ase_client = ex["user_uuid2ase_client"]
|
||||||
@@ -152,8 +158,9 @@ class AgentNamespace(Namespace):
|
|||||||
if chatmanager.chat_historys:
|
if chatmanager.chat_historys:
|
||||||
emit('messages', chatmanager.chat_historys, room=user_uuid, namespace='/agent')
|
emit('messages', chatmanager.chat_historys, room=user_uuid, namespace='/agent')
|
||||||
|
|
||||||
self.chatmanager = chatmanager
|
# 存储每个用户的chatmanager和ase_client到字典中,使用用户UUID作为键
|
||||||
self.ase_client = user_uuid2ase_client[user_uuid]
|
self.user_chatmanagers[user_uuid] = chatmanager
|
||||||
|
self.user_ase_clients[user_uuid] = user_uuid2ase_client[user_uuid]
|
||||||
|
|
||||||
# 将restart设置为False,避免后续重复处理
|
# 将restart设置为False,避免后续重复处理
|
||||||
restart = chatmanager.restart
|
restart = chatmanager.restart
|
||||||
@@ -161,14 +168,14 @@ class AgentNamespace(Namespace):
|
|||||||
|
|
||||||
user_uuid2ase_client[user_uuid].register_on_connect_entry(
|
user_uuid2ase_client[user_uuid].register_on_connect_entry(
|
||||||
callback=on_connect_to_ase,
|
callback=on_connect_to_ase,
|
||||||
entry=(self, user_uuid2ase_client[user_uuid]),
|
entry=(self, user_uuid2ase_client[user_uuid], current_app.logger),
|
||||||
init_data={'room':user_uuid, 'restart':restart}
|
init_data={'room':user_uuid, 'restart':restart}
|
||||||
)
|
)
|
||||||
user_uuid2ase_client[user_uuid].register_route_with_entry(
|
user_uuid2ase_client[user_uuid].register_route_with_entry(
|
||||||
route='dialog',
|
route='dialog',
|
||||||
callback=receive_ase_dialog,
|
callback=receive_ase_dialog,
|
||||||
entry=self,
|
entry=self,
|
||||||
init_data={'room': user_uuid}
|
init_data={'room':user_uuid}
|
||||||
)
|
)
|
||||||
user_uuid2ase_client[user_uuid].register_route_with_entry(
|
user_uuid2ase_client[user_uuid].register_route_with_entry(
|
||||||
route='dialog-hint',
|
route='dialog-hint',
|
||||||
@@ -186,13 +193,13 @@ class AgentNamespace(Namespace):
|
|||||||
route='judge',
|
route='judge',
|
||||||
callback=receive_ase_judge,
|
callback=receive_ase_judge,
|
||||||
entry=self,
|
entry=self,
|
||||||
init_data={'room': user_uuid}
|
init_data={'room':user_uuid}
|
||||||
)
|
)
|
||||||
user_uuid2ase_client[user_uuid].register_route_with_entry(
|
user_uuid2ase_client[user_uuid].register_route_with_entry(
|
||||||
route='next_chapter',
|
route='next_chapter',
|
||||||
callback=receive_ase_next_chapter,
|
callback=receive_ase_next_chapter,
|
||||||
entry=self,
|
entry=self,
|
||||||
init_data={'room': user_uuid}
|
init_data={'room':user_uuid}
|
||||||
)
|
)
|
||||||
user_uuid2ase_client[user_uuid].register_route_with_entry(
|
user_uuid2ase_client[user_uuid].register_route_with_entry(
|
||||||
route='chapter_score',
|
route='chapter_score',
|
||||||
@@ -286,4 +293,4 @@ class AgentNamespace(Namespace):
|
|||||||
leave_room(uuid, namespace='/agent')
|
leave_room(uuid, namespace='/agent')
|
||||||
|
|
||||||
self.app.logger.info("VSCode client disconnected")
|
self.app.logger.info("VSCode client disconnected")
|
||||||
self.app.logger.info(f"Disconnect reason: {str(data)}")
|
self.app.logger.info(f"Disconnect reason: {str(data)}")
|
||||||
@@ -66,25 +66,23 @@ function cleanupExpiredApprovals() {
|
|||||||
// 定时清理过期记录
|
// 定时清理过期记录
|
||||||
setInterval(cleanupExpiredApprovals, 3000); // 每3秒检查一次
|
setInterval(cleanupExpiredApprovals, 3000); // 每3秒检查一次
|
||||||
document.addEventListener('DOMContentLoaded', function () {
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
data = window.appData;
|
user_data = window.appData;
|
||||||
const host = window.location.host;
|
const host = window.location.host;
|
||||||
console.log(`Connecting to WebSocket server at wss://${host}/agent`);
|
console.log(`Connecting to WebSocket server at wss://${host}/agent`);
|
||||||
socket = io(`wss://${host}/agent`, {
|
socket = io(`wss://${host}/agent`, {
|
||||||
query: {
|
query: {
|
||||||
user_uuid: data.user_uuid,
|
user_uuid: user_data.user_uuid,
|
||||||
folder: data.folder
|
folder: user_data.folder
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 初始化聊天消息管理器
|
// 初始化聊天消息管理器
|
||||||
chatManager = new ChatMessageManager();
|
chatManager = new ChatMessageManager();
|
||||||
|
|
||||||
socket.on('connect', function () {
|
socket.on('connect', function () {
|
||||||
console.log('Connected to server');
|
console.log('Connected to WebSocket server'+socket.id+"with user"+JSON.stringify(user_data));
|
||||||
socket.emit('login',JSON.stringify(data));
|
socket.emit('login',JSON.stringify(user_data));
|
||||||
});
|
});
|
||||||
socket.on('open-iframe', function(data) {
|
socket.on('open-iframe', function() {
|
||||||
OpenIframe();
|
OpenIframe(user_data);
|
||||||
});
|
});
|
||||||
// 监听来自服务器的消息
|
// 监听来自服务器的消息
|
||||||
socket.on('voiceMessage', function (data) {
|
socket.on('voiceMessage', function (data) {
|
||||||
|
|||||||
@@ -208,7 +208,7 @@
|
|||||||
console.error('Error fetching session:', error);
|
console.error('Error fetching session:', error);
|
||||||
});
|
});
|
||||||
let lastiframe = null;
|
let lastiframe = null;
|
||||||
function OpenIframe(){
|
function OpenIframe(data){
|
||||||
// 等待1s再加载
|
// 等待1s再加载
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
// 在成功获取 session 后创建并插入 iframe
|
// 在成功获取 session 后创建并插入 iframe
|
||||||
|
|||||||
@@ -187,7 +187,7 @@
|
|||||||
|
|
||||||
|
|
||||||
let lastIframe = null;
|
let lastIframe = null;
|
||||||
function OpenIframe(){
|
function OpenIframe(data){
|
||||||
// 在成功获取 session 后创建并插入 iframe
|
// 在成功获取 session 后创建并插入 iframe
|
||||||
const iframe = document.createElement('iframe');
|
const iframe = document.createElement('iframe');
|
||||||
const div=document.getElementById('vscodeWeb')
|
const div=document.getElementById('vscodeWeb')
|
||||||
|
|||||||
@@ -91,38 +91,6 @@ def desktop(user_uuid, course_id, chapter_name, lesson_name):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
caddy_conf = f"""
|
|
||||||
handle_path /vsc{user_id}/* {{
|
|
||||||
reverse_proxy localhost:{code_server_port}
|
|
||||||
}}
|
|
||||||
"""
|
|
||||||
caddy_config_path = "/etc/caddy/Caddyfile"
|
|
||||||
with open(caddy_config_path, "r") as f:
|
|
||||||
lines = f.readlines()
|
|
||||||
pattern = re.compile(
|
|
||||||
r'handle_path\s+/vsc' + re.escape(user_id) + r'/\* {.*?reverse_proxy.*?}',
|
|
||||||
re.DOTALL
|
|
||||||
)
|
|
||||||
for i in range(len(lines)):
|
|
||||||
if (lines[i].strip()==''): lines[i]=''
|
|
||||||
content = "".join(lines)
|
|
||||||
if pattern.search(content):
|
|
||||||
new_content = pattern.sub('', content)
|
|
||||||
lines = new_content.splitlines(keepends=True)
|
|
||||||
if len(lines) >= 2:
|
|
||||||
insert_position = len(lines) - 2
|
|
||||||
else:
|
|
||||||
insert_position = len(lines)
|
|
||||||
if caddy_conf[-1] not in ['\n', '\r']:
|
|
||||||
caddy_conf += '\n'
|
|
||||||
lines.insert(insert_position, caddy_conf)
|
|
||||||
with open(caddy_config_path, "w") as f:
|
|
||||||
f.writelines(lines)
|
|
||||||
|
|
||||||
try:
|
|
||||||
subprocess.run(["sudo", "caddy", "reload", "--config", "/etc/caddy/Caddyfile"])
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
print(f"Error reload caddy {e}")
|
|
||||||
|
|
||||||
cfg = current_app.config["VSCODE_WEB_PATH"]
|
cfg = current_app.config["VSCODE_WEB_PATH"]
|
||||||
path_for_vscode = path_dir
|
path_for_vscode = path_dir
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
import eventlet
|
import eventlet
|
||||||
from eventlet import greenio
|
|
||||||
import logging
|
import logging
|
||||||
import traceback
|
|
||||||
# 获取当前 socketio 实例并配置错误处理
|
|
||||||
import sys
|
|
||||||
# 配置 eventlet 日志,抑制 IOClosed 等错误日志
|
# 配置 eventlet 日志,抑制 IOClosed 等错误日志
|
||||||
logging.getLogger('eventlet.hubs').setLevel(logging.CRITICAL)
|
logging.getLogger('eventlet.hubs').setLevel(logging.CRITICAL)
|
||||||
logging.getLogger('eventlet.greenio').setLevel(logging.CRITICAL)
|
logging.getLogger('eventlet.greenio').setLevel(logging.CRITICAL)
|
||||||
@@ -13,7 +10,11 @@ eventlet.monkey_patch()
|
|||||||
|
|
||||||
# 配置 eventlet 以更优雅地处理错误,避免疯狂报错
|
# 配置 eventlet 以更优雅地处理错误,避免疯狂报错
|
||||||
try:
|
try:
|
||||||
|
import eventlet
|
||||||
|
import traceback
|
||||||
|
|
||||||
# 1. 首先,禁用 eventlet 的调试日志,减少输出
|
# 1. 首先,禁用 eventlet 的调试日志,减少输出
|
||||||
|
import logging
|
||||||
logging.getLogger('eventlet').setLevel(logging.CRITICAL)
|
logging.getLogger('eventlet').setLevel(logging.CRITICAL)
|
||||||
logging.getLogger('socketio').setLevel(logging.CRITICAL)
|
logging.getLogger('socketio').setLevel(logging.CRITICAL)
|
||||||
logging.getLogger('werkzeug').setLevel(logging.CRITICAL)
|
logging.getLogger('werkzeug').setLevel(logging.CRITICAL)
|
||||||
@@ -40,12 +41,15 @@ try:
|
|||||||
# 4. 只修改关键的 greenio 方法,确保在致命错误时彻底关闭连接
|
# 4. 只修改关键的 greenio 方法,确保在致命错误时彻底关闭连接
|
||||||
# 增强 socketio 错误处理,确保不会因为单个连接错误而宕机
|
# 增强 socketio 错误处理,确保不会因为单个连接错误而宕机
|
||||||
try:
|
try:
|
||||||
|
from flask_socketio import SocketIO
|
||||||
|
# 获取当前 socketio 实例并配置错误处理
|
||||||
|
import sys
|
||||||
original_excepthook = sys.excepthook
|
original_excepthook = sys.excepthook
|
||||||
|
|
||||||
def custom_excepthook(exc_type, exc_value, exc_traceback):
|
def custom_excepthook(exc_type, exc_value, exc_traceback):
|
||||||
"""全局异常钩子,捕获所有未处理的异常"""
|
"""全局异常钩子,捕获所有未处理的异常"""
|
||||||
error_str = str(exc_value)
|
if exc_type.__name__ in ['OSError', 'IOError'] and 'Bad file descriptor' in str(exc_value):
|
||||||
if exc_type.__name__ in ['OSError', 'IOError'] and any(msg in error_str for msg in ['Bad file descriptor', 'Socket operation on non-socket', 'Operation on closed file']):
|
# 处理 Bad file descriptor 错误,不导致服务器宕机
|
||||||
# 处理各种socket错误,不导致服务器宕机
|
|
||||||
if not is_duplicate_error(exc_value):
|
if not is_duplicate_error(exc_value):
|
||||||
print(f"[Global Error Handler] {exc_type.__name__}: {exc_value}")
|
print(f"[Global Error Handler] {exc_type.__name__}: {exc_value}")
|
||||||
print("[Traceback]")
|
print("[Traceback]")
|
||||||
@@ -60,6 +64,7 @@ try:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
# 如果修改失败,忽略错误
|
# 如果修改失败,忽略错误
|
||||||
print(f"[Global Excepthook Patch Error] {e}")
|
print(f"[Global Excepthook Patch Error] {e}")
|
||||||
|
from eventlet import greenio
|
||||||
|
|
||||||
# 保存并替换原始的 greenio 方法
|
# 保存并替换原始的 greenio 方法
|
||||||
if hasattr(greenio.base.GreenSocket, '_recv_loop'):
|
if hasattr(greenio.base.GreenSocket, '_recv_loop'):
|
||||||
|
|||||||
@@ -3,12 +3,12 @@ from flask_socketio import SocketIO
|
|||||||
from .config import Config
|
from .config import Config
|
||||||
# 初始化扩展
|
# 初始化扩展
|
||||||
cors = CORS()
|
cors = CORS()
|
||||||
socketio = SocketIO(path='vsc-like-ws',cors_allowed_origins="*",max_payload_size=5242880, async_mode="eventlet") # 不直接传 app
|
socketio = SocketIO(path='vsc-like-ws',cors_allowed_origins="*",max_payload_size=5242880, async_mode="eventlet",always_connect=False) # 不直接传 app
|
||||||
|
|
||||||
session_paths = {}
|
session_paths = {}
|
||||||
def init_extensions(app):
|
def init_extensions(app):
|
||||||
cors.init_app(app, resources={r"/*": {"origins": "*"}}, supports_credentials=True)
|
cors.init_app(app, resources={r"/*": {"origins": "*"}}, supports_credentials=True)
|
||||||
socketio.init_app(app, cors_allowed_origins="*", max_payload_size=5242880, async_mode="eventlet")
|
socketio.init_app(app, cors_allowed_origins="*", max_payload_size=5242880, async_mode="eventlet",always_connect=False)
|
||||||
app.extensions["session_paths"] = session_paths
|
app.extensions["session_paths"] = session_paths
|
||||||
|
|
||||||
def register_namespaces(app):
|
def register_namespaces(app):
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import termios
|
|||||||
import struct
|
import struct
|
||||||
import fcntl
|
import fcntl
|
||||||
import psutil
|
import psutil
|
||||||
|
|
||||||
TERM_INIT_CONFIG = {
|
TERM_INIT_CONFIG = {
|
||||||
# instead of local server runnning this web terminal service
|
# instead of local server runnning this web terminal service
|
||||||
# "domain" is the target that you want to access through local server (with this web terminal)
|
# "domain" is the target that you want to access through local server (with this web terminal)
|
||||||
@@ -25,108 +24,9 @@ TERM_INIT_CONFIG = {
|
|||||||
'ssh': '/usr/bin/ssh'
|
'ssh': '/usr/bin/ssh'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
terminal_bp = Blueprint('terminal', __name__, url_prefix='/vsc-like')
|
terminal_bp = Blueprint('terminal', __name__, url_prefix='/vsc-like')
|
||||||
|
|
||||||
|
|
||||||
class TerminalSessionManager:
|
|
||||||
"""终端会话管理器,用于统一管理所有终端会话"""
|
|
||||||
def __init__(self):
|
|
||||||
# 存储所有终端会话,key为session_id,value为会话配置
|
|
||||||
self.sessions = {}
|
|
||||||
# 线程锁,保证多线程安全
|
|
||||||
self.lock = threading.Lock()
|
|
||||||
# 最大终端数量限制
|
|
||||||
self.max_terminals = 100
|
|
||||||
# 终端空闲超时时间(秒)
|
|
||||||
self.idle_timeout = 3600 # 1小时
|
|
||||||
|
|
||||||
def create_session(self):
|
|
||||||
"""创建新的终端会话"""
|
|
||||||
with self.lock:
|
|
||||||
# 检查是否达到最大终端数量
|
|
||||||
if len(self.sessions) >= self.max_terminals:
|
|
||||||
return None, "Maximum number of terminals reached"
|
|
||||||
|
|
||||||
# 生成唯一的会话ID和房间ID
|
|
||||||
session_id = str(uuid.uuid4())
|
|
||||||
room_id = f"terminal_{session_id}"
|
|
||||||
|
|
||||||
# 创建会话配置
|
|
||||||
session_config = {
|
|
||||||
**TERM_INIT_CONFIG.copy(),
|
|
||||||
'session_id': session_id,
|
|
||||||
'room_id': room_id,
|
|
||||||
'created_at': time.time(),
|
|
||||||
'last_activity': time.time()
|
|
||||||
}
|
|
||||||
|
|
||||||
# 保存会话
|
|
||||||
self.sessions[session_id] = session_config
|
|
||||||
|
|
||||||
return session_config, None
|
|
||||||
|
|
||||||
def get_session(self, session_id):
|
|
||||||
"""获取会话配置"""
|
|
||||||
with self.lock:
|
|
||||||
return self.sessions.get(session_id)
|
|
||||||
|
|
||||||
def update_session(self, session_id, updates):
|
|
||||||
"""更新会话配置"""
|
|
||||||
with self.lock:
|
|
||||||
if session_id not in self.sessions:
|
|
||||||
return False
|
|
||||||
|
|
||||||
# 更新会话配置
|
|
||||||
self.sessions[session_id].update(updates)
|
|
||||||
# 更新最后活动时间
|
|
||||||
self.sessions[session_id]['last_activity'] = time.time()
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
def delete_session(self, session_id):
|
|
||||||
"""删除会话"""
|
|
||||||
with self.lock:
|
|
||||||
if session_id in self.sessions:
|
|
||||||
del self.sessions[session_id]
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def get_session_by_room_id(self, room_id):
|
|
||||||
"""通过房间ID获取会话"""
|
|
||||||
with self.lock:
|
|
||||||
for session_config in self.sessions.values():
|
|
||||||
if session_config.get('room_id') == room_id:
|
|
||||||
return session_config
|
|
||||||
return None
|
|
||||||
|
|
||||||
def cleanup_inactive_sessions(self):
|
|
||||||
"""清理不活动的会话"""
|
|
||||||
with self.lock:
|
|
||||||
current_time = time.time()
|
|
||||||
expired_sessions = []
|
|
||||||
|
|
||||||
# 找出超时的会话
|
|
||||||
for session_id, session_config in self.sessions.items():
|
|
||||||
if current_time - session_config['last_activity'] > self.idle_timeout:
|
|
||||||
expired_sessions.append(session_id)
|
|
||||||
|
|
||||||
# 删除超时的会话
|
|
||||||
for session_id in expired_sessions:
|
|
||||||
del self.sessions[session_id]
|
|
||||||
|
|
||||||
return expired_sessions
|
|
||||||
|
|
||||||
def get_active_session_count(self):
|
|
||||||
"""获取当前活动会话数量"""
|
|
||||||
with self.lock:
|
|
||||||
return len(self.sessions)
|
|
||||||
|
|
||||||
|
|
||||||
# 创建全局终端会话管理器实例
|
|
||||||
terminal_manager = TerminalSessionManager()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def set_winsize(fd, row, col, xpix=0, ypix=0):
|
def set_winsize(fd, row, col, xpix=0, ypix=0):
|
||||||
try:
|
try:
|
||||||
@@ -223,46 +123,53 @@ def read_and_forward_pty_output(fd=None, pid=None, room_id=None, namespace=None)
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
class VSCLikeNameSpace(Namespace):
|
class VSCLikeNameSpace(Namespace):
|
||||||
def __init__(self, namespace=None):
|
def on_connect(self):
|
||||||
super().__init__(namespace)
|
"""new client connected"""
|
||||||
# 存储客户端socket_id到session_id的映射
|
# 确保terminal_config存在于session中
|
||||||
self.socket_session_map = {}
|
if 'terminal_config' not in session:
|
||||||
# 线程锁,保证多线程安全
|
session['terminal_config'] = TERM_INIT_CONFIG.copy()
|
||||||
self.lock = threading.Lock()
|
session.modified = True
|
||||||
|
|
||||||
def create_terminal(self, session_id):
|
|
||||||
"""创建新的终端并更新会话配置"""
|
|
||||||
# 获取会话配置
|
|
||||||
terminal_config = terminal_manager.get_session(session_id)
|
|
||||||
if not terminal_config:
|
|
||||||
current_app.logger.error(f"Session not found: {session_id}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
room_id = terminal_config['room_id']
|
terminal_config = session['terminal_config']
|
||||||
current_app.logger.debug(f"Creating new terminal for room: {room_id}")
|
|
||||||
|
|
||||||
|
# 检查是否已经有运行中的子进程
|
||||||
|
if terminal_config.get('child_pid'):
|
||||||
|
try:
|
||||||
|
# 验证进程是否真的存在
|
||||||
|
child_process = psutil.Process(terminal_config['child_pid'])
|
||||||
|
if child_process.status() in ('running', 'sleeping'):
|
||||||
|
current_app.logger.debug("Already running child process: {}".format(terminal_config['child_pid']))
|
||||||
|
return
|
||||||
|
except psutil.NoSuchProcess:
|
||||||
|
# 进程不存在,清理配置
|
||||||
|
terminal_config.pop('child_pid', None)
|
||||||
|
terminal_config.pop('fd', None)
|
||||||
|
session.modified = True
|
||||||
|
|
||||||
# create child process attached to a pty we can read from and write to
|
# create child process attached to a pty we can read from and write to
|
||||||
(child_pid, fd) = pty.fork()
|
(child_pid, fd) = pty.fork()
|
||||||
|
lesson_path = session.get('path')
|
||||||
if child_pid == 0:
|
if child_pid == 0:
|
||||||
# this is the child process fork.
|
# this is the child process fork.
|
||||||
# anything printed here will show up in the pty, including the output
|
# anything printed here will show up in the pty, including the output
|
||||||
# of this subprocess
|
# of this subprocess
|
||||||
try:
|
try:
|
||||||
# 获取终端配置
|
# 获取终端配置
|
||||||
|
terminal_config = session.get('terminal_config', {})
|
||||||
term_type = terminal_config.get('term_type')
|
term_type = terminal_config.get('term_type')
|
||||||
|
|
||||||
if not term_type:
|
if not term_type:
|
||||||
print("Terminal type not specified, exit")
|
print("Terminal type not specified, exit")
|
||||||
os._exit(1)
|
os._exit(1)
|
||||||
|
|
||||||
path = terminal_config.get('client_path', {}).get(term_type, None)
|
path = TERM_INIT_CONFIG.get('client_path', {}).get(term_type, None)
|
||||||
if not path or not os.path.exists(path):
|
if not path or not os.path.exists(path):
|
||||||
print(f"Can't locate {term_type} binary at {path}, exit")
|
print("Can't locate {} binary at {}, exit".format(term_type, path))
|
||||||
os._exit(1)
|
os._exit(1)
|
||||||
|
|
||||||
# 获取连接参数
|
# 获取连接参数
|
||||||
username = terminal_config.get('username')
|
username = terminal_config.get('username')
|
||||||
domain = terminal_config.get('domain')
|
domain = terminal_config.get('domain', TERM_INIT_CONFIG['domain'])
|
||||||
port = terminal_config.get('port')
|
port = terminal_config.get('port')
|
||||||
|
|
||||||
if not username or not domain:
|
if not username or not domain:
|
||||||
@@ -278,70 +185,34 @@ class VSCLikeNameSpace(Namespace):
|
|||||||
elif term_type == 'ssh':
|
elif term_type == 'ssh':
|
||||||
# 使用ssh连接
|
# 使用ssh连接
|
||||||
ssh_port = port if port else 22 # 默认端口22
|
ssh_port = port if port else 22 # 默认端口22
|
||||||
os.execl(path, 'ssh', '-p', str(ssh_port), f'{username}@{domain}')
|
os.execl(path, 'ssh', '-p', str(ssh_port), '{}@{}'.format(username, domain))
|
||||||
else:
|
else:
|
||||||
print(f"Wrong term type {term_type}, exit")
|
print("Wrong term type {}, exit".format(term_type))
|
||||||
os._exit(1)
|
os._exit(1)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error in child process: {e}")
|
print("Error in child process: {}", str(e))
|
||||||
os._exit(1)
|
os._exit(1)
|
||||||
else:
|
else:
|
||||||
# 更新会话配置,保存到终端管理器
|
# 更新会话配置
|
||||||
updates = {
|
terminal_config['fd'] = fd
|
||||||
'fd': fd,
|
terminal_config['child_pid'] = child_pid
|
||||||
'child_pid': child_pid
|
terminal_config['room_id'] = rooms()[0]
|
||||||
}
|
session.modified = True
|
||||||
terminal_manager.update_session(session_id, updates)
|
|
||||||
|
|
||||||
# 设置初始窗口大小
|
# 设置初始窗口大小
|
||||||
set_winsize(fd, 50, 50)
|
set_winsize(fd, 50, 50)
|
||||||
|
|
||||||
# 记录日志
|
# 记录日志
|
||||||
current_app.logger.debug(f"Child process created: {child_pid} for room: {room_id}")
|
current_app.logger.debug("child pid = {}".format(child_pid))
|
||||||
|
current_app.logger.debug("rooms of this session = {}".format(rooms()))
|
||||||
|
|
||||||
# 启动后台任务读取pty输出,传入正确的room_id
|
# 启动后台任务读取pty输出
|
||||||
socketio.start_background_task(read_and_forward_pty_output, fd, child_pid, room_id, self)
|
socketio.start_background_task(read_and_forward_pty_output, fd, child_pid, rooms()[0], self)
|
||||||
current_app.logger.debug("Background task running")
|
current_app.logger.debug("background task running")
|
||||||
|
|
||||||
return terminal_config
|
|
||||||
|
|
||||||
def on_connect(self):
|
|
||||||
"""new client connected"""
|
|
||||||
# 创建新的终端会话
|
|
||||||
terminal_config, error = terminal_manager.create_session()
|
|
||||||
if not terminal_config:
|
|
||||||
current_app.logger.error(f"Failed to create terminal session: {error}")
|
|
||||||
return
|
|
||||||
|
|
||||||
session_id = terminal_config['session_id']
|
|
||||||
room_id = terminal_config['room_id']
|
|
||||||
|
|
||||||
# 加入房间
|
|
||||||
join_room(room_id)
|
|
||||||
|
|
||||||
# 保存socket_id到session_id的映射
|
|
||||||
socket_id = request.sid
|
|
||||||
with self.lock:
|
|
||||||
self.socket_session_map[socket_id] = session_id
|
|
||||||
|
|
||||||
current_app.logger.debug(f"Client connected, session_id: {session_id}, room_id: {room_id}, socket_id: {socket_id}")
|
|
||||||
|
|
||||||
# 创建终端
|
|
||||||
self.create_terminal(session_id)
|
|
||||||
current_app.logger.debug("background task running")
|
|
||||||
|
|
||||||
def on_pty_input(self, data):
|
def on_pty_input(self, data):
|
||||||
"""write to the child pty, which now is the ssh process from this machine to the 'domain' configured
|
"""write to the child pty, which now is the ssh process from this machine to the 'domain' configured
|
||||||
"""
|
"""
|
||||||
# 获取socket_id对应的session_id
|
|
||||||
socket_id = request.sid
|
|
||||||
with self.lock:
|
|
||||||
session_id = self.socket_session_map.get(socket_id)
|
|
||||||
|
|
||||||
if not session_id:
|
|
||||||
current_app.logger.error(f"No session found for socket: {socket_id}")
|
|
||||||
return
|
|
||||||
|
|
||||||
# 输入验证
|
# 输入验证
|
||||||
if not isinstance(data, dict):
|
if not isinstance(data, dict):
|
||||||
current_app.logger.error("Invalid input format: expected dictionary")
|
current_app.logger.error("Invalid input format: expected dictionary")
|
||||||
@@ -358,77 +229,41 @@ class VSCLikeNameSpace(Namespace):
|
|||||||
current_app.logger.warning("Input truncated due to length")
|
current_app.logger.warning("Input truncated due to length")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
terminal_config = terminal_manager.get_session(session_id)
|
terminal_config = session.get('terminal_config', {})
|
||||||
if not terminal_config:
|
child_pid = terminal_config.get('child_pid')
|
||||||
current_app.logger.error(f"Session not found: {session_id}")
|
if not child_pid:
|
||||||
|
current_app.logger.error("No child process found")
|
||||||
return
|
return
|
||||||
|
|
||||||
child_pid = terminal_config.get('child_pid')
|
child_process = psutil.Process(child_pid)
|
||||||
|
if child_process.status() not in ('running', 'sleeping'):
|
||||||
|
current_app.logger.debug("Child process not running, cleaning up")
|
||||||
|
terminal_config.pop('child_pid', None)
|
||||||
|
terminal_config.pop('fd', None)
|
||||||
|
session.modified = True
|
||||||
|
return
|
||||||
|
|
||||||
# 如果没有子进程,重新创建终端
|
|
||||||
if not child_pid:
|
|
||||||
current_app.logger.debug(f"No child process found for session: {session_id}, creating new terminal")
|
|
||||||
terminal_config = self.create_terminal(session_id)
|
|
||||||
if not terminal_config:
|
|
||||||
current_app.logger.error(f"Failed to create new terminal for session: {session_id}")
|
|
||||||
return
|
|
||||||
child_pid = terminal_config.get('child_pid')
|
|
||||||
if not child_pid:
|
|
||||||
current_app.logger.error(f"Failed to get child_pid for session: {session_id}")
|
|
||||||
return
|
|
||||||
|
|
||||||
# 检查子进程是否存在且运行中
|
|
||||||
try:
|
|
||||||
child_process = psutil.Process(child_pid)
|
|
||||||
if child_process.status() not in ('running', 'sleeping'):
|
|
||||||
current_app.logger.debug(f"Child process not running for session: {session_id}, creating new terminal")
|
|
||||||
terminal_config = self.create_terminal(session_id)
|
|
||||||
if not terminal_config:
|
|
||||||
current_app.logger.error(f"Failed to create new terminal for session: {session_id}")
|
|
||||||
return
|
|
||||||
child_pid = terminal_config.get('child_pid')
|
|
||||||
if not child_pid:
|
|
||||||
current_app.logger.error(f"Failed to get child_pid for session: {session_id}")
|
|
||||||
return
|
|
||||||
except psutil.NoSuchProcess:
|
|
||||||
current_app.logger.debug(f"Child process not found for session: {session_id}, creating new terminal")
|
|
||||||
terminal_config = self.create_terminal(session_id)
|
|
||||||
if not terminal_config:
|
|
||||||
current_app.logger.error(f"Failed to create new terminal for session: {session_id}")
|
|
||||||
return
|
|
||||||
child_pid = terminal_config.get('child_pid')
|
|
||||||
if not child_pid:
|
|
||||||
current_app.logger.error(f"Failed to get child_pid for session: {session_id}")
|
|
||||||
return
|
|
||||||
|
|
||||||
# 获取文件描述符并写入数据
|
|
||||||
fd = terminal_config.get('fd')
|
fd = terminal_config.get('fd')
|
||||||
if fd:
|
if fd:
|
||||||
try:
|
try:
|
||||||
os.write(fd, input_data.encode())
|
os.write(fd, input_data.encode())
|
||||||
except (OSError, IOError) as e:
|
except (OSError, IOError) as e:
|
||||||
# File descriptor closed or invalid, create new terminal
|
# File descriptor closed or invalid, clean up
|
||||||
current_app.logger.debug(f"Error writing to file descriptor: {e}, creating new terminal for session: {session_id}")
|
current_app.logger.debug(f"Error writing to file descriptor: {e}")
|
||||||
self.create_terminal(session_id)
|
terminal_config.pop('child_pid', None)
|
||||||
|
terminal_config.pop('fd', None)
|
||||||
|
session.modified = True
|
||||||
|
except psutil.NoSuchProcess:
|
||||||
|
# Process no longer exists, clean up
|
||||||
|
terminal_config = session.get('terminal_config', {})
|
||||||
|
terminal_config.pop('child_pid', None)
|
||||||
|
terminal_config.pop('fd', None)
|
||||||
|
session.modified = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
current_app.logger.error(f"Error in pty_input handling: {e}")
|
current_app.logger.error(f"Error in pty_input handling: {e}")
|
||||||
# 发生异常时尝试重新创建终端
|
|
||||||
try:
|
|
||||||
self.create_terminal(session_id)
|
|
||||||
except Exception as create_err:
|
|
||||||
current_app.logger.error(f"Failed to recreate terminal: {create_err}")
|
|
||||||
|
|
||||||
|
|
||||||
def on_resize(self, data):
|
def on_resize(self, data):
|
||||||
# 获取socket_id对应的session_id
|
|
||||||
socket_id = request.sid
|
|
||||||
with self.lock:
|
|
||||||
session_id = self.socket_session_map.get(socket_id)
|
|
||||||
|
|
||||||
if not session_id:
|
|
||||||
current_app.logger.error(f"No session found for socket: {socket_id}")
|
|
||||||
return
|
|
||||||
|
|
||||||
# 输入验证
|
# 输入验证
|
||||||
if not isinstance(data, dict):
|
if not isinstance(data, dict):
|
||||||
current_app.logger.error("Invalid resize data format: expected dictionary")
|
current_app.logger.error("Invalid resize data format: expected dictionary")
|
||||||
@@ -448,101 +283,53 @@ class VSCLikeNameSpace(Namespace):
|
|||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
terminal_config = terminal_manager.get_session(session_id)
|
terminal_config = session.get('terminal_config', {})
|
||||||
if not terminal_config:
|
child_pid = terminal_config.get('child_pid')
|
||||||
current_app.logger.error(f"Session not found: {session_id}")
|
if not child_pid:
|
||||||
|
current_app.logger.error("No child process found for resize")
|
||||||
return
|
return
|
||||||
|
|
||||||
child_pid = terminal_config.get('child_pid')
|
|
||||||
|
|
||||||
# 如果没有子进程,重新创建终端
|
|
||||||
if not child_pid:
|
|
||||||
current_app.logger.debug(f"No child process found for resize, creating new terminal for session: {session_id}")
|
|
||||||
terminal_config = self.create_terminal(session_id)
|
|
||||||
if not terminal_config:
|
|
||||||
current_app.logger.error(f"Failed to create new terminal for resize, session: {session_id}")
|
|
||||||
return
|
|
||||||
child_pid = terminal_config.get('child_pid')
|
|
||||||
if not child_pid:
|
|
||||||
current_app.logger.error(f"Failed to get child_pid for resize, session: {session_id}")
|
|
||||||
return
|
|
||||||
|
|
||||||
# 检查子进程是否存在且运行中
|
# 检查子进程是否存在且运行中
|
||||||
try:
|
child_process = psutil.Process(child_pid)
|
||||||
child_process = psutil.Process(child_pid)
|
if child_process.status() not in ('running', 'sleeping'):
|
||||||
if child_process.status() not in ('running', 'sleeping'):
|
current_app.logger.debug("Child process not running, cleaning up resize")
|
||||||
current_app.logger.debug(f"Child process not running for resize, creating new terminal for session: {session_id}")
|
terminal_config.pop('child_pid', None)
|
||||||
terminal_config = self.create_terminal(session_id)
|
terminal_config.pop('fd', None)
|
||||||
if not terminal_config:
|
session.modified = True
|
||||||
current_app.logger.error(f"Failed to create new terminal for resize, session: {session_id}")
|
return
|
||||||
return
|
|
||||||
child_pid = terminal_config.get('child_pid')
|
|
||||||
if not child_pid:
|
|
||||||
current_app.logger.error(f"Failed to get child_pid for resize, session: {session_id}")
|
|
||||||
return
|
|
||||||
except psutil.NoSuchProcess:
|
|
||||||
current_app.logger.debug(f"Child process not found for resize, creating new terminal for session: {session_id}")
|
|
||||||
terminal_config = self.create_terminal(session_id)
|
|
||||||
if not terminal_config:
|
|
||||||
current_app.logger.error(f"Failed to create new terminal for resize, session: {session_id}")
|
|
||||||
return
|
|
||||||
child_pid = terminal_config.get('child_pid')
|
|
||||||
if not child_pid:
|
|
||||||
current_app.logger.error(f"Failed to get child_pid for resize, session: {session_id}")
|
|
||||||
return
|
|
||||||
|
|
||||||
# 获取文件描述符并调整窗口大小
|
|
||||||
fd = terminal_config.get('fd')
|
fd = terminal_config.get('fd')
|
||||||
if fd:
|
if fd:
|
||||||
|
# 检查文件描述符是否有效
|
||||||
try:
|
try:
|
||||||
os.fstat(fd)
|
os.fstat(fd)
|
||||||
set_winsize(fd, rows, cols)
|
set_winsize(fd, rows, cols)
|
||||||
except (OSError, IOError) as e:
|
except (OSError, IOError) as e:
|
||||||
# 文件描述符无效,创建新终端
|
# 文件描述符无效,清理资源
|
||||||
current_app.logger.debug(f"Error resizing terminal: {e}, creating new terminal for session: {session_id}")
|
current_app.logger.debug(f"Error resizing terminal: {e}")
|
||||||
self.create_terminal(session_id)
|
terminal_config.pop('child_pid', None)
|
||||||
|
terminal_config.pop('fd', None)
|
||||||
|
session.modified = True
|
||||||
|
except psutil.NoSuchProcess:
|
||||||
|
# 进程不存在,清理资源
|
||||||
|
terminal_config = session.get('terminal_config', {})
|
||||||
|
terminal_config.pop('child_pid', None)
|
||||||
|
terminal_config.pop('fd', None)
|
||||||
|
session.modified = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
current_app.logger.error(f"Error in resize handling: {e}")
|
current_app.logger.error(f"Error in resize handling: {e}")
|
||||||
# 发生异常时尝试重新创建终端
|
|
||||||
try:
|
|
||||||
self.create_terminal(session_id)
|
|
||||||
except Exception as create_err:
|
|
||||||
current_app.logger.error(f"Failed to recreate terminal for resize: {create_err}")
|
|
||||||
|
|
||||||
def on_disconnect(self):
|
def on_disconnect(self):
|
||||||
# 获取socket_id对应的session_id
|
terminal_config = session.get('terminal_config', {})
|
||||||
socket_id = request.sid
|
|
||||||
with self.lock:
|
|
||||||
session_id = self.socket_session_map.pop(socket_id, None)
|
|
||||||
|
|
||||||
if not session_id:
|
|
||||||
current_app.logger.error(f"No session found for socket: {socket_id}")
|
|
||||||
return
|
|
||||||
|
|
||||||
# 获取会话配置
|
|
||||||
terminal_config = terminal_manager.get_session(session_id)
|
|
||||||
if not terminal_config:
|
|
||||||
current_app.logger.error(f"Session not found: {session_id}")
|
|
||||||
return
|
|
||||||
|
|
||||||
child_pid = terminal_config.get('child_pid')
|
child_pid = terminal_config.get('child_pid')
|
||||||
fd = terminal_config.get('fd')
|
fd = terminal_config.get('fd')
|
||||||
room_id = terminal_config.get('room_id')
|
|
||||||
|
|
||||||
current_app.logger.debug(f"Client disconnecting, session_id: {session_id}, room_id: {room_id}, socket_id: {socket_id}")
|
|
||||||
|
|
||||||
# 退出房间
|
|
||||||
if room_id:
|
|
||||||
leave_room(room_id)
|
|
||||||
current_app.logger.debug(f"Client left room: {room_id}")
|
|
||||||
|
|
||||||
# 先关闭文件描述符,避免其他操作尝试使用无效的文件描述符
|
# 先关闭文件描述符,避免其他操作尝试使用无效的文件描述符
|
||||||
if fd:
|
if fd:
|
||||||
try:
|
try:
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
current_app.logger.debug(f"Closed file descriptor: {fd}")
|
except Exception:
|
||||||
except Exception as e:
|
pass
|
||||||
current_app.logger.debug(f"Error closing file descriptor: {e}")
|
|
||||||
|
|
||||||
if child_pid:
|
if child_pid:
|
||||||
try:
|
try:
|
||||||
@@ -553,28 +340,23 @@ class VSCLikeNameSpace(Namespace):
|
|||||||
child_process.terminate()
|
child_process.terminate()
|
||||||
# Wait for the process to terminate and collect its exit status
|
# Wait for the process to terminate and collect its exit status
|
||||||
child_process.wait(timeout=2)
|
child_process.wait(timeout=2)
|
||||||
current_app.logger.debug(f'User left pty alone, terminated and waited: {child_pid}')
|
current_app.logger.debug('user left the pty alone, terminated and waited')
|
||||||
except psutil.NoSuchProcess:
|
except psutil.NoSuchProcess as err:
|
||||||
# Process already terminated, try to wait anyway to clean up any zombie
|
# Process already terminated, try to wait anyway to clean up any zombie
|
||||||
try:
|
try:
|
||||||
os.waitpid(child_pid, os.WNOHANG)
|
os.waitpid(child_pid, os.WNOHANG)
|
||||||
current_app.logger.debug(f'Cleaned up zombie process: {child_pid}')
|
except Exception:
|
||||||
except Exception as e:
|
pass
|
||||||
current_app.logger.debug(f"Error cleaning up zombie process: {e}")
|
|
||||||
except psutil.TimeoutExpired:
|
except psutil.TimeoutExpired:
|
||||||
# If process didn't terminate in time, kill it forcefully
|
# If process didn't terminate in time, kill it forcefully
|
||||||
try:
|
try:
|
||||||
child_process.kill()
|
child_process.kill()
|
||||||
child_process.wait(timeout=1)
|
child_process.wait(timeout=1)
|
||||||
current_app.logger.debug(f'Forcefully killed process: {child_pid}')
|
except Exception:
|
||||||
except Exception as e:
|
pass
|
||||||
current_app.logger.debug(f"Error forcefully killing process: {e}")
|
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
current_app.logger.error(f'Error terminating process: {err}')
|
current_app.logger.error(f'Error terminating process: {err}')
|
||||||
|
|
||||||
# 从终端管理器中删除会话
|
# Reset session config
|
||||||
terminal_manager.delete_session(session_id)
|
session['terminal_config'] = TERM_INIT_CONFIG
|
||||||
current_app.logger.debug(f"Session deleted: {session_id}")
|
|
||||||
|
|
||||||
current_app.logger.debug(f"Client disconnected, session_id: {session_id}, room_id: {room_id}")
|
|
||||||
current_app.logger.debug('Client disconnected')
|
current_app.logger.debug('Client disconnected')
|
||||||
Reference in New Issue
Block a user