ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/access.pl
(Generate patch)

Comparing Coro/myhttpd/access.pl (file contents):
Revision 1.1 by root, Sat Aug 11 03:41:01 2001 UTC vs.
Revision 1.24 by root, Tue Dec 4 02:46:29 2001 UTC

1package transferqueue;
1 2
2our @blocklist; 3my @reserve = (
4 [ 1_000_000, 1],
5 [ 10_000_000, 1],
6);
3 7
8sub new {
9 my $class = shift;
10 bless {
11 slots => $_[0],
12 lastspb => 0,
13 avgspb => 0,
14 }, $class;
15}
16
17sub start_transfer {
18 my $self = shift;
19 my $size = $_[0];
20
21 my $transfer = bless {
22 queue => $self,
23 time => $::NOW,
24 size => $size,
25 coro => $Coro::current,
26 started => 0,
27 }, transfer::;
28
29 push @{$self->{wait}}, $transfer;
30
31 $self->wake_next;
32
33 $transfer;
34}
35
36sub sort {
37 my @queue = grep $_, @{$_[0]{wait}};
38
39 $_->{spb} = ($::NOW-$_->{time}) / ($_->{size} || 1) for @queue;
40
41 $_[0]{wait} = [sort { $b->{spb} <=> $a->{spb} } @queue];
42
43 Scalar::Util::weaken $_ for @{$_[0]{wait}};
44}
45
46sub wake_next {
47 my $self = shift;
48
49 $self->sort;
50
51 while (@{$self->{wait}}) {
52 my $size = $self->{wait}[0]{size};
53 my $min = 0;
54 for (@reserve) {
55 last if $size <= $_->[0];
56 $min += $_->[1];
57 }
58 last unless $self->{slots} > $min;
59 my $transfer = shift @{$self->{wait}};
60 $self->{lastspb} = $transfer->{spb};
61 $self->{avgspb} ||= $transfer->{spb};
62 $self->{avgspb} = $self->{avgspb} * 0.95 + $transfer->{spb} * 0.05;
63 $self->{started}++;
64 $transfer->wake;
65 last;
66 }
67}
68
69sub waiters {
70 $_[0]->sort;
71 @{$_[0]{wait}};
72}
73
74package transfer;
75
76use Coro::Timer ();
77
78sub wake {
79 my $self = shift;
80
81 $self->{alloc} = 1;
82 $self->{queue}{slots}--;
83 $self->{wake} and $self->{wake}->ready;
84}
85
86sub try {
87 my $self = shift;
88
89 $self->{alloc} || do {
90 my $timeout = Coro::Timer::timeout $_[0];
91 local $self->{wake} = $self->{coro};
92
93 Coro::schedule;
94
95 $self->{alloc};
96 }
97}
98
99sub DESTROY {
100 my $self = shift;
101
102 if ($self->{alloc}) {
103 $self->{queue}{slots}++;
104 $self->{queue}->wake_next;
105 }
106}
107
108package conn;
109
110our %blockuri;
111our $blockref;
112
4sub read_blocklist { 113sub read_blockuri {
5 local *B; 114 local *B;
6 my %group; 115 my %group;
7 @blocklist = (); 116 %blockuri = ();
8 if (open B, "<blocklist") { 117 if (open B, "<blockuri") {
9 while (<B>) { 118 while (<B>) {
10 chomp; 119 chomp;
11 if (/^group\s+(\S+)\s+(.*)/i) { 120 if (/^group\s+(\S+)\s+(.*)/i) {
12 $group{$1} = [split /\s+/, $2]; 121 $group{$1} = [split /\s+/, $2];
13 } elsif (/^!([^\t]*)\t\s*(.*)/) { 122 } elsif (/^!([^\t]*)\t\s*(.*)/) {
15 my @r; 124 my @r;
16 for (split /\s+/, $2) { 125 for (split /\s+/, $2) {
17 push @r, $group{$_} ? @{$group{$_}} : $_; 126 push @r, $group{$_} ? @{$group{$_}} : $_;
18 } 127 }
19 print "not($g) => (@r)\n"; 128 print "not($g) => (@r)\n";
129 push @{$blockuri{$_}}, $g for @r;
20 push @blocklist, [qr/$g/i, \@r]; 130 push @blockuri, [qr/$g/i, \@r];
21 } elsif (/\S/) { 131 } elsif (/\S/) {
22 print "blocklist: unparsable line: $_\n"; 132 print "blockuri: unparsable line: $_\n";
23 } 133 }
24 } 134 }
135 for (keys %blockuri) {
136 my $qr = join ")|(?:", @{$blockuri{$_}};
137 $blockuri{$_} = qr{(?:$qr)}i;
138 }
25 } else { 139 } else {
26 print "no blocklst\n"; 140 print "no blockuri\n";
27 } 141 }
28} 142}
29 143
30read_blocklist; 144sub read_blockref {
145 local *B;
146 my @blockref;
147 if (open B, "<blockreferer") {
148 while (<B>) {
149 chomp;
150 if (/^([^\t]*)\t\s*(.*)/) {
151 push @blockref, $1;
152 } elsif (/\S/) {
153 print "blockref: unparsable line: $_\n";
154 }
155 }
156 $blockref = join ")|(?:", @blockref;
157 $blockref = qr{^(?:$blockref)}i;
158 } else {
159 print "no blockref\n";
160 $blockref = qr{^x^};
161 }
162}
31 163
164read_blockuri;
165read_blockref;
166
167use Tie::Cache;
168tie %whois_cache, Tie::Cache::, 32;
169
32sub conn::access_check { 170sub access_check {
33 my $self = shift; 171 my $self = shift;
34 172
173 my $ref = $self->{h}{referer};
35 my $uri = $self->{path}; 174 my $uri = $self->{path};
36 my %disallow; 175 my %disallow;
37 176
38 for (@blocklist) { 177 $self->err_block_referer
39 if ($uri =~ $_->[0]) { 178 if $self->{h}{referer} =~ $blockref;
40 $disallow{$_}++ for @{$_->[1]}; 179
41 }
42 }
43
44 my $whois = ::ip_request($self->{remote_addr}); 180 my $whois = $whois_cache{$self->{remote_addr}}
181 ||= netgeo::ip_request($self->{remote_addr});
45 182
46 my $country = "XX"; 183 my $country = "XX";
47 184
48 if ($whois =~ /^\*cy: (\S+)/m) { 185 if ($whois =~ /^\*cy: (\S+)/m) {
49 $country = uc $1; 186 $country = uc $1;
50 } else { 187 } else {
51 $self->slog(9, "no country($whois)"); 188 $self->slog(9, "no country($whois)");
52 } 189 }
53 190
54 if ($disallow{$country}) { 191 $self->{country} = $country;
55 $whois =~ s/&/&amp;/g;
56 $whois =~ s/</&lt;/g;
57 $self->err(403, "forbidden", { "Content-Type" => "text/html" }, <<EOF);
58<html>
59<head>
60<title>This material is licensed in your country!</title>
61</head>
62<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000">
63 192
64<h1>This material is licensed in your country!</h1> 193 $self->err_block_country($whois)
65 194 if $self->{path} =~ $blockuri{$country};
66<p>My research has shown that your IP address
67(<b>$self->{remote_addr}</b>) most probably is located in this country:
68<b>$country</b> (ISO-3166-2 code, XX == unknown). The full record is:</p>
69
70<pre>
71$whois
72</pre>
73
74<p>My database says that the material you are trying to access is licensed
75in your country. If I would distribute these files to your country I would
76actively <em>hurt</em> the industry behind it, which includes the artists
77and authors of these videos/mangas. So I hope you understand that I try to
78avoid this.</p>
79
80<p>If you <em>really</em> think that this is wrong, i.e. the
81material you tried to access is <em>not</em> licensed in your
82country or your ip address was misdetected, you can write to <a
83href="mailto:licensed\@plan9.de">licensed\@plan9.de</a>. Please explain
84what happened and why you think this is wrong in as much detail as
85possible.</p>
86
87<div align="right">Thanks a lot for understanding.</div>
88
89</body>
90</html>
91EOF
92 }
93} 195}
94 196
951; 1971;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines