File size: 2,520 Bytes
e80ff99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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")
"""