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.

138 lines
3.3 KiB

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