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