diff --git a/start.bat b/start.bat index 593f284..d53c2c8 100644 --- a/start.bat +++ b/start.bat @@ -60,6 +60,25 @@ if "%AUTH_TOKEN%"=="" ( ) goto :EOF +:: Function to check if the $APP_KEY is set and generate a new one if not +:check_and_generate_app_key +:: Check if $APP_KEY is already set +set "APP_KEY=" +for /f "tokens=2 delims==" %%i in ('findstr /r /c:"APP_KEY=" configs\core\.env') do ( + set "APP_KEY=%%i" +) +:: If not set, generate a new key automatically +if "%APP_KEY%"=="" ( + echo No application key set. A new key will be generated automatically. + + for /f "delims=" %%i in ('powershell -Command "$RandomBytes = New-Object byte[] 32; [Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($RandomBytes); $Base64String = [Convert]::ToBase64String($RandomBytes); Write-Output $Base64String"') do ( + set APP_KEY=%%i + ) + + powershell -Command "(Get-Content 'configs\core\.env') | ForEach-Object {$_ -replace '\bAPP_KEY=.*', 'APP_KEY=base64:!APP_KEY!'} | Set-Content 'configs\core\.env'" +) +goto :EOF + :: Function to generate a daemon password and set it in the .env file :generate_daemon_password :: Generate a new daemon password @@ -175,6 +194,7 @@ git submodule update --init call :check_has_app_url call :check_has_basic_token call :check_has_daemon_password +call :check_and_generate_app_key :: Build the daemon container docker compose build daemon diff --git a/start.sh b/start.sh index cd9105d..ef321ce 100755 --- a/start.sh +++ b/start.sh @@ -61,6 +61,20 @@ check_has_basic_token() { fi } +check_and_generate_app_key() { + APP_KEY=$(awk '$1 ~ /^APP_KEY/' configs/core/.env | cut -d "=" -f 2) + if [ -z "$APP_KEY" ]; then + echo "No application key set. A new key will be generated automatically." + + if [ "$PLATFORM_OS" = "macOS" ]; then + APP_KEY=$(dd if=/dev/urandom bs=32 count=1 status=none | base64) + sed -i '' -e "s/^APP_KEY=/APP_KEY=base64:$APP_KEY/g" configs/daemon/.env + else + sed -i "s/^APP_KEY=/APP_KEY=base64:$APP_KEY/g" configs/daemon/.env + fi + fi +} + generate_daemon_password() { # Generate a new key pass for the daemon and set to .env file WALLET_PASSWORD=$(openssl rand -hex 32) @@ -158,6 +172,7 @@ git submodule update --init check_has_app_url check_has_basic_token check_has_daemon_password +check_and_generate_app_key docker compose build daemon get_daemon_address