summaryrefslogtreecommitdiffstats
path: root/proxy_wizard/scripts/Squidconf.pm
blob: d9d1c10358e7209990a87695deeb18d88c1c758f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/perl

package Squidconf;
require "__WIZ_HOME__/common/scripts/Vareqval.pm";
require "__WIZ_HOME__/common/scripts/Varspaceval.pm";
require "__WIZ_HOME__/common/scripts/DrakconnectConf.pm";
use MDK::Common;
use strict;
use standalone;

sub	network_mask {
    my $o = DrakconnectConf->new();
    my $wiz_ip_server = $o->get_from_known_dev("IP");
    my $mask = $o->get_from_known_dev("Mask");
    "$1.$2.$3.0/$mask" if $wiz_ip_server =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;
}

sub	testport {
    if ($ENV{wiz_squid_port} == 3128 || $ENV{wiz_squid_port} == 8080) {
	return 0;
    }
    elsif ($ENV{wiz_squid_port} <= 1024 || $ENV{wiz_squid_port} >= 65536) {
	return 2;
    }
    1;
}

sub	port {
    my $ret;
    my @lines = grep(/$ENV{wiz_squid_port}\/tcp/, cat_("/etc/services"));
    foreach (@lines) {
	s/\t/ /g;
	$ret .= "\n$_";
    }
    $ret;
}

sub	do_it_squid {
    my $file="/etc/squid/squid.conf";
    -f $file and MDK::Common::cp_af($file, $file.".orig");
    MDK::Common::cp_af("__WIZ_HOME__/proxy_wizard/scripts/squid.conf.default", $file);
    print "$ENV{wiz_squid_defdir}\n";
    substInFile {
	s|^\s*\#?\s*(cache_dir.*$ENV{wiz_squid_defdir}\s*)\d*(.*)|$1$ENV{wiz_squid_disk}$2|;
	s|^\s*\#?\s*(acl\s*mynetwork\s*src\s*).*$|$1$ENV{wiz_squid_mynetw}\n|;
	s|^\s*\#?\s*(cache_mem\s*)\d*(\s*MB.*)|$1$ENV{wiz_squid_mem}$2|;
	s|^\s*\#?\s*(http_port\s*)\d*(.*)|$1$ENV{wiz_squid_port}$2|;
    } $file;
    standalone::explanations("$file: cache_dir = $ENV{wiz_squid_defdir} $ENV{wiz_squid_disk} 
mynetw = $ENV{wiz_squid_mynetw} cache_mem = $ENV{wiz_squid_mem} http_port = $ENV{wiz_squid_port} 
level = $ENV{wiz_squid_level}");
    if ($ENV{wiz_squid_level} == 1) {
	substInFile {
	    s|^\s*\#?\s*(http_access\s*)deny(\s*all.*)|\#$&$1allow$2|;
	} $file;
    }
    elsif ($ENV{wiz_squid_level} == 2) {
	substInFile {
	    s|^\s*\#?\s*(http_access\s*)allow(\s*all.*)|\#$&$1deny$2|;
	    s|^\s*\#?\s*(http_access\s*allow\s*)localhost|\#$&$1mynetwork|;
	} $file;
    }
    elsif ($ENV{wiz_squid_level} == 3) {
	substInFile {
	    s|^\s*\#?\s*(http_access\s*)allow(\s*all.*)|\#$&$1deny$2|;
	    s|^\s*\#?\s*(http_access\s*allow\s*)mynetwork|\#$&$1localhost|;
	} $file;
    }
    else { # should not happen
	die "wiz_squid_level error";
    }
    my $t = 0;
    foreach (cat_($file)) {
	if (/^\s*cache_peer.*/) {
	    $t = $_;;
	    last;
	}
    }
    if ($ENV{wiz_squid_menupeer} == 1 && length $t) {
	substInFile {
	    s|^\s*!\#\s*(cache_peer.*)|\#$&|;
	} $file;
    }
    elsif ($ENV{wiz_squid_menupeer} == 2 && length $ENV{wiz_squid_cachepeer}) {
	if (lenght $t) {
	    substInFile {
		s|^\s*\#?\s*(cache_peer.*)|\#$&|;
	    } $file;
	}
	append_to_file($file, "cache_peer $ENV{wiz_squid_cachepeer} parent $ENV{wiz_squid_peerport} 3130");
    }
    system("/sbin/chkconfig --level 345 squid on");
    system("service squid start");
    10;
}
1;