From 5049796d07d5b1067f1d910c6491ac1bea9ba449 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Fri, 25 Aug 2023 14:01:36 +1000 Subject: [PATCH 1/6] bot: Added free mode --- discord-bot/bot.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/discord-bot/bot.py b/discord-bot/bot.py index 0450dbc..7a2f855 100644 --- a/discord-bot/bot.py +++ b/discord-bot/bot.py @@ -50,9 +50,8 @@ async def listworkers(ctx): @tree.command(name="licence", description="Gets a licence key") async def license(ctx): if ctx.user.id != ADMINID: - if FREE_LICENCE == False: # If free licences are enabled then anyone can get a licence - await ctx.response.send_message("You do not have permission to use this command",ephemeral=True) - return + await ctx.response.send_message("You do not have permission to use this command",ephemeral=True) + return r = requests.post(f"http://{Master_IP}:{Master_Port}/add-licence",headers={"key":os.getenv('LICENCE_KEY')}) if r.status_code == 200: @@ -66,6 +65,16 @@ async def license(ctx): @tree.command(name="createsite", description="Create a new WordPress site") async def createsite(ctx, domain: str, licence: str): + if FREE_LICENCE == False: # If free licences are enabled then auto generate a licence + r = requests.post(f"http://{Master_IP}:{Master_Port}/add-licence",headers={"key":os.getenv('LICENCE_KEY')}) + if r.status_code == 200: + json = r.json() + if json['success'] == "true": + licence = json['licence_key'] + else: + await ctx.response.send_message(f"Error getting license\n" + json['error']) + return + r = requests.post(f"http://{Master_IP}:{Master_Port}/new-site?domain={domain}",headers={"key":licence}) if r.status_code == 200: json = r.json() From 29026246370626b7d09986ff1cd92ea07f5bd97d Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Fri, 25 Aug 2023 14:09:26 +1000 Subject: [PATCH 2/6] bot: Fix to enable free licences of FREE MODE --- discord-bot/bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord-bot/bot.py b/discord-bot/bot.py index 7a2f855..0f4cf8a 100644 --- a/discord-bot/bot.py +++ b/discord-bot/bot.py @@ -65,7 +65,7 @@ async def license(ctx): @tree.command(name="createsite", description="Create a new WordPress site") async def createsite(ctx, domain: str, licence: str): - if FREE_LICENCE == False: # If free licences are enabled then auto generate a licence + if FREE_LICENCE == True: # If free licences are enabled then auto generate a licence r = requests.post(f"http://{Master_IP}:{Master_Port}/add-licence",headers={"key":os.getenv('LICENCE_KEY')}) if r.status_code == 200: json = r.json() From c4ae4561e3fb2f20bdb3f5dd559d438671b2e420 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Fri, 25 Aug 2023 14:13:22 +1000 Subject: [PATCH 3/6] main: Cleared up invalid licence response --- master/main.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/master/main.py b/master/main.py index dd5037d..a4adb5e 100644 --- a/master/main.py +++ b/master/main.py @@ -43,8 +43,16 @@ def new_site(): return jsonify({'error': 'Missing domain', 'success': 'false'}) # Check if API key is a valid site key - if api_key not in open('/data/licence_key.txt', 'r').read(): - return jsonify({'error': 'Invalid API key', 'success': 'false'}) + key_file = open('/data/licence_key.txt', 'r') + valid_key = False + for line in key_file.readlines(): + if api_key == line.strip('\n'): + valid_key = True + break + key_file.close() + if not valid_key: + return jsonify({'error': 'Invalid licence', 'success': 'false'}) + # Check if domain already exists if site_exists(domain): From fb9295c260ca65de8cab2de670d3c6264b08b135 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Fri, 25 Aug 2023 14:17:12 +1000 Subject: [PATCH 4/6] bot: Made licence an optional arg --- discord-bot/bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord-bot/bot.py b/discord-bot/bot.py index 0f4cf8a..978545f 100644 --- a/discord-bot/bot.py +++ b/discord-bot/bot.py @@ -64,7 +64,7 @@ async def license(ctx): await ctx.response.send_message(f"Error getting license\n" + r.text,ephemeral=True) @tree.command(name="createsite", description="Create a new WordPress site") -async def createsite(ctx, domain: str, licence: str): +async def createsite(ctx, domain: str, licence: str = None): if FREE_LICENCE == True: # If free licences are enabled then auto generate a licence r = requests.post(f"http://{Master_IP}:{Master_Port}/add-licence",headers={"key":os.getenv('LICENCE_KEY')}) if r.status_code == 200: From d6dffc046491feaa5b146fd43f1ec1eee63a2320 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Fri, 25 Aug 2023 14:20:58 +1000 Subject: [PATCH 5/6] main: Small fixed for licences --- discord-bot/bot.py | 5 +++++ master/main.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/discord-bot/bot.py b/discord-bot/bot.py index 978545f..54a4af5 100644 --- a/discord-bot/bot.py +++ b/discord-bot/bot.py @@ -16,6 +16,11 @@ if Master_Port == None: FREE_LICENCE = os.getenv('FREE_LICENCE') if FREE_LICENCE == None: FREE_LICENCE = False +else: + if FREE_LICENCE.lower() == "true": + FREE_LICENCE = True + else: + FREE_LICENCE = False intents = discord.Intents.default() client = discord.Client(intents=intents) diff --git a/master/main.py b/master/main.py index a4adb5e..45ffc35 100644 --- a/master/main.py +++ b/master/main.py @@ -37,7 +37,7 @@ def new_site(): # Verify both API key and domain exist if api_key == None: - return jsonify({'error': 'Missing API key', 'success': 'false'}) + return jsonify({'error': 'No licence provided', 'success': 'false'}) if domain == None: return jsonify({'error': 'Missing domain', 'success': 'false'}) From bbe70647d7e25630d14a3df96d67b7e9bd4e85cb Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Fri, 25 Aug 2023 14:26:59 +1000 Subject: [PATCH 6/6] docs: Added info on free mode --- README.md | 10 ++++++++++ discord-bot/bot.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3245bc4..6957d02 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ The bot will be used to provide an easier way to manage the master server. | Red Connections | Secured by VPN or over LAN ONLY. (NOT API SECURED) | | Yellow Connections | HTTP/HTTPS public traffic | +## Features +- [x] Add new worker server to master server pool +- [x] Create wordpress site on random worker server +- [x] Optional Free mode (see Bot section) ## Usage @@ -105,4 +109,10 @@ Alternatively you can use the discord bot to add the worker to the master server Docker install ```sh docker run -d -e MASTER_IP= -e DISCORD_TOKEN= -e LICENCE_KEY=your-api-key -e WORKER_KEY=your-api-key --name hnshosting-bot git.woodburn.au/nathanwoodburn/hnshosting-bot:latest +``` + +Enable the free mode by setting the following environment variable. +This will allow you to create a wordpress site without using a licence key using the /createsite command. +``` +FREE_MODE: true ``` \ No newline at end of file diff --git a/discord-bot/bot.py b/discord-bot/bot.py index 54a4af5..e0da690 100644 --- a/discord-bot/bot.py +++ b/discord-bot/bot.py @@ -13,7 +13,7 @@ Master_Port = os.getenv('MASTER_PORT') if Master_Port == None: Master_Port = "5000" -FREE_LICENCE = os.getenv('FREE_LICENCE') +FREE_LICENCE = os.getenv('FREE_MODE') if FREE_LICENCE == None: FREE_LICENCE = False else: