Skip to content

Commit

Permalink
Rewritten some parts of tunnel for better control, and make some
Browse files Browse the repository at this point in the history
  • Loading branch information
dkmstr committed Mar 6, 2024
1 parent 3f1fdcf commit 6f8bf4f
Show file tree
Hide file tree
Showing 10 changed files with 720 additions and 105 deletions.
6 changes: 5 additions & 1 deletion src/UDSClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def waiting_tasks_processor() -> None:
# Removing
try:
logger.debug('Executing threads before exit')
tools.exec_before_exit()
tools.execute_before_exit()
except Exception as e: # pylint: disable=broad-exception-caught
logger.debug('execBeforeExit: %s', e)

Expand Down Expand Up @@ -379,6 +379,10 @@ def parse_arguments(args: typing.List[str]) -> typing.Tuple[str, str, str, bool]
)
elif urlinfo.scheme != 'udss':
raise exceptions.MessageException('Not supported protocol') # Just shows "about" dialog

# If ticket length is not valid
if len(ticket) != consts.TICKET_LENGTH:
raise exceptions.MessageException(f'Invalid ticket: {ticket}')

return (
urlinfo.netloc,
Expand Down
2 changes: 2 additions & 0 deletions src/uds/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ def _feature_requested(env_var: str) -> bool:
LISTEN_ADDRESS_V6: typing.Final[str] = '::1'
RESPONSE_OK: typing.Final[bytes] = b'OK'

# Ticket length
TICKET_LENGTH: typing.Final[int] = 48

# Constants strings for protocol
HANDSHAKE_V1: typing.Final[bytes] = b'\x5AMGB\xA5\x01\x00'
Expand Down
28 changes: 19 additions & 9 deletions src/uds/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def process_iter(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
# at the same time for the same process, so no need to lock
_unlink_files: typing.List[types.RemovableFile] = []
_awaitable_tasks: typing.List[types.AwaitableTask] = []
_execBeforeExit: typing.List[typing.Callable[[], None]] = []
_execute_before_exit: typing.List[typing.Callable[[], None]] = []

sys_fs_enc = sys.getfilesystemencoding() or 'mbcs'

Expand Down Expand Up @@ -158,7 +158,7 @@ def unlink_files(early_stage: bool = False) -> None:
logger.debug('File %s not deleted: %s', f[0], e)

# Remove all processed files from list
_unlink_files[:] = list(filter(lambda x: x.early_stage != early_stage, _unlink_files))
_unlink_files[:] = list(set(_unlink_files) - set(files_to_unlink))


def add_task_to_wait(task: typing.Any, wait_subprocesses: bool = False) -> None:
Expand Down Expand Up @@ -197,18 +197,22 @@ def wait_for_tasks() -> None:
logger.error('Waiting for tasks to finish error: %s', e)

# Empty the list
_awaitable_tasks[:] = typing.cast(list[types.AwaitableTask], [])
_awaitable_tasks.clear()


def register_execute_before_exit(fnc: typing.Callable[[], None]) -> None:
logger.debug('Added exec before exit: %s', fnc)
_execBeforeExit.append(fnc)
_execute_before_exit.append(fnc)


def exec_before_exit() -> None:
logger.debug('Esecuting exec before exit: %s', _execBeforeExit)
for fnc in _execBeforeExit:
fnc()
def execute_before_exit() -> None:
logger.debug('Esecuting exec before exit: %s', _execute_before_exit)
for fnc in _execute_before_exit:
try:
fnc()
except Exception as e:
logger.error('Error executing before exit: %s', e)
_execute_before_exit.clear()


def verify_signature(script: bytes, signature: bytes) -> bool:
Expand Down Expand Up @@ -261,9 +265,15 @@ def get_cacerts_file() -> typing.Optional[str]:
return None


def is_mac_os() -> bool:
def is_macos() -> bool:
return 'darwin' in sys.platform

def is_linux() -> bool:
return 'linux' in sys.platform

def is_windows() -> bool:
return 'win' in sys.platform


# old compat names, to ensure compatibility with old code
# Basically, this will be here until v5.0. On 4.5 (or even later) Broker plugins will update
Expand Down
Loading

0 comments on commit 6f8bf4f

Please sign in to comment.