BinaryONe commited on
Commit
4ce8c1f
·
1 Parent(s): fc3c17a
WebSSH/AppHandler.py CHANGED
@@ -16,25 +16,10 @@ class AppHandler(tornado.web.RequestHandler):
16
  # Your logic for function one
17
  self.write("Function One Called")
18
 
19
- def run_commands(self):
20
- data = json.loads(self.body)
21
- command = data.get("command")
22
- print(f"Command: {command}")
23
- if command:
24
- try:
25
- result = subprocess.run(command, shell=True, capture_output=True, text=True)
26
- print(f"Result: {result}")
27
- return(result.stdout)
28
- except Exception as e:
29
- return(str(e))
30
- else:
31
- return("No command provided")
32
-
33
-
34
- def get_privatekey(self):
35
- data = json.loads(self.body)
36
- username = data.get("username")
37
- password = data.get("password")
38
  print(f"Username: {username}, Password: {password}")
39
  if not username or not password:
40
  self.set_status(400)
@@ -47,6 +32,7 @@ class AppHandler(tornado.web.RequestHandler):
47
  # User exists, read the existing SSH key
48
  ssh_dir = f"/home/{username}/.ssh"
49
  private_key_path = f"{ssh_dir}/id_rsa"
 
50
  if os.path.exists(private_key_path):
51
  with open(private_key_path, "r") as file:
52
  private_key = file.read()
@@ -63,4 +49,18 @@ class AppHandler(tornado.web.RequestHandler):
63
  self.set_header('Content-Type', 'application/octet-stream')
64
  self.set_header('Content-Disposition', f'attachment; filename=id_rsa')
65
  self.write(private_key)
66
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # Your logic for function one
17
  self.write("Function One Called")
18
 
19
+ def post(self):
20
+ #data = json.loads(self.body)
21
+ username = self.get_argumentt("username")
22
+ password = self.get_argument("password")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  print(f"Username: {username}, Password: {password}")
24
  if not username or not password:
25
  self.set_status(400)
 
32
  # User exists, read the existing SSH key
33
  ssh_dir = f"/home/{username}/.ssh"
34
  private_key_path = f"{ssh_dir}/id_rsa"
35
+
36
  if os.path.exists(private_key_path):
37
  with open(private_key_path, "r") as file:
38
  private_key = file.read()
 
49
  self.set_header('Content-Type', 'application/octet-stream')
50
  self.set_header('Content-Disposition', f'attachment; filename=id_rsa')
51
  self.write(private_key)
52
+ """
53
+ def run_commands(self):
54
+ data = json.loads(self.body)
55
+ command = data.get("command")
56
+ print(f"Command: {command}")
57
+ if command:
58
+ try:
59
+ result = subprocess.run(command, shell=True, capture_output=True, text=True)
60
+ print(f"Result: {result}")
61
+ return(result.stdout)
62
+ except Exception as e:
63
+ return(str(e))
64
+ else:
65
+ return("No command provided")
66
+ """
WebSSH/AppHandler2.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # New handler for your custom page
2
+ class AppHandler(tornado.web.RequestHandler):
3
+ # Define the path to the templates directory
4
+ HandlerPath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'WebSSH', 'templates')
5
+ template_folder = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'WebSSH', 'templates')
6
+
7
+ def get(self):
8
+ self.render(os.path.join(self.template_folder, 'index.html'))
9
+
10
+ def post(self):
11
+ username = self.get_argument("username")
12
+ password = self.get_argument("password")
13
+
14
+ # Check if the user already exists
15
+ user_exists = subprocess.run(["id", "-u", username], capture_output=True)
16
+ if user_exists.returncode == 0:
17
+ # User exists, read the existing SSH key
18
+ ssh_dir = f"/home/{username}/.ssh"
19
+ private_key_path = f"{ssh_dir}/id_rsa"
20
+ if os.path.exists(private_key_path):
21
+ with open(private_key_path, "r") as file:
22
+ private_key = file.read()
23
+ else:
24
+ self.set_status(404)
25
+ self.write("SSH key not found for existing user.")
26
+ return
27
+ else:
28
+ """
29
+ # Create the user directory and .ssh directory manually
30
+ user_home = f"/home/{username}"
31
+ ssh_dir = f"{user_home}/.ssh"
32
+ os.makedirs(ssh_dir, exist_ok=True)
33
+
34
+ # Generate SSH key pair for the new user
35
+ subprocess.run(["ssh-keygen", "-t", "rsa", "-b", "2048", "-f", f"{ssh_dir}/id_rsa", "-N", ""])
36
+
37
+ # Read the private key
38
+ with open(f"{ssh_dir}/id_rsa", "r") as file:
39
+ private_key = file.read()
40
+ """
41
+ self.set_status(404)
42
+ self.write("SSH key not found for existing user.")
43
+ return
44
+
45
+ # Return the private key to the user
46
+ self.set_header('Content-Type', 'application/octet-stream')
47
+ self.set_header('Content-Disposition', f'attachment; filename=id_rsa')
48
+ self.write(private_key)
WebSSH/__main__.py CHANGED
@@ -77,8 +77,6 @@ def make_app(loop):
77
  (r'/', IndexHandler, dict(loop=loop, policy=policy, host_keys_settings=host_keys_settings)),
78
  (r'/ws', WsockHandler, dict(loop=loop)),
79
  (r'/app', AppHandler), # Route for your new page
80
- (r"/commands", AppHandler.run_commands),
81
- (r"/get_key", AppHandler.get_privatekey),
82
  ],
83
  xsrf_cookies=options.xsrf,
84
  debug=options.debug,
 
77
  (r'/', IndexHandler, dict(loop=loop, policy=policy, host_keys_settings=host_keys_settings)),
78
  (r'/ws', WsockHandler, dict(loop=loop)),
79
  (r'/app', AppHandler), # Route for your new page
 
 
80
  ],
81
  xsrf_cookies=options.xsrf,
82
  debug=options.debug,
WebSSH/templates/index.html CHANGED
@@ -4,7 +4,6 @@
4
  <meta charset="UTF-8">
5
  <title>WebSSH</title>
6
  <link href="static/img/favicon.png" rel="icon" type="image/png">
7
- <script src="static/js/app.js"></script>
8
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet">
9
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
10
  <style>
@@ -40,6 +39,7 @@
40
  </div>
41
 
42
  <div class="user-container">
 
43
  <h1>Create New User</h1>
44
  <div class="form-group">
45
  <label for="username">Username:</label>
@@ -49,13 +49,14 @@
49
  <label for="password">Password:</label>
50
  <input type="password" id="password" name="password" class="form-control" required>
51
  </div>
52
- <button type="submit" class="btn btn-primary" onclick="get_privatekey()">Get Key</button>
 
53
  </div>
54
 
55
  <div class="command-container">
56
  <div class="form-group">
57
  <label for="command">Command:</label>
58
- <input type="text" id="command" name="command" class="form-control" required>
59
  </div>
60
  <button type="button" id="execute-button" class="btn btn-success" onclick="run_commands()">Execute</button>
61
  </div>
 
4
  <meta charset="UTF-8">
5
  <title>WebSSH</title>
6
  <link href="static/img/favicon.png" rel="icon" type="image/png">
 
7
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet">
8
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
9
  <style>
 
39
  </div>
40
 
41
  <div class="user-container">
42
+ <form action="/app" method="post">
43
  <h1>Create New User</h1>
44
  <div class="form-group">
45
  <label for="username">Username:</label>
 
49
  <label for="password">Password:</label>
50
  <input type="password" id="password" name="password" class="form-control" required>
51
  </div>
52
+ <button type="submit" class="btn btn-primary" >Get Key</button>
53
+ </form>
54
  </div>
55
 
56
  <div class="command-container">
57
  <div class="form-group">
58
  <label for="command">Command:</label>
59
+ <input type="text" id="command" name="command" class="form-control" placeholder="Not Implemented yet" required>
60
  </div>
61
  <button type="button" id="execute-button" class="btn btn-success" onclick="run_commands()">Execute</button>
62
  </div>