1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-01-13 13:17:54 +03:00

Added support for name resolutions on service_multi && fixed max page limit from 50 to 100

This commit is contained in:
Adolfo Gómez García 2021-03-24 11:54:03 +01:00
parent 0a0b4cb740
commit 3f881b3e17
3 changed files with 33 additions and 3 deletions

View File

@ -34,7 +34,10 @@ import requests
import logging
import typing
import dns.resolver
from uds.core import services
from uds.core.util import net
logger = logging.getLogger(__name__)
@ -45,7 +48,18 @@ class IPServiceBase(services.Service):
@staticmethod
def getIp(ipData: str) -> str:
return ipData.split('~')[0].split(';')[0]
ip = ipData.split('~')[0].split(';')[0]
# If ip is in fact a hostname...
if not net.ipToLong(ip):
# Try to resolve name...
try:
res = dns.resolver.resolve(ip)
ip = res[0].address
except Exception:
return ''
return ip
@staticmethod
def getMac(ipData: str) -> typing.Optional[str]:

View File

@ -197,6 +197,18 @@ class IPMachinesService(IPServiceBase):
consideredFreeTime = now - config.GlobalConfig.SESSION_EXPIRE_TIME.getInt(force=False) * 3600
for ip in self._ips:
theIP = IPServiceBase.getIp(ip)
if not theIP:
self.parent().doLog(
log.WARN,
'Hostname in {} could not be resolved. Skipped.'.format(
ip
),
)
logger.warning(
'Hostname in %s could not be resolved. Skipped.', ip
)
continue
theMAC = IPServiceBase.getMac(ip)
locked = self.storage.getPickle(theIP)
if not locked or locked < consideredFreeTime:
@ -265,6 +277,9 @@ class IPMachinesService(IPServiceBase):
IPMachineDeployed, userDeployment
)
theIP = IPServiceBase.getIp(assignableId)
if not theIP:
return userServiceInstance.error('Hostname could not be resolved')
if self.storage.readData(theIP) is None:
self.storage.saveData(theIP, theIP)
return userServiceInstance.assign(theIP)

View File

@ -50,6 +50,7 @@ from uds.models import Authenticator, Image, Network, Transport
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from django.http import HttpRequest # pylint: disable=ungrouped-imports
from uds.core.util.request import ExtendedHttpRequest
from uds.models import User
logger = logging.getLogger(__name__)
@ -60,7 +61,7 @@ CSRF_FIELD = 'csrfmiddlewaretoken'
@register.simple_tag(takes_context=True)
def udsJs(request: 'HttpRequest') -> str:
def udsJs(request: 'ExtendedHttpRequest') -> str:
auth_host = request.META.get('HTTP_HOST') or request.META.get('SERVER_NAME') or 'auth_host' # Last one is a placeholder in case we can't locate host name
role: str = 'user'
@ -214,7 +215,7 @@ def udsJs(request: 'HttpRequest') -> str:
# Admin config
page_size = GlobalConfig.ADMIN_PAGESIZE.getInt(True)
# Fix page size to razonable usable values
page_size = 10 if page_size < 10 else 50 if page_size > 50 else page_size
page_size = 10 if page_size < 10 else 100 if page_size > 100 else page_size
config['admin'] = {
'page_size': page_size,
}