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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
#!/usr/bin/perl -w
## written by Maarten Vanraes (c) 2009-2011
## urpmi-proxy is GPLv2+
use strict;
use warnings;
my $debug = 0;
my $config_file = '/etc/urpmi-proxy.conf';
# config defaults
my $cache_tmp_path = '/var/tmp/urpmi-proxy';
my $cache_path = '/var/cache/urpmi-proxy';
my $logfile = '/var/log/urpmi-proxy.log';
my $check_updates_only_files = 'MD5SUM';
my $check_no_updates_files;
my $merge_files = 'media.cfg';
my $sources = [
'urpmi'
];
my $connect_timeout = 120;
my $ftp_response_timeout = 30;
my $max_stall_speed = 8192;
my $max_stall_time = 60;
# load config file
if (-R $config_file) {
my $r = open(FILE, '<', $config_file);
if ($r) {
my $l = '';
while (<FILE>) {
$l .= $_;
}
eval $l;
close FILE;
}
}
print STDERR "logfile: $logfile\n" if $debug;
my $sou;
print STDERR "orig sources: " . scalar(@$sources) . "\n" if $debug;
if ($debug) {
foreach $sou (@$sources) {
print STDERR " - " . $sou . "\n";
}
}
# prepare cache path
system("mkdir -p $cache_tmp_path");
# check for valid request
my $file = $ENV{PATH_INFO};
return_error(500, 'Server error') if !$file;
# split up request
return_error(500, 'Server error') if $file !~ m!^(.+)/([^/]*)$!;
my $dest_path = $1;
my $filename = $2;
my $file_type = "";
my $merge = 0;
print STDERR "file: $file\n" if $debug;
print STDERR "dest_path: $dest_path\n" if $debug;
print STDERR "filename: $filename\n" if $debug;
# check if request needs update checking
my $check_file = 1;
$check_file = 0 if defined $check_no_updates_files && $filename =~ m/$check_no_updates_files/;
if (defined $check_updates_only_files) {
$check_file = 0;
$check_file = 1 if $filename =~ m/$check_updates_only_files/;
}
# check if request needs merging
$merge = 1 if $filename =~ m/$merge_files/;
$check_file = 1 if $merge;
print STDERR "check_file: $check_file\n" if $debug;
print STDERR "merge: $merge\n" if $debug;
# if the file needs no update checks, check in cache
return_file($cache_path, $file, $logfile, 'CACHED_NO_CHECK') if (!$check_file && $filename && -R $cache_path . $file);
# get datetime from local file if it exists
my $timestamp = 0;
if ($filename && !$check_file && -R $cache_path . $file) {
my @sv = lstat($cache_path . $file);
$timestamp = $sv[9];
print STDERR "timestamp: $timestamp\n" if $debug;
}
# set up curl with timecheck
my $curl;
my $r = 0;
my $file_sent = 0;
#my $file_deleted = 0;
my $file_unmodified = 0;
my $file_time = -1;
my $err = 200;
# prepare curl transfer
my $tmp_file = $cache_tmp_path . "/" . rand() . $$;
open(FILEHANDLE, ">", $tmp_file) or do {
_log($logfile, $file, 500, 'MISS');
return_error(500, 'Server error');
};
binmode(FILEHANDLE);
my %curldata = (fh => \*FILEHANDLE, file_sent => \$file_sent, content_type => $file_type, size => -1, merge => \$merge);
print STDERR "sources: " . scalar(@$sources) . "\n" if $debug;
if ($debug) {
foreach $sou (@$sources) {
print STDERR " - " . $sou . "\n";
}
}
# filter out duplicate sources (and expand urpmi)
my @sources;
my %seen;
foreach my $s (@$sources) {
# heh
next if $seen{$s}++;
if ($s eq "urpmi") {
# urpmi support is required
use urpm;
use urpm::cfg;
my $urpm = new urpm();
urpm::get_global_options($urpm);
my $config = urpm::cfg::load_config($urpm->{config});
my %s;
foreach my $media (@{$config->{media}}) {
if (!$media->{ignore}) {
if ($media->{mirrorlist} && !($seen{'mirrorlist:' . $media->{mirrorlist}}++)) {
# push mirrorlists now so they'll be first
push @sources, 'mirrorlist://' . $media->{mirrorlist};
}
elsif ($media->{url} && !($seen{$media->{url}}++)) {
$s{$media->{url}} = 1;
}
}
}
# push the urls
push @sources, keys %s if scalar(keys %s);
}
else {
push @sources, $s;
}
}
# check for source
print STDERR "interpolated sources: " . scalar(@sources) . "\n" if $debug;
if ($debug) {
foreach $sou (@sources) {
print STDERR " - " . $sou . "\n";
}
}
foreach my $source (@sources) {
my ($type, @loc) = split('://', $source);
my $loc = join('://', @loc);
print STDERR "source of type $type: '" . $source . "'\n" if $debug;
if ($type eq 'mirrorlist') {
# get exact url from cache and parse
my $res = open(FILE, '<', '/var/cache/urpmi/mirrors.cache');
if ($res) {
my $mirrorcache = '';
while (<FILE>) {
$mirrorcache .= $_;
}
close FILE;
my $host_loc = $loc;
$host_loc =~ s/\$/\\\$/g;
if ($mirrorcache =~ m/'$host_loc'\s+=>\s+{[\r\n]+\s+'chosen'\s+=>\s+'([^']+)'/m) {
$source = $1;
# rectify source to remove '/distrib/version/arch'
$source =~ s!/[^/]+/[^/]+/[^/]+$!!;
print STDERR "mirrorlist returns source '$source'\n" if $debug;
($type, @loc) = split('://', $source);
$loc = join('://', @loc);
if (defined $type) {
print STDERR "mirrorlist returns type $type: '" . $source . "'\n" if $debug;
}
else {
print STDERR "transfer error: mirrorlist is no url '" . $source . "'.\n" if $debug;
$type = '';
}
}
else {
print STDERR "transfer error: mirrorlist has no chosen url '" . $source . "'.\n" if $debug;
$type = '';
}
}
else {
print STDERR "transfer error: couldn't open mirrorlist cache.\n" if $debug;
}
}
if ($type eq 'rsync') {
# find the equivalent ftp mirror location by hostname
my $res = open(FILE, '<', '/var/cache/urpmi/mirrors.cache');
if ($res) {
my $mirrorcache = '';
while (<FILE>) {
$mirrorcache .= $_;
}
close FILE;
my $loc_host = $loc;
$loc_host =~ s!/.+!!;
if ($mirrorcache =~ m!'url'\s+=>\s+'((ftp|http)://$loc_host/[^']+)'!) {
$source = $1;
# rectify source to remove '/distrib/version/arch'
$source =~ s!/[^/]+/[^/]+/[^/]+$!!;
print STDERR "rsync switch returns source '$source'\n" if $debug;
($type, @loc) = split('://', $source);
$loc = join('://', @loc);
if (defined $type) {
print STDERR "rsync switch returns type $type: '" . $source . "'\n" if $debug;
}
else {
print STDERR "transfer error: rsync switch is no url '" . $source . "'.\n" if $debug;
$type = '';
}
}
else {
print STDERR "transfer error: rsync switch has no suitable url '" . $source . "'.\n" if $debug;
$type = '';
}
}
else {
print STDERR "transfer error: couldn't open mirrorlist cache.\n" if $debug;
}
}
if ($type eq 'file') {
if ($filename && -R $loc . $file) {
my $ft = `file -b --mime-type $loc$file`;
$ft =~ s/[\s\r\n]*$//;
print STDERR "mimetype: '$ft'\n" if $debug;
print STDERR "size: " . (-s $loc . $file) . "\n" if $debug;
$r = open(FILE, "<", $loc . $file);
if ($r) {
print STDERR "file fetch url '" . $loc . $file . "'\n" if $debug;
if (!$file_sent) {
$file_sent = 1;
print "Content-Type: " . $ft . "\r\n";
print "Content-Length: " . (-s $loc . $file) . "\r\n" if !$merge;
print "\r\n";
}
binmode(FILE);
my $buf;
while (read(FILE, $buf, 1024)) {
print FILEHANDLE $buf;
print $buf;
}
close FILE;
$r = 0;
}
else {
print STDERR "transfer error: couldn't open file '" . $loc . $file . "'.\n" if $debug;
$r = 1;
$err = 404;
}
}
else {
print STDERR "transfer error: couldn't read file '" . $loc . $file . "'.\n" if $debug;
}
}
elsif ($type) {
if (!defined $curl) {
use WWW::Curl::Easy;
# set up curl stuff
$curl = new WWW::Curl::Easy;
if ($timestamp > 0) {
$curl->setopt(CURLOPT_TIMECONDITION, 1); # CURL_TIMECOND_IFMODSINCE
$curl->setopt(CURLOPT_TIMEVALUE, $timestamp);
}
$curl->setopt(CURLOPT_CONNECTTIMEOUT, $connect_timeout);
$curl->setopt(CURLOPT_FTP_RESPONSE_TIMEOUT, $ftp_response_timeout);
$curl->setopt(CURLOPT_LOW_SPEED_LIMIT, $max_stall_speed);
$curl->setopt(CURLOPT_LOW_SPEED_TIME, $max_stall_time);
$curl->setopt(CURLOPT_FOLLOWLOCATION, 1);
$curl->setopt(CURLOPT_FILETIME, 1);
# hook curl transfer functions for local caching
$curl->setopt(CURLOPT_WRITEDATA, \%curldata);
$curl->setopt(CURLOPT_WRITEFUNCTION, \&write_function);
$curl->setopt(CURLOPT_WRITEHEADER, \%curldata);
$curl->setopt(CURLOPT_HEADERFUNCTION, \&header_function);
}
# depending on type check if remote file is newer
print STDERR "curl fetch url '" . $source . $file . "'\n" if $debug;
$curl->setopt(CURLOPT_URL, $source . $file);
$r = $curl->perform;
print STDERR "curl return value: " . $err . "\n" if $debug;
# use curl to get it and output it directly
if ($r == 0) {
$err = $curl->getinfo(CURLINFO_HTTP_CODE);
if ($err =~ m/^2/ || $err == 304) {
if ($curl->getinfo(CURLINFO_CONDITION_UNMET)) {
$file_unmodified = 1;
}
$file_time = $curl->getinfo(CURLINFO_FILETIME);
}
else {
# error stuff ?
print STDERR "transfer error: http code " . $err . "\n" if $debug;
}
}
else {
# error stuff ?
print STDERR "transfer error: " . $curl->strerror($r) . " ($r)\n" if $debug;
}
}
else {
print STDERR "transfer error: this source does not have a type\n" if $debug;
}
last if $file_sent && !$merge;
}
my $extra = '';
close(FILEHANDLE);
if ($file_sent && $r == 0 && $err =~ m/^2/ && $filename) {
# clean up file and move to correct location
if (system("mkdir -p $cache_path$dest_path") == 0) {
if (rename($tmp_file, $cache_path . $file)) {
utime(time(), $file_time, $cache_path . $file) if $file_time > 0;
}
else {
print STDERR "WARNING: file '$tmp_file' could not be moved to '$cache_path$file'\n";
}
}
else {
print STDERR "WARNING: containing path for '$cache_path$file' could not be created\n";
}
_log($logfile, $file, 200, 'MISS');
}
else {
unlink($tmp_file);
if ($file_sent) {
if ($filename) {
_log($logfile, $file, $err, 'MISS_FAIL_SENT');
}
else {
# It was actually successful, but paths can't be saved...
print STDERR "NOTICE: paths cant be saved: '$file'\n" if $debug;
_log($logfile, $file, $err, 'MISS');
}
exit 0;
}
$extra = '_UNMODIFIED' if $file_unmodified;
return_file($cache_path, $file, $logfile, 'HIT_AFTER_FAIL' . $extra) if $filename && -R $cache_path . $file;
_log($logfile, $file, 404, 'MISS_FAIL');
return_error(404, 'File not found');
}
print STDERR "finished." if $debug;
exit 0;
sub header_function {
my ($ptr, $data) = @_;
if (!${$data->{file_sent}}) {
$data->{http_header} = $1 if $ptr =~ m!^HTTP/[0-9.]+\s+(.+?)[\s\r\n]*$!;
$data->{content_type} = $1 if $ptr =~ m/^Content-[tT]ype:\s+(.+?)[\s\r\n]*$/;
$data->{size} = $1 if $ptr =~ m/^Content-[lL]ength:\s+(.+?)[\s\r\n]*$/;
$data->{size} = $1 if $ptr =~ m/^213\s+(.+?)[\s\r\n]*$/;
}
return length($ptr);
}
sub write_function {
my ($ptr, $data) = @_;
my $f = ${$data->{fh}};
print $f ($ptr);
if (!${$data->{file_sent}}) {
${$data->{file_sent}} = 1;
print STDERR "HTTP header: " . $data->{http_header} . "\n" if $debug && defined $data->{http_header};
print STDERR "Content-Type: " . $data->{content_type} . "\n" if $debug && defined $data->{content_type};
print STDERR "Content-Length: " . $data->{size} . "\n" if $debug;
print "Status: " . $data->{http_header} . "\r\n" if $data->{http_header} && $data->{http_header} !~ m/^2/;
print "Content-Type: " . $data->{content_type} . "\r\n" if $data->{content_type};
print "Content-Length: " . $data->{size} . "\r\n" if $data->{size} > -1 && !${$data->{merge}};
print "\r\n";
}
print $ptr;
return length($ptr);
}
sub _log {
my ($logfile, $file, $code, $cached) = @_;
my $date = `date`;
$date =~ s/[\s\r\n]*$//;
open(FILE, ">>" . $logfile) or return;
print FILE "[" . $date . "] $file - $code - $cached\n";
close(FILE);
}
sub return_file {
my ($cache_path, $file, $logfile, $cached)=@_;
open(FILE, "<", $cache_path . $file) or do {
_log($logfile, $file, 500, $cached);
return_error(500, 'Server error');
};
my $ft = `file -b --mime-type $cache_path$file`;
$ft =~ s/[\s\r\n]*$//;
print STDERR "mimetype: '$ft'\n" if $debug;
print STDERR "size: " . (-s $cache_path . $file) . "\n" if $debug;
print "Content-Type: " . $ft . "\r\n";
print "Content-Length: " . (-s $cache_path . $file) . "\r\n\r\n";
binmode(FILE);
my $buf;
while (read(FILE, $buf, 1024)) {
print $buf;
}
close FILE;
_log($logfile, $file, 200, $cached);
exit 0;
}
sub return_error {
my ($code, $text) = @_;
print "Status: $code $text\r\n\r\n";
print STDERR "$code $text.\n" if $debug;
exit 0;
}
|