router_local_http.sh 4.9 KB

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