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