1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-23 17:34:17 +03:00

some minor refactors & used sys.intern as key for rest api

This commit is contained in:
Adolfo Gómez García 2022-07-28 13:22:02 +02:00
parent f10b370eff
commit e3976794d9
6 changed files with 24 additions and 17 deletions

View File

@ -30,7 +30,7 @@
@author: Adolfo Gómez, dkmaster at dkmon dot com @author: Adolfo Gómez, dkmaster at dkmon dot com
""" """
import logging import logging
from re import M import sys
import typing import typing
from django import http from django import http
@ -200,7 +200,7 @@ class Dispatcher(View):
""" """
if not type_.name: if not type_.name:
name = type_.__name__.lower() name = sys.intern(type_.__name__.lower())
else: else:
name = type_.name name = type_.name
@ -210,15 +210,16 @@ class Dispatcher(View):
if type_.path: if type_.path:
logger.info('Path: /%s/%s', type_.path, name) logger.info('Path: /%s/%s', type_.path, name)
for k in type_.path.split('/'): for k in type_.path.split('/'):
intern_k = sys.intern(k)
if k not in service_node: if k not in service_node:
service_node[k] = {'': None} service_node[intern_k] = {'': None}
service_node = service_node[k] service_node = service_node[intern_k]
else: else:
logger.info('Path: /%s', name) logger.info('Path: /%s', name)
if name not in service_node: if name not in service_node:
service_node[name] = {'': None} service_node[name] = {'': None}
service_node[name][''] = type_ service_node[name][sys.intern('')] = type_
# Initializes the dispatchers # Initializes the dispatchers
@staticmethod @staticmethod
@ -237,7 +238,7 @@ class Dispatcher(View):
Dispatcher.registerClass, Dispatcher.registerClass,
Handler, Handler,
modName=modName, modName=modName,
checker=lambda x: not x.__subclasses__(), checker=lambda x: not x.__subclasses__(), # only register if final class, no inherited classes
packageName='methods', packageName='methods',
) )

View File

@ -18,7 +18,8 @@ ProcessType = typing.Callable[
typing.Coroutine[typing.Any, None, None], typing.Coroutine[typing.Any, None, None],
] ]
NO_CPU_PERCENT = 1000001.0 NO_CPU_PERCENT: float = 1000001.0
class Processes: class Processes:
""" """
@ -51,7 +52,9 @@ class Processes:
) )
task.start() task.start()
logger.debug('ADD CHILD PID: %s', task.pid) logger.debug('ADD CHILD PID: %s', task.pid)
self.children.append((own_conn, task, psutil.Process(task.pid))) self.children.append(
(typing.cast('Connection', own_conn), task, psutil.Process(task.pid))
)
def best_child(self) -> 'Connection': def best_child(self) -> 'Connection':
best: typing.Tuple[float, 'Connection'] = (NO_CPU_PERCENT, self.children[0][0]) best: typing.Tuple[float, 'Connection'] = (NO_CPU_PERCENT, self.children[0][0])
@ -93,7 +96,11 @@ class Processes:
if i not in missingProcesses if i not in missingProcesses
] ]
# Now add new children # Now add new children
for _ in missingProcesses: # wee need to add as many as we removed, that is the len of missingProcesses for (
_
) in (
missingProcesses
): # wee need to add as many as we removed, that is the len of missingProcesses
self.add_child_pid() self.add_child_pid()
# Recheck best if all child were missing # Recheck best if all child were missing
@ -102,7 +109,7 @@ class Processes:
return best[1] return best[1]
def stop(self): def stop(self) -> None:
# Try to stop running childs # Try to stop running childs
for i in self.children: for i in self.children:
try: try:

View File

@ -64,9 +64,7 @@ class Proxy:
logger.error('Proxy error from %s: %s', addr, e) logger.error('Proxy error from %s: %s', addr, e)
async def proxy(self, source: socket.socket, context: 'ssl.SSLContext') -> None: async def proxy(self, source: socket.socket, context: 'ssl.SSLContext') -> None:
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
# Handshake correct in this point, upgrade the connection to TSL and let # Handshake correct in this point, upgrade the connection to TSL and let
# the protocol controller do the rest # the protocol controller do the rest

View File

@ -143,7 +143,7 @@ async def getServerStats(detailed: bool = False) -> None:
# Context for local connection (ignores cert hostname) # Context for local connection (ignores cert hostname)
context = ssl.create_default_context() context = ssl.create_default_context()
context.check_hostname = False context.check_hostname = False
context.verify_mode = ssl.CERT_NONE # For ServerStats, do not checks certificate context.verify_mode = ssl.CERT_NONE # For ServerStats, does not checks certificate
try: try:
host = cfg.listen_address if cfg.listen_address != '0.0.0.0' else 'localhost' host = cfg.listen_address if cfg.listen_address != '0.0.0.0' else 'localhost'

View File

@ -23,7 +23,7 @@ class TunnelProtocol(asyncio.Protocol):
transport: 'asyncio.transports.Transport' transport: 'asyncio.transports.Transport'
other_side: 'TunnelProtocol' other_side: 'TunnelProtocol'
# Current state # Current state
runner: typing.Any runner: typing.Any # In fact, typing.Callable[[bytes], None], but mypy complains on its check
# Command buffer # Command buffer
cmd: bytes cmd: bytes
# Ticket # Ticket
@ -117,6 +117,7 @@ class TunnelProtocol(asyncio.Protocol):
self.close_connection() self.close_connection()
loop.create_task(open_other_side()) loop.create_task(open_other_side())
# From now, proxy connection
self.runner = self.do_proxy self.runner = self.do_proxy
def process_stats(self, full: bool) -> None: def process_stats(self, full: bool) -> None:

View File

@ -57,7 +57,7 @@ logger = logging.getLogger(__name__)
do_stop = False do_stop = False
def stop_signal(signum, frame): def stop_signal(signum: int, frame: typing.Any) -> None:
global do_stop global do_stop
do_stop = True do_stop = True
logger.debug('SIGNAL %s, frame: %s', signum, frame) logger.debug('SIGNAL %s, frame: %s', signum, frame)
@ -85,7 +85,7 @@ def setup_log(cfg: config.ConfigurationType) -> None:
# Setup basic logging # Setup basic logging
log = logging.getLogger() log = logging.getLogger()
log.setLevel(cfg.log_level) log.setLevel(cfg.log_level)
handler = logging.StreamHandler(sys.stdout) handler = logging.StreamHandler(sys.stderr)
handler.setLevel(cfg.log_level) handler.setLevel(cfg.log_level)
formatter = logging.Formatter( formatter = logging.Formatter(
'%(levelname)s - %(message)s' '%(levelname)s - %(message)s'
@ -169,7 +169,7 @@ def process_connection(
client.close() client.close()
def tunnel_main(): def tunnel_main() -> None:
cfg = config.read() cfg = config.read()
# Try to bind to port as running user # Try to bind to port as running user