Perl Advent Calendar 2011-12-01

Socks on the fly

by Fayland Lam

去年的 Advent 我们讲过如何在 http/https 上铺上一层 Tor. 这次我们将更近一步,谈论如果在 Socket 上用上 socks proxy.

此次主角便是 IO::Socket::Socks::Wrapper,无需更改所需模块的源码,轻松简易加套。

如下例子是使用 Net::Whois::Raw

查看它的源码,可以发现在 sub whois_query 里有 IO::Socket::INET->new 链接。所以在该 Socket 上使用 socks proxy 就变得非常简单。

use strict;
use warnings;
use Net::Whois::Raw;
use IO::Socket::Socks::Wrapper (
    'Net::Whois::Raw::whois_query()' => {
        ProxyAddr => '127.0.0.1',
        ProxyPort => 7070,
    },
);

my $dominfo = whois('perlchina.org');
print $dominfo;

如果你有 monitor socks proxy 的工具,你就会发现该 whois query 是通过了本地的 7070 端口。

谢谢。

View Source (POD)