You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.1 KiB

  1. #!/bin/sh /etc/rc.common
  2. # set (s)hellcheck exceptions
  3. # shellcheck disable=1091,2016,2034,2039,2059,2086,2143,2181,2188
  4. START=25
  5. USE_PROCD=1
  6. EXTRA_COMMANDS="scan setup"
  7. EXTRA_HELP=" scan <radio> Scan for available nearby uplinks
  8. setup [<iface>] [<zone>] [<metric>] Setup the travelmate uplink interface, by default 'trm_wwan' with firewall zone 'wan' and metric '100'"
  9. trm_init="/etc/init.d/travelmate"
  10. trm_script="/usr/bin/travelmate.sh"
  11. trm_pidfile="/var/run/travelmate.pid"
  12. boot()
  13. {
  14. if [ -s "${trm_pidfile}" ]
  15. then
  16. > "${trm_pidfile}"
  17. fi
  18. rc_procd start_service
  19. }
  20. start_service()
  21. {
  22. if [ "$("${trm_init}" enabled; printf "%u" ${?})" = "0" ]
  23. then
  24. procd_open_instance "travelmate"
  25. procd_set_param command "${trm_script}" "${@}"
  26. procd_set_param pidfile "${trm_pidfile}"
  27. procd_set_param nice "$(uci_get travelmate global trm_nice "0")"
  28. procd_set_param stdout 1
  29. procd_set_param stderr 1
  30. procd_close_instance
  31. fi
  32. }
  33. reload_service()
  34. {
  35. local ppid pid timeout
  36. timeout="$(uci_get travelmate global trm_timeout)"
  37. if [ -s "${trm_pidfile}" ]
  38. then
  39. ppid="$(cat "${trm_pidfile}" 2>/dev/null)"
  40. if [ -n "${ppid}" ]
  41. then
  42. pid="$(pgrep -xnf "sleep ${timeout:-60} 0" -P ${ppid} 2>/dev/null)"
  43. if [ -n "${pid}" ]
  44. then
  45. kill -INT ${pid} 2>/dev/null
  46. fi
  47. fi
  48. fi
  49. }
  50. stop_service()
  51. {
  52. rc_procd "${trm_script}" stop
  53. }
  54. status_service()
  55. {
  56. local key keylist value rtfile
  57. rtfile="$(uci_get travelmate global trm_rtfile "/tmp/trm_runtime.json")"
  58. json_load_file "${rtfile}" >/dev/null 2>&1
  59. json_select data >/dev/null 2>&1
  60. if [ "${?}" = "0" ]
  61. then
  62. printf "%s\n" "::: travelmate runtime information"
  63. json_get_keys keylist
  64. for key in ${keylist}
  65. do
  66. json_get_var value "${key}"
  67. printf " + %-18s : %s\n" "${key}" "${value}"
  68. done
  69. else
  70. printf "%s\n" "::: no travelmate runtime information available"
  71. fi
  72. }
  73. scan()
  74. {
  75. local result scan_dev radio="${1:-"radio0"}"
  76. scan_dev="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -l1 -e "@.${radio}.interfaces[0].ifname")"
  77. result="$(iwinfo "${scan_dev:-${radio}}" scan 2>/dev/null | \
  78. awk 'BEGIN{FS="[[:space:]]"}/Address:/{var1=$NF}/ESSID:/{var2="";
  79. for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i}}/Channel:/{var3=$NF}/Quality:/{split($NF,var0,"/")}/Encryption:/{var4="";
  80. for(j=12;j<=NF;j++)if(var4==""){var4=$j}else{var4=var4" "$j};printf " %-11i%-10s%-35s%-20s%s\n",(var0[1]*100/var0[2]),var3,var2,var1,var4}' | \
  81. sort -rn)"
  82. printf "%s\\n" "::: Available nearby uplinks on '${scan_dev:-${radio}}'"
  83. printf "%s\\n" ":::"
  84. if [ -n "${result}" ]
  85. then
  86. printf "%-15s%-10s%-35s%-20s%s\\n" " Strength" "Channel" "ESSID" "BSSID" "Encryption"
  87. printf "%s\\n" " --------------------------------------------------------------------------------------"
  88. printf "%s\\n" "${result}"
  89. else
  90. printf "%s\\n" "::: No scan results"
  91. fi
  92. }
  93. setup()
  94. {
  95. local iface cnt=0 input="${1:-"trm_wwan"}" zone="${2:-"wan"}" metric="${3:-"100"}"
  96. iface="$(uci_get travelmate global trm_iface)"
  97. input="${input//[+*~%&\$@\"\' ]/}"
  98. zone="${zone//[+*~%&\$@\"\' ]/}"
  99. metric="${metric//[^0-9]/}"
  100. if [ -n "${iface}" ] && [ "${iface}" = "${input}" ]
  101. then
  102. printf "%s\n" "The uplink interface '${input}' has been already configured"
  103. elif [ -n "${input}" ]
  104. then
  105. if [ -n "${iface}" ]
  106. then
  107. uci -q batch <<-EOC
  108. del network."${iface}"
  109. del network."${iface}6"
  110. EOC
  111. fi
  112. uci -q batch <<-EOC
  113. set travelmate.global.trm_enabled="1"
  114. set travelmate.global.trm_iface="${input}"
  115. set network."${input}"="interface"
  116. set network."${input}".proto="dhcp"
  117. set network."${input}".metric="${metric}"
  118. set network."${input}6"=interface
  119. set network."${input}6".ifname="@${input}"
  120. set network."${input}6".proto="dhcpv6"
  121. commit travelmate
  122. commit network
  123. EOC
  124. while [ -n "$(uci -q get firewall.@zone["${cnt}"].name)" ]
  125. do
  126. if [ "$(uci -q get firewall.@zone["${cnt}"].name)" = "${zone}" ]
  127. then
  128. if [ -n "${iface}" ]
  129. then
  130. uci -q batch <<-EOC
  131. del_list firewall.@zone["${cnt}"].network="${iface}"
  132. del_list firewall.@zone["${cnt}"].network="${iface}6"
  133. EOC
  134. fi
  135. uci -q batch <<-EOC
  136. add_list firewall.@zone["${cnt}"].network="${input}"
  137. add_list firewall.@zone["${cnt}"].network="${input}6"
  138. commit firewall
  139. EOC
  140. break
  141. fi
  142. cnt=$((cnt+1))
  143. done
  144. if [ -n "${iface}" ]
  145. then
  146. cnt=0
  147. while [ -n "$(uci -q get wireless.@wifi-iface["${cnt}"].network)" ]
  148. do
  149. if [ "$(uci -q get wireless.@wifi-iface["${cnt}"].network)" = "${iface}" ]
  150. then
  151. uci -q set wireless.@wifi-iface["${cnt}"].network="${input}"
  152. fi
  153. cnt=$((cnt+1))
  154. done
  155. uci -q commit wireless
  156. fi
  157. /etc/init.d/network reload >/dev/null 2>&1
  158. /etc/init.d/firewall reload >/dev/null 2>&1
  159. "${trm_init}" restart
  160. fi
  161. }
  162. service_triggers()
  163. {
  164. local iface delay
  165. iface="$(uci_get travelmate global trm_iface)"
  166. delay="$(uci_get travelmate global trm_triggerdelay "2")"
  167. PROCD_RELOAD_DELAY=$((delay * 1000))
  168. if [ -n "${iface}" ]
  169. then
  170. procd_add_interface_trigger "interface.*.down" "${iface}" "${trm_init}" reload
  171. fi
  172. procd_add_config_trigger "config.change" "travelmate" "${trm_init}" restart
  173. }