]>
Commit | Line | Data |
---|---|---|
46c01c09 MT |
1 | #!/usr/bin/perl |
2 | # | |
3 | # This code is distributed under the terms of the GPL | |
4 | # | |
0a638396 | 5 | # (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net |
46c01c09 | 6 | # |
0a638396 | 7 | # $Id: checkup,v 2.0 2007/06/17 00:00:00 marco.s Exp $ |
46c01c09 MT |
8 | # |
9 | ||
10 | use strict; | |
11 | ||
46c01c09 MT |
12 | use HTTP::Date; |
13 | ||
14 | my $swroot='/var/ipfire'; | |
46c01c09 MT |
15 | my $apphome="/var/ipfire/updatexlrator"; |
16 | my $logfile="/var/log/updatexlrator/checkup.log"; | |
0a638396 CS |
17 | my $repository='/var/updatecache'; |
18 | my $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"; | |
19 | my %proxysettings=(); | |
46c01c09 MT |
20 | my %xlratorsettings=(); |
21 | my $download=0; | |
22 | my $updatefile=''; | |
23 | my $sourceurl=''; | |
0a638396 | 24 | my @sources=(); |
46c01c09 | 25 | my @updatelist=(); |
0a638396 | 26 | my $logging=0; |
46c01c09 MT |
27 | |
28 | my $sfUnknown = "0"; | |
29 | my $sfOk = "1"; | |
30 | my $sfOutdated = "2"; | |
0a638396 | 31 | my $sfNoSource = "3"; |
46c01c09 MT |
32 | |
33 | if (-e "$swroot/updatexlrator/settings") | |
34 | { | |
35 | &readhash("$swroot/updatexlrator/settings", \%xlratorsettings); | |
36 | if ($xlratorsettings{'FULL_AUTOSYNC'} eq 'on') { $download=1; }; | |
0a638396 | 37 | if ($xlratorsettings{'ENABLE_LOG'} eq 'on') { $logging=1; }; |
46c01c09 MT |
38 | } |
39 | ||
0a638396 | 40 | if (-e "$swroot/proxy/settings") { &readhash("$swroot/proxy/settings", \%proxysettings); } |
46c01c09 | 41 | |
0a638396 CS |
42 | if (-e "$swroot/proxy/advanced/settings") |
43 | { | |
44 | %proxysettings=(); | |
45 | &readhash("$swroot/proxy/advanced/settings", \%proxysettings); | |
46 | } | |
46c01c09 | 47 | |
0a638396 CS |
48 | foreach (<$repository/*>) |
49 | { | |
50 | if (-d $_) | |
51 | { | |
52 | unless (/^$repository\/download$/) { push(@sources,$_); } | |
53 | } | |
54 | } | |
46c01c09 | 55 | |
0a638396 CS |
56 | foreach (@sources) |
57 | { | |
58 | @updatelist=<$_/*>; | |
59 | foreach(@updatelist) | |
60 | { | |
61 | if (-e "$_/source.url") | |
62 | { | |
63 | open (FILE,"$_/source.url"); | |
64 | $sourceurl=<FILE>; | |
46c01c09 | 65 | close FILE; |
0a638396 CS |
66 | chomp($sourceurl); |
67 | $updatefile = substr($sourceurl,rindex($sourceurl,'/')+1,length($sourceurl)); | |
68 | &checksource($_); | |
46c01c09 MT |
69 | } |
70 | } | |
71 | } | |
72 | ||
73 | # ------------------------------------------------------------------- | |
74 | ||
75 | sub readhash | |
76 | { | |
77 | my $filename = $_[0]; | |
78 | my $hash = $_[1]; | |
79 | my ($var, $val); | |
80 | ||
81 | if (-e $filename) | |
82 | { | |
83 | open(FILE, $filename) or die "Unable to read file $filename"; | |
84 | while (<FILE>) | |
85 | { | |
86 | chop; | |
87 | ($var, $val) = split /=/, $_, 2; | |
88 | if ($var) | |
89 | { | |
90 | $val =~ s/^\'//g; | |
91 | $val =~ s/\'$//g; | |
92 | ||
93 | # Untaint variables read from hash | |
94 | $var =~ /([A-Za-z0-9_-]*)/; $var = $1; | |
95 | $val =~ /([\w\W]*)/; $val = $1; | |
96 | $hash->{$var} = $val; | |
97 | } | |
98 | } | |
99 | close FILE; | |
100 | } | |
101 | } | |
102 | ||
103 | # ------------------------------------------------------------------- | |
104 | ||
105 | sub getmtime | |
106 | { | |
107 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($_[0]); | |
108 | ||
109 | return $mtime; | |
110 | } | |
111 | ||
112 | # ------------------------------------------------------------------- | |
113 | ||
0a638396 | 114 | sub writelog |
46c01c09 | 115 | { |
0a638396 CS |
116 | print "$_[0]\n"; |
117 | if ($logging) | |
46c01c09 | 118 | { |
0a638396 CS |
119 | open (LOGFILE,">>$logfile"); |
120 | my @now = localtime(time); | |
121 | printf LOGFILE "%04d-%02d-%02d %02d:%02d:%02d %s\n",$now[5]+1900,$now[4]+1,$now[3],$now[2],$now[1],$now[0],$_[0]; | |
122 | close LOGFILE; | |
46c01c09 | 123 | } |
46c01c09 MT |
124 | } |
125 | ||
126 | # ------------------------------------------------------------------- | |
127 | ||
0a638396 | 128 | sub setcachestatus |
46c01c09 | 129 | { |
0a638396 CS |
130 | open (FILE,">$_[0]"); |
131 | print FILE "$_[1]\n"; | |
132 | close FILE; | |
133 | } | |
46c01c09 | 134 | |
0a638396 | 135 | # ------------------------------------------------------------------- |
46c01c09 | 136 | |
0a638396 CS |
137 | sub checksource |
138 | { | |
139 | my @http_header=(); | |
140 | my $http_result='000 n/a'; | |
141 | my $returncode=0; | |
142 | my $localfile=''; | |
143 | my $remote_size=0; | |
144 | my $remote_mtime=0; | |
145 | my $login=''; | |
146 | my $url=''; | |
147 | my $cdir=$_[0]; | |
148 | ||
149 | open (FILE,"$cdir/source.url"); | |
150 | $url=<FILE>; | |
151 | close FILE; | |
152 | chomp($url); | |
153 | ||
154 | $localfile = $cdir . substr($url,rindex($url,'/'),length($url)); | |
155 | ||
156 | if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'})) | |
157 | { | |
158 | $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\""; | |
159 | if ($proxysettings{'UPSTREAM_PASSWORD'}) | |
160 | { | |
161 | $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\""; | |
162 | } | |
163 | } | |
46c01c09 | 164 | |
0a638396 CS |
165 | $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'}; |
166 | @http_header = `wget $login --user-agent="$useragent" --spider -S $url 2>&1`; | |
167 | $ENV{'http_proxy'} = ''; | |
168 | &writelog(@http_header); | |
46c01c09 | 169 | |
0a638396 | 170 | foreach (@http_header) |
46c01c09 | 171 | { |
0a638396 CS |
172 | chomp; |
173 | if (/^\s*HTTP\/\d+\.\d+\s\d+\s+\w+/) { $http_result = $_; $http_result =~ s/^\s*HTTP\/\d+\.\d+\s+//; } | |
174 | if (/^\s*Content-Length:\s/) { $remote_size = $_; $remote_size =~ s/[^0-9]//g; } | |
175 | if (/^\s*Last-Modified:\s/) { $remote_mtime = $_; $remote_mtime =~ s/^\s*Last-Modified:\s//; $remote_mtime = HTTP::Date::str2time($remote_mtime) } | |
46c01c09 MT |
176 | } |
177 | ||
0a638396 CS |
178 | &writelog($localfile); |
179 | &writelog("HTTP result: $http_result"); | |
180 | &writelog("Source size: $remote_size"); | |
181 | &writelog("Cached size: " . (-s $localfile)); | |
182 | &writelog("Source time: $remote_mtime"); | |
183 | &writelog("Cached time: " . getmtime($localfile)); | |
184 | ||
185 | if ($http_result =~ /\d+\s+OK$/) | |
46c01c09 | 186 | { |
0a638396 | 187 | if (($remote_size == -s $localfile) && ($remote_mtime == getmtime($localfile))) |
46c01c09 | 188 | { |
0a638396 CS |
189 | &writelog("Status: Ok"); |
190 | &setcachestatus("$cdir/status",$sfOk); | |
191 | } else { | |
192 | &writelog("Status: Outdated"); | |
193 | &setcachestatus("$cdir/status",$sfOutdated); | |
194 | if ($download) | |
46c01c09 | 195 | { |
0a638396 CS |
196 | &writelog("Retrieving file from source: $remote_size bytes"); |
197 | $_ = system("wget $login --user-agent=\"$useragent\" -q -O $localfile $url"); | |
198 | &writelog("Download finished with code: $_"); | |
199 | if ($_ == 0) { &setcachestatus("$cdir/status",$sfOk); } | |
46c01c09 MT |
200 | } |
201 | } | |
0a638396 CS |
202 | } else { |
203 | $_ = $http_result; | |
204 | s/\D+//; | |
205 | if ($_ eq '404') | |
206 | { | |
207 | &writelog("Status: No source"); | |
208 | &setcachestatus("$cdir/status",$sfNoSource); | |
209 | } else { | |
210 | &writelog("Status: Error"); | |
211 | &setcachestatus("$cdir/status",$sfUnknown); | |
212 | } | |
46c01c09 | 213 | } |
0a638396 CS |
214 | |
215 | &setcachestatus("$cdir/checkup.log",time); | |
46c01c09 MT |
216 | } |
217 | ||
218 | # ------------------------------------------------------------------- |