#!/bin/sh . /lib/functions.sh . /usr/share/libubox/jshn.sh if command -v apk >/dev/null 2>&1; then PKG_EXT=".apk" else PKG_EXT=".ipk" fi readonly bg_path="/www/luci-static/fluent/background" readonly tmp_path="/tmp/fluent_background.tmp" readonly update_pkg="/tmp/fluent-update${PKG_EXT}" readonly i18n_pkg="/tmp/fluent-i18n-update${PKG_EXT}" case "$1" in "list") json_init json_add_object "avail" json_close_object json_add_object "remove" json_add_string "filename" "filename" json_close_object json_add_object "rename" json_add_string "newname" "filename" json_close_object json_add_object "set_mode" json_add_string "mode" "mode" json_close_object json_add_object "get_version" json_close_object json_add_object "start_download" json_add_string "url" "url" json_add_string "i18n_url" "i18n_url" json_add_string "i18n_urls" "i18n_urls" json_close_object json_add_object "check_download" json_close_object json_add_object "do_install" json_add_string "hash" "hash" json_add_string "i18n_hash" "i18n_hash" json_add_string "i18n_hashes" "i18n_hashes" json_close_object json_add_object "get_install_log" json_close_object json_dump json_cleanup ;; "call") case "$2" in "avail") json_init json_add_int "avail" "$(df | grep -E '/$' | awk '{print $4}')" json_dump json_cleanup ;; "remove") read -r input json_load "$input" json_get_var filename "filename" json_cleanup if dirname "$filename" | grep -q ".."; then echo '{ "result": 255 }' exit 255 fi rm -f "$bg_path/$filename" echo '{ "result": 0 }' ;; "rename") read -r input json_load "$input" json_get_var newname "newname" json_cleanup if dirname "$newname" | grep -q ".."; then echo '{ "result": 255 }' exit 255 fi mkdir -p "$bg_path" if mv "$tmp_path" "$bg_path/$newname" 2>"/dev/null"; then chmod 0644 "$bg_path/$newname" echo '{ "result": 0 }' else echo '{ "result": 1 }' fi ;; "set_mode") read -r input json_load "$input" json_get_var mode "mode" json_cleanup case "$mode" in light|dark|auto) ;; *) echo '{ "result": 255 }' exit 255 ;; esac if uci -q set fluent.@global[0].mode="$mode" && uci -q commit fluent; then echo '{ "result": 0 }' else echo '{ "result": 1 }' fi ;; "get_version") version="" pkg_type="ipk" i18n_installed=0 installed_i18n="" if command -v apk >/dev/null 2>&1; then pkg_type="apk" version=$(apk list -I luci-theme-fluent 2>/dev/null | awk '{print $1}' | sed 's/^luci-theme-fluent-//' | head -1) installed_i18n=$(apk list -I 2>/dev/null | grep -E '^luci-i18n-fluent-' | awk '{print $1}' | sed 's/^luci-i18n-fluent-//') else version=$(opkg status luci-theme-fluent 2>/dev/null | grep -m1 'Version:' | awk '{print $2}') installed_i18n=$(opkg list-installed 2>/dev/null | grep -E '^luci-i18n-fluent-' | awk '{print $1}' | sed 's/^luci-i18n-fluent-//') fi if echo "$installed_i18n" | grep -q "^zh-cn$"; then i18n_installed=1 fi if [ -z "$version" ]; then version="1.0.1-dev" fi json_init json_add_string "version" "$version" json_add_string "pkg_type" "$pkg_type" json_add_boolean "i18n_zh_cn_installed" "$i18n_installed" json_add_array "installed_i18n" for lang in $installed_i18n; do json_add_string "" "$lang" done json_close_array json_dump json_cleanup ;; "start_download") read -r input json_load "$input" json_get_var url "url" json_get_var i18n_url "i18n_url" json_get_var i18n_urls "i18n_urls" json_cleanup rm -f "$update_pkg" rm -f /tmp/fluent-i18n-* rm -f /tmp/fluent-update.log if [ -n "$url" ]; then download_cmd="" if command -v uclient-fetch >/dev/null 2>&1; then download_cmd="uclient-fetch -O $update_pkg \"$url\"" urls_to_dl="${i18n_urls:-$i18n_url}" idx=1 for u in $urls_to_dl; do download_cmd="$download_cmd && uclient-fetch -O /tmp/fluent-i18n-${idx}${PKG_EXT} \"$u\"" idx=$((idx + 1)) done else download_cmd="wget -O $update_pkg \"$url\"" urls_to_dl="${i18n_urls:-$i18n_url}" idx=1 for u in $urls_to_dl; do download_cmd="$download_cmd && wget -O /tmp/fluent-i18n-${idx}${PKG_EXT} \"$u\"" idx=$((idx + 1)) done fi nohup sh -c "$download_cmd >/tmp/fluent-update.log 2>&1" >/dev/null 2>&1 & echo '{ "result": 0 }' else echo '{ "result": 255, "message": "Missing URL" }' fi ;; "check_download") json_init size1=$(ls -l $update_pkg 2>/dev/null | awk '{print $5}') size2=$(ls -l /tmp/fluent-i18n-* 2>/dev/null | awk '{sum+=$5} END {print sum}') size=$(( ${size1:-0} + ${size2:-0} )) json_add_int "size" "$size" if pgrep -f "fluent-update" >/dev/null; then json_add_boolean "running" 1 else json_add_boolean "running" 0 if [ "$size" -eq 0 ]; then # If process exited and size is 0, download failed json_add_int "code" 255 else json_add_int "code" 0 fi fi json_dump json_cleanup ;; "do_install") read -r input json_load "$input" json_get_var hash "hash" json_get_var i18n_hash "i18n_hash" json_get_var i18n_hashes "i18n_hashes" json_cleanup if [ -z "$hash" ]; then echo '{ "result": 255, "message": "Missing hash parameter" }' exit 255 fi if [ ! -f "$update_pkg" ]; then echo '{ "result": 255, "message": "Package file not found" }' exit 255 fi if [ "$hash" != "skip" ]; then if ! echo "$hash" | grep -qE '^[0-9a-fA-F]{64}$'; then echo '{ "result": 255, "message": "Invalid hash parameter" }' exit 255 fi actual_hash=$(sha256sum "$update_pkg" | awk '{print $1}') if [ "$actual_hash" != "$hash" ]; then echo '{ "result": 255, "message": "Hash verification failed" }' exit 255 fi fi hashes_to_check="${i18n_hashes:-$i18n_hash}" if [ -n "$hashes_to_check" ]; then idx=1 for h in $hashes_to_check; do target_i18n_pkg="/tmp/fluent-i18n-${idx}${PKG_EXT}" if [ -f "$target_i18n_pkg" ] && [ "$h" != "skip" ]; then if ! echo "$h" | grep -qE '^[0-9a-fA-F]{64}$'; then echo '{ "result": 255, "message": "Invalid i18n hash parameter" }' exit 255 fi actual_i18n_hash=$(sha256sum "$target_i18n_pkg" | awk '{print $1}') if [ "$actual_i18n_hash" != "$h" ]; then echo '{ "result": 255, "message": "i18n Hash verification failed" }' exit 255 fi fi idx=$((idx + 1)) done fi install_cmd="" pkgs="$update_pkg" i18n_files=$(ls /tmp/fluent-i18n-* 2>/dev/null) if [ -n "$i18n_files" ]; then pkgs="$pkgs $i18n_files" fi if command -v apk >/dev/null 2>&1; then install_cmd="apk add --allow-untrusted $pkgs" else install_cmd="opkg install $pkgs" fi nohup sh -c "sleep 1 && $install_cmd > /tmp/fluent-install.log 2>&1 ; rm -f $pkgs ; /etc/init.d/rpcd restart" >/dev/null 2>&1 & echo '{ "result": 0 }' ;; "get_install_log") json_init if [ -f "/tmp/fluent-install.log" ]; then json_add_string "log" "$(cat /tmp/fluent-install.log)" else json_add_string "log" "" fi json_dump json_cleanup ;; esac ;; esac