Spaces:
Paused
Paused
| import os | |
| import json | |
| import tornado.web | |
| import subprocess | |
| # New handler for your custom page | |
| class AppHandler(tornado.web.RequestHandler): | |
| HandlerPath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'WebSSH', 'templates') | |
| template_folder = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'WebSSH', 'templates') | |
| def get(self): | |
| print(self.request) | |
| self.render(os.path.join(self.template_folder, 'index.html')) | |
| def function_two(self): | |
| # Your logic for function one | |
| self.write("Function One Called") | |
| def post(self): | |
| #data = json.loads(self.body) | |
| username = self.get_argument("username") | |
| password = self.get_argument("password") | |
| print(f"Username: {username}, Password: {password}") | |
| if not username or not password: | |
| self.set_status(400) | |
| self.write("Username and password are required.") | |
| return | |
| # Check if the user already exists | |
| user_exists = subprocess.run(["id", "-u", username], capture_output=True) | |
| if user_exists.returncode == 0: | |
| # User exists, read the existing SSH key | |
| ssh_dir = f"/home/{username}/.ssh" | |
| private_key_path = f"{ssh_dir}/id_rsa" | |
| if os.path.exists(private_key_path): | |
| with open(private_key_path, "r") as file: | |
| private_key = file.read() | |
| else: | |
| #self.set_status(404) | |
| #self.write("SSH key not found for existing user.") | |
| return {response: 404, message: "SSH key not found for existing user."} | |
| else: | |
| #self.set_status(404) | |
| #self.write("User not found.") | |
| return {response: 404, message:"User not found."} | |
| # Return the private key to the user | |
| self.set_header('Content-Type', 'application/octet-stream') | |
| self.set_header('Content-Disposition', f'attachment; filename=id_rsa') | |
| self.write(private_key) | |
| """ | |
| def run_commands(self): | |
| data = json.loads(self.body) | |
| command = data.get("command") | |
| print(f"Command: {command}") | |
| if command: | |
| try: | |
| result = subprocess.run(command, shell=True, capture_output=True, text=True) | |
| print(f"Result: {result}") | |
| return(result.stdout) | |
| except Exception as e: | |
| return(str(e)) | |
| else: | |
| return("No command provided") | |
| """ |