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