|
|
1 | package transferqueue; |
1 | |
2 | |
2 | our @blocklist; |
3 | sub new { |
|
|
4 | my $class = shift; |
|
|
5 | bless { |
|
|
6 | slots => $_[0], |
|
|
7 | lastspb => 0, |
|
|
8 | }, $class; |
|
|
9 | } |
3 | |
10 | |
|
|
11 | sub 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 | |
|
|
30 | sub 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 | |
|
|
45 | sub sort { |
|
|
46 | $_[0]{wait} = [ |
|
|
47 | sort { $b->{spb} <=> $a->{spb} } |
|
|
48 | grep { $_ && ($_->{spb} = ($::NOW-$_->{time})/($_->{size}||1)), $_ } |
|
|
49 | @{$_[0]{wait}} |
|
|
50 | ]; |
|
|
51 | } |
|
|
52 | |
|
|
53 | sub waiters { |
|
|
54 | $_[0]->sort; |
|
|
55 | @{$_[0]{wait}}; |
|
|
56 | } |
|
|
57 | |
|
|
58 | package transfer; |
|
|
59 | |
|
|
60 | use Coro::Timer (); |
|
|
61 | |
|
|
62 | sub wake { |
|
|
63 | my $self = shift; |
|
|
64 | |
|
|
65 | $self->{alloc} = 1; |
|
|
66 | $self->{queue}{slots}--; |
|
|
67 | $self->{wake} and $self->{wake}->ready; |
|
|
68 | } |
|
|
69 | |
|
|
70 | sub 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 | |
|
|
83 | sub DESTROY { |
|
|
84 | my $self = shift; |
|
|
85 | |
|
|
86 | if ($self->{alloc}) { |
|
|
87 | $self->{queue}{slots}++; |
|
|
88 | $self->{queue}->wake_next; |
|
|
89 | } |
|
|
90 | } |
|
|
91 | |
|
|
92 | package conn; |
|
|
93 | |
|
|
94 | our %blockuri; |
|
|
95 | our $blockref; |
|
|
96 | |
4 | sub read_blocklist { |
97 | sub 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 | |
30 | read_blocklist; |
128 | sub 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 | |
|
|
148 | read_blockuri; |
|
|
149 | read_blockref; |
|
|
150 | |
|
|
151 | use Tie::Cache; |
|
|
152 | tie %whois_cache, Tie::Cache::, 32; |
|
|
153 | |
32 | sub conn::access_check { |
154 | sub 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; |
50 | } else { |
171 | } else { |
51 | $self->slog(9, "no country($whois)"); |
172 | $self->slog(9, "no country($whois)"); |
52 | } |
173 | } |
53 | |
174 | |
54 | if ($disallow{$country}) { |
175 | $self->{country} = $country; |
55 | $self->slog(6, "DISALLOW($uri,$country)"); |
|
|
56 | $whois =~ s/&/&/g; |
|
|
57 | $whois =~ s/</</g; |
|
|
58 | $self->err(403, "forbidden", { "Content-Type" => "text/html" }, <<EOF); |
|
|
59 | <html> |
|
|
60 | <head> |
|
|
61 | <title>This material is licensed in your country!</title> |
|
|
62 | </head> |
|
|
63 | <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000"> |
|
|
64 | |
176 | |
65 | <h1>This material is licensed in your country!</h1> |
177 | $self->err_block_country($whois) |
66 | |
178 | if $self->{path} =~ $blockuri{$country}; |
67 | <p>My research has shown that your IP address |
|
|
68 | (<b>$self->{remote_addr}</b>) most probably is located in this country: |
|
|
69 | <b>$country</b> (ISO-3166-2 code, XX == unknown). The full record is:</p> |
|
|
70 | |
|
|
71 | <pre> |
|
|
72 | $whois |
|
|
73 | </pre> |
|
|
74 | |
|
|
75 | <p>My database says that the material you are trying to access is licensed |
|
|
76 | in your country. If I would distribute these files to your country I would |
|
|
77 | actively <em>hurt</em> the industry behind it, which includes the artists |
|
|
78 | and authors of these videos/mangas. So I hope you understand that I try to |
|
|
79 | avoid this.</p> |
|
|
80 | |
|
|
81 | <p>If you <em>really</em> think that this is wrong, i.e. the |
|
|
82 | material you tried to access is <em>not</em> licensed in your |
|
|
83 | country or your ip address was misdetected, you can write to <a |
|
|
84 | href="mailto:licensed\@plan9.de">licensed\@plan9.de</a>. Please explain |
|
|
85 | what happened and why you think this is wrong in as much detail as |
|
|
86 | possible.</p> |
|
|
87 | |
|
|
88 | <div align="right">Thanks a lot for understanding.</div> |
|
|
89 | |
|
|
90 | </body> |
|
|
91 | </html> |
|
|
92 | EOF |
|
|
93 | } |
|
|
94 | } |
179 | } |
95 | |
180 | |
96 | 1; |
181 | 1; |