uhttpd.init 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2010 Jo-Philipp Wich
  3. START=50
  4. USE_PROCD=1
  5. UHTTPD_BIN="/usr/sbin/uhttpd"
  6. PX5G_BIN="/usr/sbin/px5g"
  7. append_arg() {
  8. local cfg="$1"
  9. local var="$2"
  10. local opt="$3"
  11. local def="$4"
  12. local val
  13. config_get val "$cfg" "$var"
  14. [ -n "$val" -o -n "$def" ] && procd_append_param command "$opt" "${val:-$def}"
  15. }
  16. append_bool() {
  17. local cfg="$1"
  18. local var="$2"
  19. local opt="$3"
  20. local def="$4"
  21. local val
  22. config_get_bool val "$cfg" "$var" "$def"
  23. [ "$val" = 1 ] && procd_append_param command "$opt"
  24. }
  25. generate_keys() {
  26. local cfg="$1"
  27. local key="$2"
  28. local crt="$3"
  29. local days bits country state location commonname
  30. config_get days "$cfg" days
  31. config_get bits "$cfg" bits
  32. config_get country "$cfg" country
  33. config_get state "$cfg" state
  34. config_get location "$cfg" location
  35. config_get commonname "$cfg" commonname
  36. [ -x "$PX5G_BIN" ] && {
  37. $PX5G_BIN selfsigned -der \
  38. -days ${days:-730} -newkey rsa:${bits:-1024} -keyout "${UHTTPD_KEY}.new" -out "${UHTTPD_CERT}.new" \
  39. -subj /C="${country:-DE}"/ST="${state:-Saxony}"/L="${location:-Leipzig}"/CN="${commonname:-OpenWrt}"
  40. sync
  41. mv "${UHTTPD_KEY}.new" "${UHTTPD_KEY}"
  42. mv "${UHTTPD_CERT}.new" "${UHTTPD_CERT}"
  43. }
  44. }
  45. start_instance()
  46. {
  47. UHTTPD_CERT=""
  48. UHTTPD_KEY=""
  49. local cfg="$1"
  50. local realm="$(uci_get system.@system[0].hostname)"
  51. local listen http https interpreter indexes path handler
  52. procd_open_instance
  53. procd_set_param respawn
  54. procd_set_param stderr 1
  55. procd_set_param command "$UHTTPD_BIN" -f
  56. append_arg "$cfg" home "-h"
  57. append_arg "$cfg" realm "-r" "${realm:-OpenWrt}"
  58. append_arg "$cfg" config "-c"
  59. append_arg "$cfg" cgi_prefix "-x"
  60. [ -f /usr/lib/uhttpd_lua.so ] && {
  61. config_get handler "$cfg" lua_handler
  62. [ -f "$handler" ] && append_arg "$cfg" lua_prefix "-l" && {
  63. procd_append_param command "-L" "$handler"
  64. }
  65. }
  66. [ -f /usr/lib/uhttpd_ubus.so ] && {
  67. append_arg "$cfg" ubus_prefix "-u"
  68. append_arg "$cfg" ubus_socket "-U"
  69. append_bool "$cfg" ubus_cors "-X" 0
  70. }
  71. append_arg "$cfg" script_timeout "-t"
  72. append_arg "$cfg" network_timeout "-T"
  73. append_arg "$cfg" http_keepalive "-k"
  74. append_arg "$cfg" tcp_keepalive "-A"
  75. append_arg "$cfg" error_page "-E"
  76. append_arg "$cfg" max_requests "-n" 3
  77. append_arg "$cfg" max_connections "-N"
  78. append_bool "$cfg" no_ubusauth "-a" 0
  79. append_bool "$cfg" no_symlinks "-S" 0
  80. append_bool "$cfg" no_dirlists "-D" 0
  81. append_bool "$cfg" rfc1918_filter "-R" 0
  82. config_get alias_list "$cfg" alias
  83. for alias in $alias_list; do
  84. procd_append_param command -y "$alias"
  85. done
  86. config_get http "$cfg" listen_http
  87. for listen in $http; do
  88. procd_append_param command -p "$listen"
  89. done
  90. config_get interpreter "$cfg" interpreter
  91. for path in $interpreter; do
  92. procd_append_param command -i "$path"
  93. done
  94. config_get indexes "$cfg" index_page
  95. for path in $indexes; do
  96. procd_append_param command -I "$path"
  97. done
  98. config_get https "$cfg" listen_https
  99. config_get UHTTPD_KEY "$cfg" key /etc/uhttpd.key
  100. config_get UHTTPD_CERT "$cfg" cert /etc/uhttpd.crt
  101. [ -f /lib/libustream-ssl.so ] && [ -n "$https" ] && {
  102. [ -s "$UHTTPD_CERT" -a -s "$UHTTPD_KEY" ] || {
  103. config_foreach generate_keys cert
  104. }
  105. [ -f "$UHTTPD_CERT" -a -f "$UHTTPD_KEY" ] && {
  106. append_arg "$cfg" cert "-C"
  107. append_arg "$cfg" key "-K"
  108. for listen in $https; do
  109. procd_append_param command -s "$listen"
  110. done
  111. }
  112. append_bool "$cfg" redirect_https "-q" 0
  113. }
  114. for file in /etc/uhttpd/*.json; do
  115. [ -s "$file" ] && procd_append_param command -H "$file"
  116. done
  117. procd_close_instance
  118. }
  119. service_triggers()
  120. {
  121. procd_add_reload_trigger "uhttpd"
  122. }
  123. start_service() {
  124. config_load uhttpd
  125. config_foreach start_instance uhttpd
  126. }