|
|
1 | package transferqueue; |
1 | |
2 | |
2 | our @blocklist; |
3 | my @reserve = ( |
|
|
4 | [ 1_000_000, 1], |
|
|
5 | [ 10_000_000, 1], |
|
|
6 | ); |
3 | |
7 | |
|
|
8 | sub new { |
|
|
9 | my $class = shift; |
|
|
10 | bless { |
|
|
11 | slots => $_[0], |
|
|
12 | lastspb => 0, |
|
|
13 | avgspb => 0, |
|
|
14 | }, $class; |
|
|
15 | } |
|
|
16 | |
|
|
17 | sub 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 | |
|
|
36 | sub 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 | |
|
|
46 | sub 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 | |
|
|
69 | sub waiters { |
|
|
70 | $_[0]->sort; |
|
|
71 | @{$_[0]{wait}}; |
|
|
72 | } |
|
|
73 | |
|
|
74 | package transfer; |
|
|
75 | |
|
|
76 | use Coro::Timer (); |
|
|
77 | |
|
|
78 | sub wake { |
|
|
79 | my $self = shift; |
|
|
80 | |
|
|
81 | $self->{alloc} = 1; |
|
|
82 | $self->{queue}{slots}--; |
|
|
83 | $self->{wake} and $self->{wake}->ready; |
|
|
84 | } |
|
|
85 | |
|
|
86 | sub 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 | |
|
|
99 | sub DESTROY { |
|
|
100 | my $self = shift; |
|
|
101 | |
|
|
102 | if ($self->{alloc}) { |
|
|
103 | $self->{queue}{slots}++; |
|
|
104 | $self->{queue}->wake_next; |
|
|
105 | } |
|
|
106 | } |
|
|
107 | |
|
|
108 | package conn; |
|
|
109 | |
|
|
110 | our %blockuri; |
|
|
111 | our $blockref; |
|
|
112 | |
4 | sub read_blocklist { |
113 | sub 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 | |
30 | read_blocklist; |
144 | sub 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 | |
|
|
164 | read_blockuri; |
|
|
165 | read_blockref; |
|
|
166 | |
|
|
167 | use Tie::Cache; |
|
|
168 | tie %whois_cache, Tie::Cache::, 32; |
|
|
169 | |
32 | sub conn::access_check { |
170 | sub 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; |
… | |
… | |
51 | $self->slog(9, "no country($whois)"); |
188 | $self->slog(9, "no country($whois)"); |
52 | } |
189 | } |
53 | |
190 | |
54 | $self->{country} = $country; |
191 | $self->{country} = $country; |
55 | |
192 | |
56 | if ($disallow{$country}) { |
193 | $self->err_block_country($whois) |
57 | $self->slog(6, "DISALLOW($uri,$country)"); |
194 | if $self->{path} =~ $blockuri{$country}; |
58 | |
|
|
59 | $whois =~ s/&/&/g; |
|
|
60 | $whois =~ s/</</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 |
|
|
79 | in your country. If I would distribute these files to your country I would |
|
|
80 | actively <em>hurt</em> the industry behind it, which includes the artists |
|
|
81 | and authors of these videos/mangas. So I hope you understand that I try to |
|
|
82 | avoid this.</p> |
|
|
83 | |
|
|
84 | <p>If you <em>really</em> think that this is wrong, i.e. the |
|
|
85 | material you tried to access is <em>not</em> licensed in your |
|
|
86 | country or your ip address was misdetected, you can write to <a |
|
|
87 | href="mailto:licensed\@plan9.de">licensed\@plan9.de</a>. Please explain |
|
|
88 | what happened and why you think this is wrong in as much detail as |
|
|
89 | possible.</p> |
|
|
90 | |
|
|
91 | <div align="right">Thanks a lot for understanding.</div> |
|
|
92 | |
|
|
93 | </body> |
|
|
94 | </html> |
|
|
95 | EOF |
|
|
96 | } |
|
|
97 | } |
195 | } |
98 | |
196 | |
99 | 1; |
197 | 1; |