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