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.

284 lines
9.1 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2018 Dengfeng Liu
  3. . /lib/functions/network.sh
  4. START=99
  5. USE_PROCD=1
  6. PROG=/usr/bin/wifidogx
  7. CONFIGFILE=/tmp/wifidogx.conf
  8. extra_command "status" "Print the status of the service"
  9. PX5G_BIN="/usr/sbin/px5g"
  10. OPENSSL_BIN="/usr/bin/openssl"
  11. APFREE_CERT="/etc/apfree.crt"
  12. APFREE_KEY="/etc/apfree.key"
  13. generate_keys() {
  14. local days bits country state location commonname
  15. local UNIQUEID GENKEY_CMD
  16. # Prefer px5g for certificate generation (existence evaluated last)
  17. UNIQUEID=$(hexdump -n 4 -e '4/1 "%02x" "\n"' /dev/urandom)
  18. [ -x "$OPENSSL_BIN" ] && GENKEY_CMD="$OPENSSL_BIN req -x509 -sha256 -outform pem -nodes"
  19. [ -x "$PX5G_BIN" ] && GENKEY_CMD="$PX5G_BIN selfsigned -pem"
  20. [ -n "$GENKEY_CMD" ] && {
  21. $GENKEY_CMD \
  22. -days "${days:-720}" -newkey rsa:"${bits:-2048}" -keyout "${APFREE_KEY}.new" -out "${APFREE_CERT}.new" \
  23. -subj /C="${country:-CN}"/ST="${state:-Beijing}"/L="${location:-Unknown}"/O="${commonname:-ApFreeWiFidog}$UNIQUEID"/CN="${commonname:-ApFreeWiFidog}"
  24. sync
  25. mv "${APFREE_KEY}.new" "${APFREE_KEY}"
  26. mv "${APFREE_CERT}.new" "${APFREE_CERT}"
  27. }
  28. }
  29. service_trigger() {
  30. procd_add_reload_trigger "wifidogx"
  31. }
  32. echo_firewall_rule() {
  33. echo " FirewallRule $1"
  34. }
  35. prepare_mqtt_conf() {
  36. local cfg=$1
  37. local serveraddr
  38. local serverport
  39. config_get serveraddr "$cfg" "serveraddr"
  40. config_get serverport "$cfg" "serverport"
  41. [ -z "${serveraddr}" ] || [ -z "${serverport}" ] && return 1
  42. cat <<-EOF >>${CONFIGFILE}
  43. MQTT {
  44. ServerAddr ${serveraddr}
  45. ServerPort ${serverport}
  46. }
  47. EOF
  48. }
  49. prepare_wifidog_conf() {
  50. local cfg=$1
  51. local disabled
  52. local gateway_id
  53. local gateway_interface
  54. local auth_server_hostname
  55. local auth_server_path
  56. local auth_server_path_login
  57. local auth_server_path_portal
  58. local auth_server_path_msg
  59. local auth_server_path_ping
  60. local auth_server_path_auth
  61. local delta_traffic
  62. local check_interval
  63. local client_timeout
  64. local trusted_domains
  65. local js_filter
  66. local trusted_maclist
  67. local untrusted_maclist
  68. local pool_mode
  69. local thread_number
  70. local queue_size
  71. local wired_passed
  72. local trusted_iplist
  73. local trusted_pan_domains
  74. local proxy_port
  75. local no_auth
  76. local apple_cna
  77. local update_domain_interval
  78. local dns_timeout
  79. local default_gateway_id
  80. local external_interface
  81. local auth_server_port
  82. [ -f ${CONFIGFILE} ] && rm -f ${CONFIGFILE}
  83. config_get disabled "${cfg}" "disabled" 1
  84. if [ "${disabled}" = "1" ]; then
  85. echo "wifidogx disabled in /etc/config/wifidogx file, please set disabled to 0 to enable it" >&2
  86. return
  87. fi
  88. default_gateway_id=$(sed -e 's/://g' /sys/class/net/br-lan/address)
  89. network_get_device external_interface wan
  90. config_get gateway_id "${cfg}" "gateway_id" "${default_gateway_id}"
  91. config_get gateway_interface "${cfg}" "gateway_interface" "br-lan"
  92. config_get auth_server_hostname "${cfg}" "auth_server_hostname"
  93. config_get auth_server_port "${cfg}" "auth_server_port" "80"
  94. config_get auth_server_path "${cfg}" "auth_server_path" "/wifidog/"
  95. config_get auth_server_path_login "${cfg}" "auth_server_path_login"
  96. config_get auth_server_path_portal "${cfg}" "auth_server_path_portal"
  97. config_get auth_server_path_msg "${cfg}" "auth_server_path_msg"
  98. config_get auth_server_path_ping "${cfg}" "auth_server_path_ping"
  99. config_get auth_server_path_auth "${cfg}" "auth_server_path_auth"
  100. config_get delta_traffic "${cfg}" "delta_traffic"
  101. config_get check_interval "${cfg}" "check_interval" "60"
  102. config_get js_filter "${cfg}" "js_filter" 1
  103. config_get client_timeout "${cfg}" "client_timeout" "5"
  104. config_get trusted_domains "${cfg}" "trusted_domains"
  105. config_get trusted_maclist "${cfg}" "trusted_maclist"
  106. config_get untrusted_maclist "${cfg}" "untrusted_maclist"
  107. config_get pool_mode "${cfg}" "pool_mode" 0
  108. config_get thread_number "${cfg}" "thread_number" 20
  109. config_get queue_size "${cfg}" "queue_size" 200
  110. config_get wired_passed "${cfg}" "wired_passed" 1
  111. config_get trusted_iplist "${cfg}" "trusted_iplist"
  112. config_get trusted_pan_domains "${cfg}" "trusted_pan_domains"
  113. config_get proxy_port "${cfg}" "proxy_port"
  114. config_get no_auth "${cfg}" "no_auth"
  115. config_get apple_cna "${cfg}" "bypass_apple_cna"
  116. config_get update_domain_interval "${cfg}" "update_domain_interval"
  117. config_get dns_timeout "${cfg}" "dns_timeout"
  118. local set_auth_server_path_login
  119. local set_auth_server_path_portal
  120. local set_auth_server_path_msg
  121. local set_auth_server_path_ping
  122. local set_auth_server_path_auth
  123. local set_delta_traffic
  124. local set_trusted_maclist
  125. local set_untrusted_maclist
  126. local set_trusted_domains
  127. local set_trusted_iplist
  128. local set_trusted_pan_domains
  129. local set_proxy_port
  130. local set_no_auth
  131. local set_firewall_rule_global
  132. local set_firewall_rule_validating_users
  133. local set_firewall_rule_known_users
  134. local set_firewall_rule_auth_is_down
  135. local set_firewall_rule_unknown_users
  136. local set_firewall_rule_locked_users
  137. local set_apple_cna
  138. local set_update_domain_interval
  139. local set_dns_timeout
  140. set_auth_server_path_login=$([ -n "$auth_server_path_login" ] && echo " LoginScriptPathFragment $auth_server_path_login")
  141. set_auth_server_path_portal=$([ -n "$auth_server_path_portal" ] && echo " PortalScriptPathFragment $auth_server_path_portal")
  142. set_auth_server_path_msg=$([ -n "$auth_server_path_msg" ] && echo " MsgScriptPathFragment $auth_server_path_msg")
  143. set_auth_server_path_ping=$([ -n "$auth_server_path_ping" ] && echo " PingScriptPathFragment $auth_server_path_ping")
  144. set_auth_server_path_auth=$([ -n "$auth_server_path_auth" ] && echo " AuthScriptPathFragment $auth_server_path_auth")
  145. set_delta_traffic=$([ -n "$delta_traffic" ] && echo "DeltaTraffic $delta_traffic")
  146. set_trusted_maclist=$([ -n "$trusted_maclist" ] && echo "TrustedMACList $trusted_maclist")
  147. set_untrusted_maclist=$([ -n "$untrusted_maclist" ] && echo "UntrustedMACList $untrusted_maclist")
  148. set_trusted_domains=$([ -n "$trusted_domains" ] && echo "TrustedDomains $trusted_domains")
  149. set_trusted_iplist=$([ -n "$trusted_iplist" ] && echo "TrustedIpList $trusted_iplist")
  150. set_trusted_pan_domains=$([ -n "$trusted_pan_domains" ] && echo "TrustedPanDomains $trusted_pan_domains")
  151. set_proxy_port=$([ -n "$proxy_port" ] && echo "Proxyport $proxy_port")
  152. set_no_auth=$([ -n "$no_auth" ] && echo "NoAuth $no_auth")
  153. set_firewall_rule_global=$(config_list_foreach "$cfg" "firewall_rule_global" echo_firewall_rule)
  154. set_firewall_rule_validating_users=$(config_list_foreach "$cfg" "firewall_rule_validating_users" echo_firewall_rule)
  155. set_firewall_rule_known_users=$(config_list_foreach "$cfg" "firewall_rule_known_users" echo_firewall_rule)
  156. set_firewall_rule_auth_is_down=$(config_list_foreach "$cfg" "firewall_rule_auth_is_down" echo_firewall_rule)
  157. set_firewall_rule_unknown_users=$(config_list_foreach "$cfg" "firewall_rule_unknown_users" echo_firewall_rule)
  158. set_firewall_rule_locked_users=$(config_list_foreach "$cfg" "firewall_rule_locked_users" echo_firewall_rule)
  159. set_apple_cna=$([ -n "$apple_cna" ] && echo "BypassAppleCNA $apple_cna")
  160. set_update_domain_interval=$([ -n "$update_domain_interval" ] && echo "UpdateDomainInterval $update_domain_interval")
  161. set_dns_timeout=$([ -n "$dns_timeout" ] && echo "DNSTimeout $dns_timeout")
  162. cat <<-EOF >$CONFIGFILE
  163. GatewayID $gateway_id
  164. GatewayInterface $gateway_interface
  165. Externalinterface $external_interface
  166. AuthServer {
  167. Hostname $auth_server_hostname
  168. HTTPPort $auth_server_port
  169. Path $auth_server_path
  170. $set_auth_server_path_login
  171. $set_auth_server_path_portal
  172. $set_auth_server_path_msg
  173. $set_auth_server_path_ping
  174. $set_auth_server_path_auth
  175. }
  176. $set_delta_traffic
  177. CheckInterval $check_interval
  178. ClientTimeout $client_timeout
  179. JsFilter $js_filter
  180. WiredPassed $wired_passed
  181. $set_trusted_domains
  182. $set_untrusted_maclist
  183. $set_trusted_maclist
  184. $set_trusted_iplist
  185. $set_trusted_pan_domains
  186. $set_proxy_port
  187. $set_no_auth
  188. $set_apple_cna
  189. $set_update_domain_interval
  190. $set_dns_timeout
  191. FirewallRuleSet global {
  192. $set_firewall_rule_global
  193. }
  194. FirewallRuleSet validating-users {
  195. $set_firewall_rule_validating_users
  196. FirewallRule allow to 0.0.0.0/0
  197. }
  198. FirewallRuleSet known-users {
  199. $set_firewall_rule_known_users
  200. FirewallRule allow to 0.0.0.0/0
  201. }
  202. FirewallRuleSet auth-is-down {
  203. $set_firewall_rule_auth_is_down
  204. }
  205. FirewallRuleSet unknown-users {
  206. $set_firewall_rule_unknown_users
  207. FirewallRule allow udp port 53
  208. FirewallRule allow tcp port 53
  209. FirewallRule allow udp port 67
  210. FirewallRule allow tcp port 67
  211. }
  212. FirewallRuleSet locked-users {
  213. $set_firewall_rule_locked_users
  214. FirewallRule block to 0.0.0.0/0
  215. }
  216. EOF
  217. }
  218. init_config() {
  219. config_load wifidogx
  220. config_foreach prepare_wifidog_conf wifidog
  221. if [ ! -f ${CONFIGFILE} ]; then
  222. echo "no wifidogx.conf, exit..." >&2
  223. exit
  224. fi
  225. if [ ! -s "${APFREE_CERT}" ] || [ ! -s "${APFREE_KEY}" ]; then
  226. generate_keys
  227. fi
  228. if [ ! -s ${APFREE_KEY} ] || [ ! -s ${APFREE_CERT} ]; then
  229. echo "no cert or key, exit..." >&2
  230. exit
  231. fi
  232. config_foreach prepare_mqtt_conf mqtt
  233. sed -i -e '/^$/d' ${CONFIGFILE}
  234. }
  235. start_service() {
  236. init_config
  237. procd_open_instance
  238. # -f: run in foreground
  239. procd_set_param command $PROG -c $CONFIGFILE -f -d 0
  240. procd_set_param respawn # respawn automatically if something died
  241. procd_set_param file $CONFIGFILE
  242. procd_close_instance
  243. }
  244. status_service() {
  245. /usr/bin/wdctlx status
  246. }