From 48c37987b271aa7ffe306e38459b4db38d0fc413 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 18 Jan 2023 14:23:25 +0100 Subject: [PATCH 1/9] fixed logging and added current timezone info --- app/main.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 204a55a..76a42b2 100644 --- a/app/main.py +++ b/app/main.py @@ -22,9 +22,10 @@ from sqlalchemy.orm import sessionmaker from util import load_key, load_file from orm import Origin, Lease, init as db_init, migrate -logger = logging.getLogger() load_dotenv('../version.env') +TZ = datetime.now().astimezone().tzinfo + VERSION, COMMIT, DEBUG = env('VERSION', 'unknown'), env('COMMIT', 'unknown'), bool(env('DEBUG', False)) config = dict(openapi_url='/-/openapi.json', docs_url='/-/docs', redoc_url='/-/redoc') @@ -58,6 +59,8 @@ app.add_middleware( allow_headers=['*'], ) +logging.basicConfig() +logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG if DEBUG else logging.INFO) @@ -97,6 +100,7 @@ async def _config(): 'LEASE_EXPIRE_DELTA': str(LEASE_EXPIRE_DELTA), 'LEASE_RENEWAL_PERIOD': str(LEASE_RENEWAL_PERIOD), 'CORS_ORIGINS': str(CORS_ORIGINS), + 'TZ': str(TZ), }) @@ -529,6 +533,11 @@ async def leasing_v1_lessor_shutdown(request: Request): return JSONr(response) +@app.on_event('startup') +async def app_on_startup(): + logger.info(f'Using timezone: {str(TZ)}. Make sure this is correct and match your clients!') + + if __name__ == '__main__': import uvicorn From 9411759f6db5e4a13422be18c292b3f69fd35058 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 18 Jan 2023 14:23:34 +0100 Subject: [PATCH 2/9] added system requirements and preparements --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 28a7bdc..04b013e 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,15 @@ Only the clients need a connection to this service on configured port. # Setup (Service) +**System requirements**: + +- LXC with 256mb ram and 4gb hdd is enough (Ubuntu 22.10 from Proxmox templates) \ + (actually its consuming 100mb ram and 750mb hdd) + +**Prepare your system**: + +- Make sure your timezone is set correct on you fastapi-dls server and your client + ## Docker Docker-Images are available here: From 38177fa2594e273d748d2cf3c6aa45a27cc4e23e Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 18 Jan 2023 14:29:48 +0100 Subject: [PATCH 3/9] styling --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 04b013e..884cdd3 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,14 @@ Only the clients need a connection to this service on configured port. [[_TOC_]] - # Setup (Service) **System requirements**: -- LXC with 256mb ram and 4gb hdd is enough (Ubuntu 22.10 from Proxmox templates) \ - (actually its consuming 100mb ram and 750mb hdd) +- 256mb ram +- 4gb hdd + +Tested with Ubuntu 22.10 (from Proxmox templates), actually its consuming 100mb ram and 750mb hdd. **Prepare your system**: From 7045692958b4348e225891abf27553f977e021bd Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Thu, 19 Jan 2023 07:25:24 +0100 Subject: [PATCH 4/9] added official links --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 884cdd3..c0a9c0b 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,14 @@ Compatibility tested with official DLS 2.0.1. This service can be used without internet connection. Only the clients need a connection to this service on configured port. +**Official Links** + +- https://git.collinwebdesigns.de/oscar.krause/fastapi-dls +- https://gitea.publichub.eu/oscar.krause/fastapi-dls +- Docker Image `collinwebdesigns/fastapi-dls:latest` + +*All other repositories are forks!* + [[_TOC_]] # Setup (Service) From 91be7b226cc1966b03a2b9d67c088b3d4bc5e96e Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Thu, 19 Jan 2023 07:25:44 +0100 Subject: [PATCH 5/9] added some comments for default values --- app/orm.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/orm.py b/app/orm.py index bfd6557..387c828 100644 --- a/app/orm.py +++ b/app/orm.py @@ -170,6 +170,14 @@ class Lease(Base): renew = delta.total_seconds() * LEASE_RENEWAL_PERIOD renew = datetime.timedelta(seconds=renew) expires = delta - renew # 19.2 + + import datetime + LEASE_RENEWAL_PERIOD=0.15 # 15% + delta = datetime.timedelta(days=90) + renew = delta.total_seconds() * LEASE_RENEWAL_PERIOD + renew = datetime.timedelta(seconds=renew) + expires = delta - renew # 76 days, 12:00:00 hours + """ renew = delta.total_seconds() * renewal_period renew = timedelta(seconds=renew) From c3ea0aa48c7304e1adeb0d304b3951d39228e609 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Thu, 19 Jan 2023 07:26:07 +0100 Subject: [PATCH 6/9] added variable for client-token-expire-delta --- app/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 76a42b2..b4ee325 100644 --- a/app/main.py +++ b/app/main.py @@ -45,6 +45,7 @@ TOKEN_EXPIRE_DELTA = relativedelta(days=int(env('TOKEN_EXPIRE_DAYS', 1)), hours= LEASE_EXPIRE_DELTA = relativedelta(days=int(env('LEASE_EXPIRE_DAYS', 90)), hours=int(env('LEASE_EXPIRE_HOURS', 0))) LEASE_RENEWAL_PERIOD = float(env('LEASE_RENEWAL_PERIOD', 0.15)) LEASE_RENEWAL_DELTA = timedelta(days=int(env('LEASE_EXPIRE_DAYS', 90)), hours=int(env('LEASE_EXPIRE_HOURS', 0))) +CLIENT_TOKEN_EXPIRE_DELTA = relativedelta(years=12) CORS_ORIGINS = str(env('CORS_ORIGINS', '')).split(',') if (env('CORS_ORIGINS')) else [f'https://{DLS_URL}'] jwt_encode_key = jwk.construct(INSTANCE_KEY_RSA.export_key().decode('utf-8'), algorithm=ALGORITHMS.RS256) @@ -196,7 +197,7 @@ async def _lease_delete(request: Request, lease_ref: str): @app.get('/-/client-token', summary='* Client-Token', description='creates a new messenger token for this service instance') async def _client_token(): cur_time = datetime.utcnow() - exp_time = cur_time + relativedelta(years=12) + exp_time = cur_time + CLIENT_TOKEN_EXPIRE_DELTA payload = { "jti": str(uuid4()), From 95427d430ef7e911f34e56f3728f50436b17a448 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Thu, 19 Jan 2023 07:26:22 +0100 Subject: [PATCH 7/9] added startup script --- app/main.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index b4ee325..e9ed1dd 100644 --- a/app/main.py +++ b/app/main.py @@ -536,7 +536,14 @@ async def leasing_v1_lessor_shutdown(request: Request): @app.on_event('startup') async def app_on_startup(): - logger.info(f'Using timezone: {str(TZ)}. Make sure this is correct and match your clients!') + logger.info(f''' + Using timezone: {str(TZ)}. Make sure this is correct and match your clients! + + Your clients renew their license every {str(Lease.calculate_renewal(LEASE_RENEWAL_PERIOD, LEASE_RENEWAL_DELTA))}. + If the renewal fails, the license is {str(LEASE_RENEWAL_DELTA)} valid. + + Your client-token file (.tok) is valid for {str(CLIENT_TOKEN_EXPIRE_DELTA)}. + ''') if __name__ == '__main__': From bed24b56ce9fda25c4cabe29605e122f06b4f1de Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Thu, 19 Jan 2023 08:26:35 +0100 Subject: [PATCH 8/9] styling --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c0a9c0b..bb590f0 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ Only the clients need a connection to this service on configured port. *All other repositories are forks!* +--- + [[_TOC_]] # Setup (Service) From f7ef8d76b6d2f07b536761a046b6db78483a669c Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Mon, 23 Jan 2023 07:12:02 +0100 Subject: [PATCH 9/9] fixed Origin.delete() --- app/orm.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/orm.py b/app/orm.py index 387c828..6f4858f 100644 --- a/app/orm.py +++ b/app/orm.py @@ -57,12 +57,12 @@ class Origin(Base): session.close() @staticmethod - def delete(engine: Engine, origins: ["Origin"] = None) -> int: + def delete(engine: Engine, origin_refs: [str] = None) -> int: session = sessionmaker(bind=engine)() - if origins is None: + if origin_refs is None: deletions = session.query(Origin).delete() else: - deletions = session.query(Origin).filter(Origin.origin_ref in origins).delete() + deletions = session.query(Origin).filter(Origin.origin_ref in origin_refs).delete() session.commit() session.close() return deletions