Net::Proxy::Type - Get proxy type
use strict; use Net::Proxy::Type; # get proxy type and print its name my $proxytype = Net::Proxy::Type->new(); my $type = $proxytype->get('localhost:1111'); warn 'proxy type is: ', $Net::Proxy::Type::NAME{$type}; # same as warn 'proxy type is: ', Net::Proxy::Type->new()->get_as_string('localhost:1111');
use strict; use Net::Proxy::Type ':types'; # import proxy type constants my $proxytype = Net::Proxy::Type->new(http_strict => 1); # strict check for http proxies - recommended my $proxy1 = 'localhost:1080'; my $proxy2 = 'localhost:8080'; my $proxy3 = 'localhost:3128'; # check each type separately if($proxytype->is_http($proxy1)) { warn "$proxy1 is http proxy"; } elsif($proxytype->is_socks4($proxy1)) { warn "$proxy1 is socks4 proxy"; } elsif($proxytype->is_socks5($proxy1)) { warn "$proxy1 is socks5 proxy"; } else { warn "$proxy1 is unknown proxy"; } # get proxy type and do something depending returned value my $type = $proxytype->get($proxy2); if($type == HTTP_PROXY) { warn "$proxy2 is http proxy"; } elsif($type == SOCKS4_PROXY) { warn "$proxy2 is socks4 proxy"; } elsif($type == SOCKS5_PROXY) { warn "$proxy2 is socks5 proxy"; } elsif($type == DEAD_PROXY) { warn "$proxy2 does not work"; } else { warn "$proxy2 is unknown proxy"; } # return value of the "checker" methods is: 1 if type corresponds, 0 if not, undef if proxy server not connectable my $rv = $proxytype->is_http($proxy3); if($rv) { warn "$proxy3 is http proxy"; } elsif(defined($rv)) { warn "$proxy3 is not http proxy, but it works"; } else { warn "$proxy3 doesn't work"; }
The Net::Proxy::Type is a module which can help you to get proxy type if you know host and port of the proxy server. Supported proxy types for now are: http proxy, socks4 proxy and socks5 proxy.
Net::Proxy::Type
This method constructs new Net::Proxy::Type object. Key / value pairs can be passed as an argument to specify the initial state. The following options correspond to attribute methods described below:
KEY DEFAULT ----------- ----------------------------------- connect_timeout $Net::Proxy::Type::CONNECT_TIMEOUT write_timeout $Net::Proxy::Type::WRITE_TIMEOUT read_timeout $Net::Proxy::Type::READ_TIMEOUT timeout undef http_strict undef socks4_strict undef socks5_strict undef strict undef url $Net::Proxy::Type::URL keyword $Net::Proxy::Type::KEYWORD noauth undef
Description:
connect_timeout - maximum number of seconds to wait until connection success write_timeout - maximum number of seconds to wait until write operation success read_timeout - maximum number of seconds to wait until read operation success timeout - set value of all *_timeout options above to this value http_strict - use or not strict method to check http proxies socks4_strict - use or not strict method to check socks4 proxies socks5_strict - use or not strict method to check socks5 proxies strict - set value of all *_strict options above to this value (about strict checking see below) url - url which header should be checked for keyword when strict mode enabled keyword - keyword which must be found in the url header noauth - if proxy works, but authorization required, then false will be returned if noauth has true value
Get proxy type. Checkmask allows to check proxy only for specified types, its value can be any combination of the valid proxy types constants (HTTP_PROXY, SOCKS4_PROXY, SOCKS5_PROXY for now), joined with the binary OR (|) operator. Will check for all types if mask not defined. In scalar context returned value is proxy type - one of the module constants descibed below. In list context returned value is an array with proxy type as first element and connect time in seconds as second.
Example:
# check only for socks type # if it is HTTP_PROXY returned value will be UNKNOWN_PROXY # because there is no check for HTTP_PROXY my $type = $proxytype->get('localhost:1080', SOCKS4_PROXY | SOCKS5_PROXY);
Same as get(), but returns string instead of constant. In all contexts returns only one value.
Check is this is http proxy. Returned value is 1 if it is http proxy, 0 if it is not http proxy and undef if proxy host not connectable or proxy address is not valid. In list context returns array where second element is connect time (empty array if proxy not connectable).
Check is this is socks4 proxy. Returned value is 1 if it is socks4 proxy, 0 if it is not socks4 proxy and undef if proxy host not connectable or proxy address is not valid. In list context returns array where second element is connect time (empty array if proxy not connectable).
Check is this is socks5 proxy. Returned value is 1 if it is socks5 proxy, 0 if it is not socks5 proxy and undef if proxy host not connectable or proxy address is not valid. In list context returns array where second element is connect time (empty array if proxy not connectable).
Set timeout for all operations. See constructor options description above
Set or unset strict checking mode. See constructor options description above
Methods below gets or sets corresponding options from the constructor:
How this module works? To check proxy type it simply do some request to the proxy server and checks response. Each proxy type has its own response type. For socks proxies we can do socks initialize request and response should be as its described in socks proxy documentation. For http proxies we can do http request to some host and check for example if response begins from `HTTP'. Problem is that if we, for example, will check `yahoo.com:80' for http proxy this way, we will get positive response, but `yahoo.com' is not a proxy it is a web server. So strict checking helps us to avoid this problems. What we do? We send http request to the server, specified by the `url' option in the constructor via proxy and checks if response header contains keyword, specified by `keyword' option. If there is no keyword in the header it means that this proxy is not of the cheking type. This is not best solution, but it works. So strict mode recommended to check http proxies if you want to cut off such "proxies" as `yahoo.com:80', but you can use it with other proxy types too.
Following proxy type constants available and could be imported separately or together with `:types' tag:
Following variables available (not importable):
Dictionary between proxy type constant and proxy type name
Copyright 2010-2011 Oleg G <oleg@cpan.org>.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Net::Proxy::Type, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::Proxy::Type
CPAN shell
perl -MCPAN -e shell install Net::Proxy::Type
For more information on module installation, please visit the detailed CPAN module installation guide.