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