/
qoopdata
/
IT-Planet_Pro_SberLinux
Обзор
Документация
Войти
/
qoopdata
/
IT-Planet_Pro_SberLinux
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
ssh_vt/main.py
80 строк
2 KB
Kirill Bezuglyi
solutions
17 ноя 2024, 23:43
17 ноя 2024, 23:43
2b02cf3
Код
Авторство
О чём код?
import os import pty import subprocess import crypt import docker import threading import resource from hashlib import sha256 # Global storage for user credentials USER_DB = { # Pre-hashed password for "password123" "testuser": sha256("password123".encode()).hexdigest(), } def hash_password(password): """Hash the password securely.""" return sha256(password.encode()).hexdigest() def authenticate(username, password): """Authenticate a user.""" return USER_DB.get(username) == hash_password(password) def create_isolated_container(username): """Create an isolated Docker container for the user.""" client = docker.from_env() container = client.containers.run( image="debian:latest", # Base Linux image command="/bin/bash", stdin_open=True, tty=True, detach=True, mem_limit="256m", # Limit memory cpu_quota=50000, # Limit CPU (50ms every 100ms) name=f"session_{username}", hostname=f"{username}_host", ) return container def execute_command(container, command): """Execute a command inside the user's container.""" exec_id = container.client.api.exec_create(container.id, command) output = container.client.api.exec_start(exec_id) return output.decode() def handle_session(username): """Handle a user session.""" try: container = create_isolated_container(username) print(f"Connected to session for {username}. Type 'exit' to quit.") while True: try: cmd = input(f"{username}@virtual-terminal> ") if cmd.strip() == "exit": print("Exiting session.") break output = execute_command(container, cmd) print(output) except Exception as e: print(f"Error executing command: {e}") finally: if container: container.remove(force=True) print(f"Session for {username} cleaned up.") def main(): """Main function to start the terminal.""" print("Welcome to the Virtual Terminal.") username = input("Username: ") password = input("Password: ") if not authenticate(username, password): print("Authentication failed!") return print(f"Authentication successful. Starting session for {username}...") handle_session(username) if __name__ == "__main__": main()