mirror of
https://github.com/dkmstr/openuds.git
synced 2024-12-25 23:21:41 +03:00
Added some more typing checks
This commit is contained in:
parent
22415b98cd
commit
3e061275b4
@ -319,7 +319,7 @@ class UserServiceManager:
|
||||
cache: typing.Optional[UserService] = None
|
||||
# Now try to locate 1 from cache already "ready" (must be usable and at level 1)
|
||||
with transaction.atomic():
|
||||
caches = servicePool.cachedUserServices().select_for_update().filter(cache_level=services.UserDeployment.L1_CACHE, state=State.USABLE, os_state=State.USABLE)[:1]
|
||||
caches = typing.cast(typing.List[UserService], servicePool.cachedUserServices().select_for_update().filter(cache_level=services.UserDeployment.L1_CACHE, state=State.USABLE, os_state=State.USABLE)[:1])
|
||||
if caches:
|
||||
cache = caches[0]
|
||||
# Ensure element is reserved correctly on DB
|
||||
@ -331,9 +331,9 @@ class UserServiceManager:
|
||||
# Out of previous atomic
|
||||
if not cache:
|
||||
with transaction.atomic():
|
||||
cache = servicePool.cachedUserServices().select_for_update().filter(cache_level=services.UserDeployment.L1_CACHE, state=State.USABLE)[:1]
|
||||
caches = typing.cast(typing.List[UserService], servicePool.cachedUserServices().select_for_update().filter(cache_level=services.UserDeployment.L1_CACHE, state=State.USABLE)[:1])
|
||||
if cache:
|
||||
cache = cache[0]
|
||||
cache = caches[0]
|
||||
if servicePool.cachedUserServices().select_for_update().filter(user=None, uuid=typing.cast(UserService, cache).uuid).update(user=user, cache_level=0) != 1:
|
||||
cache = None
|
||||
else:
|
||||
|
@ -291,6 +291,15 @@ class Service(Module):
|
||||
return None
|
||||
|
||||
def getValidId(self, idsList: typing.Iterable[str]) -> typing.Optional[str]:
|
||||
"""
|
||||
Looks for an "owned" id in the provided list. If found, returns it, else return None
|
||||
|
||||
Args:
|
||||
idsList (typing.Iterable[str]): List of IPs and MACs that acts as
|
||||
|
||||
Returns:
|
||||
typing.Optional[str]: [description]
|
||||
"""
|
||||
return None
|
||||
|
||||
def processLogin(self, id: str, remote_login: bool) -> None:
|
||||
@ -315,6 +324,15 @@ class Service(Module):
|
||||
"""
|
||||
return
|
||||
|
||||
def notifyInitialization(self, id: str) -> None:
|
||||
"""
|
||||
In the case that the startup of a "tokenized" method is invoked (unmanaged method of actor_v3 rest api),
|
||||
this method is forwarded, so the tokenized method could take proper actions on a "known-to-be-free service"
|
||||
|
||||
Args:
|
||||
id (str): Id validated through "getValidId"
|
||||
"""
|
||||
|
||||
def storeIdInfo(self, id: str, data: typing.Any) -> None:
|
||||
self.storage.putPickle('__nfo_' + id, data)
|
||||
|
||||
@ -339,7 +357,7 @@ class Service(Module):
|
||||
"""
|
||||
Helper to query if a class is custom (implements getJavascript method)
|
||||
"""
|
||||
return cls.listAssignables != Service.listAssignables and cls.assignFromAssignables != Service.assignFromAssignables
|
||||
return cls.listAssignables is not Service.listAssignables and cls.assignFromAssignables is not Service.assignFromAssignables
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
|
@ -31,6 +31,7 @@
|
||||
"""
|
||||
import threading
|
||||
import datetime
|
||||
from uds.core.util.tools import DictAsObj
|
||||
import weakref
|
||||
import logging
|
||||
import typing
|
||||
@ -42,24 +43,33 @@ from uds.core.util.config import GlobalConfig
|
||||
from uds.core.auths.auth import ROOT_ID, USER_KEY, getRootUser
|
||||
from uds.models import User
|
||||
|
||||
# How often to check the requests cache for stuck objects
|
||||
CHECK_SECONDS = 3600 * 24 # Once a day is more than enough
|
||||
|
||||
class ExtendedHttpRequest(HttpRequest):
|
||||
ip: str
|
||||
ip_proxy: str
|
||||
os: DictAsObj
|
||||
user: typing.Optional[User] # type: ignore # HttpRequests users "user" for it own, but we redefine it because base is not used...
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_requests: typing.Dict[int, typing.Tuple[weakref.ref, datetime.datetime]] = {}
|
||||
|
||||
# How often to check the requests cache for stuck objects
|
||||
CHECK_SECONDS = 3600 * 24 # Once a day is more than enough
|
||||
|
||||
def getIdent() -> int:
|
||||
ident = threading.current_thread().ident
|
||||
return ident if ident else -1
|
||||
|
||||
|
||||
def getRequest() -> HttpRequest:
|
||||
def getRequest() -> ExtendedHttpRequest:
|
||||
ident = getIdent()
|
||||
if ident in _requests:
|
||||
return _requests[ident][0]() # Return obj from weakref
|
||||
val = typing.cast(typing.Optional[ExtendedHttpRequest], _requests[ident][0]()) # Return obj from weakref
|
||||
if val:
|
||||
return val
|
||||
|
||||
return HttpRequest()
|
||||
return ExtendedHttpRequest()
|
||||
|
||||
|
||||
class GlobalRequestMiddleware:
|
||||
@ -68,9 +78,9 @@ class GlobalRequestMiddleware:
|
||||
def __init__(self, get_response: typing.Callable[[HttpRequest], HttpResponse]):
|
||||
self._get_response: typing.Callable[[HttpRequest], HttpResponse] = get_response
|
||||
|
||||
def _process_request(self, request: HttpRequest) -> None:
|
||||
def _process_request(self, request: ExtendedHttpRequest) -> None:
|
||||
# Store request on cache
|
||||
_requests[getIdent()] = (weakref.ref(request), datetime.datetime.now())
|
||||
_requests[getIdent()] = (weakref.ref(typing.cast(ExtendedHttpRequest, request)), datetime.datetime.now())
|
||||
|
||||
# Add IP to request
|
||||
GlobalRequestMiddleware.fillIps(request)
|
||||
@ -79,7 +89,7 @@ class GlobalRequestMiddleware:
|
||||
# Ensures that requests contains the valid user
|
||||
GlobalRequestMiddleware.getUser(request)
|
||||
|
||||
def _process_response(self, request: HttpRequest, response: HttpResponse):
|
||||
def _process_response(self, request: ExtendedHttpRequest, response: HttpResponse):
|
||||
# Remove IP from global cache (processing responses after this will make global request unavailable,
|
||||
# but can be got from request again)
|
||||
ident = getIdent()
|
||||
@ -97,7 +107,7 @@ class GlobalRequestMiddleware:
|
||||
|
||||
return response
|
||||
|
||||
def __call__(self, request: HttpRequest):
|
||||
def __call__(self, request: ExtendedHttpRequest):
|
||||
self._process_request(request)
|
||||
|
||||
response = self._get_response(request)
|
||||
@ -124,7 +134,7 @@ class GlobalRequestMiddleware:
|
||||
|
||||
|
||||
@staticmethod
|
||||
def fillIps(request: HttpRequest):
|
||||
def fillIps(request: ExtendedHttpRequest):
|
||||
"""
|
||||
Obtains the IP of a Django Request, even behind a proxy
|
||||
|
||||
@ -152,11 +162,11 @@ class GlobalRequestMiddleware:
|
||||
|
||||
|
||||
@staticmethod
|
||||
def getUser(request: HttpRequest)-> None:
|
||||
def getUser(request: ExtendedHttpRequest)-> None:
|
||||
"""
|
||||
Ensures request user is the correct user
|
||||
"""
|
||||
user_id: str = request.session.get(USER_KEY)
|
||||
user_id = request.session.get(USER_KEY)
|
||||
user: typing.Optional[User] = None
|
||||
if user_id:
|
||||
try:
|
||||
|
Loading…
Reference in New Issue
Block a user