Compare commits
10 Commits
20863d024a
...
feat/termi
| Author | SHA1 | Date | |
|---|---|---|---|
|
d46f2ed40d
|
|||
|
687e7af91b
|
|||
|
f97e362192
|
|||
|
6c7242b167
|
|||
|
0fdcf435e1
|
|||
|
27517ad857
|
|||
|
6657151f81
|
|||
|
c43e453d05
|
|||
|
91950e5a78
|
|||
|
2a42378ad7
|
665
blueprints/terminal.py
Normal file
665
blueprints/terminal.py
Normal file
@@ -0,0 +1,665 @@
|
|||||||
|
from flask import Blueprint, request, render_template, session
|
||||||
|
from tools import json_response, getClientIP
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
terminal_bp = Blueprint('terminal', __name__)
|
||||||
|
|
||||||
|
|
||||||
|
@terminal_bp.route("/terminal")
|
||||||
|
def index():
|
||||||
|
return render_template("terminal.html", user=getClientIP(request))
|
||||||
|
|
||||||
|
|
||||||
|
COMMANDS = {
|
||||||
|
"help": "Show this help message",
|
||||||
|
"about": "About this terminal",
|
||||||
|
"clear": "Clear the terminal",
|
||||||
|
"echo [text]": "Echo text back",
|
||||||
|
"whoami": "Display the current user",
|
||||||
|
"ls": "List directory contents",
|
||||||
|
"pwd": "Print working directory",
|
||||||
|
"date": "Show current date and time with optional format",
|
||||||
|
"cd [path]": "Change directory to path",
|
||||||
|
"cat [file]": "Display file contents",
|
||||||
|
"rm [file]": "Remove a file",
|
||||||
|
"tree [path]": "Display directory tree",
|
||||||
|
"touch [file]": "Create a new empty file",
|
||||||
|
"nano [file]": "Edit a file",
|
||||||
|
"reset": "Reset the terminal session",
|
||||||
|
"exit": "Exit the terminal session",
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOT_FILES = [
|
||||||
|
"amd-ucode.img",
|
||||||
|
"EFI",
|
||||||
|
"initramfs-linux-fallback.img",
|
||||||
|
"initramfs-linux.img",
|
||||||
|
"initramfs-linux-lts-fallback.img",
|
||||||
|
"initramfs-linux-lts.img",
|
||||||
|
"intel-ucode.img",
|
||||||
|
"loader",
|
||||||
|
"vmlinuz-linux",
|
||||||
|
"vmlinuz-linux-lts"
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def get_nodes_in_directory(path: str) -> list[str]:
|
||||||
|
"""Simulate getting files in a directory for the terminal."""
|
||||||
|
# If path is valid, get files from session
|
||||||
|
if session.get("paths", None) is None:
|
||||||
|
setup_path_session()
|
||||||
|
|
||||||
|
# Split path into parts
|
||||||
|
parts = path.strip("/").split("/")
|
||||||
|
if not parts or parts == [""]:
|
||||||
|
return session["paths"]
|
||||||
|
|
||||||
|
current_level = session["paths"]
|
||||||
|
for part in parts:
|
||||||
|
found = False
|
||||||
|
for item in current_level:
|
||||||
|
if item["name"] == part and item["type"] == 0:
|
||||||
|
current_level = item.get("children", [])
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
if not found:
|
||||||
|
return []
|
||||||
|
|
||||||
|
return current_level
|
||||||
|
|
||||||
|
|
||||||
|
def setup_path_session():
|
||||||
|
"""Initialize the session path variables if not already set."""
|
||||||
|
if "pwd" not in session:
|
||||||
|
session["pwd"] = f"/home/{getClientIP(request)}"
|
||||||
|
if "paths" not in session:
|
||||||
|
binaries = []
|
||||||
|
for cmd in COMMANDS.keys():
|
||||||
|
binaries.append({"name": cmd.split()[0], "type": 2, "content": "", "permissions": 1})
|
||||||
|
boot_files = []
|
||||||
|
for bin_file in BOOT_FILES:
|
||||||
|
boot_files.append({"name": bin_file, "type": 1, "content": "", "permissions": 1})
|
||||||
|
|
||||||
|
session["paths"] = [
|
||||||
|
{"name": "home", "type": 0, "children": [
|
||||||
|
{"name": str(getClientIP(request)), "type": 0, "children": [
|
||||||
|
{"name": "Readme.txt", "type": 1, "content": "This is a README file.", "permissions": 2},
|
||||||
|
], "permissions": 2}
|
||||||
|
], "permissions": 1},
|
||||||
|
{"name": "bin", "type": 0, "children": binaries, "permissions": 1},
|
||||||
|
{"name": "boot", "type": 0, "children": boot_files, "permissions": 1},
|
||||||
|
{"name": "dev", "type": 0, "children": [], "permissions": 1},
|
||||||
|
{"name": "etc", "type": 0, "children": [], "permissions": 1},
|
||||||
|
{"name": "lib", "type": 0, "children": [], "permissions": 1},
|
||||||
|
{"name": "lib64", "type": 0, "children": [], "permissions": 1},
|
||||||
|
{"name": "opt", "type": 0, "children": [], "permissions": 1},
|
||||||
|
{"name": "proc", "type": 0, "children": [], "permissions": 1},
|
||||||
|
{"name": "root", "type": 0, "children": [], "permissions": 1},
|
||||||
|
{"name": "run", "type": 0, "children": [], "permissions": 1},
|
||||||
|
{"name": "sbin", "type": 0, "children": [], "permissions": 1},
|
||||||
|
{"name": "srv", "type": 0, "children": [], "permissions": 1},
|
||||||
|
{"name": "sys", "type": 0, "children": [], "permissions": 1},
|
||||||
|
{"name": "tmp", "type": 0, "children": [], "permissions": 1},
|
||||||
|
{"name": "usr", "type": 0, "children": [], "permissions": 1},
|
||||||
|
{"name": "var", "type": 0, "children": [], "permissions": 1},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid_path(path: str) -> bool:
|
||||||
|
"""Check if the given path is valid in the simulated terminal."""
|
||||||
|
if session.get("paths", None) is None:
|
||||||
|
setup_path_session()
|
||||||
|
|
||||||
|
# Split path into parts
|
||||||
|
parts = path.strip("/").split("/")
|
||||||
|
if not parts or parts == [""]:
|
||||||
|
return True # Root path is valid
|
||||||
|
|
||||||
|
current_level = session["paths"]
|
||||||
|
for part in parts:
|
||||||
|
found = False
|
||||||
|
for item in current_level:
|
||||||
|
if item["name"] == part and item["type"] == 0:
|
||||||
|
current_level = item.get("children", [])
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
if not found:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def is_valid_file(path: str) -> bool:
|
||||||
|
"""Check if the given file exists in the current directory."""
|
||||||
|
if session.get("paths", None) is None:
|
||||||
|
setup_path_session()
|
||||||
|
|
||||||
|
# Get path parts
|
||||||
|
parts = path.split("/")
|
||||||
|
# Get files in the directory
|
||||||
|
if is_valid_path("/".join(parts[:-1])):
|
||||||
|
files = get_nodes_in_directory("/".join(parts[:-1]))
|
||||||
|
for item in files:
|
||||||
|
if item["name"] == parts[-1] and item["type"] == 1:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def is_valid_binary(path: str) -> bool:
|
||||||
|
"""Check if the given file exists in the current directory."""
|
||||||
|
if session.get("paths", None) is None:
|
||||||
|
setup_path_session()
|
||||||
|
|
||||||
|
# Get path parts
|
||||||
|
parts = path.split("/")
|
||||||
|
# Get files in the directory
|
||||||
|
if is_valid_path("/".join(parts[:-1])):
|
||||||
|
files = get_nodes_in_directory("/".join(parts[:-1]))
|
||||||
|
for item in files:
|
||||||
|
if item["name"] == parts[-1] and item["type"] == 2:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_node(path: str) -> dict:
|
||||||
|
"""Get the node (file or directory) at the given path."""
|
||||||
|
if session.get("paths", None) is None:
|
||||||
|
setup_path_session()
|
||||||
|
|
||||||
|
parts = path.strip("/").split("/")
|
||||||
|
current_level = session["paths"]
|
||||||
|
for part in parts:
|
||||||
|
for item in current_level:
|
||||||
|
if item["name"] == part:
|
||||||
|
if part == parts[-1]:
|
||||||
|
return item
|
||||||
|
else:
|
||||||
|
current_level = item.get("children", [])
|
||||||
|
break
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def build_tree(path: str, prefix: str = "") -> str:
|
||||||
|
output = ""
|
||||||
|
files = get_nodes_in_directory(path)
|
||||||
|
for i, item in enumerate(files):
|
||||||
|
connector = "└── " if i == len(files) - 1 else "├── "
|
||||||
|
output += f"{prefix}{connector}{item['name']}\n"
|
||||||
|
if item["type"] == 0: # Directory
|
||||||
|
extension = " " if i == len(files) - 1 else "│ "
|
||||||
|
output += build_tree(sanitize_path(path + "/" + item["name"]), prefix + extension)
|
||||||
|
return output
|
||||||
|
|
||||||
|
def sanitize_path(path: str) -> str:
|
||||||
|
"""Sanitize the given path to prevent directory traversal."""
|
||||||
|
parts = path.strip("/").split("/")
|
||||||
|
sanitized_parts = []
|
||||||
|
for part in parts:
|
||||||
|
if part == "" or part == ".":
|
||||||
|
continue
|
||||||
|
elif part == "..":
|
||||||
|
if sanitized_parts:
|
||||||
|
sanitized_parts.pop()
|
||||||
|
else:
|
||||||
|
sanitized_parts.append(part)
|
||||||
|
return "/" + "/".join(sanitized_parts)
|
||||||
|
|
||||||
|
def remove_node(path: str) -> bool:
|
||||||
|
"""Remove the node (file or directory) at the given path."""
|
||||||
|
if session.get("paths", None) is None:
|
||||||
|
setup_path_session()
|
||||||
|
|
||||||
|
parts = path.strip("/").split("/")
|
||||||
|
current_level = session["paths"]
|
||||||
|
for i, part in enumerate(parts):
|
||||||
|
for j, item in enumerate(current_level):
|
||||||
|
if item["name"] == part:
|
||||||
|
if i == len(parts) - 1:
|
||||||
|
# Remove the item
|
||||||
|
del current_level[j]
|
||||||
|
|
||||||
|
# Update the session paths
|
||||||
|
session["paths"] = session["paths"]
|
||||||
|
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
current_level = item.get("children", [])
|
||||||
|
break
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
@terminal_bp.route("/terminal/execute/ls", methods=["POST"])
|
||||||
|
def ls():
|
||||||
|
data = request.get_json() or {}
|
||||||
|
args = data.get("args", "")
|
||||||
|
args = args.split()
|
||||||
|
# Check if -a flag is provided
|
||||||
|
# Pop if -a flag from args
|
||||||
|
all_files = False
|
||||||
|
if "-a" in args:
|
||||||
|
all_files = True
|
||||||
|
args.remove("-a")
|
||||||
|
elif "--all" in args:
|
||||||
|
all_files = True
|
||||||
|
args.remove("--all")
|
||||||
|
|
||||||
|
long_format = False
|
||||||
|
if "-l" in args:
|
||||||
|
long_format = True
|
||||||
|
args.remove("-l")
|
||||||
|
elif "--long" in args:
|
||||||
|
long_format = True
|
||||||
|
args.remove("--long")
|
||||||
|
|
||||||
|
for arg in args:
|
||||||
|
if arg.startswith("-") and not arg.startswith("--"):
|
||||||
|
if "l" in arg:
|
||||||
|
long_format = True
|
||||||
|
if "a" in arg:
|
||||||
|
all_files = True
|
||||||
|
args.remove(arg)
|
||||||
|
|
||||||
|
|
||||||
|
ip = getClientIP(request)
|
||||||
|
path = session.get("pwd", f"/home/{ip}")
|
||||||
|
if args:
|
||||||
|
path = args[0]
|
||||||
|
if not path.startswith("/"):
|
||||||
|
# Relative path
|
||||||
|
path = sanitize_path(session.get("pwd", f"/home/{ip}") + "/" + path)
|
||||||
|
if not is_valid_path(path):
|
||||||
|
# If it is a file or binary, return it
|
||||||
|
if is_valid_file(path) or is_valid_binary(path):
|
||||||
|
if long_format:
|
||||||
|
node = get_node(path)
|
||||||
|
permissions = node.get("permissions", 0)
|
||||||
|
perm_str = ""
|
||||||
|
perm_str += "r" if permissions > 0 else "-"
|
||||||
|
perm_str += "w" if permissions > 1 else "-"
|
||||||
|
perm_str += "x" if (node.get("type", 0) == 2 and permissions > 1) else "-"
|
||||||
|
user = f"{ip}" if path.startswith(f"/home/{ip}") else "root"
|
||||||
|
output = f"{perm_str} {user} {user} {path.split('/')[-1]}"
|
||||||
|
return json_response(request, {"output": output}, 200)
|
||||||
|
return json_response(request, {"output": path.split("/")[-1]}, 200)
|
||||||
|
|
||||||
|
return json_response(request, {"output": f"ls: cannot access '{path}': No such file or directory"}, 200)
|
||||||
|
|
||||||
|
files = get_nodes_in_directory(path)
|
||||||
|
output = []
|
||||||
|
if long_format:
|
||||||
|
for f in files:
|
||||||
|
permissions = f.get("permissions", 0)
|
||||||
|
perm_str = ""
|
||||||
|
perm_str += "r" if permissions > 0 else "-"
|
||||||
|
perm_str += "w" if permissions > 1 else "-"
|
||||||
|
perm_str += "x" if (f.get("type", 0) == 2 and permissions >= 1) else "-"
|
||||||
|
user = f"{ip}" if path.startswith(f"/home/{ip}") else "root"
|
||||||
|
output.append(f"{perm_str} {user} {user} {f['name']}")
|
||||||
|
else:
|
||||||
|
output = [f["name"] for f in files]
|
||||||
|
if all_files:
|
||||||
|
output.insert(0, ".")
|
||||||
|
output.insert(1, "..")
|
||||||
|
else:
|
||||||
|
output = [file for file in output if not file.startswith(".")]
|
||||||
|
if long_format:
|
||||||
|
output = "\n".join(output)
|
||||||
|
else:
|
||||||
|
output = " ".join(output)
|
||||||
|
|
||||||
|
return json_response(request, {"output": output}, 200)
|
||||||
|
|
||||||
|
|
||||||
|
@terminal_bp.route("/terminal/pwd")
|
||||||
|
def pwd():
|
||||||
|
if "pwd" not in session:
|
||||||
|
session["pwd"] = f"/home/{getClientIP(request)}"
|
||||||
|
pwd = session["pwd"]
|
||||||
|
if pwd == "/home/" + getClientIP(request):
|
||||||
|
pwd = "~"
|
||||||
|
return json_response(request, {"output": pwd, "raw": session["pwd"]}, 200)
|
||||||
|
|
||||||
|
|
||||||
|
@terminal_bp.route("/terminal/execute/cd", methods=["POST"])
|
||||||
|
def cd():
|
||||||
|
data = request.get_json() or {}
|
||||||
|
args = data.get("args", "")
|
||||||
|
args = args.split()
|
||||||
|
|
||||||
|
if not args:
|
||||||
|
# No path provided, go to home
|
||||||
|
session["pwd"] = f"/home/{getClientIP(request)}"
|
||||||
|
output = ""
|
||||||
|
else:
|
||||||
|
path = args[0]
|
||||||
|
# Simulate changing directory
|
||||||
|
if path == "~":
|
||||||
|
session["pwd"] = f"/home/{getClientIP(request)}"
|
||||||
|
output = ""
|
||||||
|
|
||||||
|
if not path.startswith("/"):
|
||||||
|
path = sanitize_path(session.get("pwd", f"/home/{getClientIP(request)}") + "/" + path)
|
||||||
|
|
||||||
|
if is_valid_path(path):
|
||||||
|
session["pwd"] = sanitize_path(path)
|
||||||
|
output = ""
|
||||||
|
else:
|
||||||
|
output = f"bash: cd: {path}: No such file or directory"
|
||||||
|
|
||||||
|
return json_response(request, {"output": output}, 200)
|
||||||
|
|
||||||
|
def can_write_to_path(path: str) -> bool:
|
||||||
|
"""Check if the user can write to the given path (must be in their home directory)."""
|
||||||
|
user_home = f"/home/{getClientIP(request)}"
|
||||||
|
normalized_path = sanitize_path(path)
|
||||||
|
return normalized_path.startswith(user_home)
|
||||||
|
|
||||||
|
def create_file(path: str, content: str = "") -> bool:
|
||||||
|
"""Create a new file at the given path."""
|
||||||
|
if session.get("paths", None) is None:
|
||||||
|
setup_path_session()
|
||||||
|
|
||||||
|
parts = path.strip("/").split("/")
|
||||||
|
filename = parts[-1]
|
||||||
|
dir_path = "/".join(parts[:-1])
|
||||||
|
|
||||||
|
# Get the directory
|
||||||
|
if not is_valid_path("/" + dir_path):
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Get nodes in directory
|
||||||
|
dir_parts = dir_path.strip("/").split("/")
|
||||||
|
current_level = session["paths"]
|
||||||
|
|
||||||
|
for part in dir_parts:
|
||||||
|
if part == "":
|
||||||
|
continue
|
||||||
|
for item in current_level:
|
||||||
|
if item["name"] == part and item["type"] == 0:
|
||||||
|
current_level = item.get("children", [])
|
||||||
|
break
|
||||||
|
|
||||||
|
# Check if file already exists
|
||||||
|
for item in current_level:
|
||||||
|
if item["name"] == filename:
|
||||||
|
# Update existing file
|
||||||
|
item["content"] = content
|
||||||
|
session.modified = True
|
||||||
|
return True
|
||||||
|
|
||||||
|
# Create new file
|
||||||
|
new_file = {
|
||||||
|
"name": filename,
|
||||||
|
"type": 1,
|
||||||
|
"content": content,
|
||||||
|
"permissions": 2
|
||||||
|
}
|
||||||
|
current_level.append(new_file)
|
||||||
|
session.modified = True
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_file_content(path: str, content: str) -> bool:
|
||||||
|
"""Update the content of an existing file."""
|
||||||
|
if session.get("paths", None) is None:
|
||||||
|
setup_path_session()
|
||||||
|
|
||||||
|
parts = path.strip("/").split("/")
|
||||||
|
current_level = session["paths"]
|
||||||
|
|
||||||
|
for i, part in enumerate(parts):
|
||||||
|
for item in current_level:
|
||||||
|
if item["name"] == part:
|
||||||
|
if i == len(parts) - 1:
|
||||||
|
# Update the file content
|
||||||
|
if item["type"] == 1:
|
||||||
|
item["content"] = content
|
||||||
|
session.modified = True
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
current_level = item.get("children", [])
|
||||||
|
break
|
||||||
|
return False
|
||||||
|
|
||||||
|
@terminal_bp.route("/terminal/execute/touch", methods=["POST"])
|
||||||
|
def touch():
|
||||||
|
try:
|
||||||
|
data = request.get_json() or {}
|
||||||
|
args = data.get("args", "")
|
||||||
|
args = args.split()
|
||||||
|
|
||||||
|
if not args:
|
||||||
|
return json_response(request, {"output": "touch: missing file operand"}, 200)
|
||||||
|
|
||||||
|
path = args[0]
|
||||||
|
if not path.startswith("/"):
|
||||||
|
path = sanitize_path(session.get("pwd", f"/home/{getClientIP(request)}") + "/" + path)
|
||||||
|
|
||||||
|
# Check if user can write to this path
|
||||||
|
if not can_write_to_path(path):
|
||||||
|
return json_response(request, {"output": f"touch: cannot touch '{path}': Permission denied"}, 200)
|
||||||
|
|
||||||
|
# Check if file already exists
|
||||||
|
if is_valid_file(path):
|
||||||
|
return json_response(request, {"output": f"touch: '{path}': File already exists"}, 200)
|
||||||
|
|
||||||
|
# Create the file
|
||||||
|
if create_file(path, ""):
|
||||||
|
output = ""
|
||||||
|
else:
|
||||||
|
output = f"touch: cannot create '{path}': No such file or directory"
|
||||||
|
|
||||||
|
return json_response(request, {"output": output}, 200)
|
||||||
|
except Exception as e:
|
||||||
|
return json_response(request, {"output": f"touch: {str(e)}"}, 200)
|
||||||
|
|
||||||
|
@terminal_bp.route("/terminal/execute/nano", methods=["POST"])
|
||||||
|
def nano():
|
||||||
|
try:
|
||||||
|
data = request.get_json() or {}
|
||||||
|
args = data.get("args", "")
|
||||||
|
|
||||||
|
# Parse args - format: filename CONTENT content here
|
||||||
|
parts = args.split(" CONTENT ", 1)
|
||||||
|
if len(parts) != 2:
|
||||||
|
return json_response(request, {"output": "Usage: nano [file] CONTENT [text]\nExample: nano test.txt CONTENT Hello World"}, 200)
|
||||||
|
|
||||||
|
path = parts[0].strip()
|
||||||
|
content = parts[1]
|
||||||
|
|
||||||
|
if not path.startswith("/"):
|
||||||
|
path = sanitize_path(session.get("pwd", f"/home/{getClientIP(request)}") + "/" + path)
|
||||||
|
|
||||||
|
# Check if user can write to this path
|
||||||
|
if not can_write_to_path(path):
|
||||||
|
return json_response(request, {"output": f"nano: cannot write to '{path}': Permission denied"}, 200)
|
||||||
|
|
||||||
|
# Check if file exists
|
||||||
|
if is_valid_file(path):
|
||||||
|
# Update existing file
|
||||||
|
node = get_node(path)
|
||||||
|
if node.get("permissions", 0) < 2:
|
||||||
|
return json_response(request, {"output": f"nano: cannot write to '{path}': Permission denied"}, 200)
|
||||||
|
|
||||||
|
if update_file_content(path, content):
|
||||||
|
output = ""
|
||||||
|
else:
|
||||||
|
output = f"nano: failed to update '{path}'"
|
||||||
|
else:
|
||||||
|
# Create new file
|
||||||
|
if create_file(path, content):
|
||||||
|
output = f"File '{path}' created successfully"
|
||||||
|
else:
|
||||||
|
output = f"nano: cannot create '{path}': No such file or directory"
|
||||||
|
|
||||||
|
return json_response(request, {"output": output}, 200)
|
||||||
|
except Exception as e:
|
||||||
|
return json_response(request, {"output": f"nano: {str(e)}"}, 200)
|
||||||
|
|
||||||
|
@terminal_bp.route("/terminal/execute/cat", methods=["POST"])
|
||||||
|
def cat():
|
||||||
|
try:
|
||||||
|
data = request.get_json() or {}
|
||||||
|
args = data.get("args", "")
|
||||||
|
args = args.split()
|
||||||
|
|
||||||
|
if not args:
|
||||||
|
return json_response(request, {"output": "cat: missing file operand"}, 200)
|
||||||
|
|
||||||
|
path = args[0]
|
||||||
|
if not path.startswith("/"):
|
||||||
|
path = sanitize_path(session.get("pwd", f"/home/{getClientIP(request)}") + "/" + path)
|
||||||
|
|
||||||
|
# Check if path is valid
|
||||||
|
if not is_valid_file(path):
|
||||||
|
# Check if it is a binary
|
||||||
|
if is_valid_binary(path):
|
||||||
|
return json_response(request, {"output": f"cat: {path}: Binary file"}, 200)
|
||||||
|
return json_response(request, {"output": f"cat: {path}: No such file or directory"}, 200)
|
||||||
|
|
||||||
|
output = get_node(path).get("content", "")
|
||||||
|
|
||||||
|
return json_response(request, {"output": output}, 200)
|
||||||
|
except Exception as e:
|
||||||
|
return json_response(request, {"output": f"cat: {str(e)}"}, 200)
|
||||||
|
|
||||||
|
@terminal_bp.route("/terminal/execute/echo", methods=["POST"])
|
||||||
|
def echo():
|
||||||
|
try:
|
||||||
|
data = request.get_json() or {}
|
||||||
|
args = data.get("args", "")
|
||||||
|
return json_response(request, {"output": args}, 200)
|
||||||
|
except Exception as e:
|
||||||
|
return json_response(request, {"output": f"echo: {str(e)}"}, 200)
|
||||||
|
|
||||||
|
@terminal_bp.route("/terminal/execute/date", methods=["POST"])
|
||||||
|
def date():
|
||||||
|
try:
|
||||||
|
# See if any args are passed
|
||||||
|
data = request.get_json() or {}
|
||||||
|
args = data.get("args", "")
|
||||||
|
|
||||||
|
# Use arguments to format date if needed
|
||||||
|
if args:
|
||||||
|
# If args if --help or -h, show help message
|
||||||
|
if args in ("--help", "-h"):
|
||||||
|
output = (
|
||||||
|
"Usage: date [FORMAT]\n\n"
|
||||||
|
"Display the current date and time.\n\n"
|
||||||
|
"FORMAT controls the output. Some common format specifiers:\n"
|
||||||
|
" %a Abbreviated weekday name (e.g., 'Mon')\n"
|
||||||
|
" %b Abbreviated month name (e.g., 'Jan')\n"
|
||||||
|
" %d Day of the month (01 to 31)\n"
|
||||||
|
" %H Hour (00 to 23)\n"
|
||||||
|
" %M Minute (00 to 59)\n"
|
||||||
|
" %S Second (00 to 60)\n"
|
||||||
|
" %Y Year with century (e.g., 2024)\n\n"
|
||||||
|
"Example: date '%Y-%m-%d %H:%M:%S'"
|
||||||
|
)
|
||||||
|
return json_response(request, {"output": output}, 200)
|
||||||
|
|
||||||
|
try:
|
||||||
|
output = datetime.now().strftime(args)
|
||||||
|
except Exception:
|
||||||
|
output = "Invalid date format."
|
||||||
|
else:
|
||||||
|
output = datetime.now().strftime("%a %b %d %H:%M:%S %Z %Y")
|
||||||
|
return json_response(request, {"output": output}, 200)
|
||||||
|
except Exception as e:
|
||||||
|
return json_response(request, {"output": f"date: {str(e)}"}, 200)
|
||||||
|
|
||||||
|
@terminal_bp.route("/terminal/execute/rm", methods=["POST"])
|
||||||
|
def rm():
|
||||||
|
try:
|
||||||
|
data = request.get_json() or {}
|
||||||
|
args = data.get("args", "")
|
||||||
|
args = args.split()
|
||||||
|
|
||||||
|
if not args:
|
||||||
|
return json_response(request, {"output": "rm: missing operand"}, 200)
|
||||||
|
|
||||||
|
path = args[0]
|
||||||
|
if not path.startswith("/"):
|
||||||
|
path = sanitize_path(session.get("pwd", f"/home/{getClientIP(request)}") + "/" + path)
|
||||||
|
|
||||||
|
# Check if user can write to this path
|
||||||
|
if not can_write_to_path(path):
|
||||||
|
return json_response(request, {"output": f"rm: cannot remove '{path}': Permission denied"}, 200)
|
||||||
|
|
||||||
|
# Check if path is valid
|
||||||
|
if not is_valid_file(path) and not is_valid_binary(path):
|
||||||
|
return json_response(request, {"output": f"rm: cannot remove '{path}': No such file"}, 200)
|
||||||
|
|
||||||
|
# Get the node
|
||||||
|
node = get_node(path)
|
||||||
|
# Only let user delete files if the permission is >1 (writable)
|
||||||
|
if node.get("permissions", 0) < 2:
|
||||||
|
return json_response(request, {"output": f"rm: cannot remove '{path}': Permission denied"}, 200)
|
||||||
|
|
||||||
|
# Only let the user remove files
|
||||||
|
if node.get("type", 1) != 1:
|
||||||
|
return json_response(request, {"output": f"rm: cannot remove '{path}': Is a directory"}, 200)
|
||||||
|
|
||||||
|
# Remove the file from session paths
|
||||||
|
if remove_node(path):
|
||||||
|
output = f"Removed '{path}'"
|
||||||
|
else:
|
||||||
|
output = f"Failed to remove '{path}'"
|
||||||
|
|
||||||
|
return json_response(request, {"output": output}, 200)
|
||||||
|
except Exception as e:
|
||||||
|
return json_response(request, {"output": f"rm: {str(e)}"}, 200)
|
||||||
|
|
||||||
|
@terminal_bp.route("/terminal/execute/tree", methods=["POST"])
|
||||||
|
def tree():
|
||||||
|
try:
|
||||||
|
data = request.get_json() or {}
|
||||||
|
args = data.get("args", "")
|
||||||
|
path = session.get("pwd", f"/home/{getClientIP(request)}")
|
||||||
|
if args:
|
||||||
|
path = args
|
||||||
|
if not path.startswith("/"):
|
||||||
|
path = sanitize_path(session.get("pwd", f"/home/{getClientIP(request)}") + "/" + path)
|
||||||
|
if not is_valid_path(path):
|
||||||
|
return json_response(request, {"output": f"tree: cannot access '{path}': No such file or directory"}, 200)
|
||||||
|
|
||||||
|
output = build_tree(path).rstrip()
|
||||||
|
return json_response(request, {"output": output}, 200)
|
||||||
|
except Exception as e:
|
||||||
|
return json_response(request, {"output": f"tree: {str(e)}"}, 200)
|
||||||
|
|
||||||
|
@terminal_bp.route("/terminal/execute/<command>", methods=["POST"])
|
||||||
|
def execute_catch(command):
|
||||||
|
try:
|
||||||
|
# Basic command processing
|
||||||
|
if command == "help":
|
||||||
|
output = "Available commands:\n" + \
|
||||||
|
"\n".join(f" {cmd}: {desc}" for cmd, desc in COMMANDS.items())
|
||||||
|
elif command == "about":
|
||||||
|
output = "This is a simulated terminal interface created by Nathan Woodburn."
|
||||||
|
|
||||||
|
elif command == "whoami":
|
||||||
|
output = getClientIP(request)
|
||||||
|
elif command == "pwd":
|
||||||
|
# Get pwd from session or simulate
|
||||||
|
output = session.get("pwd", f"/home/{getClientIP(request)}")
|
||||||
|
else:
|
||||||
|
output = f"Command not found: {command}"
|
||||||
|
|
||||||
|
return json_response(request, {"output": output}, 200)
|
||||||
|
except Exception as e:
|
||||||
|
return json_response(request, {"output": f"{command}: {str(e)}"}, 200)
|
||||||
|
|
||||||
|
@terminal_bp.route("/terminal/execute/reset", methods=["POST"])
|
||||||
|
def reset():
|
||||||
|
try:
|
||||||
|
# Clear the session data related to terminal
|
||||||
|
session.pop("pwd", None)
|
||||||
|
session.pop("paths", None)
|
||||||
|
output = "Terminal session has been reset."
|
||||||
|
return json_response(request, {"output": output}, 200)
|
||||||
|
except Exception as e:
|
||||||
|
return json_response(request, {"output": f"reset: {str(e)}"}, 200)
|
||||||
|
|
||||||
|
# Add error handler at the end
|
||||||
|
@terminal_bp.errorhandler(Exception)
|
||||||
|
def handle_terminal_error(error):
|
||||||
|
"""Handle all exceptions in terminal blueprint."""
|
||||||
|
error_message = f"Terminal error: {str(error)}"
|
||||||
|
return json_response(request, {"output": error_message}, 500)
|
||||||
33
curl.py
33
curl.py
@@ -77,6 +77,35 @@ def get_projects():
|
|||||||
|
|
||||||
return projects
|
return projects
|
||||||
|
|
||||||
|
@lru_cache(maxsize=32)
|
||||||
|
def valid_curl_path(path: str) -> bool:
|
||||||
|
"""Check if the given path corresponds to a valid curl/ascii response."""
|
||||||
|
path = clean_path(path)
|
||||||
|
|
||||||
|
# Special cases
|
||||||
|
special_paths = [
|
||||||
|
"index",
|
||||||
|
"projects",
|
||||||
|
"donate",
|
||||||
|
"donate/more",
|
||||||
|
"tools"
|
||||||
|
]
|
||||||
|
if path in special_paths:
|
||||||
|
return True
|
||||||
|
|
||||||
|
# Check for donate/<coin> pattern
|
||||||
|
if path.startswith("donate/"):
|
||||||
|
coin = path.split("/")[1]
|
||||||
|
address = getAddress(coin)
|
||||||
|
if address != "":
|
||||||
|
return True
|
||||||
|
|
||||||
|
# Check if .ascii template exists
|
||||||
|
if os.path.exists(f"templates/{path}.ascii"):
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
def curl_response(request):
|
def curl_response(request):
|
||||||
# Check if <path>.ascii exists
|
# Check if <path>.ascii exists
|
||||||
path = clean_path(request.path)
|
path = clean_path(request.path)
|
||||||
@@ -119,8 +148,4 @@ def curl_response(request):
|
|||||||
if os.path.exists(f"templates/{path}.ascii"):
|
if os.path.exists(f"templates/{path}.ascii"):
|
||||||
return render_template(f"{path}.ascii",header=get_header()), 200, {'Content-Type': 'text/plain; charset=utf-8'}
|
return render_template(f"{path}.ascii",header=get_header()), 200, {'Content-Type': 'text/plain; charset=utf-8'}
|
||||||
|
|
||||||
# Fallback to html if it exists
|
|
||||||
if os.path.exists(f"templates/{path}.html"):
|
|
||||||
return render_template(f"{path}.html")
|
|
||||||
|
|
||||||
return error_response(request)
|
return error_response(request)
|
||||||
@@ -26,10 +26,12 @@ from blueprints.api import api_bp
|
|||||||
from blueprints.podcast import podcast_bp
|
from blueprints.podcast import podcast_bp
|
||||||
from blueprints.acme import acme_bp
|
from blueprints.acme import acme_bp
|
||||||
from blueprints.spotify import spotify_bp
|
from blueprints.spotify import spotify_bp
|
||||||
|
from blueprints.terminal import terminal_bp
|
||||||
from tools import isCurl, isCrawler, getAddress, getFilePath, error_response, getClientIP, json_response, getHandshakeScript, get_tools_data
|
from tools import isCurl, isCrawler, getAddress, getFilePath, error_response, getClientIP, json_response, getHandshakeScript, get_tools_data
|
||||||
from curl import curl_response
|
from curl import curl_response, valid_curl_path
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
app.secret_key = os.getenv("FLASK_SECRET_KEY", "supersecretkey")
|
||||||
CORS(app)
|
CORS(app)
|
||||||
|
|
||||||
# Register blueprints
|
# Register blueprints
|
||||||
@@ -40,6 +42,7 @@ app.register_blueprint(api_bp, url_prefix='/api/v1')
|
|||||||
app.register_blueprint(podcast_bp)
|
app.register_blueprint(podcast_bp)
|
||||||
app.register_blueprint(acme_bp)
|
app.register_blueprint(acme_bp)
|
||||||
app.register_blueprint(spotify_bp, url_prefix='/spotify')
|
app.register_blueprint(spotify_bp, url_prefix='/spotify')
|
||||||
|
app.register_blueprint(terminal_bp)
|
||||||
|
|
||||||
dotenv.load_dotenv()
|
dotenv.load_dotenv()
|
||||||
|
|
||||||
@@ -704,7 +707,7 @@ def catch_all(path: str):
|
|||||||
return error_response(request, message="Restricted route", code=403)
|
return error_response(request, message="Restricted route", code=403)
|
||||||
|
|
||||||
# If curl request, return curl response
|
# If curl request, return curl response
|
||||||
if isCurl(request):
|
if isCurl(request) and valid_curl_path(path):
|
||||||
return curl_response(request)
|
return curl_response(request)
|
||||||
|
|
||||||
if path in REDIRECT_ROUTES:
|
if path in REDIRECT_ROUTES:
|
||||||
|
|||||||
472
templates/terminal.html
Normal file
472
templates/terminal.html
Normal file
@@ -0,0 +1,472 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Terminal | Nathan.Woodburn/</title>
|
||||||
|
<style>
|
||||||
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||||
|
body {
|
||||||
|
background: #000;
|
||||||
|
font-family: 'Courier New', monospace;
|
||||||
|
color: #0f0;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
#terminal {
|
||||||
|
width: 100vw;
|
||||||
|
height: 100vh;
|
||||||
|
padding: 20px;
|
||||||
|
overflow-y: auto;
|
||||||
|
}
|
||||||
|
.line { margin-bottom: 5px; }
|
||||||
|
.prompt { color: #0f0; white-space: pre; }
|
||||||
|
.prompt-line { color: #0f0; }
|
||||||
|
.output { color: #fff; white-space: pre-wrap; }
|
||||||
|
.error { color: #f00; }
|
||||||
|
#input-line {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
.input-row {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
#command-input {
|
||||||
|
background: transparent;
|
||||||
|
border: none;
|
||||||
|
color: #0f0;
|
||||||
|
font-family: 'Courier New', monospace;
|
||||||
|
font-size: 16px;
|
||||||
|
outline: none;
|
||||||
|
flex: 1;
|
||||||
|
caret-color: #0f0;
|
||||||
|
}
|
||||||
|
#nano-editor {
|
||||||
|
display: none;
|
||||||
|
position: fixed;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100vw;
|
||||||
|
height: 100vh;
|
||||||
|
background: #000;
|
||||||
|
z-index: 1000;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
#nano-editor.active {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
#nano-header {
|
||||||
|
padding: 10px 20px;
|
||||||
|
border-bottom: 1px solid #0f0;
|
||||||
|
color: #0f0;
|
||||||
|
}
|
||||||
|
#nano-textarea {
|
||||||
|
flex: 1;
|
||||||
|
background: #000;
|
||||||
|
color: #fff;
|
||||||
|
border: none;
|
||||||
|
padding: 20px;
|
||||||
|
font-family: 'Courier New', monospace;
|
||||||
|
font-size: 16px;
|
||||||
|
resize: none;
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
#nano-footer {
|
||||||
|
padding: 10px 20px;
|
||||||
|
border-top: 1px solid #0f0;
|
||||||
|
color: #0f0;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
#nano-save-prompt {
|
||||||
|
display: none;
|
||||||
|
padding: 10px 20px;
|
||||||
|
background: #111;
|
||||||
|
border-top: 1px solid #0f0;
|
||||||
|
color: #fff;
|
||||||
|
}
|
||||||
|
#nano-save-prompt.active {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div id="terminal">
|
||||||
|
<div class="line output">Nathan.Woodburn/</div>
|
||||||
|
<div class="line output">Type 'help' for available commands</div>
|
||||||
|
<div class="line output"></div>
|
||||||
|
<div id="input-line">
|
||||||
|
<div class="prompt-line">┌──({{user}}@NW)-[~]</div>
|
||||||
|
<div class="input-row">
|
||||||
|
<span class="prompt-line">└─$ </span>
|
||||||
|
<input type="text" id="command-input" autofocus autocomplete="off" spellcheck="false" autocapitalize="none">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Nano Editor -->
|
||||||
|
<div id="nano-editor">
|
||||||
|
<div id="nano-header">nano - <span id="nano-filename"></span></div>
|
||||||
|
<textarea id="nano-textarea"></textarea>
|
||||||
|
<div id="nano-save-prompt">Save modified buffer? (Y/N)</div>
|
||||||
|
<div id="nano-footer">^X Exit ^O Save (Ctrl+X to exit, Ctrl+O to save)</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const terminal = document.getElementById('terminal');
|
||||||
|
const input = document.getElementById('command-input');
|
||||||
|
const inputLine = document.getElementById('input-line');
|
||||||
|
const nanoEditor = document.getElementById('nano-editor');
|
||||||
|
const nanoTextarea = document.getElementById('nano-textarea');
|
||||||
|
const nanoFilename = document.getElementById('nano-filename');
|
||||||
|
let commandHistory = [];
|
||||||
|
let historyIndex = -1;
|
||||||
|
let currentPwd = '~';
|
||||||
|
let nanoCurrentFile = '';
|
||||||
|
let nanoOriginalContent = '';
|
||||||
|
let nanoSavePromptActive = false;
|
||||||
|
let tabCompletionIndex = 0;
|
||||||
|
let tabCompletionMatches = [];
|
||||||
|
let lastTabInput = '';
|
||||||
|
|
||||||
|
// Function to update the prompt with current pwd
|
||||||
|
async function updatePrompt() {
|
||||||
|
try {
|
||||||
|
const response = await fetch('/terminal/pwd');
|
||||||
|
const data = await response.json();
|
||||||
|
if (data.output) {
|
||||||
|
currentPwd = data.output;
|
||||||
|
inputLine.querySelector('.prompt-line:first-child').textContent = `┌──({{user}}@NW)-[${currentPwd}]`;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to fetch pwd:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to open nano editor
|
||||||
|
async function openNano(filename) {
|
||||||
|
nanoCurrentFile = filename;
|
||||||
|
nanoFilename.textContent = filename;
|
||||||
|
nanoSavePromptActive = false;
|
||||||
|
document.getElementById('nano-save-prompt').classList.remove('active');
|
||||||
|
|
||||||
|
// Try to fetch existing file content
|
||||||
|
try {
|
||||||
|
const parts = ['cat'].concat(filename.split(' '));
|
||||||
|
const response = await fetch('/terminal/execute/cat', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ args: filename })
|
||||||
|
});
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (data.output && !data.output.startsWith('cat:')) {
|
||||||
|
nanoTextarea.value = data.output;
|
||||||
|
nanoOriginalContent = data.output;
|
||||||
|
} else {
|
||||||
|
nanoTextarea.value = '';
|
||||||
|
nanoOriginalContent = '';
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
nanoTextarea.value = '';
|
||||||
|
nanoOriginalContent = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
nanoEditor.classList.add('active');
|
||||||
|
nanoTextarea.focus();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to close nano editor
|
||||||
|
async function closeNano(shouldSave = false) {
|
||||||
|
if (shouldSave) {
|
||||||
|
await saveNano();
|
||||||
|
}
|
||||||
|
|
||||||
|
nanoEditor.classList.remove('active');
|
||||||
|
nanoSavePromptActive = false;
|
||||||
|
document.getElementById('nano-save-prompt').classList.remove('active');
|
||||||
|
terminal.appendChild(inputLine);
|
||||||
|
inputLine.style.display = 'block';
|
||||||
|
input.disabled = false;
|
||||||
|
input.focus();
|
||||||
|
terminal.scrollTop = terminal.scrollHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to save nano content
|
||||||
|
async function saveNano() {
|
||||||
|
const content = nanoTextarea.value;
|
||||||
|
try {
|
||||||
|
const response = await fetch('/terminal/execute/nano', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ args: `${nanoCurrentFile} CONTENT ${content}` })
|
||||||
|
});
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
nanoOriginalContent = content; // Update original content after save
|
||||||
|
// If output contains error, display it
|
||||||
|
if (data.output && data.output.startsWith('nano:')) {
|
||||||
|
const errorLine = document.createElement('div');
|
||||||
|
errorLine.className = 'line error';
|
||||||
|
errorLine.textContent = data.output;
|
||||||
|
terminal.appendChild(errorLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't display output - nano saves silently
|
||||||
|
} catch (err) {
|
||||||
|
const errorLine = document.createElement('div');
|
||||||
|
errorLine.className = 'line error';
|
||||||
|
errorLine.textContent = 'Error saving file: ' + err.message;
|
||||||
|
terminal.appendChild(errorLine);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if content has changed
|
||||||
|
function hasUnsavedChanges() {
|
||||||
|
return nanoTextarea.value !== nanoOriginalContent;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nano editor keyboard shortcuts
|
||||||
|
nanoTextarea.addEventListener('keydown', async (e) => {
|
||||||
|
if (e.ctrlKey && e.key === 'x') {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
if (nanoSavePromptActive) {
|
||||||
|
return; // Ignore if prompt is already active
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasUnsavedChanges()) {
|
||||||
|
// Show save prompt
|
||||||
|
nanoSavePromptActive = true;
|
||||||
|
document.getElementById('nano-save-prompt').classList.add('active');
|
||||||
|
document.getElementById('nano-footer').textContent = 'Press Y to save, N to discard, Ctrl+C to cancel';
|
||||||
|
} else {
|
||||||
|
await closeNano(false);
|
||||||
|
}
|
||||||
|
} else if (e.ctrlKey && e.key === 'o') {
|
||||||
|
e.preventDefault();
|
||||||
|
await saveNano();
|
||||||
|
} else if (e.ctrlKey && e.key === 'c' && nanoSavePromptActive) {
|
||||||
|
e.preventDefault();
|
||||||
|
// Cancel save prompt
|
||||||
|
nanoSavePromptActive = false;
|
||||||
|
document.getElementById('nano-save-prompt').classList.remove('active');
|
||||||
|
document.getElementById('nano-footer').textContent = '^X Exit ^O Save (Ctrl+X to exit, Ctrl+O to save)';
|
||||||
|
} else if (nanoSavePromptActive && (e.key === 'y' || e.key === 'Y')) {
|
||||||
|
e.preventDefault();
|
||||||
|
await closeNano(true);
|
||||||
|
} else if (nanoSavePromptActive && (e.key === 'n' || e.key === 'N')) {
|
||||||
|
e.preventDefault();
|
||||||
|
await closeNano(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Function to get available commands
|
||||||
|
function getAvailableCommands() {
|
||||||
|
return ['help', 'about', 'clear', 'echo', 'whoami', 'ls', 'pwd', 'date', 'cd', 'cat', 'rm', 'tree', 'touch', 'nano', 'reset', 'exit'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to get files/directories for tab completion
|
||||||
|
async function getFilesForCompletion(path = '') {
|
||||||
|
try {
|
||||||
|
const response = await fetch('/terminal/execute/ls', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ args: path ? path : '' })
|
||||||
|
});
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (data.output && !data.output.startsWith('ls:')) {
|
||||||
|
return data.output.split(' ').filter(f => f.length > 0);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to get files:', err);
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to handle tab completion
|
||||||
|
async function handleTabCompletion(currentInput) {
|
||||||
|
const parts = currentInput.split(' ');
|
||||||
|
const isFirstWord = parts.length === 1;
|
||||||
|
|
||||||
|
if (isFirstWord) {
|
||||||
|
// Complete command names
|
||||||
|
const commands = getAvailableCommands();
|
||||||
|
const matches = commands.filter(cmd => cmd.startsWith(parts[0]));
|
||||||
|
|
||||||
|
if (matches.length === 1) {
|
||||||
|
return matches[0] + ' ';
|
||||||
|
} else if (matches.length > 1) {
|
||||||
|
// Cycle through matches
|
||||||
|
if (lastTabInput === currentInput) {
|
||||||
|
tabCompletionIndex = (tabCompletionIndex + 1) % matches.length;
|
||||||
|
} else {
|
||||||
|
tabCompletionIndex = 0;
|
||||||
|
tabCompletionMatches = matches;
|
||||||
|
}
|
||||||
|
lastTabInput = currentInput;
|
||||||
|
return matches[tabCompletionIndex] + ' ';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Complete file/directory names
|
||||||
|
const lastPart = parts[parts.length - 1];
|
||||||
|
const files = await getFilesForCompletion();
|
||||||
|
const matches = files.filter(f => f.startsWith(lastPart));
|
||||||
|
|
||||||
|
if (matches.length === 1) {
|
||||||
|
parts[parts.length - 1] = matches[0];
|
||||||
|
return parts.join(' ') + ' ';
|
||||||
|
} else if (matches.length > 1) {
|
||||||
|
// Cycle through matches
|
||||||
|
if (lastTabInput === currentInput) {
|
||||||
|
tabCompletionIndex = (tabCompletionIndex + 1) % matches.length;
|
||||||
|
} else {
|
||||||
|
tabCompletionIndex = 0;
|
||||||
|
tabCompletionMatches = matches;
|
||||||
|
}
|
||||||
|
lastTabInput = currentInput;
|
||||||
|
parts[parts.length - 1] = matches[tabCompletionIndex];
|
||||||
|
return parts.join(' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update prompt on load
|
||||||
|
updatePrompt();
|
||||||
|
|
||||||
|
input.addEventListener('keydown', async (e) => {
|
||||||
|
if (e.key === 'Tab') {
|
||||||
|
e.preventDefault();
|
||||||
|
const currentInput = input.value;
|
||||||
|
const completed = await handleTabCompletion(currentInput);
|
||||||
|
input.value = completed;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset tab completion on any other key
|
||||||
|
if (e.key !== 'Tab') {
|
||||||
|
tabCompletionIndex = 0;
|
||||||
|
tabCompletionMatches = [];
|
||||||
|
lastTabInput = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (e.key === 'Enter') {
|
||||||
|
const command = input.value.trim();
|
||||||
|
|
||||||
|
// Disable input and hide prompt during execution
|
||||||
|
input.disabled = true;
|
||||||
|
inputLine.style.display = 'none';
|
||||||
|
|
||||||
|
// Display command
|
||||||
|
const cmdLine = document.createElement('div');
|
||||||
|
cmdLine.className = 'line';
|
||||||
|
cmdLine.innerHTML = `<span class="prompt">┌──({{user}}@NW)-[${currentPwd}]\n└─$ </span>${command}`;
|
||||||
|
terminal.appendChild(cmdLine);
|
||||||
|
|
||||||
|
// Add to history
|
||||||
|
if (command) {
|
||||||
|
commandHistory.push(command);
|
||||||
|
historyIndex = commandHistory.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
input.value = '';
|
||||||
|
|
||||||
|
// Handle clear command locally
|
||||||
|
if (command === 'clear') {
|
||||||
|
terminal.innerHTML = '';
|
||||||
|
terminal.appendChild(inputLine);
|
||||||
|
inputLine.style.display = 'block';
|
||||||
|
input.disabled = false;
|
||||||
|
input.focus();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (command === 'exit') {
|
||||||
|
// Redirect to /
|
||||||
|
window.location.href = '/';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle nano command specially
|
||||||
|
const parts = command.split(' ');
|
||||||
|
const baseCommand = parts[0];
|
||||||
|
|
||||||
|
if (baseCommand === 'nano') {
|
||||||
|
if (parts.length < 2) {
|
||||||
|
const errorLine = document.createElement('div');
|
||||||
|
errorLine.className = 'line output';
|
||||||
|
errorLine.textContent = 'Usage: nano [filename]';
|
||||||
|
terminal.appendChild(errorLine);
|
||||||
|
terminal.appendChild(inputLine);
|
||||||
|
inputLine.style.display = 'block';
|
||||||
|
input.disabled = false;
|
||||||
|
input.focus();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await openNano(parts.slice(1).join(' '));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute command
|
||||||
|
if (command) {
|
||||||
|
try {
|
||||||
|
// Split command by space
|
||||||
|
const args = parts.slice(1).join(' ');
|
||||||
|
|
||||||
|
// POST request to /terminal/execute/command
|
||||||
|
const response = await fetch(`/terminal/execute/${encodeURIComponent(baseCommand)}`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ args: args })
|
||||||
|
});
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (data.output) {
|
||||||
|
const outputLine = document.createElement('div');
|
||||||
|
outputLine.className = 'line output';
|
||||||
|
outputLine.textContent = data.output;
|
||||||
|
terminal.appendChild(outputLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update prompt after cd command
|
||||||
|
if (baseCommand === 'cd' || baseCommand === 'reset') {
|
||||||
|
await updatePrompt();
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
const errorLine = document.createElement('div');
|
||||||
|
errorLine.className = 'line error';
|
||||||
|
errorLine.textContent = 'Error: ' + err.message;
|
||||||
|
terminal.appendChild(errorLine);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show prompt and re-enable input after command completes
|
||||||
|
terminal.appendChild(inputLine);
|
||||||
|
inputLine.style.display = 'block';
|
||||||
|
input.disabled = false;
|
||||||
|
input.focus();
|
||||||
|
terminal.scrollTop = terminal.scrollHeight;
|
||||||
|
} else if (e.key === 'ArrowUp') {
|
||||||
|
e.preventDefault();
|
||||||
|
if (historyIndex > 0) {
|
||||||
|
historyIndex--;
|
||||||
|
input.value = commandHistory[historyIndex];
|
||||||
|
}
|
||||||
|
} else if (e.key === 'ArrowDown') {
|
||||||
|
e.preventDefault();
|
||||||
|
if (historyIndex < commandHistory.length - 1) {
|
||||||
|
historyIndex++;
|
||||||
|
input.value = commandHistory[historyIndex];
|
||||||
|
} else {
|
||||||
|
historyIndex = commandHistory.length;
|
||||||
|
input.value = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Keep input focused
|
||||||
|
document.addEventListener('click', () => input.focus());
|
||||||
|
input.focus();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user