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