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