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

Comparing cvsroot/Coro/myhttpd/access.pl (file contents):
Revision 1.4 by root, Tue Aug 14 04:33:58 2001 UTC vs.
Revision 1.19 by root, Mon Dec 3 04:57:22 2001 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines