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.

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