router_local_http.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/bin/sh
  2. set -eu
  3. timestamp() {
  4. date '+%Y-%m-%d %H:%M:%S'
  5. }
  6. log() {
  7. printf '[%s] %s\n' "$(timestamp)" "$*"
  8. }
  9. log_err() {
  10. printf '[%s] %s\n' "$(timestamp)" "$*" >&2
  11. }
  12. SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
  13. APP_DIR=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)
  14. CONFIG_PATH=${1:-"$APP_DIR/config_router.conf"}
  15. CONFIG_DIR=$(CDPATH= cd -- "$(dirname -- "$CONFIG_PATH")" && pwd)
  16. if [ ! -r "$CONFIG_PATH" ]; then
  17. log_err "[router-http] config not found: $CONFIG_PATH"
  18. exit 1
  19. fi
  20. # shellcheck disable=SC1090
  21. . "$CONFIG_PATH"
  22. BUSYBOX_BIN=${BUSYBOX_BIN:-"$APP_DIR/busybox_armv7l"}
  23. RUNTIME_DIR=${RUNTIME_DIR:-"$APP_DIR/cfip_runtime"}
  24. VALUE_TEXT_FILE=${VALUE_TEXT_FILE:-"current_ip.txt"}
  25. HTTP_PORT=${HTTP_PORT:-8080}
  26. # Trim possible CRLF endings from config values edited on Windows/Web UI.
  27. CR=$(printf '\r')
  28. BUSYBOX_BIN=${BUSYBOX_BIN%"$CR"}
  29. RUNTIME_DIR=${RUNTIME_DIR%"$CR"}
  30. VALUE_TEXT_FILE=${VALUE_TEXT_FILE%"$CR"}
  31. HTTP_PORT=${HTTP_PORT%"$CR"}
  32. TEXT_PATH="$RUNTIME_DIR/$VALUE_TEXT_FILE"
  33. INDEX_PATH="$RUNTIME_DIR/index.html"
  34. resolve_path_from_config() {
  35. p="$1"
  36. case "$p" in
  37. /*) printf '%s\n' "$p" ;;
  38. *) printf '%s/%s\n' "$CONFIG_DIR" "$p" ;;
  39. esac
  40. }
  41. resolve_busybox() {
  42. if [ -n "$BUSYBOX_BIN" ]; then
  43. busybox_from_cfg=$(resolve_path_from_config "$BUSYBOX_BIN")
  44. if [ -x "$busybox_from_cfg" ]; then
  45. printf '%s\n' "$busybox_from_cfg"
  46. return 0
  47. fi
  48. fi
  49. if [ -n "$BUSYBOX_BIN" ] && [ -x "$BUSYBOX_BIN" ]; then
  50. printf '%s\n' "$BUSYBOX_BIN"
  51. return 0
  52. fi
  53. if [ -x "$APP_DIR/busybox_armv7l" ]; then
  54. printf '%s\n' "$APP_DIR/busybox_armv7l"
  55. return 0
  56. fi
  57. if command -v busybox >/dev/null 2>&1; then
  58. command -v busybox
  59. return 0
  60. fi
  61. return 1
  62. }
  63. print_busybox_debug() {
  64. log_err "[router-http] debug CONFIG_PATH=$CONFIG_PATH"
  65. log_err "[router-http] debug CONFIG_DIR=$CONFIG_DIR"
  66. log_err "[router-http] debug APP_DIR=$APP_DIR"
  67. log_err "[router-http] debug BUSYBOX_BIN(raw)=$BUSYBOX_BIN"
  68. resolved_from_cfg=$(resolve_path_from_config "$BUSYBOX_BIN")
  69. log_err "[router-http] debug BUSYBOX_BIN(resolved)=$resolved_from_cfg"
  70. if [ -e "$resolved_from_cfg" ]; then
  71. if [ -x "$resolved_from_cfg" ]; then
  72. log_err "[router-http] debug resolved BusyBox exists and is executable"
  73. else
  74. log_err "[router-http] debug resolved BusyBox exists but is NOT executable; run: chmod +x $resolved_from_cfg"
  75. fi
  76. else
  77. log_err "[router-http] debug resolved BusyBox does not exist"
  78. fi
  79. if [ -e "$APP_DIR/busybox_armv7l" ]; then
  80. if [ -x "$APP_DIR/busybox_armv7l" ]; then
  81. log_err "[router-http] debug fallback $APP_DIR/busybox_armv7l exists and is executable"
  82. else
  83. log_err "[router-http] debug fallback $APP_DIR/busybox_armv7l exists but is NOT executable; run: chmod +x $APP_DIR/busybox_armv7l"
  84. fi
  85. else
  86. log_err "[router-http] debug fallback $APP_DIR/busybox_armv7l does not exist"
  87. fi
  88. }
  89. busybox_has_applet() {
  90. busybox_bin="$1"
  91. applet="$2"
  92. if "$busybox_bin" --list 2>/dev/null | grep "^$applet$" >/dev/null; then
  93. return 0
  94. fi
  95. "$busybox_bin" 2>/dev/null | grep "[[:space:]]$applet[,[:space:]]" >/dev/null
  96. }
  97. ensure_index() {
  98. if [ ! -f "$TEXT_PATH" ]; then
  99. return 0
  100. fi
  101. rm -f "$INDEX_PATH"
  102. cp "$TEXT_PATH" "$INDEX_PATH"
  103. }
  104. start_httpd() {
  105. busybox_bin=$(resolve_busybox || true)
  106. if [ -n "$busybox_bin" ] && busybox_has_applet "$busybox_bin" httpd; then
  107. log "[router-http] serving $RUNTIME_DIR on 0.0.0.0:$HTTP_PORT via $busybox_bin httpd"
  108. "$busybox_bin" httpd -f -p "$HTTP_PORT" -h "$RUNTIME_DIR" &
  109. httpd_pid=$!
  110. trap 'kill "$httpd_pid" 2>/dev/null || true' INT TERM EXIT
  111. wait "$httpd_pid"
  112. exit $?
  113. fi
  114. if [ -n "$busybox_bin" ]; then
  115. log_err "[router-http] selected BusyBox has no httpd applet: $busybox_bin"
  116. fi
  117. if command -v httpd >/dev/null 2>&1; then
  118. log_err "[router-http] found system httpd at $(command -v httpd), but it is not used"
  119. log_err "[router-http] ASUS firmware httpd is usually the router admin web server and does not support serving an arbitrary -h directory"
  120. fi
  121. print_busybox_debug
  122. log_err "[router-http] usable BusyBox httpd applet not found"
  123. log_err "[router-http] set BUSYBOX_BIN in config_router.conf to your full BusyBox binary, for example: BUSYBOX_BIN=\"./busybox_armv7l\""
  124. exit 1
  125. }
  126. if [ ! -d "$RUNTIME_DIR" ]; then
  127. log_err "[router-http] runtime dir not found: $RUNTIME_DIR"
  128. log_err "[router-http] run: sh scripts/router_local_update.sh $CONFIG_PATH"
  129. exit 1
  130. fi
  131. ensure_index
  132. start_httpd