Only log messages if logging is explicitly enabled

This commit is contained in:
ChaoticByte 2024-06-19 11:43:22 +02:00
parent fa9ee50336
commit 4e2e322066
No known key found for this signature in database

View file

@ -17,10 +17,11 @@ import yaml
config_host = "" config_host = ""
config_port = 8022 config_port = 8022
connected_clients = []
config_clients = { config_clients = {
# username: asyncssh.SSHAuthorizedKeys # username: asyncssh.SSHAuthorizedKeys
} }
enable_logging = False
connected_clients = []
class SSHServer(asyncssh.SSHServer): class SSHServer(asyncssh.SSHServer):
@ -56,12 +57,14 @@ async def handle_connection(process: asyncssh.SSHServerProcess):
try: try:
# hello there # hello there
connected_msg = f"[connected] {username}\n" connected_msg = f"[connected] {username}\n"
if enable_logging:
stderr.write(connected_msg) stderr.write(connected_msg)
broadcast(connected_msg, True) broadcast(connected_msg, True)
if process.command is not None: if process.command is not None:
# client has provided a command as a ssh commandline argument # client has provided a command as a ssh commandline argument
line = process.command.strip("\r\n") line = process.command.strip("\r\n")
msg = f"{username}: {line}\n" msg = f"{username}: {line}\n"
if enable_logging:
stdout.write(msg) stdout.write(msg)
broadcast(msg) broadcast(msg)
else: else:
@ -72,6 +75,7 @@ async def handle_connection(process: asyncssh.SSHServerProcess):
if line == "": raise asyncssh.BreakReceived(0) if line == "": raise asyncssh.BreakReceived(0)
line = line.strip('\r\n') line = line.strip('\r\n')
msg = f"{username}: {line}\n" msg = f"{username}: {line}\n"
if enable_logging:
stdout.write(msg) stdout.write(msg)
broadcast(msg) broadcast(msg)
except asyncssh.TerminalSizeChanged: except asyncssh.TerminalSizeChanged:
@ -88,6 +92,7 @@ async def handle_connection(process: asyncssh.SSHServerProcess):
disconnected_msg = f"[disconnected] {username}\n" disconnected_msg = f"[disconnected] {username}\n"
process.exit(0) process.exit(0)
connected_clients.remove(process) connected_clients.remove(process)
if enable_logging:
stderr.write(disconnected_msg) stderr.write(disconnected_msg)
broadcast(disconnected_msg, True) broadcast(disconnected_msg, True)
@ -97,11 +102,13 @@ if __name__ == "__main__":
argp = ArgumentParser() argp = ArgumentParser()
argp.add_argument("config", type=Path, help="The path to the config file") argp.add_argument("config", type=Path, help="The path to the config file")
argp.add_argument("pkey", type=Path, help="The path to the ssh private key") argp.add_argument("pkey", type=Path, help="The path to the ssh private key")
argp.add_argument("--log", action="store_true", help="Enable logging to stdout and stderr")
args = argp.parse_args() args = argp.parse_args()
# read config # read config
config = yaml.safe_load(args.config.read_text()) config = yaml.safe_load(args.config.read_text())
config_host = str(config["host"]) config_host = str(config["host"])
config_port = int(config["port"]) config_port = int(config["port"])
enable_logging = args.log
try: try:
config_private_key = asyncssh.import_private_key(args.pkey.read_text()) config_private_key = asyncssh.import_private_key(args.pkey.read_text())
except asyncssh.public_key.KeyImportError as e: except asyncssh.public_key.KeyImportError as e: