-
Notifications
You must be signed in to change notification settings - Fork 11
/
coralized-proxy.pl
executable file
·72 lines (63 loc) · 1.81 KB
/
coralized-proxy.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env perl
sub is_blocked {
my $host = shift;
if ($host =~ m/(?:
doubleclick\.net |
(?: untang|sohu)\.com
)$/x) {
print "$host Fnord\n";
return 1;
}
}
sub should_not_proxy {
my $host = shift;
$host =~ /(?:
\.nyud\.net |
( google-analytics
| google
| fooooo
| youtube
| googlesyndication
| flickr
| mac
)\.com
)$/x;
}
use HTTP::Proxy;
use HTTP::Proxy::HeaderFilter::simple;
my $verbose = 0;
my $proxy = HTTP::Proxy->new(
port => 3128,
keep_alive => 0,
max_clients => 100
);
$proxy->push_filter(
request =>HTTP::Proxy::HeaderFilter::simple->new(
sub {
my ( $self, $headers, $message ) = @_;
my $host = $message->uri()->host();
if (is_blocked($host)) {
print "!!! " . $message->uri . "\n" if $verbose;
$message->uri("file:///dev/null");
return;
}
unless ($message->uri()->port() != 80 || should_not_proxy($host)) {
$host .= ".nyud.net";
$message->uri()->host($host);
$headers->header(Host => $host);
print "==> " . $message->uri()->as_string . "\n" if $verbose;
}
}
),
response => HTTP::Proxy::HeaderFilter::simple->new(
sub {
my ($self, $headers, $message) = @_;
if ($message->code == 302) {
my $location = $headers->header('Location');
$location =~ s/\.nyud\.net//;
$headers->header(Location => $location);
}
}
)
);
$proxy->start;