Skip to content

Commit

Permalink
import fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
haliphax committed Feb 28, 2023
1 parent 2adf4e7 commit 2b72ac0
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 33 deletions.
6 changes: 4 additions & 2 deletions xthulu/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import click

# local
from . import config, db, log
from . import db
from .configuration import get_config
from .logger import log
from .ssh import start_server

loop = get_event_loop()
Expand Down Expand Up @@ -88,7 +90,7 @@ def main():
"""Main method for CLI; binds to the database before invoking methods."""

async def f():
await db.set_bind(config["db"]["bind"])
await db.set_bind(get_config("db.bind"))

loop.run_until_complete(f())
cli()
Expand Down
2 changes: 1 addition & 1 deletion xthulu/locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from functools import partial

# local
from . import log
from .logger import log


class Locks:
Expand Down
3 changes: 2 additions & 1 deletion xthulu/ssh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
from asyncssh import listen

# local
from .. import db, log
from .. import db
from ..configuration import get_config
from ..logger import log
from .proxy_protocol import ProxyProtocolListener
from .server import SSHServer

Expand Down
18 changes: 8 additions & 10 deletions xthulu/ssh/server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""SSH server implementation"""

# stdlib
import asyncio as aio
from asyncio import gather, IncompleteReadError, Queue, TimeoutError, wait_for
from datetime import datetime
from multiprocessing import Pipe, Process

Expand All @@ -20,11 +20,11 @@
from ..context import Context
from ..events import EventQueues
from ..exceptions import Goto, ProcessClosing
from ..logger import log
from ..models import User
from ..structs import EventData, Script
from ..terminal import terminal_process
from ..terminal.proxy_terminal import ProxyTerminal
from . import log


class SSHServer(AsyncSSHServer):
Expand All @@ -50,7 +50,7 @@ def connection_made(self, conn: SSHServerConnection):

self._peername = conn.get_extra_info("peername")
self._sid = "{}:{}".format(*self._peername)
EventQueues.q[self._sid] = aio.Queue()
EventQueues.q[self._sid] = Queue()
log.info(f"{self._peername[0]} connecting")

def connection_lost(self, exc: Exception):
Expand Down Expand Up @@ -178,7 +178,7 @@ async def handle_client(cls, proc: SSHServerProcess):
cx.env["COLUMNS"] = str(w)
cx.env["LINES"] = str(h)
proxy_pipe, subproc_pipe = Pipe()
session_stdin = aio.Queue()
session_stdin = Queue()
timeout = int(get_config("ssh.session.timeout", 120))
await cx.user.update(last=datetime.utcnow()).apply() # type: ignore

Expand All @@ -188,18 +188,16 @@ async def input_loop():
while True:
try:
if timeout > 0:
r = await aio.wait_for(
proc.stdin.readexactly(1), timeout
)
r = await wait_for(proc.stdin.readexactly(1), timeout)
else:
r = await proc.stdin.readexactly(1)

await session_stdin.put(r)

except aio.IncompleteReadError:
except IncompleteReadError:
return

except aio.TimeoutError:
except TimeoutError:
cx.echo(cx.term.bold_red_on_black("\r\nTimed out.\r\n"))
log.warning(f"{cx.user.name}@{cx.sid} timed out")
proc.channel.close()
Expand Down Expand Up @@ -264,4 +262,4 @@ async def main_process():
proc.close()

log.info(f"{cx.user}@{cx.ip} starting terminal session")
await aio.gather(input_loop(), main_process(), return_exceptions=True)
await gather(input_loop(), main_process(), return_exceptions=True)
6 changes: 4 additions & 2 deletions xthulu/terminal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import os

# local
from .. import config, log
from ..configuration import get_config
from ..logger import log
from .subprocess import SubprocessTerminal

debug_term = bool(config.get("debug", {}).get("term", False))
debug_term = bool(get_config("debug.term", False))
"""Whether terminal debugging is enabled"""


def terminal_process(
Expand Down
23 changes: 12 additions & 11 deletions xthulu/terminal/proxy_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, Callable

# stdlib
import asyncio as aio
from asyncio import IncompleteReadError, TimeoutError, Queue, wait_for
import contextlib
from multiprocessing.connection import Connection

Expand All @@ -14,11 +14,12 @@
from blessed.formatters import FormattingOtherString, ParameterizingString

# local
from .. import config, log
from ..configuration import get_config
from ..exceptions import ProcessClosing
from ..logger import log
from .proxy_call import TerminalProxyCall

debug_term = bool(config.get("debug", {}).get("term", False))
debug_term = bool(get_config("debug.term", False))
"""Whether terminal debugging is enabled"""


Expand Down Expand Up @@ -51,7 +52,7 @@ class ProxyTerminal:

def __init__(
self,
stdin: aio.Queue[bytes],
stdin: Queue[bytes],
stdout: Any,
encoding: str,
pipe_master: Connection,
Expand Down Expand Up @@ -198,18 +199,18 @@ async def inkey(
# don't actually wait indefinitely; wait in 0.1 second
# increments so that the coroutine can be aborted if
# the connection is dropped
inp = await aio.wait_for(self.stdin.get(), 0.1)
inp = await wait_for(self.stdin.get(), 0.1)
ucs += inp.decode(self.encoding)
else:
inp = await aio.wait_for(self.stdin.get(), timeout)
inp = await wait_for(self.stdin.get(), timeout)
ucs += inp.decode(self.encoding)

break

except aio.IncompleteReadError:
except IncompleteReadError:
raise ProcessClosing()

except aio.TimeoutError:
except TimeoutError:
if timeout is not None:
break

Expand All @@ -221,13 +222,13 @@ async def inkey(
# esc was received; let's see if we're getting a key sequence
while ucs in self._keymap_prefixes: # type: ignore
try:
inp = await aio.wait_for(self.stdin.get(), esc_delay)
inp = await wait_for(self.stdin.get(), esc_delay)
ucs += inp.decode(self.encoding)

except aio.IncompleteReadError:
except IncompleteReadError:
raise ProcessClosing()

except aio.TimeoutError:
except TimeoutError:
break

ks = (
Expand Down
2 changes: 1 addition & 1 deletion xthulu/terminal/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from blessed.terminal import Terminal

# local
from .. import log
from ..logger import log


class SubprocessTerminal(Terminal):
Expand Down
8 changes: 4 additions & 4 deletions xthulu/ui/art.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""art module"""

# stdlib
import asyncio as aio
from asyncio import sleep
from os.path import exists, isfile
import re

# 3rd party
import aiofiles
from aiofiles import open
from blessed.keyboard import Keystroke
from blessed.sequences import SequenceTextWrapper

Expand Down Expand Up @@ -59,7 +59,7 @@ def newline():

file_lines: list[str]

async with aiofiles.open(filename, "r", encoding=encoding) as f:
async with open(filename, "r", encoding=encoding) as f:
file_lines = await f.readlines()

lines: list[str] = []
Expand Down Expand Up @@ -116,6 +116,6 @@ def newline():
newline()
return ks
else:
await aio.sleep(delay)
await sleep(delay)

newline()
2 changes: 1 addition & 1 deletion xthulu/ui/editors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from blessed.keyboard import Keystroke

# local
from .. import log
from ..logger import log
from ..terminal.proxy_terminal import ProxyTerminal


Expand Down

0 comments on commit 2b72ac0

Please sign in to comment.