Spaces:
Paused
Paused
BinaryONe commited on
Commit ·
e80ff99
1
Parent(s): 2bb1fde
Chnages
Browse files- WebSSH/AppHandler.py +24 -34
- WebSSH/{AppHandler2.py → AppHandler3.py} +39 -21
WebSSH/AppHandler.py
CHANGED
|
@@ -1,30 +1,23 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import tornado.web
|
| 4 |
import subprocess
|
| 5 |
|
|
|
|
|
|
|
| 6 |
# New handler for your custom page
|
| 7 |
class AppHandler(tornado.web.RequestHandler):
|
|
|
|
| 8 |
HandlerPath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'WebSSH', 'templates')
|
| 9 |
template_folder = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'WebSSH', 'templates')
|
| 10 |
|
| 11 |
def get(self):
|
| 12 |
-
print(self.request)
|
| 13 |
self.render(os.path.join(self.template_folder, 'index.html'))
|
| 14 |
-
|
| 15 |
-
def function_two(self):
|
| 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_argument("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)
|
| 26 |
-
self.write("Username and password are required.")
|
| 27 |
-
return
|
| 28 |
|
| 29 |
# Check if the user already exists
|
| 30 |
user_exists = subprocess.run(["id", "-u", username], capture_output=True)
|
|
@@ -32,35 +25,32 @@ class AppHandler(tornado.web.RequestHandler):
|
|
| 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()
|
| 39 |
else:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
return
|
| 43 |
else:
|
| 44 |
-
|
| 45 |
-
#
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Return the private key to the user
|
| 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 |
-
"""
|
|
|
|
| 1 |
+
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import tornado.web
|
| 5 |
import subprocess
|
| 6 |
|
| 7 |
+
|
| 8 |
+
|
| 9 |
# New handler for your custom page
|
| 10 |
class AppHandler(tornado.web.RequestHandler):
|
| 11 |
+
# Define the path to the templates directory
|
| 12 |
HandlerPath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'WebSSH', 'templates')
|
| 13 |
template_folder = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'WebSSH', 'templates')
|
| 14 |
|
| 15 |
def get(self):
|
|
|
|
| 16 |
self.render(os.path.join(self.template_folder, 'index.html'))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def post(self):
|
|
|
|
| 19 |
username = self.get_argument("username")
|
| 20 |
password = self.get_argument("password")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Check if the user already exists
|
| 23 |
user_exists = subprocess.run(["id", "-u", username], capture_output=True)
|
|
|
|
| 25 |
# User exists, read the existing SSH key
|
| 26 |
ssh_dir = f"/home/{username}/.ssh"
|
| 27 |
private_key_path = f"{ssh_dir}/id_rsa"
|
|
|
|
| 28 |
if os.path.exists(private_key_path):
|
| 29 |
with open(private_key_path, "r") as file:
|
| 30 |
private_key = file.read()
|
| 31 |
else:
|
| 32 |
+
self.set_status(404)
|
| 33 |
+
self.write("SSH key not found for existing user.")
|
| 34 |
+
return
|
| 35 |
else:
|
| 36 |
+
"""
|
| 37 |
+
# Create the user directory and .ssh directory manually
|
| 38 |
+
user_home = f"/home/{username}"
|
| 39 |
+
ssh_dir = f"{user_home}/.ssh"
|
| 40 |
+
os.makedirs(ssh_dir, exist_ok=True)
|
| 41 |
+
|
| 42 |
+
# Generate SSH key pair for the new user
|
| 43 |
+
subprocess.run(["ssh-keygen", "-t", "rsa", "-b", "2048", "-f", f"{ssh_dir}/id_rsa", "-N", ""])
|
| 44 |
+
|
| 45 |
+
# Read the private key
|
| 46 |
+
with open(f"{ssh_dir}/id_rsa", "r") as file:
|
| 47 |
+
private_key = file.read()
|
| 48 |
+
"""
|
| 49 |
+
self.set_status(404)
|
| 50 |
+
self.write("SSH key not found for existing user.")
|
| 51 |
+
return
|
| 52 |
|
| 53 |
# Return the private key to the user
|
| 54 |
self.set_header('Content-Type', 'application/octet-stream')
|
| 55 |
self.set_header('Content-Disposition', f'attachment; filename=id_rsa')
|
| 56 |
+
self.write(private_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WebSSH/{AppHandler2.py → AppHandler3.py}
RENAMED
|
@@ -1,15 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
@@ -17,32 +32,35 @@ class AppHandler(tornado.web.RequestHandler):
|
|
| 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 |
-
#
|
| 30 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import tornado.web
|
| 4 |
+
import subprocess
|
| 5 |
+
|
| 6 |
# New handler for your custom page
|
| 7 |
class AppHandler(tornado.web.RequestHandler):
|
|
|
|
| 8 |
HandlerPath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'WebSSH', 'templates')
|
| 9 |
template_folder = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'WebSSH', 'templates')
|
| 10 |
|
| 11 |
def get(self):
|
| 12 |
+
print(self.request)
|
| 13 |
self.render(os.path.join(self.template_folder, 'index.html'))
|
| 14 |
+
|
| 15 |
+
def function_two(self):
|
| 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_argument("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)
|
| 26 |
+
self.write("Username and password are required.")
|
| 27 |
+
return
|
| 28 |
|
| 29 |
# Check if the user already exists
|
| 30 |
user_exists = subprocess.run(["id", "-u", username], capture_output=True)
|
|
|
|
| 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()
|
| 39 |
else:
|
| 40 |
+
#self.set_status(404)
|
| 41 |
+
#self.write("SSH key not found for existing user.")
|
| 42 |
+
return {response: 404, message: "SSH key not found for existing user."}
|
| 43 |
else:
|
| 44 |
+
#self.set_status(404)
|
| 45 |
+
#self.write("User not found.")
|
| 46 |
+
return {response: 404, message:"User not found."}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Return the private key to the user
|
| 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 |
+
"""
|