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.

148 lines
3.1 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2008-2011 OpenWrt.org
  3. START=50
  4. USE_PROCD=1
  5. CFGFILE=/var/etc/tinyproxy.conf
  6. PROG=/usr/bin/tinyproxy
  7. section_enabled() {
  8. local enabled
  9. config_get_bool enabled "$1" 'enabled' 0
  10. [ "$enabled" -gt 0 ]
  11. }
  12. write_upstream() {
  13. local type
  14. local via
  15. local target
  16. config_get "type" "$1" "type"
  17. config_get via "$1" via
  18. config_get target "$1" target
  19. [ -n "$target" ] && target=' "'"$target"'"'
  20. [ "$type" = "proxy" ] && [ -n "$via" ] && \
  21. echo "upstream $via$target"
  22. [ "$type" = "reject" ] && [ -n "$target" ] && \
  23. echo "no upstream$target"
  24. }
  25. proxy_atom() {
  26. local SECTION=$1
  27. local OPTION=$2
  28. local DEFAULT=$3
  29. config_get _value "$SECTION" "$OPTION"
  30. [ -z "$_value" ] && _value="$DEFAULT"
  31. [ -n "$_value" ] && echo "$OPTION $_value"
  32. }
  33. proxy_string() {
  34. local SECTION=$1
  35. local OPTION=$2
  36. local ALIAS=$3
  37. local DEFAULT=$4
  38. config_get _value "$SECTION" "$OPTION"
  39. [ -z "$_value" ] && _value="$DEFAULT"
  40. [ -n "$_value" ] && echo "${ALIAS:-${OPTION}} "'"'"$_value"'"'
  41. [ -n "$_value" ] && [ "$OPTION" = "LogFile" ] && {
  42. touch "$_value"
  43. chmod 666 "$_value"
  44. }
  45. }
  46. proxy_flag() {
  47. local SECTION=$1
  48. local OPTION=$2
  49. local TRUE="${3:-On}"
  50. local FALSE="${4:-Off}"
  51. config_get_bool _value "$SECTION" "$OPTION" 0
  52. [ "$_value" -eq "1" ] && _value="$TRUE" || _value="$FALSE"
  53. echo "$OPTION $_value"
  54. }
  55. proxy_list() {
  56. local SECTION=$1
  57. local OPTION=$2
  58. local ENCLOSE=$3
  59. config_get _value "$SECTION" "$OPTION"
  60. [ -n "$_value" ] && {
  61. for entry in $_value; do
  62. echo "$OPTION ${ENCLOSE}${entry}${ENCLOSE}"
  63. done
  64. }
  65. }
  66. start_proxy() {
  67. section_enabled "$1" || return 1
  68. mkdir -p /var/etc
  69. chmod 0755 /var/etc
  70. {
  71. echo '### AUTOGENERATED CONFIGURATION'
  72. echo '### DO NOT EDIT'
  73. echo '### SEE /etc/config/tinyproxy INSTEAD'
  74. echo ''
  75. proxy_atom "$1" User
  76. proxy_atom "$1" Group
  77. proxy_atom "$1" Port 8888
  78. proxy_atom "$1" Listen
  79. proxy_atom "$1" Bind
  80. proxy_atom "$1" Timeout
  81. proxy_string "$1" ErrorFile_400 "ErrorFile 400"
  82. proxy_string "$1" ErrorFile_403 "ErrorFile 403"
  83. proxy_string "$1" ErrorFile_404 "ErrorFile 404"
  84. proxy_string "$1" ErrorFile_408 "ErrorFile 408"
  85. proxy_string "$1" ErrorFile_503 "ErrorFile 503"
  86. proxy_string "$1" DefaultErrorFile
  87. proxy_string "$1" StatHost StatHost 127.0.0.1
  88. proxy_string "$1" StatFile
  89. proxy_string "$1" LogFile
  90. proxy_flag "$1" Syslog
  91. proxy_atom "$1" LogLevel
  92. proxy_flag "$1" XTinyproxy
  93. proxy_atom "$1" MaxClients
  94. proxy_atom "$1" MinSpareServers
  95. proxy_atom "$1" MaxSpareServers
  96. proxy_atom "$1" StartServers
  97. proxy_atom "$1" MaxRequestsPerChild
  98. proxy_list "$1" Allow
  99. proxy_string "$1" ViaProxyName
  100. proxy_string "$1" Filter
  101. proxy_flag "$1" FilterURLs
  102. proxy_flag "$1" FilterExtended
  103. proxy_flag "$1" FilterCaseSensitive
  104. proxy_flag "$1" FilterDefaultDeny Yes No
  105. proxy_list "$1" Anonymous '"'
  106. proxy_list "$1" ConnectPort
  107. config_foreach write_upstream upstream
  108. } > "$CFGFILE"
  109. procd_open_instance
  110. procd_set_param command "$PROG"
  111. procd_append_param command -c "$CFGFILE"
  112. procd_append_param command -d
  113. procd_close_instance
  114. }
  115. start_service() {
  116. config_load 'tinyproxy'
  117. config_foreach start_proxy 'tinyproxy'
  118. }