1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-02-03 13:47:14 +03:00

Renamed Tunnel tokens Table to Server Tokens and adapted code to it. Also recreated admin using Angular 16

This commit is contained in:
Adolfo Gómez García 2023-07-12 04:30:37 +02:00
parent 5da7b06d62
commit 81db3278bd
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
7 changed files with 80 additions and 92 deletions

View File

@ -33,9 +33,14 @@ import secrets
import logging
import typing
from django.utils.translation import gettext_lazy as _
from uds import models
from uds.core.util.model import getSqlDatetimeAsUnix, getSqlDatetime
from uds.REST import Handler
from uds.REST.exceptions import RequestError, NotFound
from uds.REST.model import ModelHandler, OK
from uds.core.util import permissions
logger = logging.getLogger(__name__)
@ -73,3 +78,49 @@ class ServerRegister(Handler):
except Exception as e:
return {'result': '', 'stamp': now, 'error': str(e)}
return {'result': serverToken.token, 'stamp': now}
class ServersTokens(ModelHandler):
model = models.RegisteredServers
path = 'servers'
name = 'tokens'
table_title = _('Servers tokens')
table_fields = [
{'token': {'title': _('Token')}},
{'stamp': {'title': _('Date'), 'type': 'datetime'}},
{'username': {'title': _('Issued by')}},
{'hostname': {'title': _('Origin')}},
{'type': {'title': _('Type')}},
{'ip': {'title': _('IP')}},
]
def item_as_dict(self, item: models.RegisteredServers) -> typing.Dict[str, typing.Any]:
return {
'id': item.token,
'name': str(_('Token isued by {} from {}')).format(item.username, item.ip),
'stamp': item.stamp,
'username': item.username,
'ip': item.ip,
'hostname': item.hostname,
'token': item.token,
'type': models.RegisteredServers.ServerType(item.kind).as_str(), # type is a reserved word, so we use "kind" instead on model
}
def delete(self) -> str:
"""
Processes a DELETE request
"""
if len(self._args) != 1:
raise RequestError('Delete need one and only one argument')
self.ensureAccess(
self.model(), permissions.PermissionType.ALL, root=True
) # Must have write permissions to delete
try:
self.model.objects.get(token=self._args[0]).delete()
except self.model.DoesNotExist:
raise NotFound('Element do not exists') from None
return OK

View File

@ -160,5 +160,5 @@ class TunnelRegister(ServerRegister):
# Just a compatibility method for old tunnel servers
def post(self) -> typing.MutableMapping[str, typing.Any]:
self._params['type'] = models.RegisteredServers.ServerKind.TUNNEL
self._params['type'] = models.RegisteredServers.ServerType.TUNNEL
return super().post()

View File

@ -1,87 +0,0 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2023 Virtual Cable S.L.U.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import logging
import typing
from django.utils.translation import gettext_lazy as _
from uds.models import RegisteredServers
from uds.REST.exceptions import RequestError, NotFound
from uds.REST.model import ModelHandler, OK
from uds.core.util import permissions
logger = logging.getLogger(__name__)
class ServersTokens(ModelHandler):
model = RegisteredServers
table_title = _('Servers tokens')
table_fields = [
{'token': {'title': _('Token')}},
{'stamp': {'title': _('Date'), 'type': 'datetime'}},
{'username': {'title': _('Issued by')}},
{'hostname': {'title': _('Origin')}},
{'type': {'title': _('Type')}},
{'ip': {'title': _('IP')}},
]
def item_as_dict(self, item: RegisteredServers) -> typing.Dict[str, typing.Any]:
return {
'id': item.token,
'name': str(_('Token isued by {} from {}')).format(item.username, item.ip),
'stamp': item.stamp,
'username': item.username,
'ip': item.ip,
'hostname': item.hostname,
'token': item.token,
'type': item.kind, # type is a reserved word, so we use "kind" instead on model
}
def delete(self) -> str:
"""
Processes a DELETE request
"""
if len(self._args) != 1:
raise RequestError('Delete need one and only one argument')
self.ensureAccess(
self.model(), permissions.PermissionType.ALL, root=True
) # Must have write permissions to delete
try:
self.model.objects.get(token=self._args[0]).delete()
except self.model.DoesNotExist:
raise NotFound('Element do not exists') from None
return OK

View File

@ -0,0 +1,21 @@
# Generated by Django 4.2.1 on 2023-07-12 04:04
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("uds", "0045_actortoken_custom_log_name"),
]
operations = [
migrations.RenameModel(
'TunnelToken',
'RegisteredServers',
),
migrations.AddField(
model_name="registeredservers",
name="kind",
field=models.IntegerField(default=1),
),
]

View File

@ -53,10 +53,13 @@ class RegisteredServers(models.Model):
If server is Other, but not Tunnel, it will be allowed to access API, but will not be able to
create tunnels.
"""
class ServerKind(enum.IntFlag):
class ServerType(enum.IntFlag):
TUNNEL = 1
OTHER = 2
def as_str(self) -> str:
return self.name.lower() # type: ignore
username = models.CharField(max_length=128)
ip_from = models.CharField(max_length=MAX_IPV6_LENGTH)
ip = models.CharField(max_length=MAX_IPV6_LENGTH)
@ -65,7 +68,7 @@ class RegisteredServers(models.Model):
token = models.CharField(max_length=48, db_index=True, unique=True)
stamp = models.DateTimeField() # Date creation or validation of this entry
kind = models.IntegerField(default=ServerKind.TUNNEL.value) # Defaults to tunnel server, so we can migrate from previous versions
kind = models.IntegerField(default=ServerType.TUNNEL.value) # Defaults to tunnel server, so we can migrate from previous versions
# "fake" declarations for type checking
# objects: 'models.manager.Manager[TunnelToken]'

File diff suppressed because one or more lines are too long

View File

@ -102,6 +102,6 @@
</svg>
</div>
</uds-root>
<script src="/uds/res/admin/runtime.js?stamp=1689127201" type="module"></script><script src="/uds/res/admin/polyfills.js?stamp=1689127201" type="module"></script><script src="/uds/res/admin/main.js?stamp=1689127201" type="module"></script></body>
<script src="/uds/res/admin/runtime.js?stamp=1689128984" type="module"></script><script src="/uds/res/admin/polyfills.js?stamp=1689128984" type="module"></script><script src="/uds/res/admin/main.js?stamp=1689128984" type="module"></script></body>
</html>