Skip to content

Commit

Permalink
Add VIAAAS Config Parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
RoblKyogre committed Feb 21, 2024
1 parent 11aa600 commit caf2ce8
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 33 deletions.
24 changes: 12 additions & 12 deletions docker.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,49 @@
################

# See https://docs.microsoft.com/en-us/azure/active-directory/develop/scenario-spa-app-registration
AZURE_CLIENT_ID="my-client-id"
WEB_AZURE_CLIENT_ID="my-client-id"

# Change to your Web UI domain(s)
WHITELISTED_ORIGINS="
WEB_WHITELISTED_ORIGINS="
https://viaaas.example.com
https://anotherone.com
"

# OPTIONAL
#CORS_PROXY="cors.example.com"
#WEB_CORS_PROXY="cors.example.com"

# OPTIONAL
#INSTANCE_SUFFIX="viaaas.example.com[:25565]"
#WEB_INSTANCE_SUFFIX="viaaas.example.com[:25565]"

###################
## VIAAAS CONFIG ##
###################

VIAAAS_BIND_ADDRESSES="
0.0.0.0
0.0.0.0
"

VIAAAS_BACKEND_PROXY=''
VIAAAS_BACKEND_PROXY=

VIAAAS_COMPRESSION_LEVEL=6

VIAAAS_REQUIRE_HOST_NAME=true

VIAAAS_HOST_NAME="
viaaas.localhost
via.localhost
via.localho.st
viaaas.localhost
via.localhost
via.localho.st
"

VIAAAS_DEFAULT_BACKEND_PORT=25565
VIAAAS_DEFAULT_PARAMENTERS="{}"
VIAAAS_BLOCK_LOCAL_ADDRESS=true
VIAAAS_BLOCKED_BACK_ADDRESSES='
*.hypixel.net
*.minemen.club
*.hypixel.net
*.minemen.club
'
VIAAAS_ALLOWED_BACK_ADDRESSES='
*
*
'
VIAAAS_SHOW_VERSION_PING=true
VIAAAS_SHOW_BRAND_INFO=true
Expand Down
86 changes: 86 additions & 0 deletions root/scripts/parse-viaaas-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/bash

CONFIG_DIR=/app/config
CONFIG_FILE=${CONFIG_DIR}/viaaas.yml

{
if [ -f ${CONFIG_FILE} ]; then
echo "${CONFIG_FILE} exists, skipping parse..."
exit 0
fi
}

set -e

echo "Parsing environment for ${CONFIG_FILE}"
mkdir -p ${CONFIG_DIR}
touch ${CONFIG_FILE}

# parse env vars
IFS='
'
parsedBindAddresses="${VIAAAS_BIND_ADDRESSES:-0.0.0.0}"
parsedBackendProxy="${VIAAAS_BACKEND_PROXY}"
parsedCompressionLevel="${VIAAAS_COMPRESSION_LEVEL:-6}"
parsedRequireHostName="${VIAAAS_REQUIRE_HOST_NAME:-true}"
parsedHostName="${VIAAAS_HOST_NAME:-viaaas.localhost${IFS}via.localhost${IFS}via.localho.st}"
parsedBackendPort="${VIAAAS_BACKEND_PORT:-25565}"
parsedDefaultParameters="${VIAAAS_DEFAULT_PARAMETERS:-{}}"
parsedBlockLocalAddress="${VIAAAS_BLOCK_LOCAL_ADDRESS:-true}"
parsedBlockedBackAddresses="${VIAAAS_BLOCKED_BACK_ADDRESSES:-*.hypixel.net${IFS}*.minemen.club}"
parsedAllowedBackAddresses="${VIAAAS_ALLOWED_BACK_ADDRESSES:-*}"
parsedShowVersionPing="${VIAAAS_SHOW_VERSION_PING:-true}"
parsedShowBrandInfo="${VIAAAS_SHOW_BRAND_INFO:-true}"
parsedRateLimitWs="${VIAAAS_RATE_LIMIT_WS:-1.5}"
parsedRateLimitConnectionMc="${VIAAAS_RATE_LIMIT_CONNECTION_MC:-10.0}"
parsedRateLimitLoginMc="${VIAAAS_RATE_LIMIT_LOGIN_MC:-0.2}"
parsedListeningWsLimit="${VIAAAS_LISTENING_WS_LIMIT:-10}"
parsedFaviconUrl="${VIAAAS_FAVICON_URL:-https://raw.githubusercontent.com/ViaVersion/VIAaaS/396dcb40249ed0ef292e223c32e7f130699e7153/src/main/resources/web/logo.png}"
parsedForceOnlineMode="${VIAAAS_FORCE_ONLINE_MODE:-false}"
parsedMaxPlayers="${VIAAAS_MAX_PLAYERS:-20}"
parsedProtocolDetectorCache="${VIAAAS_PROTOCOL_DETECTOR_CACHE:-20}"
parsedJwtSecret="${VIAAAS_JWT_SECRET:-$(head -c 64 /dev/urandom | base64 --wrap=0)}"
parsedBackendAddress="${VIAAAS_BACKEND_ADDRESS}"

# write to config file
echo 'bind-addresses:' >> ${CONFIG_FILE}
for addr in ${parsedBindAddresses}; do
echo "- $(echo ${addr} | xargs echo -n)" >> ${CONFIG_FILE}
done

echo "backend-proxy: '${parsedBackendProxy}'" >> ${CONFIG_FILE}
echo "compression-level: ${parsedCompressionLevel}" >> ${CONFIG_FILE}
echo "require-host-name: ${parsedRequireHostName}" >> ${CONFIG_FILE}

echo 'host-name:' >> ${CONFIG_FILE}
for name in ${parsedHostName}; do
echo "- $(echo ${name} | xargs echo -n)" >> ${CONFIG_FILE}
done

echo "default-backend-port: ${parsedBackendPort}" >> ${CONFIG_FILE}
echo "default-parameters: ${parsedDefaultParameters}" >> ${CONFIG_FILE}
echo "block-local-address: ${parsedBlockLocalAddress}" >> ${CONFIG_FILE}


echo 'blocked-back-addresses:' >> ${CONFIG_FILE}
for baddr in ${parsedBlockedBackAddresses}; do
echo "- '$(echo ${baddr} | xargs echo -n)'" >> ${CONFIG_FILE}
done

echo 'allowed-back-addresses:' >> ${CONFIG_FILE}
for aaddr in ${parsedAllowedBackAddresses}; do
echo "- '$(echo ${aaddr} | xargs echo -n)'" >> ${CONFIG_FILE}
done

echo "show-version-ping: ${parsedShowVersionPing}" >> ${CONFIG_FILE}
echo "show-brand-info: ${parsedShowBrandInfo}" >> ${CONFIG_FILE}
echo "rate-limit-ws: ${parsedRateLimitWs}" >> ${CONFIG_FILE}
echo "rate-limit-connection-mc: ${parsedRateLimitConnectionMc}" >> ${CONFIG_FILE}
echo "rate-limit-login-mc: ${parsedRateLimitLoginMc}" >> ${CONFIG_FILE}
echo "listening-ws-limit: ${parsedListeningWsLimit}" >> ${CONFIG_FILE}
echo "favicon-url: ${parsedFaviconUrl}" >> ${CONFIG_FILE}
echo "force-online-mode: ${parsedForceOnlineMode}" >> ${CONFIG_FILE}
echo "max-players: ${parsedMaxPlayers}" >> ${CONFIG_FILE}
echo "protocol-detector-cache: ${parsedProtocolDetectorCache}" >> ${CONFIG_FILE}
echo "jwt-secret: ${parsedJwtSecret}" >> ${CONFIG_FILE}
echo "backend-address: '${parsedBackendAddress}'" >> ${CONFIG_FILE}
39 changes: 18 additions & 21 deletions root/scripts/parse-web-config.sh
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
#!/usr/bin/bash

WEB_CONFIG_DIR=/app/config/web/js
WEB_CONFIG_FILE=${WEB_CONFIG_DIR}/config.js
CONFIG_DIR=/app/config/web/js
CONFIG_FILE=${CONFIG_DIR}/config.js

{
if [ -f ${WEB_CONFIG_FILE} ]; then
echo "${WEB_CONFIG_FILE} exists, skipping parse..."
if [ -f ${CONFIG_FILE} ]; then
echo "${CONFIG_FILE} exists, skipping parse..."
exit 0
fi
}

set -e

echo "Parsing environment for ${WEB_CONFIG_FILE}"
mkdir -p ${WEB_CONFIG_DIR}
touch ${WEB_CONFIG_FILE}
cat /dev/null >| ${WEB_CONFIG_FILE}
echo "Parsing environment for ${CONFIG_FILE}"
mkdir -p ${CONFIG_DIR}
touch ${CONFIG_FILE}

# defaults lifted from https://github.com/ViaVersion/VIAaaS/blob/master/src/main/resources/web/js/config.js
[[ -z "${AZURE_CLIENT_ID}" ]] && parsedClientId='a370fff9-7648-4dbf-b96e-2b4f8d539ac2' || parsedClientId="${AZURE_CLIENT_ID}"
[[ -z "${WHITELISTED_ORIGINS}" ]] && parsedOrigins='https://via.re.yt.nom.br' || parsedOrigins="${WHITELISTED_ORIGINS}"
[[ -z "${CORS_PROXY}" ]] && parsedProxy='https://cors.re.yt.nom.br/' || parsedProxy="${CORS_PROXY}"
[[ -z "${INSTANCE_SUFFIX}" ]] && parsedSuffix='null' || parsedSuffix="\"${INSTANCE_SUFFIX}\""
parsedClientId="${WEB_AZURE_CLIENT_ID:-a370fff9-7648-4dbf-b96e-2b4f8d539ac2}"
parsedOrigins="${WEB_WHITELISTED_ORIGINS:-https://via.re.yt.nom.br}"
parsedProxy="${WEB_CORS_PROXY:-https://cors.re.yt.nom.br/}"
[[ -z "${WEB_INSTANCE_SUFFIX}" ]] && parsedSuffix='null' || parsedSuffix="\"${WEB_INSTANCE_SUFFIX}\""

# write to config file
echo "const azureClientId = \"${parsedClientId}\";" >> ${WEB_CONFIG_FILE}

echo -n "const whitelistedOrigin = [" >> ${WEB_CONFIG_FILE}
echo "const azureClientId = \"${parsedClientId}\";" >> ${CONFIG_FILE}

echo -n "const whitelistedOrigin = [" >> ${CONFIG_FILE}
IFS='
'
count=0
for origin in ${parsedOrigins}
do
if [ ${count} -ne 0 ]; then
echo -n "," >> ${WEB_CONFIG_FILE}
echo -n "," >> ${CONFIG_FILE}
fi
echo -n "\"$(echo ${origin} | xargs echo -n)\"" >> ${WEB_CONFIG_FILE}
echo -n "\"$(echo ${origin} | xargs echo -n)\"" >> ${CONFIG_FILE}
count=$((count+1))
done
echo "];" >> ${CONFIG_FILE}

echo "];" >> ${WEB_CONFIG_FILE}

echo "var defaultCorsProxy = \"${parsedProxy}\";" >> ${WEB_CONFIG_FILE}
echo "var defaultInstanceSuffix = ${parsedSuffix};" >> ${WEB_CONFIG_FILE}
echo "var defaultCorsProxy = \"${parsedProxy}\";" >> ${CONFIG_FILE}
echo "var defaultInstanceSuffix = ${parsedSuffix};" >> ${CONFIG_FILE}

0 comments on commit caf2ce8

Please sign in to comment.