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 on returned value
     my $type = $proxytype->get($proxy2);
     if ($type == CONNECT_PROXY) {
            warn "$proxy2 is connect proxy";
     }
     elsif ($type == HTTPS_PROXY) {
            warn "$proxy2 is https proxy";
     }
     elsif($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 is connectable";
     }
     else {
            warn "can't connect to $proxy3";
     }

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, https proxy, connect 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
   https_strict         undef
   socks4_strict        undef
   socks5_strict        undef
   connect_strict       undef
   strict               undef
   url                  $Net::Proxy::Type::URL
   https_url            $Net::Proxy::Type::HTTPS_URL
   keyword              $Net::Proxy::Type::KEYWORD
   https_keyword        $Net::Proxy::Type::HTTPS_KEYWORD
   noauth               undef
   http_ver             $Net::Proxy::Type::HTTP_VER

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
   https_strict    - use or not strict method to check https proxies
   socks4_strict   - use or not strict method to check socks4 proxies
   socks5_strict   - use or not strict method to check socks5 proxies
   connect_strict  - use or not strict method to check connect proxies
   strict          - set value of all *_strict options above to this value (about strict checking see below)
   url             - url which response header should be checked for keyword when strict mode enabled (for all proxy types excluding HTTPS_PROXY)
   https_url       - url which response header should be checked for https_keyword when strict mode enabled (for HTTPS_PROXY only)
   keyword         - keyword which must be found in the respose header for url (for all types excluding HTTPS_PROXY)
   https_keyword   - keyword which must be found in the respose header for url (for HTTPS_PROXY only)
   noauth          - if proxy works, but authorization required, then false will be returned if noauth has true value
   http_ver        - http version which will be used in http request when strict mode is on (one of 0.9, 1.0, 1.1), default is 1.1
$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 (HTTPS_PROXY, HTTP_PROXY, CONNECT_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, HTTPS_PROXY or CONNECT_PROXY returned value will be UNKNOWN_PROXY
  # because there is no check for HTTP_PROXY, HTTPS_PROXY and CONNECT_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->get_all($proxyaddress, $checkmask=undef)
$proxytype->get_all($proxyhost, $proxyport, $checkmask=undef)

Same as get(), but will not stop checking after first found result. In scalar context returns integer value (found proxy types joined with binary OR (|) operator), so you can use binary AND (&) to find is this proxy of specified type. In list context additionally returns connection time as second element.

    my $type = $proxytype->get_all($host, $port);
    #  my ($type, $con_time) = $proxytype->get_all($host, $port);
    if ($type == DEAD_PROXY || $type == UNKNOWN_PROXY) {
        die "bad proxy";
    }
    
    while (my ($t, $n) = each %Net::Proxy::Type::NAME) {
        next if $t == DEAD_PROXY || $t == UNKNOWN_PROXY;
        if ($type & $t) {
            warn "this is ", $n, "\n";
        }
    }
$proxytype->get_all_as_string($proxyaddress, $checkmask=undef)
$proxytype->get_all_as_string($proxyhost, $proxyport, $checkmask=undef)

Same as get_all but always returns list with proxy types names.

$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_https($proxyaddress)
$proxytype->is_https($proxyhost, $proxyport)

Check is this is https proxy (http proxy which accepts CONNECT method). Returned value is 1 if it is https proxy, 0 if it is not https 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_connect($proxyaddress)
$proxytype->is_connect($proxyhost, $proxyport)

Check is this is conenct proxy (http proxy which accepts CONNECT method even for 80 port, so you can make direct traffic transfer). Returned value is 1 if it is connect proxy, 0 if it is not connect 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->https_strict
$proxytype->https_strict($boolean)
$proxytype->connect_strict
$proxytype->connect_strict($boolean)
$proxytype->socks4_strict
$proxytype->socks4_strict($boolean)
$proxytype->socks5_strict
$proxytype->socks5_strict($boolean)
$proxytype->url
$proxytype->url($url)
$proxytype->https_url
$proxytype->https_url($url)
$proxytype->keyword
$proxytype->keyword($keyword)
$proxytype->https_keyword
$proxytype->https_keyword($keyword)
$proxytype->noauth
$proxytype->noauth($boolean)
$proxytype->http_ver
$proxytype->http_ver($version)

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 (same for connect and https proxy). 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
HTTPS_PROXY
CONNECT_PROXY
SOCKS4_PROXY
SOCKS5_PROXY

Following variables available (not importable):

$CONNECT_TIMEOUT = 5
$WRITE_TIMEOUT = 5
$READ_TIMEOUT = 5
$URL = 'http://www.google.com/'
$HTTPS_URL = 'https://www.google.com/'
$KEYWORD = 'google'
$HTTPS_KEYWORD = 'google'
$HTTP_VER = '1.1'
%NAME

Dictionary between proxy type constant and proxy type name

COPYRIGHT

Copyright 2010-2014 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.