Advertisement
Guest User

uu installer on padavan

a guest
Oct 22nd, 2022
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.32 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. set -x
  4.  
  5. # Online URL
  6. URL_PREFIX="https://"
  7. UNINSTALL_DOWNLOAD_URL="router.uu.163.com/api/script/uninstall?type="
  8. MONITOR_DOWNLOAD_URL="router.uu.163.com/api/script/monitor?type="
  9.  
  10. ROUTER="${1:-asuswrt-merlin}"
  11. MODEL="${2}"
  12.  
  13. BASEDIR=$(dirname "$0")
  14. UNINSTALL_FILE="${BASEDIR}/uninstall.sh"
  15. INSTALL_DIR=""
  16. MONITOR_FILE=""
  17. MONITOR_CONFIG=""
  18.  
  19. ASUSWRT_MERLIN="asuswrt-merlin"
  20. XIAOMI="xiaomi"
  21. HIWIFI="hiwifi"
  22. OPENWRT="openwrt"
  23.  
  24. init_param() {
  25.     local router="${ROUTER}"
  26.     local monitor_filename="uuplugin_monitor.sh"
  27.  
  28.     case "${router}" in
  29.     ${ASUSWRT_MERLIN})
  30.         INSTALL_DIR="/jffs/uu"
  31.         MONITOR_FILE="${INSTALL_DIR}/${monitor_filename}"
  32.         MONITOR_CONFIG="${INSTALL_DIR}/uuplugin_monitor.config"
  33.         UNINSTALL_DOWNLOAD_URL="${URL_PREFIX}${UNINSTALL_DOWNLOAD_URL}${ASUSWRT_MERLIN}"
  34.         MONITOR_DOWNLOAD_URL="${URL_PREFIX}${MONITOR_DOWNLOAD_URL}${ASUSWRT_MERLIN}"
  35.         return 0
  36.         ;;
  37.     ${XIAOMI})
  38.         URL_PREFIX="http://"
  39.         INSTALL_DIR="/data/uu"
  40.         MONITOR_FILE="${INSTALL_DIR}/${monitor_filename}"
  41.         MONITOR_CONFIG="${INSTALL_DIR}/uuplugin_monitor.config"
  42.         UNINSTALL_DOWNLOAD_URL="${URL_PREFIX}${UNINSTALL_DOWNLOAD_URL}${XIAOMI}"
  43.         MONITOR_DOWNLOAD_URL="${URL_PREFIX}${MONITOR_DOWNLOAD_URL}${XIAOMI}"
  44.         return 0
  45.         ;;
  46.     ${HIWIFI})
  47.         INSTALL_DIR="/plugins/uu"
  48.         MONITOR_FILE="${INSTALL_DIR}/${monitor_filename}"
  49.         MONITOR_CONFIG="${INSTALL_DIR}/uuplugin_monitor.config"
  50.         UNINSTALL_DOWNLOAD_URL="${URL_PREFIX}${UNINSTALL_DOWNLOAD_URL}${HIWIFI}"
  51.         MONITOR_DOWNLOAD_URL="${URL_PREFIX}${MONITOR_DOWNLOAD_URL}${HIWIFI}"
  52.         return 0
  53.         ;;
  54.     ${OPENWRT})
  55.         URL_PREFIX="http://"
  56.         INSTALL_DIR="/tmp/uu/"
  57.         MONITOR_FILE="${INSTALL_DIR}/${monitor_filename}"
  58.         MONITOR_CONFIG="${INSTALL_DIR}/uuplugin_monitor.config"
  59.         UNINSTALL_DOWNLOAD_URL="${URL_PREFIX}${UNINSTALL_DOWNLOAD_URL}${OPENWRT}"
  60.         MONITOR_DOWNLOAD_URL="${URL_PREFIX}${MONITOR_DOWNLOAD_URL}${OPENWRT}"
  61.         return 0
  62.         ;;
  63.     *)
  64.         return 1
  65.         ;;
  66.     esac
  67. }
  68.  
  69. # Return: 0 means success.
  70. config_asuswrt() {
  71.     # Config jffs file system
  72.     nvram set jffs2_enable=1
  73.     nvram set jffs2_scripts=1
  74.     nvram commit &
  75.     return 0
  76. }
  77.  
  78. # Return: 0 means success.
  79. check_dir() {
  80.     if [ ! -d "${INSTALL_DIR}" ];then
  81.         mkdir -p "${INSTALL_DIR}"
  82.         [ "$?" != "0" ] && return 1
  83.     fi
  84.  
  85.     return 0
  86. }
  87.  
  88. clean_up() {
  89.     [ ! -f "${UNINSTALL_FILE}" ] && return 1
  90.  
  91.     chmod u+x "${UNINSTALL_FILE}"
  92.     /bin/sh "${UNINSTALL_FILE}" "${ROUTER}" "${MODEL}" 1>/dev/null 2>&1
  93.     [ "$?" != "0" ] && return 1
  94.  
  95.     return 0
  96. }
  97.  
  98. # Return: 0 means success.
  99. download() {
  100.     local url="$1"
  101.     local file="$2"
  102.     local plugin_info=$(curl -L -s -k -H "Accept:text/plain" "${url}" || \
  103.         wget -q --no-check-certificate -O - "${url}&output=text" || \
  104.         wget -q -O - "${url}&output=text" || \
  105.         curl -s -k -H "Accept:text/plain" "${url}"
  106.     )
  107.  
  108.     [ "$?" != "0" ] && return 1
  109.     [ -z "$plugin_info" ] && return 1
  110.  
  111.     local plugin_url=$(echo "$plugin_info" | cut  -d ',' -f 1)
  112.     local plugin_md5=$(echo "$plugin_info" | cut  -d ',' -f 2)
  113.  
  114.     [ -z "${plugin_url}" ] && return 1
  115.     [ -z "${plugin_md5}" ] && return 1
  116.  
  117.     curl -L -s -k "$plugin_url" -o "${file}" >/dev/null 2>&1 || \
  118.         wget -q --no-check-certificate "$plugin_url" -O "${file}" >/dev/null 2>&1 || \
  119.         wget -q "$plugin_url" -O "${file}" >/dev/null 2>&1 || \
  120.         curl -s -k "$plugin_url" -o "${file}" >/dev/null 2>&1
  121.  
  122.     if [ "$?" != "0" ];then
  123.         [ -f "${file}" ] && rm "${file}"
  124.         return 1
  125.     fi
  126.  
  127.     if [ -f "${file}" ];then
  128.         local download_md5=$(md5sum "${file}")
  129.         local download_md5=$(echo "$download_md5" | sed 's/[ ][ ]*/ /g' | cut -d' ' -f1)
  130.         if [ "$download_md5" != "$plugin_md5" ];then
  131.             rm "${file}"
  132.             return 1
  133.         fi
  134.         return 0
  135.     else
  136.         return 1
  137.     fi
  138. }
  139.  
  140. # Return: 0 means success.
  141. start_monitor() {
  142.     [ ! -f  "${MONITOR_FILE}" ] && return 1
  143.     sed -i 's/\/usr\/sbin/\/tmp/g' ${MONITOR_FILE}
  144.     {
  145.         echo "router=${ROUTER}";
  146.         echo "model=${MODEL}"
  147.     } > ${MONITOR_CONFIG}
  148.  
  149.     [ "$?" != "0" ] && return 1
  150.  
  151.     chmod u+x "${MONITOR_FILE}"
  152.     /bin/sh "${MONITOR_FILE}" 1>/dev/null 2>&1 &
  153.     return 0
  154. }
  155.  
  156. # Return: 0 means running.
  157. check_running() {
  158.     local PID_FILE="/var/run/uuplugin.pid"
  159.     local PLUGIN_EXE="uuplugin"
  160.     local TIMES="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25"
  161.     TIMES=${TIMES}" 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45"
  162.     TIMES=${TIMES}" 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65"
  163.     TIMES=${TIMES}" 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85"
  164.     TIMES=${TIMES}" 86 87 88 89 90"
  165.     for i in ${TIMES};do
  166.         if [ -f "$PID_FILE" ];then
  167.             local pid=$(cat $PID_FILE)
  168.             local running_pid=$(ps | sed 's/^[ \t]*//g;s/[ \t]*$//g' | \
  169.                 sed 's/[ ][ ]*/#/g' | grep "${PLUGIN_EXE}" | \
  170.                 grep -v "grep" | cut -d'#' -f1 | grep -e "^${pid}$")
  171.             if [ "$pid" = "${running_pid}" ];then
  172.                 return 0
  173.             else
  174.                 sleep 1
  175.             fi
  176.         else
  177.             sleep 1
  178.         fi
  179.     done
  180.  
  181.     return 1
  182. }
  183.  
  184. # Return: 0 means it is merlin.
  185. check_merlin() {
  186.     # Check to see if it is merlin
  187.     local br0=$(ip -4 a s br0 | grep inet | grep -v 'grep' | \
  188.         sed 's/^[ \t]*//g;s/[ \t]*$//g' | sed 's/[ ][ ]*/#/g' | cut -d'#' -f2 | cut -d/ -f1)
  189.     [ -z "${br0}" ] && return 1
  190.  
  191.     local code=$(curl -s -o /dev/null -w "%{http_code}" "http://${br0}/images/merlin-logo.png")
  192.     [ "$code" = "200" ] && return 0
  193.  
  194.     code=$(wget -Sq -O - "http://${br0}/images/merlin-logo.png" 2>&1 | grep 'HTTP' | \
  195.         sed 's/^[ \t]*//g;s/[ \t]*$//g' | sed 's/[ ][ ]*/#/g' | cut -d'#' -f2)
  196.     [ "$code" = "200" ] && return 0
  197.     return 1
  198. }
  199.  
  200. config_asuswrt_bootup() {
  201.     check_merlin
  202.     if [ "$?" = "0" ];then
  203.         config_services_start
  204.         return $?
  205.     else
  206.         config_exec_start
  207.         return $?
  208.     fi
  209. }
  210.  
  211. # Return: 0 means success.
  212. config_exec_start() {
  213.     local bootup_script="${INSTALL_DIR}/uuplugin_bootup.sh"
  214.     {
  215.         echo "#!/bin/sh"
  216.         echo "nohup /bin/sh ${MONITOR_FILE} &"
  217.     } > ${bootup_script}
  218.  
  219.     chmod u+x ${bootup_script}
  220.     nvram set jffs2_exec="${bootup_script}"
  221.     nvram commit &
  222.     return 0
  223. }
  224.  
  225. # Return: 0 means success.
  226. # Config ${MONITOR_FILE} starts on boot.
  227. config_services_start() {
  228.     local SERVICES_START_FILE="/jffs/scripts/services-start"
  229.     if [ ! -e "${SERVICES_START_FILE}" ];then
  230.         mkdir -p /jffs/scripts
  231.         [ "$?" != "0" ] && return 1
  232.  
  233.         touch "${SERVICES_START_FILE}"
  234.         [ "$?" != "0" ] && return 1
  235.  
  236.         { echo "#!/bin/sh"; echo ""; echo ""; } >> "${SERVICES_START_FILE}"
  237.         [ "$?" != "0" ] && return 1
  238.     fi
  239.  
  240.     chmod u+x "${SERVICES_START_FILE}"
  241.     grep "${MONITOR_FILE}" "${SERVICES_START_FILE}" 1>/dev/null 2>&1
  242.     if [ "$?" != "0" ];then
  243.         echo "/bin/sh ${MONITOR_FILE} &" >> "${SERVICES_START_FILE}"
  244.         [ "$?" != "0" ] && return 1
  245.     fi
  246.  
  247.     return 0
  248. }
  249.  
  250. # Return: 0 means success.
  251. config_xiaomi_bootup() {
  252.     config_bootup_implemention
  253.     return $?
  254. }
  255.  
  256. # Return: 0 means success.
  257. config_hiwifi_bootup() {
  258.     config_bootup_implemention
  259.     return $?
  260. }
  261.  
  262. # Return: 0 means success.
  263. config_openwrt_bootup() {
  264.     config_bootup_implemention
  265.     return $?
  266. }
  267.  
  268. config_bootup_implemention() {
  269.     local init_script="${INSTALL_DIR}/S99uuplugin"
  270.     local link_script="/etc/rc.d/S99uuplugin"
  271.  
  272.     {
  273.         echo "#!/bin/sh /etc/rc.common";
  274.         echo "";
  275.         echo "";
  276.         echo "START=99";
  277.         echo "start() {"
  278.         echo "    /bin/sh ${MONITOR_FILE} &";
  279.         echo "}"
  280.     } > "${init_script}"
  281.  
  282.     [ "$?" != "0" ] && return 1
  283.     [ ! -f "${init_script}" ] && return 1
  284.     chmod u+x ${init_script}
  285.  
  286.     ln -sf ${init_script} ${link_script}
  287.     if [ "$?" != "0" ];then
  288.         [ -f "${init_script}" ] && rm ${init_script}
  289.         return 1
  290.     fi
  291.     return 0
  292. }
  293.  
  294. # Return: 0 means success.
  295. config_bootup() {
  296.     local router="${ROUTER}"
  297.     case "${router}" in
  298.     ${ASUSWRT_MERLIN})
  299.         config_asuswrt_bootup
  300.         return $?
  301.         ;;
  302.     ${XIAOMI})
  303.         config_xiaomi_bootup
  304.         return $?
  305.         ;;
  306.     ${HIWIFI})
  307.         config_hiwifi_bootup
  308.         return $?
  309.         ;;
  310.     ${OPENWRT})
  311.         config_openwrt_bootup
  312.         return $?
  313.         ;;
  314.     *)
  315.         return 1
  316.         ;;
  317.     esac
  318. }
  319.  
  320. # Return: 0 means success.
  321. config_router() {
  322.     local router="${ROUTER}"
  323.     case "${router}" in
  324.     ${ASUSWRT_MERLIN})
  325.         config_asuswrt
  326.         return $?
  327.         ;;
  328.     ${XIAOMI} | ${HIWIFI} | ${OPENWRT})
  329.         return 0
  330.         ;;
  331.     *)
  332.         return 1
  333.         ;;
  334.     esac
  335. }
  336.  
  337. print_sn() {
  338.     local interface=""
  339.     case "${ROUTER}" in
  340.         ${ASUSWRT_MERLIN})
  341.             interface="br0"
  342.             ;;
  343.         ${XIAOMI} | ${HIWIFI} | ${OPENWRT})
  344.             interface="br0"
  345.             ;;
  346.         *)
  347.             return 1
  348.             ;;
  349.     esac
  350.  
  351.     local info=$(ip addr show ${interface})
  352.     local mac=$(echo "${info}" | grep "link/ether" | awk '{print $2}')
  353.     echo "sn=${mac}"
  354.     return 0
  355. }
  356.  
  357. install() {
  358.     init_param
  359.     [ "$?" != "0" ] && return 9
  360.  
  361.     config_router
  362.     [ "$?" != "0" ] && return 1
  363.  
  364.     check_dir
  365.     [ "$?" != "0" ] && return 2
  366.  
  367.     download "${UNINSTALL_DOWNLOAD_URL}" "${UNINSTALL_FILE}"
  368.     [ "$?" != "0" ] && return 3
  369.  
  370.     clean_up
  371.     [ "$?" != "0" ] && return 4
  372.  
  373.     download "${MONITOR_DOWNLOAD_URL}" "${MONITOR_FILE}"
  374.     if [ "$?" != "0" ];then
  375.         [ -f "${MONITOR_FILE}" ] && rm "${MONITOR_FILE}"
  376.         return 5
  377.     fi
  378.  
  379.     start_monitor
  380.     [ "$?" != "0" ] && return 6
  381.  
  382.     check_running
  383.     [ "$?" != "0" ] && return 7
  384.  
  385.     # config_bootup
  386.     # [ "$?" != "0" ] && return 8
  387.  
  388.     print_sn
  389.     [ "$?" != "0" ] && return 10
  390.     return 0
  391. }
  392.  
  393. # Start to install.
  394. install
  395. status_code=$?
  396.  
  397. if [ ${status_code} -gt 4 ];then
  398.     if [ -f "${UNINSTALL_FILE}" ];then
  399.         clean_up
  400.     fi
  401. fi
  402.  
  403. [ -f "${UNINSTALL_FILE}" ] && rm "${UNINSTALL_FILE}"
  404. return $status_code
  405.  
Tags: uu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement