Files
hsa/code-server-like/app/views/routes.py

83 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import subprocess
import eventlet
from flask import Blueprint, request, jsonify, current_app, render_template, session
from ..services.file_service import get_file_tree, load_config
# 禁用Eventlet的同时读取检测解决subprocess.run()的问题
eventlet.debug.hub_prevent_multiple_readers(False)
main_bp = Blueprint('main', __name__, url_prefix='/vsc-like')
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': '127.0.0.1', # 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)
session['uuid'] = uuid
session['path'] = path #记录本节课的根目录
session['user_id'] = user_id
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
# if config.get('uuid') != uuid:
# return jsonify({"error": "UUID does not match"}), 403
user_root = os.path.join(base_path, user_id)
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)}")
try:#使用sudo创建shared_group
subprocess.run(["sudo", "groupadd", f"shared_group_{user_id}"], check=True)
except subprocess.CalledProcessError as e:
print(f"Error creating shared_group: {e}")
try:#使用sudo将user_id加入shared_group
subprocess.run(["sudo", "usermod", "-a", "-G", f"shared_group_{user_id}", user_id], check=True)
# 自己也加入
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}", path], check=True)
subprocess.run(["sudo", "chmod", "-R", "755", user_root], check=True)
subprocess.run(["sudo", "chmod", "-R", "775", 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', course_id=course_id, chapter_name=chapter_name, lesson_name=lesson_name, code_like_config=config)