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, $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 |
package conn; |
64 |
|
65 |
our %blockuri; |
66 |
our $blockref; |
67 |
|
68 |
sub read_blockuri { |
69 |
local *B; |
70 |
my %group; |
71 |
%blockuri = (); |
72 |
if (open B, "<blockuri") { |
73 |
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 |
push @{$blockuri{$_}}, $g for @r; |
85 |
push @blockuri, [qr/$g/i, \@r]; |
86 |
} elsif (/\S/) { |
87 |
print "blockuri: unparsable line: $_\n"; |
88 |
} |
89 |
} |
90 |
for (keys %blockuri) { |
91 |
my $qr = join ")|(?:", @{$blockuri{$_}}; |
92 |
$blockuri{$_} = qr{(?:$qr)}i; |
93 |
} |
94 |
} else { |
95 |
print "no blockuri\n"; |
96 |
} |
97 |
} |
98 |
|
99 |
sub read_blockref { |
100 |
local *B; |
101 |
my @blockref; |
102 |
if (open B, "<blockreferer") { |
103 |
while (<B>) { |
104 |
chomp; |
105 |
if (/^([^\t]*)\t\s*(.*)/) { |
106 |
push @blockref, $1; |
107 |
} elsif (/\S/) { |
108 |
print "blockref: unparsable line: $_\n"; |
109 |
} |
110 |
} |
111 |
$blockref = join ")|(?:", @blockref; |
112 |
$blockref = qr{^(?:$blockref)}i; |
113 |
} else { |
114 |
print "no blockref\n"; |
115 |
$blockref = qr{^x^}; |
116 |
} |
117 |
} |
118 |
|
119 |
read_blockuri; |
120 |
read_blockref; |
121 |
|
122 |
use Tie::Cache; |
123 |
tie %whois_cache, Tie::Cache::, 32; |
124 |
|
125 |
sub access_check { |
126 |
my $self = shift; |
127 |
|
128 |
my $ref = $self->{h}{referer}; |
129 |
my $uri = $self->{path}; |
130 |
my %disallow; |
131 |
|
132 |
$self->err_block_referer |
133 |
if $self->{h}{referer} =~ $blockref; |
134 |
|
135 |
my $whois = $whois_cache{$self->{remote_addr}} |
136 |
||= netgeo::ip_request($self->{remote_addr}); |
137 |
|
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 |
$self->err_block_country($whois) |
149 |
if $self->{path} =~ $blockuri{$country}; |
150 |
} |
151 |
|
152 |
1; |