Formatting and some completion to install script
This commit is contained in:
90
main.py
90
main.py
@@ -2,6 +2,7 @@
|
||||
This is the PyJail, a jailing tool for running Python apps in a sandboxed environment.
|
||||
Version: edge0005-base0.2.1
|
||||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
import runpy
|
||||
@@ -11,6 +12,7 @@ class PyJail:
|
||||
"""
|
||||
The jail manager, handles all system calls and such.
|
||||
"""
|
||||
|
||||
def __init__(self, debug=False):
|
||||
self.rootpath = ""
|
||||
self.rootpath = self.fs()
|
||||
@@ -31,15 +33,21 @@ class PyJail:
|
||||
"""
|
||||
path_to_bin = self.fs(path_to_bin)
|
||||
if path_to_bin == 3 or path_to_bin == 2:
|
||||
self.msg("jailmgr.run_program()", "An error has occurred launching the program.", True,
|
||||
"WARNING")
|
||||
self.msg(
|
||||
"jailmgr.run_program()",
|
||||
"An error has occurred launching the program.",
|
||||
True,
|
||||
"WARNING",
|
||||
)
|
||||
else:
|
||||
with open(self.fs("/proc/kproc"), "a+") as f:
|
||||
f.write(f"proc: {path_to_bin}({self._program_counter})")
|
||||
self._program_counter += 1
|
||||
runpy.run_path(path_to_bin)
|
||||
|
||||
def msg(self, caller: str, message: str, emit: bool = False, log_level: str = "INFO"):
|
||||
def msg(
|
||||
self, caller: str, message: str, emit: bool = False, log_level: str = "INFO"
|
||||
):
|
||||
"""
|
||||
The custom message parser, can parse messages and alert apps of said messages.
|
||||
Replaces print statements.
|
||||
@@ -53,7 +61,9 @@ class PyJail:
|
||||
emit = True
|
||||
accepted_log_levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
|
||||
if log_level.upper() not in accepted_log_levels:
|
||||
self.msg(f"jailmgr.msg()",f"Not accepted loglevel!! {log_level}", False, "ERROR")
|
||||
self.msg(
|
||||
"jailmgr.msg()", f"Not accepted loglevel!! {log_level}", False, "ERROR"
|
||||
)
|
||||
return 1
|
||||
msg = f"[{time.time}] [{caller}] [{log_level}] {message}"
|
||||
with open(self.fs("/proc/klog"), "a+") as f:
|
||||
@@ -83,7 +93,11 @@ class PyJail:
|
||||
check_path_split = check_path.split("/", -1)
|
||||
prepend_path = ""
|
||||
for i, path in enumerate(check_path_split):
|
||||
prepend_path = f"{prepend_path}/{path}" if not path.endswith("/") else f"{prepend_path}/{path}/"
|
||||
prepend_path = (
|
||||
f"{prepend_path}/{path}"
|
||||
if not path.endswith("/")
|
||||
else f"{prepend_path}/{path}/"
|
||||
)
|
||||
check_path_split[i] = f"{prepend_path}"
|
||||
check_path = ""
|
||||
for i, path in enumerate(check_path_split):
|
||||
@@ -93,7 +107,9 @@ class PyJail:
|
||||
elif os.path.isdir(path) and i == (len(check_path_split) - 1):
|
||||
# The last thing to access was a directory. We can safely return the full path now.
|
||||
return path
|
||||
elif not os.path.isdir(path) and i != (len(check_path_split) - 1):
|
||||
elif not os.path.isdir(path) and i != (
|
||||
len(check_path_split) - 1
|
||||
):
|
||||
# This was not the last thing we needed to access, so we assume it's a symlink.
|
||||
# One problem is that we can't be sure, so we make sure it is a symlink.
|
||||
with open(self.fs(path, False)) as f:
|
||||
@@ -109,12 +125,21 @@ class PyJail:
|
||||
return symlink_dest
|
||||
else:
|
||||
# This is either not a symlink or an improperly configured one.
|
||||
self.msg("jailmgr.fs()", "reached non-symlink file not at end of list",
|
||||
False, "ERROR")
|
||||
self.msg("jailmgr.fs()", "What was assumed to be a directory isn't a"
|
||||
" directory nor a symlink! This might be because of a "
|
||||
"typo or misconfigured symlink. The directory in question: "
|
||||
f"{path}", True, "WARNING")
|
||||
self.msg(
|
||||
"jailmgr.fs()",
|
||||
"reached non-symlink file not at end of list",
|
||||
False,
|
||||
"ERROR",
|
||||
)
|
||||
self.msg(
|
||||
"jailmgr.fs()",
|
||||
"What was assumed to be a directory isn't a"
|
||||
" directory nor a symlink! This might be because of a "
|
||||
"typo or misconfigured symlink. The directory in question: "
|
||||
f"{path}",
|
||||
True,
|
||||
"WARNING",
|
||||
)
|
||||
return 2
|
||||
|
||||
return self.rootpath + check_path
|
||||
@@ -122,11 +147,19 @@ class PyJail:
|
||||
check_path = check_path.lstrip(".")
|
||||
return os.getcwd() + check_path if "vfs" in os.getcwd() else 2
|
||||
elif self.rootpath in check_path:
|
||||
self.msg(f"jailmgr.fs()", "Cannot parse rootpath, expected vfspath", log_level="ERROR")
|
||||
self.msg(
|
||||
"jailmgr.fs()",
|
||||
"Cannot parse rootpath, expected vfspath",
|
||||
log_level="ERROR",
|
||||
)
|
||||
return 3
|
||||
else:
|
||||
# Path is not in the jailed fs, so we say it doesn't exist.
|
||||
self.msg(f"jailmgr.fs()", "File/directory doesn't exist in vfspath", log_level="ERROR")
|
||||
self.msg(
|
||||
"jailmgr.fs()",
|
||||
"File/directory doesn't exist in vfspath",
|
||||
log_level="ERROR",
|
||||
)
|
||||
return 2
|
||||
else:
|
||||
rootpath = os.getcwd() + "/vfs"
|
||||
@@ -137,7 +170,7 @@ class PyJail:
|
||||
"""
|
||||
Returns the kernel version
|
||||
"""
|
||||
return "edge0005-base0.2.1"
|
||||
return "edge0006-base0.2.1"
|
||||
|
||||
def netsock(self, ip, port, mode, msg):
|
||||
"""
|
||||
@@ -160,21 +193,30 @@ class PyJail:
|
||||
if mode == "TCP":
|
||||
try:
|
||||
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
except Exception as e:
|
||||
self.msg("jailmgr.netsock()", "Socket import failed!", False, "CRITICAL")
|
||||
self.msg("jailmgr.netsock()", "An unexpected error occurred!", True, "ERROR")
|
||||
except Exception:
|
||||
self.msg(
|
||||
"jailmgr.netsock()", "Socket import failed!", False, "CRITICAL"
|
||||
)
|
||||
self.msg(
|
||||
"jailmgr.netsock()", "An unexpected error occurred!", True, "ERROR"
|
||||
)
|
||||
return None
|
||||
# Connect to the server
|
||||
client_socket.connect((ip, port))
|
||||
self.msg("jailmgr.netsock()", f"Connected to server at {server_ip}:{server_port}", False, "INFO")
|
||||
self.msg(
|
||||
"jailmgr.netsock()",
|
||||
f"Connected to server at {ip}:{port}",
|
||||
False,
|
||||
"INFO",
|
||||
)
|
||||
|
||||
# Send the message to the server
|
||||
client_socket.send(message.encode())
|
||||
client_socket.send(msg.encode())
|
||||
|
||||
# Receive the response from the server
|
||||
response = client_socket.recv(1024).decode()
|
||||
client_socket.close() # Close the connection
|
||||
self.msg("jailmgr.netsock", f"Received from server: {response}", False "INFO")
|
||||
self.msg("jailmgr.netsock", f"Received from server: {response}")
|
||||
|
||||
return response
|
||||
|
||||
@@ -182,15 +224,15 @@ class PyJail:
|
||||
# raise NotImplementedError("TODO: PKG will be implemented later!")
|
||||
file_io = requests.get(ip)
|
||||
if file_io.startswith("PYPAK PMD"):
|
||||
with open(self.fs(f"/usr/netsock/cache/{pkg}.pmd"), "a+") as f:
|
||||
with open(self.fs(f"/usr/netsock/cache/{msg}.pmd"), "a+") as f:
|
||||
f.write(file_io)
|
||||
f.close()
|
||||
else:
|
||||
with open(self.fs(f"/usr/netsock/cache/{pkg}.py"))
|
||||
with open(self.fs(f"/usr/netsock/cache/{msg}.py"), "a+") as f:
|
||||
f.write(file_io)
|
||||
f.close()
|
||||
|
||||
else:
|
||||
raise NotImplementedError("TODO: UDP will be implemented later!")
|
||||
|
||||
# raise NotImplementedError("TODO: Netsock will be implemented once 0.3.0 comes around!")
|
||||
|
||||
|
Reference in New Issue
Block a user