/[soft]/drakwizard/trunk/proxy_wizard/Squid.pm
ViewVC logotype

Contents of /drakwizard/trunk/proxy_wizard/Squid.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8720 - (show annotations) (download)
Fri Aug 23 23:53:27 2013 UTC (10 years, 7 months ago) by djennings
File size: 12395 byte(s)
- fix race condition checking service status (mga#10934)
- fix directory definition in proxy wizard
- fix parameter format in proxy wizard
- updated default proxy config file

1 #!/usr/bin/perl
2
3 # Drakwizard
4
5 # Copyright (C) 2002,2003 Mandrakesoft
6 #
7 # Authors: Arnaud Desmons <adesmons@mandrakesoft.com>
8 # Florent Villard <warly@mandrakesoft.com>
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2, or (at your option)
13 # any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24 package MDK::Wizard::Squid;
25 use strict;
26
27 use common;
28 use services;
29 use MDK::Wizard::Wizcommon;
30
31 my $wiz = new MDK::Wizard::Wizcommon;
32 my $in = interactive->vnew;
33 my $HOSTNAME = $wiz->{net}->network_get("HOSTNAME");
34 my $DOMAINNAME = chomp_(`dnsdomainname`);
35 my $SHORTHOSTNAME = chomp_(`hostname -s`);
36 if (!$HOSTNAME) { $HOSTNAME = chomp_(`hostname`) }
37 my $df = common::to_utf8(scalar(-d '/var/spool/squid' ? `/bin/df -m /var/spool/squid` : `/bin/df -m /var/spool`));
38 my ($free_disk) = $df =~ m/\d+\s+\d+\s+\d+\s+(\d+)/;
39 my $mem = `/usr/bin/free -m`;
40 ($mem) = $mem =~ /Mem:\s+(\d+)/;
41 my %level = (
42 1 => N("All - No access restriction"),
43 2 => N("Local Network - access for local network (recommended)"),
44 3 => N("Localhost - access restricted to this server only")
45 );
46 my %upper = (
47 1 => N("No upper level proxy (recommended)"),
48 2 => N("Define an upper level proxy")
49 );
50
51 my $o = {
52 name => N("Squid wizard"),
53 var => {
54 wiz_squid_level => '',
55 wiz_squid_mem => '',
56 wiz_squid_port => '',
57 wiz_squid_mynetw => '',
58 wiz_squid_defdir => '/var/spool/squid',
59 wiz_squid_cachepeer => '',
60 wiz_squid_peerport => '',
61 wiz_squid_menupeer => '',
62 wiz_squid_disk => ''
63 },
64 init => sub {
65 my ($err, $msg) = test_host_domain($SHORTHOSTNAME, $DOMAINNAME);
66 if (!$err) {
67 $in->ask_warn(N("Error"), $msg);
68 die 'wizcancel';
69 }
70 ($err, $msg);
71 },
72 needed_rpm => [ 'squid' ],
73 defaultimage => "/usr/share/mcc/themes/default/drakproxy-mdk.png",
74 };
75
76 $o->{pages} = {
77 welcome => {
78 name => N("Proxy Configuration Wizard") . "\n\n" . N("Squid is a web caching proxy server, it allows faster web access for your local network.") . "\n\n" . N("This wizard will help you in configuring your proxy server."),
79 no_back => 1,
80 next => 'port'
81 },
82 port => {
83 name => N("Proxy Port") . "\n\n" . N("Proxy port value sets what port the proxy server will listen on for http requests. Default is 3128, other common value can be 8080, the port value needs to be greater than 1024.") . "\n\n",
84 pre => sub {
85 $o->{var}{wiz_squid_port} ||= 3128;
86 },
87 post => \&testport,
88 data => [
89 { label => N("Proxy port:"), val => \$o->{var}{wiz_squid_port} },
90 ],
91 next => 'memory'
92 },
93 warning_port => {
94 name => N("Warning.") . "\n\n" . N("You have entered a port that may be useful for this service:") . "\n\n" . N("Press Next if you want to keep this value, or Back to correct your choice."),
95 data => [
96 { label => '/etc/services:', val_ref => \$o->{var}{used_by} },
97 ],
98 next => 'memory'
99 },
100 error_low => {
101 name => N("Warning.") . "\n\n" . N("You must choose a port greater than 1024 and lower than 65535") . "\n\n" . N("Press back to change the value."),
102 next => 'memory'
103 },
104 memory => {
105 name => N("Proxy Cache Size") . "\n\n" . N("Memory Cache is the amount of RAM dedicated to cache memory operations (note that actual memory usage of the whole squid process is bigger).") . "\n\n" . N("Disk Cache is the amount of disk space that can be used for caching on disk.") . "\n\n" . N("For your information, here is /var/spool/squid space on disk:") . "\n\n" . $df,
106 pre => sub {
107 $o->{var}{wiz_squid_mem} ||= int($mem / 4);
108 $o->{var}{wiz_squid_disk} ||= int($free_disk/2);
109 },
110 data => [
111 { label => N("Memory cache (MB):"), val => \$o->{var}{wiz_squid_mem} },
112 { label => N("Disk space (MB):"), val => \$o->{var}{wiz_squid_disk} },
113 ],
114 next => 'level'
115 },
116 level => {
117 name => N("Access Control") . "\n\n" . N("The proxy can be configured to use different access control levels") . "\n\n" . N("Choose the level that suits your needs. If you don't know, the Local Network level is usually the most appropriate. Beware that the All level may be not secure."),
118 pre => sub {
119 $o->{var}{wiz_squid_level} ||= 2;
120 },
121 data => [
122 { label => '', val => \$o->{var}{wiz_squid_level}, list => [ keys %level ], format => sub { $level{$_[0]} } },
123 ],
124 post => sub {
125 return 'shownet' if $o->{var}{wiz_squid_level} == 2;
126 },
127 next => 'cachepeer'
128 },
129 shownet => {
130 name => N("Grant access on local network") . "\n\n" . N("Access will be allowed for hosts on the network. Here is the information found about your current local network, you can modify it if needed.") . "\n\n" . N("You can use either a numeric format like \"192.168.1.0/255.255.255.0\" or a text format like \".domain.net\""),
131 pre => sub {
132 $o->{var}{wiz_squid_mynetw} ||= network_mask();
133 },
134 data => [
135 { label => N("Authorized network:"), val => \$o->{var}{wiz_squid_mynetw}, help => N("Use numeric format like \"192.168.1.0/255.255.255.0\" or a text format like \".domain.net\"") },
136 ],
137 next => 'cachepeer'
138 },
139 cachepeer => {
140 name => N("Cache hierarchy") . "\n\n" . N("As an option, Squid can be configured in proxy cascading. You can add a new upper level proxy by specifying its hostname and port.") . "\n\n" . N("You can safely select \"No upper level proxy\" if you don't need this feature."),
141 pre => sub {
142 $o->{var}{wiz_squid_menupeer} ||= 1;
143 },
144 data => [
145 { label => '', val => \$o->{var}{wiz_squid_menupeer}, list => [ keys %upper ], format => sub { $upper{$_[0]} } },
146 ],
147 post => sub {
148 return 'valcachepeer' if $o->{var}{wiz_squid_menupeer} == 2;
149 },
150 next => 'summary_2'
151 },
152 valcachepeer => {
153 name => N("Cache hierarchy") . "\n\n" . N("Enter the qualified hostname (like \"cache.domain.net\") and the port of the proxy to use."),
154 data => [
155 { label => N("Upper level proxy hostname:"), val => \$o->{var}{wiz_squid_cachepeer} },
156 { label => N("Upper level proxy port:"), val => \$o->{var}{wiz_squid_peerport} },
157 ],
158 next => 'summary'
159 },
160 summary => {
161 name => N("Configuring the Proxy") . "\n\n" . N("The wizard collected the following parameters needed to configure your proxy:") . "\n\n" . N("To accept these values, and configure your server, click the Next button or use the Back button to correct them."),
162 data => [
163 { label => N("Port:"), type => 'field', val_ref => \$o->{var}{wiz_squid_port} },
164 { label => N("Memory cache (MB):"), type => 'field', val_ref => \$o->{var}{wiz_squid_mem} },
165 { label => N("Disk space (MB):"), type => 'field', val_ref => \$o->{var}{wiz_squid_disk} },
166 { label => N("Access Control:"), type => 'field', val_ref => \$o->{var}{wiz_squid_mynetw} },
167 { label => N("Upper level proxy hostname:"), type => 'field', val_ref => \$o->{var}{wiz_squid_cachepeer} },
168 { label => N("Upper level proxy port:"), type => 'field', val_ref => \$o->{var}{wiz_squid_peerport} },
169 ],
170 post => \&do_it,
171 next => 'end'
172 },
173 summary_2 => {
174 name => N("Configuring the Proxy") . "\n\n" . N("The wizard collected the following parameters needed to configure your proxy:") . "\n\n" . N("To accept these values, and configure your server, click the Next button or use the Back button to correct them."),
175 data => [
176 { label => N("Port:"), type => 'field', val_ref => \$o->{var}{wiz_squid_port} },
177 { label => N("Memory cache (MB):"), type => 'field', val_ref => \$o->{var}{wiz_squid_mem} },
178 { label => N("Disk space (MB):"), type => 'field', val_ref => \$o->{var}{wiz_squid_disk} },
179 { label => N("Access Control:"), type => 'field', val_ref => \$o->{var}{wiz_squid_mynetw} },
180 ],
181 post => \&do_it,
182 next => 'end'
183 },
184 end => {
185 name => N("Congratulations") . N("The wizard has successfully configured your proxy server."),
186 end => 1,
187 },
188 error_end => {
189 name => N("Failed"),
190 data => [ { label => N("Relaunch drakwizard, and try to change some parameters.") } ],
191 no_back => 1,
192 end => 1,
193 },
194
195 };
196
197 sub new {
198 my ($class) = @_;
199 bless $o, $class;
200 }
201
202 sub network_mask {
203 my $wiz_ip_server = $wiz->{net}->itf_get("IPADDR");
204 my $mask = $wiz->{net}->itf_get("NETMASK");
205 "$1.$2.$3.0/$mask" if $wiz_ip_server =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;
206 }
207
208 sub testport {
209 if (member($o->{var}{wiz_squid_port}, qw(3128 8080))) {
210 return '';
211 } elsif ($o->{var}{wiz_squid_port} <= 1024 || $o->{var}{wiz_squid_port} >= 65536) {
212 return 'error_low';
213 }
214 $o->{var}{used_by} = `/bin/grep $o->{var}{wiz_squid_port} /etc/services`;
215 return 'warning_port' if $o->{var}{used_by};
216 }
217
218 sub port {
219 my $ret;
220 my @lines = grep { m!$o->{var}{wiz_squid_port}/tcp! } cat_("/etc/services");
221 foreach (@lines) {
222 s/\t/ /g;
223 $ret .= "\n$_";
224 }
225 $ret;
226 }
227
228 sub do_it {
229 $::testing and return;
230 my $in = 'interactive'->vnew('su', 'Squid Config');
231 check_starts_on_boot($in, 'squid');
232 my $w = $in->wait_message(N("Squid proxy"), N("Configuring your system as a Proxy server..."));
233 my $file="/etc/squid/squid.conf";
234 -f $file and MDK::Common::cp_af($file, $file . ".orig");
235 MDK::Common::cp_af("$::Wiz_dir/proxy_wizard/scripts/squid.conf.default", $file);
236 substInFile {
237 s|^\s*#?\s*cache_dir ufs /var/spool/squid 100 16 256.*|cache_dir ufs $o->{var}{wiz_squid_defdir} $o->{var}{wiz_squid_disk} 16 256|;
238 s|^\s*#?\s*acl\s*localnet\s*src.*|acl localnet src $o->{var}{wiz_squid_mynetw}|;
239 s|^\s*#?\s*(cache_mem\s*)\d+(\s*MB.*)|$1$o->{var}{wiz_squid_mem} MB|;
240 s|^\s*#?\s*(http_port\s*)\d+(.*)|$1$o->{var}{wiz_squid_port}|;
241 } $file;
242 standalone::explanations("$file: cache_dir = $o->{var}{wiz_squid_defdir} $o->{var}{wiz_squid_disk}
243 mynetw = $o->{var}{wiz_squid_mynetw} cache_mem = $o->{var}{wiz_squid_mem} http_port = $o->{var}{wiz_squid_port}
244 level = $o->{var}{wiz_squid_level}");
245 if ($o->{var}{wiz_squid_level} == 1) {
246 substInFile {
247 s|^\s*#?\s*(http_access\s*)deny(\s*all.*)|#$&\n$1allow$2|;
248 } $file;
249 } elsif ($o->{var}{wiz_squid_level} == 2) {
250 substInFile {
251 s|^\s*#?\s*(http_access\s*)allow(\s*all.*)|#$&\n$1deny$2|;
252 s|^\s*#?\s*(http_access\s*allow\s*)localhost|#$&\n$1localnet|;
253 } $file;
254 } elsif ($o->{var}{wiz_squid_level} == 3) {
255 substInFile {
256 s|^\s*#?\s*(http_access\s*)allow(\s*all.*)|#$&\n$1deny$2|;
257 s|^\s*#?\s*(http_access\s*allow\s*)localnet|#$&\n$1localhost|;
258 } $file;
259 } else { # should not happen
260 die "wiz_squid_level error";
261 }
262 my $t = find { /^\s*cache_peer/ } cat_($file);
263 if ($o->{var}{wiz_squid_menupeer} == 1 && length $t) {
264 substInFile {
265 s|^\s*!#\s*(cache_peer.*)|#$&|;
266 } $file;
267 } elsif ($o->{var}{wiz_squid_menupeer} == 2 && length $o->{var}{wiz_squid_cachepeer}) {
268 if (length($t)) {
269 substInFile {
270 s|^\s*#?\s*(cache_peer.*)|#$&|;
271 } $file;
272 }
273 append_to_file($file, "cache_peer $o->{var}{wiz_squid_cachepeer} parent $o->{var}{wiz_squid_peerport} 3130");
274 }
275
276 substInFile {
277 s|^visible_hostname.*|visible_hostname $HOSTNAME|;
278 } $file;
279
280 system("/sbin/chkconfig --level 345 squid on");
281 reload_or_restart('squid');
282 undef $w;
283 check_started('squid');
284 }
285 1;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.30