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