entrypoint.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/bin/bash
  2. echo "Setting up docker env..."
  3. echo "MODE: $MODE"
  4. echo "USE_NEW_DESIGN: $USE_NEW_DESIGN"
  5. echo "WEBPORT: $WEBPORT"
  6. echo "REDACT_IP_ADDRESSES: $REDACT_IP_ADDRESSES"
  7. echo "DB_TYPE: $DB_TYPE"
  8. echo "ENABLE_ID_OBFUSCATION: $ENABLE_ID_OBFUSCATION"
  9. echo "GDPR_EMAIL: $GDPR_EMAIL"
  10. set -e
  11. #set -x
  12. is_alpine() {
  13. [ -f /etc/alpine-release ]
  14. }
  15. # Cleanup
  16. rm -rf /var/www/html/*
  17. # Copy frontend files
  18. cp /speedtest/*.js /var/www/html/
  19. # Copy design switch files
  20. cp /speedtest/config.json /var/www/html/
  21. cp /speedtest/design-switch.js /var/www/html/
  22. # Copy favicon
  23. cp /speedtest/favicon.ico /var/www/html/
  24. # Set custom webroot on alpine
  25. if is_alpine; then
  26. echo "ALPINE IMAGE"
  27. sed -i "s#\"/var/www/localhost/htdocs\"#\"/var/www/html\"#g" /etc/apache2/httpd.conf
  28. else
  29. echo "DEBIAN IMAGE"
  30. fi
  31. # Set up backend side for standlone modes
  32. if [[ "$MODE" == "standalone" || "$MODE" == "dual" ]]; then
  33. cp -r /speedtest/backend/ /var/www/html/backend
  34. if [ ! -z "$IPINFO_APIKEY" ]; then
  35. sed -i s/\$IPINFO_APIKEY\ =\ \'\'/\$IPINFO_APIKEY\ =\ \'$IPINFO_APIKEY\'/g /var/www/html/backend/getIP_ipInfo_apikey.php
  36. fi
  37. fi
  38. if [ "$MODE" == "backend" ]; then
  39. cp -r /speedtest/backend/* /var/www/html
  40. if [ ! -z "$IPINFO_APIKEY" ]; then
  41. sed -i s/\$IPINFO_APIKEY\ =\ \'\'/\$IPINFO_APIKEY\ =\ \'$IPINFO_APIKEY\'/g /var/www/html/getIP_ipInfo_apikey.php
  42. fi
  43. fi
  44. # Set up index.php for frontend-only or standalone modes
  45. if [[ "$MODE" == "frontend" || "$MODE" == "dual" || "$MODE" == "standalone" ]]; then
  46. # Copy design files (switcher + both designs)
  47. cp /speedtest/index.html /var/www/html/
  48. cp /speedtest/index-classic.html /var/www/html/
  49. cp /speedtest/index-modern.html /var/www/html/
  50. # Copy frontend assets directly to root-level subdirectories (no frontend/ parent dir)
  51. mkdir -p /var/www/html/styling /var/www/html/javascript /var/www/html/images /var/www/html/fonts
  52. cp -a /speedtest/frontend/styling/* /var/www/html/styling/
  53. cp -a /speedtest/frontend/javascript/* /var/www/html/javascript/
  54. cp -a /speedtest/frontend/images/* /var/www/html/images/
  55. cp -a /speedtest/frontend/fonts/* /var/www/html/fonts/ 2>/dev/null || true
  56. # Copy frontend config files
  57. cp /speedtest/frontend/settings.json /var/www/html/settings.json 2>/dev/null || true
  58. if [ ! -f /var/www/html/server-list.json ]; then
  59. echo "no server-list.json found, create one for local host"
  60. # generate config for just the local server
  61. echo '[{"name":"local","server":"/backend", "dlURL": "garbage.php", "ulURL": "empty.php", "pingURL": "empty.php", "getIpURL": "getIP.php", "sponsorName": "", "sponsorURL": "", "id":1 }]' > /var/www/html/server-list.json
  62. fi
  63. # Replace GDPR email placeholder if GDPR_EMAIL is set
  64. if [ ! -z "$GDPR_EMAIL" ]; then
  65. # Escape special sed characters: & (replacement), / (delimiter), \ (escape), $ (variable)
  66. GDPR_EMAIL_ESCAPED=$(printf '%s\n' "$GDPR_EMAIL" | sed 's/[&/\\]/\\&/g; s/\$/\\$/g')
  67. for html_file in /var/www/html/index-modern.html /var/www/html/index-classic.html; do
  68. if [ -f "$html_file" ]; then
  69. sed -i "s/TO BE FILLED BY DEVELOPER/$GDPR_EMAIL_ESCAPED/g; s/PUT@YOUR_EMAIL.HERE/$GDPR_EMAIL_ESCAPED/g" "$html_file"
  70. fi
  71. done
  72. fi
  73. fi
  74. # Configure design preference via config.json
  75. if [ "$USE_NEW_DESIGN" == "true" ]; then
  76. sed -i 's/"useNewDesign": false/"useNewDesign": true/' /var/www/html/config.json
  77. fi
  78. # Apply Telemetry settings when running in standalone or frontend mode and telemetry is enabled
  79. if [[ "$TELEMETRY" == "true" && ("$MODE" == "frontend" || "$MODE" == "standalone" || "$MODE" == "dual") ]]; then
  80. cp -r /speedtest/results /var/www/html/results
  81. sed -i 's/telemetry_level": ".*"/telemetry_level": "basic"/' /var/www/html/settings.json
  82. if [ "$MODE" == "frontend" ]; then
  83. mkdir /var/www/html/backend
  84. cp /speedtest/backend/getIP_util.php /var/www/html/backend
  85. fi
  86. if [ "$DB_TYPE" == "mysql" ]; then
  87. sed -i 's/$db_type = '\''.*'\''/$db_type = '\'$DB_TYPE\''/g' /var/www/html/results/telemetry_settings.php
  88. sed -i 's/$MySql_username = '\''.*'\''/$MySql_username = '\'$DB_USERNAME\''/g' /var/www/html/results/telemetry_settings.php
  89. sed -i 's/$MySql_password = '\''.*'\''/$MySql_password = '\'$DB_PASSWORD\''/g' /var/www/html/results/telemetry_settings.php
  90. sed -i 's/$MySql_hostname = '\''.*'\''/$MySql_hostname = '\'$DB_HOSTNAME\''/g' /var/www/html/results/telemetry_settings.php
  91. sed -i 's/$MySql_databasename = '\''.*'\''/$MySql_databasename = '\'$DB_NAME\''/g' /var/www/html/results/telemetry_settings.php
  92. if [ "$DB_PORT" != "" ]; then
  93. sed -i 's/$MySql_port = '\''.*'\''/$MySql_port = '\'$DB_PORT\''/g' /var/www/html/results/telemetry_settings.php
  94. fi
  95. elif [ "$DB_TYPE" == "postgresql" ]; then
  96. sed -i 's/$db_type = '\''.*'\''/$db_type = '\'$DB_TYPE\''/g' /var/www/html/results/telemetry_settings.php
  97. sed -i 's/$PostgreSql_username = '\''.*'\''/$PostgreSql_username = '\'$DB_USERNAME\''/g' /var/www/html/results/telemetry_settings.php
  98. sed -i 's/$PostgreSql_password = '\''.*'\''/$PostgreSql_password = '\'$DB_PASSWORD\''/g' /var/www/html/results/telemetry_settings.php
  99. sed -i 's/$PostgreSql_hostname = '\''.*'\''/$PostgreSql_hostname = '\'$DB_HOSTNAME\''/g' /var/www/html/results/telemetry_settings.php
  100. sed -i 's/$PostgreSql_databasename = '\''.*'\''/$PostgreSql_databasename = '\'$DB_NAME\''/g' /var/www/html/results/telemetry_settings.php
  101. else
  102. sed -i s/\$db_type\ =\ \'.*\'/\$db_type\ =\ \'sqlite\'\/g /var/www/html/results/telemetry_settings.php
  103. fi
  104. sed -i s/\$Sqlite_db_file\ =\ \'.*\'/\$Sqlite_db_file=\'\\\/database\\\/db.sql\'/g /var/www/html/results/telemetry_settings.php
  105. sed -i s/\$stats_password\ =\ \'.*\'/\$stats_password\ =\ \'$PASSWORD\'/g /var/www/html/results/telemetry_settings.php
  106. if [ "$ENABLE_ID_OBFUSCATION" == "true" ]; then
  107. sed -i s/\$enable_id_obfuscation\ =\ .*\;/\$enable_id_obfuscation\ =\ true\;/g /var/www/html/results/telemetry_settings.php
  108. if [ ! -z "$OBFUSCATION_SALT" ]; then
  109. if [[ "$OBFUSCATION_SALT" =~ ^0x[0-9a-fA-F]+$ ]]; then
  110. echo "<?php" > /var/www/html/results/idObfuscation_salt.php
  111. echo "\$OBFUSCATION_SALT = $OBFUSCATION_SALT;" >> /var/www/html/results/idObfuscation_salt.php
  112. else
  113. echo "WARNING: Invalid OBFUSCATION_SALT format. It must be a hex string (e.g., 0x1234abcd). Using random salt." >&2
  114. fi
  115. fi
  116. fi
  117. if [ "$REDACT_IP_ADDRESSES" == "true" ]; then
  118. sed -i s/\$redact_ip_addresses\ =\ .*\;/\$redact_ip_addresses\ =\ true\;/g /var/www/html/results/telemetry_settings.php
  119. fi
  120. mkdir -p /database/
  121. if is_alpine; then
  122. chown -R apache /database/
  123. else
  124. chown -R www-data /database/
  125. fi
  126. fi
  127. if is_alpine; then
  128. chown -R apache /var/www/html/*
  129. else
  130. chown -R www-data /var/www/html/*
  131. fi
  132. # Allow selection of Apache port for network_mode: host
  133. if [ "$WEBPORT" != "80" ]; then
  134. if is_alpine; then
  135. sed -i "s/^Listen 80\$/Listen $WEBPORT/g" /etc/apache2/httpd.conf
  136. else
  137. sed -i "s/^Listen 80\$/Listen $WEBPORT/g" /etc/apache2/ports.conf
  138. sed -i "s/*:80>/*:$WEBPORT>/g" /etc/apache2/sites-available/000-default.conf
  139. fi
  140. fi
  141. echo "Done, Starting APACHE"
  142. # This runs apache
  143. if is_alpine; then
  144. exec httpd -DFOREGROUND
  145. else
  146. exec apache2-foreground
  147. fi