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.

201 lines
5.3 KiB

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