142 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. [ -n "$_value" -a "$OPTION" = "LogFile" ] && {
  88. touch $_value
  89. chmod 666 $_value
  90. }
  91. }
  92. proxy_flag() {
  93. local SECTION=$1
  94. local OPTION=$2
  95. local TRUE="${3:-On}"
  96. local FALSE="${4:-Off}"
  97. config_get_bool _value "$SECTION" "$OPTION" 0
  98. [ "$_value" -eq "1" ] && _value="$TRUE" || _value="$FALSE"
  99. echo "$OPTION $_value"
  100. }
  101. proxy_list() {
  102. local SECTION=$1
  103. local OPTION=$2
  104. local ENCLOSE=$3
  105. config_get _value "$SECTION" "$OPTION"
  106. [ -n "$_value" ] && {
  107. for entry in $_value; do
  108. echo "$OPTION ${ENCLOSE}${entry}${ENCLOSE}"
  109. done
  110. }
  111. }