Net-Proxy-Type

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

0.08 Sat Jan  4 18:21:30 NOVT 2014
      - add connect proxy support
      - add get_all() and get_all_as_string() methods

0.07 Mon Dec 3 22:30:31 2012
      - fix for possible SIGPIPE
      - fix for strict socks5 check
      - check $! for EAGAIN in addition to EWOULDBLOCK

0.06 Sun Oct 21 14:41:08 2012
      - fix for `noauth' option for HTTP_PROXY
      - HTTPS_PROXY type added
      - `http_ver' option and $HTTP_VER variable added

0.05 Thu Jun 14 17:24:32 2012
      - fix for qw() deprication warning (rt #77815)

0.04 Thu Jul 14 23:15:03 2011
      - return connection time in list context
      - more tests added

lib/Net/Proxy/Type.pm  view on Meta::CPAN

use strict;
use Exporter;
use Errno qw(EWOULDBLOCK EAGAIN);
use Carp;
use IO::Socket::INET qw(:DEFAULT :crlf);
use IO::Select;

use constant {
	UNKNOWN_PROXY => 4294967296,
	DEAD_PROXY    => 0,
	HTTP_PROXY    => 1,
	SOCKS4_PROXY  => 2,
	SOCKS5_PROXY  => 4,
	HTTPS_PROXY   => 8,
	CONNECT_PROXY => 16,
};

our $VERSION = '0.09';
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(HTTP_PROXY HTTPS_PROXY CONNECT_PROXY SOCKS4_PROXY SOCKS5_PROXY UNKNOWN_PROXY DEAD_PROXY);
our %EXPORT_TAGS = (types => [qw(HTTP_PROXY HTTPS_PROXY CONNECT_PROXY SOCKS4_PROXY SOCKS5_PROXY UNKNOWN_PROXY DEAD_PROXY)]);

our $CONNECT_TIMEOUT = 5;
our $WRITE_TIMEOUT = 5;
our $READ_TIMEOUT = 5;
our $URL = 'http://www.google.com/';
our $HTTPS_URL = 'https://www.google.com/';
our $KEYWORD = 'google';
our $HTTPS_KEYWORD = 'google';
our $HTTP_VER = '1.1';
our %NAME = (
	UNKNOWN_PROXY, 'UNKNOWN_PROXY',
	DEAD_PROXY, 'DEAD_PROXY',
	HTTP_PROXY, 'HTTP_PROXY',
	HTTPS_PROXY, 'HTTPS_PROXY',
	CONNECT_PROXY, 'CONNECT_PROXY',
	SOCKS4_PROXY, 'SOCKS4_PROXY',
	SOCKS5_PROXY, 'SOCKS5_PROXY',
);

sub new
{
	my ($class, %opts) = @_;
	my $self = bless {}, $class;

lib/Net/Proxy/Type.pm  view on Meta::CPAN

			or croak('Incorrect url specified. Should be https://[^:/]+(/.*)?');
		return $self->{https_url} = $_[0];
	}
	
	return $self->{https_url};
}

my @checkers = (
	CONNECT_PROXY, \&is_connect,
	HTTPS_PROXY, \&is_https,
	HTTP_PROXY, \&is_http,
	SOCKS4_PROXY, \&is_socks4,
	SOCKS5_PROXY, \&is_socks5
);

sub _get
{ # base get method
	my $self = shift;
	my $max  = pop;
	my ($proxyaddr, $proxyport, $checkmask);
	my @found;

lib/Net/Proxy/Type.pm  view on Meta::CPAN

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

lib/Net/Proxy/Type.pm  view on Meta::CPAN

   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

=item $proxytype->get($proxyaddress, $checkmask=undef)

=item $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);

=item $proxytype->get_as_string($proxyaddress, $checkmask=undef)

=item $proxytype->get_as_string($proxyhost, $proxyport, $checkmask=undef)

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

=item $proxytype->get_all($proxyaddress, $checkmask=undef)

lib/Net/Proxy/Type.pm  view on Meta::CPAN

=head1 PACKAGE CONSTANTS AND VARIABLES

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

=over

=item UNKNOWN_PROXY

=item DEAD_PROXY

=item HTTP_PROXY

=item HTTPS_PROXY

=item CONNECT_PROXY

=item SOCKS4_PROXY

=item SOCKS5_PROXY

=back

t/01_Net-Proxy-Type.t  view on Meta::CPAN

	or die $@;
my ($host, $port) = ($sock->sockhost eq "0.0.0.0" ? "127.0.0.1" : $sock->sockhost, $sock->sockport);
$sock->close();
is($pt->get($host, $port), Net::Proxy::Type::DEAD_PROXY, "DEAD_PROXY test");
my ($type, $conn_time) = $pt->get($host, $port);
is($type, Net::Proxy::Type::DEAD_PROXY, "DEAD_PROXY in list context test");
is($conn_time, 0, "DEAD_PROXY conn time");

my $pid;
($pid, $host, $port) = make_fake_http_proxy();
is($pt->is_http($host, $port), 1, 'HTTP_PROXY');
is($pt->is_https($host, $port), 0, 'Not HTTPS_PROXY');
$pt->strict(1);
is($pt->get($host, $port), Net::Proxy::Type::HTTP_PROXY, 'get for HTTP_PROXY');
kill 15, $pid;

($pid, $host, $port) = make_fake_https_proxy(0);
$pt->https_strict(0);
$pt->timeout(3);
is($pt->get($host, $port), Net::Proxy::Type::HTTPS_PROXY, 'non strict get for HTTPS_PROXY');
diag "next test will take about 10 sec";
$pt->strict(1);
is($pt->get($host, $port), Net::Proxy::Type::UNKNOWN_PROXY, 'strict get for HTTPS_PROXY');
$pt->strict(0);

t/02_real_proxy.t  view on Meta::CPAN

is($pt->is_socks5("$ENV{TEST_PROXY_HOST}:1080"), 1, 'is socks5');
is($pt->is_http("$ENV{TEST_PROXY_HOST}:1080"), 0, 'is http for socks');
is($pt->is_https("$ENV{TEST_PROXY_HOST}:1080"), 0, 'is https for socks');
is($pt->get("$ENV{TEST_PROXY_HOST}:3128"), Net::Proxy::Type::CONNECT_PROXY, 'get for http(s)');
is($pt->is_https("$ENV{TEST_PROXY_HOST}:3128"), 1, 'is https');
is($pt->is_http("$ENV{TEST_PROXY_HOST}:3128"), 1, 'is http');
is($pt->is_socks4("$ENV{TEST_PROXY_HOST}:3128"), 0, 'is socks4 for http(s)');
is($pt->is_socks5("$ENV{TEST_PROXY_HOST}:3128"), 0, 'is socks5 for http(s)');
$pt->strict(0);
my $types = $pt->get_all($ENV{TEST_PROXY_HOST}, 3128);
is($types, HTTP_PROXY|HTTPS_PROXY|CONNECT_PROXY, 'get_all for http(s) proxy');
$types = $pt->get_all($ENV{TEST_PROXY_HOST}, 1080);
is($types, SOCKS4_PROXY|SOCKS5_PROXY, 'get all for socks proxy');

__END__

3proxy settings:
socks
proxy



( run in 2.354 seconds using v1.01-cache-2.11-cpan-71847e10f99 )