code-server-liek

This commit is contained in:
Cai
2025-09-17 07:46:38 +00:00
parent 50a570eccf
commit 025c8c1a20
12 changed files with 797 additions and 1511 deletions

View File

@@ -4,9 +4,18 @@ from flask import Blueprint, request, jsonify, current_app, render_template, ses
from ..services.file_service import get_file_tree, load_config
main_bp = Blueprint('main', __name__, url_prefix='/vsc-like')
@main_bp.route('/<user_uuid>/<user_id>/<course_id>/<chapter_name>/<lesson_name>', methods=['GET'])
def vsc_like(user_uuid, user_id, course_id, chapter_name, lesson_name):
TERM_INIT_CONFIG = {
# 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)
# and before doing so - make sure you have username and port (on the "domain") to implement remote access
'domain': '15.168.14.27', # or ip address like 192.168.10.11
'client_path': {
'telnet': '/usr/bin/telnet', # confirmed location of your client binary (with cmd like 'which telnet')
'ssh': '/usr/bin/ssh'
}
}
@main_bp.route('/<user_uuid>/<user_id>/<course_id>/<chapter_name>/<lesson_name>/<port>', methods=['GET'])
def vsc_like(user_uuid, user_id, course_id, chapter_name, lesson_name, port):
uuid = user_uuid
base_path = current_app.config['ROOT_WORKSPACE_PATH']
path = os.path.join(base_path, user_id, course_id, chapter_name, lesson_name)
@@ -16,6 +25,11 @@ def vsc_like(user_uuid, user_id, course_id, chapter_name, lesson_name):
session['course_id'] = course_id
session['chapter_name'] = chapter_name
session['lesson_name'] = lesson_name
session['terminal_config'] = TERM_INIT_CONFIG
session['terminal_config']['term_type'] = 'ssh'
session['terminal_config']['username'] = user_id
session['terminal_config']['port'] = port
session.modified = True
config = load_config(path)
# if not config:
# return jsonify({"error": "Configuration file not found or invalid"}), 404
@@ -26,11 +40,17 @@ def vsc_like(user_uuid, user_id, course_id, chapter_name, lesson_name):
try:
subprocess.run(["sudo", "useradd", "-m", "-d", user_root, user_id]) # 创建用户
subprocess.run(["sudo", "chown", "-R", user_id, user_root]) # 设置目录权限为新用户
except subprocess.CalledProcessError as e:
print(f"Error creating user {user_id}: {e}")
try:
# 使用 sudo 执行 mkdir 命令来创建目录
subprocess.run(["sudo", "-u", user_id, "mkdir", "-p", path], check=True)
subprocess.run(["sudo", "-u", user_id, "mkdir", "-p", f'{user_root}/.ssh'], check=True)
flask_pub_key = "/home/flask/.ssh/id_ed25519.pub"
subprocess.run(["sudo", "cp", flask_pub_key, f"{user_root}/.ssh/authorized_keys"], check=True)
subprocess.run(["sudo", "chmod", "700", f"{user_root}/.ssh"], check=True)
subprocess.run(["sudo", "chmod", "600", f"{user_root}/.ssh/authorized_keys"], check=True)
print(f"Directory {path} created successfully for user {user_id}")
except subprocess.CalledProcessError as e:
print(f"Error creating directory {path} details {str(e)}")
@@ -44,17 +64,15 @@ def vsc_like(user_uuid, user_id, course_id, chapter_name, lesson_name):
subprocess.run(["sudo", "usermod", "-a", "-G", f"shared_group_{user_id}", 'flask'], check=True)
except subprocess.CalledProcessError as e:
print(f"Error adding user {user_id} to shared_group: {e}")
try:#将文件
subprocess.run(["sudo", "chown", "-R", f"flask:shared_group_{user_id}", user_root], check=True)
try:
subprocess.run(["sudo", "chown", "-R", f"flask:shared_group_{user_id}", path], check=True)
subprocess.run(["sudo", "chmod", "-R", "750", user_root], check=True)
subprocess.run(["sudo", "chmod", "-R", "770", path], check=True)
except subprocess.CalledProcessError as e:
print(f"Error changing directory {path} to shared_group_{user_id}: {e}")
try:#继承目录
subprocess.run(["sudo", "chmod", "g+s", path], check=True)
except subprocess.CalledProcessError as e:
print(f"Error changing directory {path} to shared_group_{user_id}: {e}")
return render_template('index.html')
return render_template('index.html', course_id=course_id, chapter_name=chapter_name, lesson_name=lesson_name)