1 |
root |
1.11 |
package transferqueue; |
2 |
|
|
|
3 |
|
|
sub new { |
4 |
|
|
my $class = shift; |
5 |
|
|
bless { |
6 |
|
|
conns => $_[0], |
7 |
|
|
}, $class; |
8 |
|
|
} |
9 |
|
|
|
10 |
|
|
sub start_transfer { |
11 |
|
|
my $self = shift; |
12 |
|
|
|
13 |
|
|
my $trans = bless [ $self, $Coro::current ], transfer::; |
14 |
|
|
Scalar::Util::weaken($trans->[0]); |
15 |
|
|
|
16 |
|
|
push @{$self->{wait}}, $trans; |
17 |
root |
1.13 |
Scalar::Util::weaken($self->{wait}[-1]); |
18 |
root |
1.11 |
|
19 |
|
|
if (--$self->{conns} >= 0) { |
20 |
|
|
$self->wake_next; |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
$trans; |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
sub wake_next { |
27 |
|
|
my $self = shift; |
28 |
|
|
|
29 |
|
|
return unless $self->{conns} >= 0; |
30 |
|
|
|
31 |
root |
1.13 |
if (@{$self->{wait}}) { |
32 |
|
|
while() { |
33 |
|
|
my $transfer = shift @{$self->{wait}}; |
34 |
|
|
if ($transfer) { |
35 |
|
|
$transfer->wake; |
36 |
|
|
last; |
37 |
|
|
} |
38 |
|
|
} |
39 |
|
|
} |
40 |
root |
1.11 |
} |
41 |
|
|
|
42 |
|
|
sub waiters { |
43 |
|
|
map $_->[1], @{$_[0]{wait}}; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
package transfer; |
47 |
|
|
|
48 |
|
|
use Coro::Timer (); |
49 |
|
|
|
50 |
|
|
sub try { |
51 |
|
|
my $self = shift; |
52 |
|
|
my $timeout = Coro::Timer::timeout $_[0]; |
53 |
|
|
|
54 |
|
|
Coro::schedule; |
55 |
|
|
|
56 |
|
|
return $self->[2]; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
sub wake { |
60 |
|
|
my $self = shift; |
61 |
|
|
$self->[2] = 1; |
62 |
|
|
$self->[1]->ready; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
sub DESTROY { |
66 |
|
|
my $self = shift; |
67 |
|
|
$self->[0]{conns}++; |
68 |
|
|
$self->[0]->wake_next; |
69 |
|
|
} |
70 |
|
|
|
71 |
root |
1.8 |
package conn; |
72 |
root |
1.1 |
|
73 |
root |
1.8 |
our %blockuri; |
74 |
|
|
our $blockref; |
75 |
root |
1.1 |
|
76 |
root |
1.6 |
sub read_blockuri { |
77 |
root |
1.1 |
local *B; |
78 |
|
|
my %group; |
79 |
root |
1.8 |
%blockuri = (); |
80 |
root |
1.6 |
if (open B, "<blockuri") { |
81 |
root |
1.1 |
while (<B>) { |
82 |
|
|
chomp; |
83 |
|
|
if (/^group\s+(\S+)\s+(.*)/i) { |
84 |
|
|
$group{$1} = [split /\s+/, $2]; |
85 |
|
|
} elsif (/^!([^\t]*)\t\s*(.*)/) { |
86 |
|
|
my $g = $1; |
87 |
|
|
my @r; |
88 |
|
|
for (split /\s+/, $2) { |
89 |
|
|
push @r, $group{$_} ? @{$group{$_}} : $_; |
90 |
|
|
} |
91 |
|
|
print "not($g) => (@r)\n"; |
92 |
root |
1.8 |
push @{$blockuri{$_}}, $g for @r; |
93 |
root |
1.6 |
push @blockuri, [qr/$g/i, \@r]; |
94 |
root |
1.1 |
} elsif (/\S/) { |
95 |
root |
1.6 |
print "blockuri: unparsable line: $_\n"; |
96 |
root |
1.1 |
} |
97 |
|
|
} |
98 |
root |
1.8 |
for (keys %blockuri) { |
99 |
|
|
my $qr = join ")|(?:", @{$blockuri{$_}}; |
100 |
|
|
$blockuri{$_} = qr{(?:$qr)}i; |
101 |
|
|
} |
102 |
root |
1.1 |
} else { |
103 |
root |
1.6 |
print "no blockuri\n"; |
104 |
root |
1.1 |
} |
105 |
|
|
} |
106 |
|
|
|
107 |
root |
1.6 |
sub read_blockref { |
108 |
|
|
local *B; |
109 |
root |
1.8 |
my @blockref; |
110 |
root |
1.6 |
if (open B, "<blockreferer") { |
111 |
|
|
while (<B>) { |
112 |
|
|
chomp; |
113 |
|
|
if (/^([^\t]*)\t\s*(.*)/) { |
114 |
root |
1.8 |
push @blockref, $1; |
115 |
root |
1.6 |
} elsif (/\S/) { |
116 |
|
|
print "blockref: unparsable line: $_\n"; |
117 |
|
|
} |
118 |
|
|
} |
119 |
root |
1.8 |
$blockref = join ")|(?:", @blockref; |
120 |
|
|
$blockref = qr{^(?:$blockref)}i; |
121 |
root |
1.6 |
} else { |
122 |
|
|
print "no blockref\n"; |
123 |
root |
1.8 |
$blockref = qr{^x^}; |
124 |
root |
1.6 |
} |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
read_blockuri; |
128 |
|
|
read_blockref; |
129 |
root |
1.1 |
|
130 |
root |
1.5 |
use Tie::Cache; |
131 |
root |
1.10 |
tie %whois_cache, Tie::Cache::, 32; |
132 |
root |
1.1 |
|
133 |
root |
1.8 |
sub access_check { |
134 |
root |
1.6 |
my $self = shift; |
135 |
|
|
|
136 |
|
|
my $ref = $self->{h}{referer}; |
137 |
|
|
my $uri = $self->{path}; |
138 |
|
|
my %disallow; |
139 |
|
|
|
140 |
root |
1.8 |
$self->err_block_referer |
141 |
|
|
if $self->{h}{referer} =~ $blockref; |
142 |
root |
1.6 |
|
143 |
|
|
my $whois = $whois_cache{$self->{remote_addr}} |
144 |
root |
1.9 |
||= netgeo::ip_request($self->{remote_addr}); |
145 |
root |
1.6 |
|
146 |
|
|
my $country = "XX"; |
147 |
|
|
|
148 |
|
|
if ($whois =~ /^\*cy: (\S+)/m) { |
149 |
|
|
$country = uc $1; |
150 |
|
|
} else { |
151 |
|
|
$self->slog(9, "no country($whois)"); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
$self->{country} = $country; |
155 |
|
|
|
156 |
root |
1.8 |
$self->err_block_country($whois) |
157 |
|
|
if $self->{path} =~ $blockuri{$country}; |
158 |
root |
1.1 |
} |
159 |
|
|
|
160 |
|
|
1; |