The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Net::Proxy::Type - Get proxy type

SYNOPSIS

     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";
     }

DESCRIPTION

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.

METHODS

Net::Proxy::Type->new( %options )

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
$proxytype->get($proxyaddress, $checkmask=undef)
$proxytype->get($proxyhost, $proxyport, $checkmask=undef)

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);
$proxytype->get_as_string($proxyaddress, $checkmask=undef)
$proxytype->get_as_string($proxyhost, $proxyport, $checkmask=undef)

Same as get(), but returns string instead of constant. In all contexts returns only one value.

$proxytype->is_http($proxyaddress)
$proxytype->is_http($proxyhost, $proxyport)

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).

$proxytype->is_socks4($proxyaddress)
$proxytype->is_socks4($proxyhost, $proxyport)

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).

$proxytype->is_socks5($proxyaddress)
$proxytype->is_socks5($proxyhost, $proxyport)

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).

$proxytype->timeout($timeout)

Set timeout for all operations. See constructor options description above

$proxytype->strict($boolean)

Set or unset strict checking mode. See constructor options description above

Methods below gets or sets corresponding options from the constructor:

$proxytype->connect_timeout
$proxytype->connect_timeout($timeout)
$proxytype->read_timeout
$proxytype->read_timeout($timeout)
$proxytype->write_timeout
$proxytype->write_timeout($timeout)
$proxytype->http_strict
$proxytype->http_strict($boolean)
$proxytype->socks4_strict
$proxytype->socks4_strict($boolean)
$proxytype->socks5_strict
$proxytype->socks5_strict($boolean)
$proxytype->url($url)
$proxytype->keyword($keyword)
$proxytype->noauth($boolean)

STRICT CHECKING

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.

PACKAGE CONSTANTS AND VARIABLES

Following proxy type constants available and could be imported separately or together with `:types' tag:

UNKNOWN_PROXY
DEAD_PROXY
HTTP_PROXY
SOCKS4_PROXY
SOCKS5_PROXY

Following variables available (not importable):

$CONNECT_TIMEOUT = 5
$WRITE_TIMEOUT = 5
$READ_TIMEOUT = 5
$URL = 'http://www.google.com/'
$KEYWORD = 'google'
%NAME

Dictionary between proxy type constant and proxy type name

COPYRIGHT

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.