1 |
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 ], transfer::; |
14 |
Scalar::Util::weaken($trans->[0]); |
15 |
|
16 |
push @{$self->{wait}}, $trans; |
17 |
Scalar::Util::weaken($self->{wait}[-1]); |
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 |
if ($self->{conns} >= 0) { |
30 |
while(@{$self->{wait}}) { |
31 |
my $transfer = shift @{$self->{wait}}; |
32 |
if ($transfer) { |
33 |
$transfer->wake; |
34 |
last; |
35 |
} |
36 |
} |
37 |
} |
38 |
} |
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 |
unless ($self->[2]) { |
53 |
local $self->[1] = $Coro::current; |
54 |
Coro::schedule; |
55 |
} |
56 |
|
57 |
return $self->[2]; |
58 |
} |
59 |
|
60 |
sub wake { |
61 |
my $self = shift; |
62 |
$self->[2] = 1; |
63 |
ref $self->[1] and $self->[1]->ready; |
64 |
} |
65 |
|
66 |
sub DESTROY { |
67 |
my $self = shift; |
68 |
$self->[0]{conns}++; |
69 |
$self->[0]->wake_next; |
70 |
} |
71 |
|
72 |
package conn; |
73 |
|
74 |
our %blockuri; |
75 |
our $blockref; |
76 |
|
77 |
sub read_blockuri { |
78 |
local *B; |
79 |
my %group; |
80 |
%blockuri = (); |
81 |
if (open B, "<blockuri") { |
82 |
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 |
push @{$blockuri{$_}}, $g for @r; |
94 |
push @blockuri, [qr/$g/i, \@r]; |
95 |
} elsif (/\S/) { |
96 |
print "blockuri: unparsable line: $_\n"; |
97 |
} |
98 |
} |
99 |
for (keys %blockuri) { |
100 |
my $qr = join ")|(?:", @{$blockuri{$_}}; |
101 |
$blockuri{$_} = qr{(?:$qr)}i; |
102 |
} |
103 |
} else { |
104 |
print "no blockuri\n"; |
105 |
} |
106 |
} |
107 |
|
108 |
sub read_blockref { |
109 |
local *B; |
110 |
my @blockref; |
111 |
if (open B, "<blockreferer") { |
112 |
while (<B>) { |
113 |
chomp; |
114 |
if (/^([^\t]*)\t\s*(.*)/) { |
115 |
push @blockref, $1; |
116 |
} elsif (/\S/) { |
117 |
print "blockref: unparsable line: $_\n"; |
118 |
} |
119 |
} |
120 |
$blockref = join ")|(?:", @blockref; |
121 |
$blockref = qr{^(?:$blockref)}i; |
122 |
} else { |
123 |
print "no blockref\n"; |
124 |
$blockref = qr{^x^}; |
125 |
} |
126 |
} |
127 |
|
128 |
read_blockuri; |
129 |
read_blockref; |
130 |
|
131 |
use Tie::Cache; |
132 |
tie %whois_cache, Tie::Cache::, 32; |
133 |
|
134 |
sub access_check { |
135 |
my $self = shift; |
136 |
|
137 |
my $ref = $self->{h}{referer}; |
138 |
my $uri = $self->{path}; |
139 |
my %disallow; |
140 |
|
141 |
$self->err_block_referer |
142 |
if $self->{h}{referer} =~ $blockref; |
143 |
|
144 |
my $whois = $whois_cache{$self->{remote_addr}} |
145 |
||= netgeo::ip_request($self->{remote_addr}); |
146 |
|
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 |
$self->err_block_country($whois) |
158 |
if $self->{path} =~ $blockuri{$country}; |
159 |
} |
160 |
|
161 |
1; |