Working prototype and clear and easy to understand logic.

This commit is contained in:
Daan 2025-03-22 03:21:45 +01:00
parent 59194abc27
commit 660312eff3
4 changed files with 167 additions and 79 deletions

View File

@ -93,7 +93,7 @@ WORKDIR /opt/meshcentral
RUN apk update \ RUN apk update \
&& apk add --no-cache --update \ && apk add --no-cache --update \
bash gcc g++ jq make nodejs npm postgresql-client python3 tzdata \ bash gcc g++ jq make nodejs npm python3 tzdata \
&& rm -rf /var/cache/* \ && rm -rf /var/cache/* \
/tmp/* \ /tmp/* \
/usr/share/man/ \ /usr/share/man/ \

View File

@ -18,47 +18,57 @@
You can place the `config.json` file directly under `./meshcentral/data/`, or use the following `.env` file instead. You can place the `config.json` file directly under `./meshcentral/data/`, or use the following `.env` file instead.
```ini ```ini
NODE_ENV=production NODE_ENV = "production"
# Leave CONFIG_FILE as per default by using this, or removing it completely from the list. Otherwise if you know what you are doing, you can use this.
CONFIG_FILE = "/opt/meshcentral/meshcentral-data/config.json"
# DYNAMIC_CONFIG enables the config to be rechecked on every restart. If disabled then the container runtime will not change the config.json.
DYNAMIC_CONFIG = "true"
USE_MONGODB=false # Environment variables for the MeshCentral Config.json
# set already exist mongo connection string url here ALLOWPLUGINS = "false"
MONGO_URL= ALLOW_NEW_ACCOUNTS = "false"
# or set following init params for new mongodb, use it with docker-compose file with mongodb version ALLOWED_ORIGIN = "false"
MONGO_INITDB_ROOT_USERNAME=mongodbadmin ARGS = ""
MONGO_INITDB_ROOT_PASSWORD=mongodbpasswd HOSTNAME = "localhost"
IFRAME = "false"
LOCALSESSIONRECORDING = "true"
MINIFY = "true"
REGENSESSIONKEY = "false"
REVERSE_PROXY = ""
REVERSE_PROXY_TLS_PORT = ""
WEBRTC = "false"
# initial meshcentral-variables # MongoDB Variables
# the following options are only used if no config.json exists in the data-folder INCLUDE_MONGODB_TOOLS = "false"
USE_MONGODB = "false"
MONGO_HOST = ""
MONGO_PORT = "27017"
MONGO_USERNAME = ""
MONGO_PASS = ""
MONGO_URL = ""
# your hostname # PostgreSQL Variables
HOSTNAME=my.domain.com INCLUDE_POSTGRESQL_TOOLS = "false"
# set to your reverse proxy IP if you want to put meshcentral behind a reverse proxy USE_POSTGRESQL = "false"
REVERSE_PROXY=false PSQL_HOST = ""
REVERSE_PROXY_TLS_PORT= PSQL_PORT = "5432"
# set to true if you wish to enable iframe support PSQL_USER = ""
IFRAME=false PSQL_PASS = ""
# set to false if you want disable self-service creation of new accounts besides the first (admin) PSQL_DATABASE = ""
ALLOW_NEW_ACCOUNTS=true
# set to true to enable WebRTC - per documentation it is not officially released with meshcentral and currently experimental. Use with caution # MariaDB/MySQL Variables (Alpine Linux only provides MariaDB binaries)
WEBRTC=false INCLUDE_MARIADB_TOOLS = "false"
# set to true to allow plugins USE_MARIADB = "false"
ALLOWPLUGINS=false MARIADB_HOST = ""
# set to true to allow session recording MARIADB_PORT = "3306"
LOCALSESSIONRECORDING=false MARIADB_USER = ""
# set to enable or disable minification of json, reduces traffic MARIADB_PASS = ""
MINIFY=true MARIADB_DATABASE = ""
# set this value to add extra arguments to meshcentral on startup (e.g --debug ldap)
ARGS=
# set to the hostname(s) meshcentral will be reachable on, or true to disable origin checking
# forms allowed "hostname" or "hostname1,hostname2" or ["hostname1","hostname2"]
ALLOWED_ORIGIN=false
``` ```
## docker-compose.yml ## docker-compose.yml
```yaml ```yaml
version: '3'
services: services:
meshcentral: meshcentral:
restart: always restart: always
@ -66,7 +76,6 @@ services:
# use the official meshcentral container # use the official meshcentral container
image: ghcr.io/ylianst/meshcentral:latest image: ghcr.io/ylianst/meshcentral:latest
ports: ports:
# MeshCentral will moan and try everything not to use port 80, but you can also use it if you so desire, just change the config.json according to your needs
- 8086:443 - 8086:443
env_file: env_file:
- .env - .env

View File

@ -1,7 +1,9 @@
{ {
"$schema": "https://raw.githubusercontent.com/Ylianst/MeshCentral/master/meshcentral-config-schema.json", "$schema": "https://raw.githubusercontent.com/Ylianst/MeshCentral/master/meshcentral-config-schema.json",
"settings": { "settings": {
"plugins":{"enabled": false}, "plugins":{
"enabled": false
},
"cert": "myserver.mydomain.com", "cert": "myserver.mydomain.com",
"_WANonly": true, "_WANonly": true,
"_LANonly": true, "_LANonly": true,
@ -22,6 +24,13 @@
"user": "", "user": "",
"password": "", "password": "",
"database": "" "database": ""
},
"_mariaDB": {
"host": "",
"port": "",
"user": "",
"password": "",
"database": ""
} }
}, },
"domains": { "domains": {

View File

@ -3,9 +3,11 @@
graceful_shutdown() { graceful_shutdown() {
echo "Received SIGTERM. Cleaning up..." echo "Received SIGTERM. Cleaning up..."
node /opt/meshcentral/meshcentral/meshcentral --stop node /opt/meshcentral/meshcentral/meshcentral --stop
echo "MeshCentral process stopped. Exiting..."
exit 0 exit 0
} }
trap cleanup SIGTERM trap graceful_shutdown SIGTERM
### Start MeshCentral Docker Container. ### Start MeshCentral Docker Container.
@ -20,8 +22,13 @@ else
fi fi
if [[ "$DYNAMIC_CONFIG" =~ ^(true|yes)$ ]]; then if [[ "$DYNAMIC_CONFIG" =~ ^(true|yes)$ ]]; then
cat $CONFIG_FILE
echo "Using Dynamic Configuration values..."
# BEGIN DATABASE CONFIGURATION FIELDS
if [[ "$USE_MONGODB" =~ ^(true|yes)$ ]]; then if [[ "$USE_MONGODB" =~ ^(true|yes)$ ]]; then
echo "Enabling MongoDB-connector..."
if [[ -n "$MONGO_URL" ]]; then if [[ -n "$MONGO_URL" ]]; then
echo "MONGO_URL is set, using that..." echo "MONGO_URL is set, using that..."
else else
@ -32,20 +39,49 @@ if [[ "$DYNAMIC_CONFIG" =~ ^(true|yes)$ ]]; then
sed -i 's/"_mongoDb"/"mongoDb"/' "$CONFIG_FILE" sed -i 's/"_mongoDb"/"mongoDb"/' "$CONFIG_FILE"
sed -i "s/\"mongoDb\": *\"[^\"]*\"/\"mongoDb\": \"$ESCAPED_MONGO_URL\"/" "$CONFIG_FILE" sed -i "s/\"mongoDb\": *\"[^\"]*\"/\"mongoDb\": \"$ESCAPED_MONGO_URL\"/" "$CONFIG_FILE"
else else
echo "Disabling MongoDB-connector..."
sed -i 's/"mongoDb"/"_mongoDb"/' "$CONFIG_FILE" sed -i 's/"mongoDb"/"_mongoDb"/' "$CONFIG_FILE"
fi fi
if [[ "$USE_POSTGRESQL" =~ ^(true|yes)$ ]]; then if [[ "$USE_POSTGRESQL" =~ ^(true|yes)$ ]]; then
echo "So you wanna postgres" echo "Enabling PostgreSQL-connector..."
sed -i 's/"_postgres"/"postgres"/' "$CONFIG_FILE"
jq --arg psql_host "$PSQL_HOST" \
--arg psql_port "$PSQL_PORT" \
--arg psql_user "$PSQL_USER" \
--arg psql_pass "$PSQL_PASS" \
--arg psql_db "$PSQL_DATABASE" \
'.settings.postgres.host = $psql_host |
.settings.postgres.port = $psql_port |
.settings.postgres.user = $psql_user |
.settings.postgres.password = $psql_pass |
.settings.postgres.database = $psql_db' \
"$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
else else
echo "No Postgres" echo "Disabling PostgreSQL-connector..."
sed -i 's/"postgres"/"_postgres"/' "$CONFIG_FILE"
fi fi
if [[ "$USE_MARIADB" =~ ^(true|yes)$ ]]; then if [[ "$USE_MARIADB" =~ ^(true|yes)$ ]]; then
echo "So you wanna MariaDB" echo "Enabling MariaDB-connector..."
sed -i 's/"_mariaDB"/"mariaDB"/' "$CONFIG_FILE"
jq --arg mariadb_host "$MARIADB_HOST" \
--arg mariadb_port "$MARIADB_PORT" \
--arg mariadb_user "$MARIADB_USER" \
--arg mariadb_pass "$MARIADB_PASS" \
--arg mariadb_db "$MARIADB_DATABASE" \
'.settings.mariaDB.host = $mariadb_host |
.settings.mariaDB.port = $mariadb_port |
.settings.mariaDB.user = $mariadb_user |
.settings.mariaDB.password = $mariadb_pass |
.settings.mariaDB.database = $mariadb_db' \
"$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
else else
echo "No MariaDB" echo "Disabling MariaDB-connector..."
sed -i 's/"mariaDB"/"_mariaDB"/' "$CONFIG_FILE"
fi fi
# END DATABASE CONFIGURATION FIELDS
# Doing the bulk with JQ utility. Given the remaining variables an opportunity with Sed. # Doing the bulk with JQ utility. Given the remaining variables an opportunity with Sed.
# The way this works is if the environment variable is empty, it will add a _ in front of the variable, commenting it. # The way this works is if the environment variable is empty, it will add a _ in front of the variable, commenting it.
@ -60,102 +96,136 @@ if [[ "$DYNAMIC_CONFIG" =~ ^(true|yes)$ ]]; then
SESSION_KEY=$(tr -dc 'A-Z0-9' < /dev/urandom | fold -w 96 | head -n 1) SESSION_KEY=$(tr -dc 'A-Z0-9' < /dev/urandom | fold -w 96 | head -n 1)
sed -i 's/"_sessionKey"/"sessionKey"/' "$CONFIG_FILE" sed -i 's/"_sessionKey"/"sessionKey"/' "$CONFIG_FILE"
sed -i "s/\"sessionKey\": *\"[^\"]*\"/\"sessionKey\": \"$SESSION_KEY\"/" "$CONFIG_FILE" jq --arg session_key "$SESSION_KEY" \
'.settings.sessionKey = $session_key' \
"$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
else else
echo "REGENSESSIONKEY is not 'true' or 'yes', therefore it's being kept as is." echo "REGENSESSIONKEY is not 'true' or 'yes', therefore it's being kept as is."
fi fi
# HOSTNAME # HOSTNAME
if [[ -n $HOSTNAME ]] && [[ $HOSTNAME =~ ^([a-zA-Z0-9-]+\.)+[a-zA-Z0-9-]+$ ]]; then if [[ -n $HOSTNAME ]] && [[ $HOSTNAME =~ ^[a-zA-Z0-9-]+$ ]]; then
echo "Setting hostname (cert)... - $HOSTNAME" echo "Setting hostname (cert)... $HOSTNAME"
sed -i 's/"_cert"/"cert"/' "$CONFIG_FILE"
sed -i "s/\"cert\": *\"[^\"]*\"/\"cert\": \"$HOSTNAME\"/" "$CONFIG_FILE" jq --arg hostname "$HOSTNAME" \
'.settings.cert = $hostname' \
"$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
else else
echo "Invalid hostname, commenting it out..." echo "Invalid or no hostname, defaulting to 'localhost', value given: $HOSTNAME"
sed -i "s/\"cert\": *\"[^\"]*\"/\"cert\": \"localhost\"/" "$CONFIG_FILE" jq --arg hostname "localhost" \
'.settings.cert = $hostname' \
"$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
fi fi
# ALLOW_NEW_ACCOUNTS # ALLOW_NEW_ACCOUNTS
if [[ -n $ALLOW_NEW_ACCOUNTS ]] && [[ $ALLOW_NEW_ACCOUNTS =~ ^(true|false)$ ]]; then if [[ -n $ALLOW_NEW_ACCOUNTS ]] && [[ $ALLOW_NEW_ACCOUNTS =~ ^(true|false)$ ]]; then
echo "Setting NewAccounts... - $ALLOW_NEW_ACCOUNTS" echo "Setting NewAccounts... $ALLOW_NEW_ACCOUNTS"
sed -i 's/"_NewAccounts"/"NewAccounts"/' "$CONFIG_FILE" sed -i 's/"_NewAccounts"/"NewAccounts"/' "$CONFIG_FILE"
sed -i "s/\"NewAccounts\": *[a-z]*/\"NewAccounts\": $ALLOW_NEW_ACCOUNTS/" "$CONFIG_FILE" jq --argjson new_accounts "$ALLOW_NEW_ACCOUNTS" \
'.domains[""].NewAccounts = $new_accounts' \
"$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
else else
echo "Invalid ALLOW_NEW_ACCOUNTS value given, commenting out so default applies..." echo "Invalid or no ALLOW_NEW_ACCOUNTS value given, commenting out so default applies... Value given: $ALLOW_NEW_ACCOUNTS"
sed -i 's/"NewAccounts":/"_NewAccounts":/g' "$CONFIG_FILE" sed -i 's/"NewAccounts":/"_NewAccounts":/g' "$CONFIG_FILE"
fi fi
# ALLOWPLUGINS # ALLOWPLUGINS
if [[ -n $ALLOWPLUGINS ]] && [[ $ALLOWPLUGINS =~ ^(true|false)$ ]]; then if [[ -n $ALLOWPLUGINS ]] && [[ $ALLOWPLUGINS =~ ^(true|false)$ ]]; then
echo "Setting plugins... - $ALLOWPLUGINS" echo "Setting plugins... $ALLOWPLUGINS"
sed -i 's/"_plugins"/"plugins"/' "$CONFIG_FILE" sed -i 's/"_plugins"/"plugins"/' "$CONFIG_FILE"
sed -i "s/\"plugins\": *{[^}]*}/\"plugins\": {\"enabled\": $ALLOWPLUGINS}/" "$CONFIG_FILE" jq --argjson allow_plugins "$ALLOWPLUGINS" \
'.settings.plugins.enabled = $allow_plugins' \
"$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
else else
echo "Invalid ALLOWPLUGINS value given, commenting out so default applies..." echo "Invalid or no ALLOWPLUGINS value given, commenting out so default applies... Value given: $ALLOWPLUGINS"
sed -i 's/"plugins":/"_plugins":/g' "$CONFIG_FILE" sed -i 's/"plugins":/"_plugins":/g' "$CONFIG_FILE"
fi fi
# LOCALSESSIONRECORDING # LOCALSESSIONRECORDING
if [[ -n $LOCALSESSIONRECORDING ]] && [[ $LOCALSESSIONRECORDING =~ ^(true|false)$ ]]; then if [[ -n $LOCALSESSIONRECORDING ]] && [[ $LOCALSESSIONRECORDING =~ ^(true|false)$ ]]; then
echo "Setting localSessionRecording... - $LOCALSESSIONRECORDING" echo "Setting localSessionRecording... $LOCALSESSIONRECORDING"
sed -i 's/"_localSessionRecording"/"localSessionRecording"/' "$CONFIG_FILE" sed -i 's/"_localSessionRecording"/"localSessionRecording"/' "$CONFIG_FILE"
sed -i "s/\"localSessionRecording\": *[a-z]*/\"localSessionRecording\": $LOCALSESSIONRECORDING/" "$CONFIG_FILE" jq --argjson session_recording "$LOCALSESSIONRECORDING" \
'.domains[""].localSessionRecording = $session_recording' \
"$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
else else
echo "Invalid LOCALSESSIONRECORDING value given, commenting out so default applies..." echo "Invalid or no LOCALSESSIONRECORDING value given, commenting out so default applies... Value given: $LOCALSESSIONRECORDING"
sed -i 's/"localSessionRecording":/"_localSessionRecording":/g' "$CONFIG_FILE" sed -i 's/"localSessionRecording":/"_localSessionRecording":/g' "$CONFIG_FILE"
fi fi
# MINIFY # MINIFY
if [[ -n $MINIFY ]] && [[ $MINIFY =~ ^(true|false)$ ]]; then if [[ -n $MINIFY ]] && [[ $MINIFY =~ ^(true|false)$ ]]; then
echo "Setting minify... - $MINIFY" echo "Setting minify... $MINIFY"
sed -i 's/"_minify"/"minify"/' "$CONFIG_FILE" sed -i 's/"_minify"/"minify"/' "$CONFIG_FILE"
sed -i "s/\"minify\": *[a-z]*/\"minify\": $MINIFY/" "$CONFIG_FILE" jq --arg minify "$MINIFY" \
'.domains[""].minify = $minify' \
"$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
#sed -i "s/\"minify\": *[a-z]*/\"minify\": $MINIFY/" "$CONFIG_FILE"
else else
echo "Invalid MINIFY value given, commenting out so default applies..." echo "Invalid or no MINIFY value given, commenting out so default applies... Value given: $MINIFY"
sed -i 's/"minify":/"_minify":/g' "$CONFIG_FILE" sed -i 's/"minify":/"_minify":/g' "$CONFIG_FILE"
fi fi
# WEBRTC # WEBRTC
if [[ -n $WEBRTC ]] && [[ $WEBRTC =~ ^(true|false)$ ]]; then if [[ -n $WEBRTC ]] && [[ $WEBRTC =~ ^(true|false)$ ]]; then
echo "Setting WebRTC... - $WEBRTC" echo "Setting WebRTC... $WEBRTC"
sed -i 's/"_WebRTC"/"WebRTC"/' "$CONFIG_FILE" sed -i 's/"_WebRTC"/"WebRTC"/' "$CONFIG_FILE"
sed -i "s/\"WebRTC\": *[a-z]*/\"WebRTC\": $WEBRTC/" "$CONFIG_FILE" jq --argjson webrtc "$WEBRTC" \
'.settings.WebRTC = $webrtc' \
"$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
#sed -i "s/\"WebRTC\": *[a-z]*/\"WebRTC\": $WEBRTC/" "$CONFIG_FILE"
else else
echo "Invalid WEBRTC value given, commenting out so default applies..." echo "Invalid or no WEBRTC value given, commenting out so default applies... Value given: $WEBRTC"
sed -i 's/"WebRTC":/"_WebRTC":/g' "$CONFIG_FILE" sed -i 's/"WebRTC":/"_WebRTC":/g' "$CONFIG_FILE"
fi fi
# IFRAME # IFRAME
if [[ -n $IFRAME ]] && [[ $IFRAME =~ ^(true|false)$ ]]; then if [[ -n $IFRAME ]] && [[ $IFRAME =~ ^(true|false)$ ]]; then
echo "Setting AllowFraming... - $IFRAME" echo "Setting AllowFraming... $IFRAME"
sed -i 's/"_AllowFraming"/"AllowFraming"/' "$CONFIG_FILE" sed -i 's/"_AllowFraming"/"AllowFraming"/' "$CONFIG_FILE"
sed -i "s/\"AllowFraming\": *[a-z]*/\"AllowFraming\": $IFRAME/" "$CONFIG_FILE" jq --argjson allow_framing "$IFRAME" \
'.settings.AllowFraming = $allow_framing' \
"$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
else else
echo "Invalid IFRAME value given, commenting out so default applies..." echo "Invalid or no IFRAME value given, commenting out so default applies... Value given: $IFRAME"
sed -i 's/"AllowFraming":/"_AllowFraming":/g' "$CONFIG_FILE" sed -i 's/"AllowFraming":/"_AllowFraming":/g' "$CONFIG_FILE"
fi fi
# ALLOWED_ORIGIN # ALLOWED_ORIGIN
if [[ -n $ALLOWED_ORIGIN ]] && [[ $ALLOWED_ORIGIN =~ ^(true|false)$ ]]; then if [[ -n $ALLOWED_ORIGIN ]] && [[ $ALLOWED_ORIGIN =~ ^(true|false)$ ]]; then
echo "Setting allowedOrigin... - $ALLOWED_ORIGIN" echo "Setting allowedOrigin... $ALLOWED_ORIGIN"
sed -i 's/"_allowedOrigin"/"allowedOrigin"/' "$CONFIG_FILE" sed -i 's/"_allowedOrigin"/"allowedOrigin"/' "$CONFIG_FILE"
sed -i "s/\"allowedOrigin\": *[a-z]*/\"allowedOrigin\": $ALLOWED_ORIGIN/" "$CONFIG_FILE" jq --arg allowed_origin "$ALLOWED_ORIGIN" \
'.domains[""].allowedOrigin = $allowed_origin' \
"$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
else else
echo "Invalid ALLOWED_ORIGIN value given, commenting out so default applies..." echo "Invalid or no ALLOWED_ORIGIN value given, commenting out so default applies... Value given: $ALLOWED_ORIGIN"
sed -i 's/"allowedOrigin":/"_allowedOrigin":/g' "$CONFIG_FILE" sed -i 's/"allowedOrigin":/"_allowedOrigin":/g' "$CONFIG_FILE"
fi fi
# certUrl
if [[ -n $REVERSE_PROXY ]] && [[ -n $REVERSE_PROXY_TLS_PORT ]]; then
REVERSE_PROXY_STRING="${REVERSE_PROXY}:${REVERSE_PROXY_TLS_PORT}"
echo "Setting certUrl... - $REVERSE_PROXY_STRING"
sed -i 's/"_certUrl"/"certUrl"/' "$CONFIG_FILE"
jq --arg cert_url "$REVERSE_PROXY_STRING" \
'.domains[""].certUrl = $cert_url' \
"$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
#sed -i "s/\"certUrl\": *[a-z]*/\"certUrl\": $REVERSE_PROXY_STRING/" "$CONFIG_FILE"
else
echo "Invalid or no REVERSE_PROXY and/or REVERSE_PROXY_TLS_PORT value given, commenting out so default applies... Value(s) given: $REVERSE_PROXY_STRING"
sed -i 's/"certUrl":/"_certUrl":/g' "$CONFIG_FILE"
fi
echo -e "\n$(cat "$CONFIG_FILE")" echo -e "\n$(cat "$CONFIG_FILE")"
# TO DO CERTURL - POSTGRESQL - MONGO_INITDB_ROOT_PASSWORD="pass"
#if [[ "$ALLOWED_ORIGIN" =~ ^\[.*\]|^true|^false ]]; then
# sed -i "s/\"allowedOrigin\": false/\"allowedOrigin\": $ALLOWED_ORIGIN/" meshcentral-data/"${CONFIG_FILE}"
#else
# sed -i "s/\"allowedOrigin\": false/\"allowedOrigin\": \"$ALLOWED_ORIGIN\"/" meshcentral-data/"${CONFIG_FILE}"
#fi
#SESSION_KEY= # Session key should be random. Not passed in through arguments.
#sed -i "s/\"_sessionKey\": \"MyReallySecretPassword1\"/\"sessionKey\": \"$SESSION_KEY\"/" meshcentral-data/"${CONFIG_FILE}"
else else
echo "Leaving config as-is." echo "Leaving config as-is."
fi fi