Skip to content

Commit 33bf555

Browse files
committed
PUSH
-> Key regen logic
1 parent 762f04f commit 33bf555

File tree

6 files changed

+198
-4
lines changed

6 files changed

+198
-4
lines changed

backend/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ RUN COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev --optimize-autoloader
4040
COPY . .
4141

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

backend/app/App.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,52 @@ public function loadEnv(): void
148148
exit;
149149
}
150150
}
151+
/**
152+
* Update the value of an environment variable.
153+
*
154+
* @param string $key The key of the environment variable
155+
* @param string $value The value of the environment variable
156+
* @param bool $encode If the value should be encoded
157+
*
158+
* @return bool If the value was updated
159+
*/
160+
public function updateEnvValue(string $key, string $value, bool $encode): bool
161+
{
162+
$envFile = __DIR__ . '/../storage/.env'; // Path to your .env file
163+
if (!file_exists($envFile)) {
164+
return false; // Return false if .env file doesn't exist
165+
}
166+
167+
// Read the .env file into an array of lines
168+
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
169+
170+
$updated = false;
171+
foreach ($lines as &$line) {
172+
// Skip comments and lines that don't contain '='
173+
if (strpos(trim($line), '#') === 0 || strpos($line, '=') === false) {
174+
continue;
175+
}
176+
177+
// Split the line into key and value
178+
[$envKey, $envValue] = explode('=', $line, 2);
179+
180+
// Trim whitespace from the key
181+
if (trim($envKey) === $key) {
182+
// Update the value
183+
$line = "$key=\"$value\"";
184+
$updated = true;
185+
}
186+
}
187+
188+
// If the key doesn't exist, add it
189+
if (!$updated) {
190+
$lines[] = "$key=$value";
191+
}
192+
193+
// Write the updated lines back to the .env file
194+
return file_put_contents($envFile, implode(PHP_EOL, $lines)) !== false;
195+
}
196+
151197

152198
/**
153199
* Get the config factory.

backend/app/Cli/App.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

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

backend/app/Cli/Commands/Colors.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of MythicalClient.
5+
* Please view the LICENSE file that was distributed with this source code.
6+
*
7+
* MIT License
8+
*
9+
* (c) MythicalSystems <mythicalsystems.xyz> - All rights reserved
10+
* (c) NaysKutzu <nayskutzu.xyz> - All rights reserved
11+
* (c) Cassian Gherman <nayskutzu.xyz> - All rights reserved
12+
*
13+
* Permission is hereby granted, free of charge, to any person obtaining a copy
14+
* of this software and associated documentation files (the "Software"), to deal
15+
* in the Software without restriction, including without limitation the rights
16+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
* copies of the Software, and to permit persons to whom the Software is
18+
* furnished to do so, subject to the following conditions:
19+
*
20+
* The above copyright notice and this permission notice shall be included in all
21+
* copies or substantial portions of the Software.
22+
*
23+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29+
* SOFTWARE.
30+
*/
31+
32+
namespace MythicalClient\Cli\Commands;
33+
34+
use MythicalClient\Cli\App;
35+
use MythicalClient\Cli\CommandBuilder;
36+
37+
class Colors extends App implements CommandBuilder
38+
{
39+
public static function execute(array $args): void
40+
{
41+
$app = App::getInstance();
42+
$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';
43+
44+
$app->send($colors);
45+
$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";
46+
$app->send($colors);
47+
}
48+
49+
public static function getDescription(): string
50+
{
51+
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';
52+
}
53+
54+
public static function getSubCommands(int $index): array
55+
{
56+
return [];
57+
}
58+
}

backend/app/Cli/Commands/KeyRegen.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/*
4+
* This file is part of MythicalClient.
5+
* Please view the LICENSE file that was distributed with this source code.
6+
*
7+
* MIT License
8+
*
9+
* (c) MythicalSystems <mythicalsystems.xyz> - All rights reserved
10+
* (c) NaysKutzu <nayskutzu.xyz> - All rights reserved
11+
* (c) Cassian Gherman <nayskutzu.xyz> - All rights reserved
12+
*
13+
* Permission is hereby granted, free of charge, to any person obtaining a copy
14+
* of this software and associated documentation files (the "Software"), to deal
15+
* in the Software without restriction, including without limitation the rights
16+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
* copies of the Software, and to permit persons to whom the Software is
18+
* furnished to do so, subject to the following conditions:
19+
*
20+
* The above copyright notice and this permission notice shall be included in all
21+
* copies or substantial portions of the Software.
22+
*
23+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29+
* SOFTWARE.
30+
*/
31+
32+
namespace MythicalClient\Cli\Commands;
33+
34+
use MythicalClient\Cli\App;
35+
use MythicalClient\Cli\CommandBuilder;
36+
use MythicalClient\Config\ConfigInterface;
37+
use MythicalSystems\Utils\XChaCha20;
38+
39+
class KeyRegen extends App implements CommandBuilder
40+
{
41+
public static function execute(array $args): void
42+
{
43+
$app = App::getInstance();
44+
if (in_array('-force', $args)) {
45+
$isForced = true;
46+
} else {
47+
$isForced = false;
48+
}
49+
50+
if (!$isForced) {
51+
$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.');
52+
$app->send('&7This action is irreversible!');
53+
$app->send('&7Type your answer below:');
54+
$line = trim(readline('> '));
55+
if ($line !== 'yes') {
56+
$app->send('&cAction cancelled.');
57+
return;
58+
} else {
59+
$isForced = true; // If the user types yes, then we can force the key reset
60+
}
61+
}
62+
63+
if ($isForced) {
64+
$mainApp = \MythicalClient\App::getInstance(true);
65+
$mainApp->loadEnv();
66+
$mainApp->getLogger()->warning('Old encryption key was: '.$_ENV['DATABASE_ENCRYPTION_KEY']);
67+
$app->send(message: '&7Old encryption key was: &e'.$_ENV['DATABASE_ENCRYPTION_KEY']);
68+
$newKey = XChaCha20::generateStrongKey(true);
69+
$mainApp->updateEnvValue('DATABASE_ENCRYPTION_KEY',$newKey,true);
70+
sleep(3);
71+
$_ENV['DATABASE_ENCRYPTION_KEY'] = $newKey;
72+
$mainApp->getLogger()->warning('New encryption key is: '.$_ENV['DATABASE_ENCRYPTION_KEY']);
73+
$app->send(message: '&7New encryption key is: &e'.$_ENV['DATABASE_ENCRYPTION_KEY']);
74+
$app->send(message: '&7Key reset successfully!');
75+
} else {
76+
$app->send('&cAction cancelled.');
77+
return;
78+
}
79+
}
80+
81+
public static function getDescription(): string
82+
{
83+
return 'Regenerate the encryption key';
84+
}
85+
86+
public static function getSubCommands(int $index): array
87+
{
88+
return [];
89+
}
90+
}

install.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ chmod -R 777 ./
2828
# Migrations
2929
docker exec mythicalclient_backend bash -c "php mythicalclient migrate"
3030

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

3434

0 commit comments

Comments
 (0)