Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Key regen logic
  • Loading branch information
NaysKutzu committed Nov 24, 2024
1 parent 762f04f commit 33bf555
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 4 deletions.
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ RUN COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev --optimize-autoloader
COPY . .

# Add a cron job
RUN (crontab -l ; echo "* * * * * cd /var/www/html/backend && php mythicaldash schedule:work >> /dev/null 2>&1") | crontab
RUN (crontab -l ; echo "* * * * * cd /var/www/html/backend && php mythicaldash schedule_work >> /dev/null 2>&1") | crontab
RUN (crontab -l ; echo "* * * * * cd /var/www/html/backend/storage/cron && php runner.php >> /dev/null 2>&1") | crontab
RUN (crontab -l ; echo "* * * * * cd /var/www/html/backend/storage/cron && bash runner.php >> /dev/null 2>&1") | crontab

Expand Down
46 changes: 46 additions & 0 deletions backend/app/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,52 @@ public function loadEnv(): void
exit;
}
}
/**
* Update the value of an environment variable.
*
* @param string $key The key of the environment variable
* @param string $value The value of the environment variable
* @param bool $encode If the value should be encoded
*
* @return bool If the value was updated
*/
public function updateEnvValue(string $key, string $value, bool $encode): bool
{
$envFile = __DIR__ . '/../storage/.env'; // Path to your .env file
if (!file_exists($envFile)) {
return false; // Return false if .env file doesn't exist
}

// Read the .env file into an array of lines
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$updated = false;
foreach ($lines as &$line) {
// Skip comments and lines that don't contain '='
if (strpos(trim($line), '#') === 0 || strpos($line, '=') === false) {
continue;
}

// Split the line into key and value
[$envKey, $envValue] = explode('=', $line, 2);

// Trim whitespace from the key
if (trim($envKey) === $key) {
// Update the value
$line = "$key=\"$value\"";
$updated = true;
}
}

// If the key doesn't exist, add it
if (!$updated) {
$lines[] = "$key=$value";
}

// Write the updated lines back to the .env file
return file_put_contents($envFile, implode(PHP_EOL, $lines)) !== false;
}


/**
* Get the config factory.
Expand Down
2 changes: 1 addition & 1 deletion backend/app/Cli/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

class App extends \MythicalSystems\Utils\BungeeChatApi
{
public $prefix = '&7[&5&lMythical&d&lDash&7] &8&l| &7';
public $prefix = '&7[&5&lMythical&d&lClient&7] &8&l| &7';
public $bars = '&7&m-----------------------------------------------------&r';
public static App $instance;

Expand Down
58 changes: 58 additions & 0 deletions backend/app/Cli/Commands/Colors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of MythicalClient.
* Please view the LICENSE file that was distributed with this source code.
*
* MIT License
*
* (c) MythicalSystems <mythicalsystems.xyz> - All rights reserved
* (c) NaysKutzu <nayskutzu.xyz> - All rights reserved
* (c) Cassian Gherman <nayskutzu.xyz> - All rights reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace MythicalClient\Cli\Commands;

use MythicalClient\Cli\App;
use MythicalClient\Cli\CommandBuilder;

class Colors extends App implements CommandBuilder
{
public static function execute(array $args): void
{
$app = App::getInstance();
$colors = 'Colors: &0Black&r, &1Dark Blue&r, &2Dark Green&r, &3Dark Aqua&r, &4Dark Red&r, &5Dark Purple&r, &6Gold&r, &7Gray&r, &8Dark Gray&r, &9Blue&r, &aGreen&r, &bAqua&r, &cRed&r, &dLight Purple&r, &eYellow&r, &rWhite&r, &rReset&r, &lBold&r, &nUnderline&r, &mStrikethrough&r';

$app->send($colors);
$colors = "&0 0 &1 1 &2 2 &3 3 &4 4 &5 5 &6 6 &7 7 &8 8 &9 9 &a a &b b &c c &d d &e e &r r &l l &n n&r &m m &r";
$app->send($colors);
}

public static function getDescription(): string
{
return '&c&l&n&oS&6&l&n&ou&e&l&n&op&a&l&n&op&3&l&n&oo&9&l&n&or&5&l&n&ot&c&l&n&oe&6&l&n&od&e&l&n&o &a&l&n&oC&3&l&n&oo&9&l&n&ol&5&l&n&oo&c&l&n&or&6&l&n&os&e&l&n&o &a&l&n&oB&3&l&n&oy&9&l&n&o &5&l&n&oO&c&l&n&ou&6&l&n&or&e&l&n&o &a&l&n&oA&3&l&n&ow&9&l&n&os&5&l&n&oo&c&l&n&om&6&l&n&oe&e&l&n&o &a&l&n&oC&3&l&n&oL&9&l&n&oI';
}

public static function getSubCommands(int $index): array
{
return [];
}
}
90 changes: 90 additions & 0 deletions backend/app/Cli/Commands/KeyRegen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/*
* This file is part of MythicalClient.
* Please view the LICENSE file that was distributed with this source code.
*
* MIT License
*
* (c) MythicalSystems <mythicalsystems.xyz> - All rights reserved
* (c) NaysKutzu <nayskutzu.xyz> - All rights reserved
* (c) Cassian Gherman <nayskutzu.xyz> - All rights reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace MythicalClient\Cli\Commands;

use MythicalClient\Cli\App;
use MythicalClient\Cli\CommandBuilder;
use MythicalClient\Config\ConfigInterface;
use MythicalSystems\Utils\XChaCha20;

class KeyRegen extends App implements CommandBuilder
{
public static function execute(array $args): void
{
$app = App::getInstance();
if (in_array('-force', $args)) {
$isForced = true;
} else {
$isForced = false;
}

if (!$isForced) {
$app->send('&7Are you sure you want to reset the key? This will corupt all data there may be in the database! Type &ayes &7to continue or &cno &7to cancel.');
$app->send('&7This action is irreversible!');
$app->send('&7Type your answer below:');
$line = trim(readline('> '));
if ($line !== 'yes') {
$app->send('&cAction cancelled.');
return;
} else {
$isForced = true; // If the user types yes, then we can force the key reset
}
}

if ($isForced) {
$mainApp = \MythicalClient\App::getInstance(true);
$mainApp->loadEnv();
$mainApp->getLogger()->warning('Old encryption key was: '.$_ENV['DATABASE_ENCRYPTION_KEY']);
$app->send(message: '&7Old encryption key was: &e'.$_ENV['DATABASE_ENCRYPTION_KEY']);
$newKey = XChaCha20::generateStrongKey(true);
$mainApp->updateEnvValue('DATABASE_ENCRYPTION_KEY',$newKey,true);
sleep(3);
$_ENV['DATABASE_ENCRYPTION_KEY'] = $newKey;
$mainApp->getLogger()->warning('New encryption key is: '.$_ENV['DATABASE_ENCRYPTION_KEY']);
$app->send(message: '&7New encryption key is: &e'.$_ENV['DATABASE_ENCRYPTION_KEY']);
$app->send(message: '&7Key reset successfully!');
} else {
$app->send('&cAction cancelled.');
return;
}
}

public static function getDescription(): string
{
return 'Regenerate the encryption key';
}

public static function getSubCommands(int $index): array
{
return [];
}
}
4 changes: 2 additions & 2 deletions install.bash
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ chmod -R 777 ./
# Migrations
docker exec mythicalclient_backend bash -c "php mythicalclient migrate"

# Create the first user
#docker exec mythicalclient_backend bash -c "php mythicalclient user:create"
# Reset the encryption key
docker exec mythicalclient_backend bash -c "php mythicalclient keyRegen -force"


0 comments on commit 33bf555

Please sign in to comment.