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