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

@@ -12,8 +12,7 @@ def create_folder():
filepath = request.json.get('path', '')
path = session.get('path')
parent = request.json.get('parent', '')
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
base_path = f'{root_workspace_path}/{path}'
base_path = f'{path}'
filepath = f'{base_path}/{parent}/{filepath}'
try:
os.makedirs(filepath)
@@ -27,8 +26,7 @@ def create_file():
filepath = request.json.get('path', '')
path = session.get('path')
parent = request.json.get('parent', '')
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
base_path = f'{root_workspace_path}/{path}'
base_path = f'{path}'
filepath = f'{base_path}/{parent}/{filepath}'
try:
open(filepath, 'w').close()
@@ -41,8 +39,7 @@ def rename_file():
user_root = session.get('path')
path = request.json.get('path', '')
new_name = request.json.get('newName', '')
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
base_path = f'{root_workspace_path}/{user_root}'
base_path = f'{user_root}'
filepath = f'{base_path}/{path}'
try:
path_list = filepath.split('/')
@@ -58,8 +55,7 @@ def rename_folder():
user_root = session.get('path')
path = request.json.get('path', '')
new_name = request.json.get('newName', '')
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
base_path = f'{root_workspace_path}/{user_root}'
base_path = f'{user_root}'
folderpath = f'{base_path}/{path}'
try:
path_list = folderpath.split('/')
@@ -74,9 +70,8 @@ def rename_folder():
def copy_files():
old_path = request.json.get('oldPath', '')
new_path = request.json.get('newPath', '')
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
user_root = session.get('path')
base_path = f'{root_workspace_path}/{user_root}'
base_path = f'{user_root}'
old_path = f'{base_path}/{old_path}'
new_path = f'{base_path}/{new_path}'
# oldpath的父目录就是newpath的话,对oldpath进行重命名
@@ -106,9 +101,8 @@ def copy_files():
@file_bp.route('/delete-file', methods=['POST'])
def delete_file():
path = request.json.get('path', '')
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
user_root = session.get('path')
base_path = f'{root_workspace_path}/{user_root}'
base_path = f'{user_root}'
filepath = f'{base_path}/{path}'
if os.path.isdir(filepath):
shutil.rmtree(filepath)
@@ -119,8 +113,7 @@ def delete_file():
@file_bp.route('/file-content', methods=['POST'])
def file_content():
path = session.get('path')
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
base_path = f'{root_workspace_path}/{path}'
base_path = f'{path}'
filepath = request.json.get('path', '')
filepath = f'{base_path}/{filepath}'
if not os.path.exists(filepath):
@@ -135,8 +128,7 @@ def file_content():
@file_bp.route('/save-file', methods=['POST'])
def save_file():
path = session.get('path')
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
base_path = f'{root_workspace_path}/{path}'
base_path = f'{path}'
filepath = request.json.get('path', '')
filepath = f'{base_path}/{filepath}'
content = request.json.get('content', '')

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)