| 1 | package Net::Async::SOCKS; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: basic SOCKS5 connection support for IO::Async | ||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | Net::Async::SOCKS - some degree of SOCKS5 proxy support in L<IO::Async> | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Version 0.002 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Currently provides a very basic implementation of SOCKS_connect: | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | $loop->connect( | ||||||||||||||||||||||||||||
| 21 | extensions => [qw(SOCKS)], | ||||||||||||||||||||||||||||
| 22 | SOCKS_host => 'localhost', | ||||||||||||||||||||||||||||
| 23 | SOCKS_port => 1080, | ||||||||||||||||||||||||||||
| 24 | host => '1.2.3.4', | ||||||||||||||||||||||||||||
| 25 | port => 80, | ||||||||||||||||||||||||||||
| 26 | )->then(sub { | ||||||||||||||||||||||||||||
| 27 | my ($stream) = @_; | ||||||||||||||||||||||||||||
| 28 | $stream->write("GET / HTTP/1.1..."); | ||||||||||||||||||||||||||||
| 29 | }) | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use Protocol::SOCKS::Client; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use Protocol::SOCKS::Constants qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | use IO::Async::Loop; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | use IO::Async::Stream; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 METHODS | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | The following methods are added to L<IO::Async::Loop> | ||||||||||||||||||||||||||||
| 42 | but are not intended to be called directly - use the | ||||||||||||||||||||||||||||
| 43 | extensions feature instead. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head2 SOCKS_connect | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Establish a TCP connection via SOCKS5 proxy. | ||||||||||||||||||||||||||||
| 50 | Only allows IPv4 host and numerical port for now. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =cut | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub IO::Async::Loop::SOCKS_connect { | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | my ($loop, %params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
SOCKS_connect: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my %socks_params = map { /^SOCKS_(.*)$/ ? ($1 => delete $params{$_}) : () } keys %params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Start with the usual boilerplate to Future-wrap things and apply our initial stream handle | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | my $on_done; | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | if(exists $params{on_connected}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 63 | 0 | my $on_connected = delete $params{on_connected}; | |||||||||||||||||||||||||||
| 64 | $on_done = sub { | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | my ( $stream ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 66 | 0 | $on_connected->( $stream->read_handle ); | |||||||||||||||||||||||||||
| 67 | 0 | }; | |||||||||||||||||||||||||||
| 68 | } elsif( exists $params{on_stream} ) { | ||||||||||||||||||||||||||||
| 69 | 0 | my $on_stream = delete $params{on_stream}; | |||||||||||||||||||||||||||
| 70 | 0 | $on_done = $on_stream; | |||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | croak "Expected 'on_connected' or 'on_stream' or to return a Future" unless defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | my $on_socks_error = delete $params{on_socks_error} or defined wantarray or | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | croak "Expected 'on_socks_error' or to return a Future"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
my $on_socks_error = delete $params{"on_socks_error"} or defined wantarray
MC/DC0%0/2
my $on_socks_error = delete $params{"on_socks_error"} or defined wantarray
my $on_socks_error = delete $params{"on_socks_error"}
defined wantarray
Truth table0%0/3
A or B
A= my $on_socks_error = delete $params{"on_socks_error"}B= defined wantarray
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $stream = delete $params{handle} || IO::Async::Stream->new; | ||||||||||||||||||||||||||
|
Condition0%0/3
delete $params{'handle'} || 'IO::Async::Stream'->new
MC/DC0%0/2
delete $params{'handle'} || 'IO::Async::Stream'->new
delete $params{'handle'}
'IO::Async::Stream'->new
Truth table0%0/3
A || B
A= delete $params{'handle'}B= 'IO::Async::Stream'->new
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $stream->isa( "IO::Async::Stream" ) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | croak "Can only SOCKS_connect a handle instance of IO::Async::Stream"; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Now we begin the SOCKS negotiation dance. | ||||||||||||||||||||||||||||
| 84 | # * Connect to SOCKS5 host:port | ||||||||||||||||||||||||||||
| 85 | # * Authenticate if required | ||||||||||||||||||||||||||||
| 86 | # * Send connect request | ||||||||||||||||||||||||||||
| 87 | # * Accept server response - this may have the proxied server endpoint, | ||||||||||||||||||||||||||||
| 88 | # in practice (ssh socks5) the endpoint could be blank | ||||||||||||||||||||||||||||
| 89 | # * use this stream for TCP traffic | ||||||||||||||||||||||||||||
| 90 | 0 | my $f; | |||||||||||||||||||||||||||
| 91 | $f = $loop->connect( | ||||||||||||||||||||||||||||
| 92 | # The ->connect API has many ways of specifying | ||||||||||||||||||||||||||||
| 93 | # the connection endpoint. More may be added in | ||||||||||||||||||||||||||||
| 94 | # future. This makes passing the original parameters | ||||||||||||||||||||||||||||
| 95 | # risky - they might override the SOCKS host/port | ||||||||||||||||||||||||||||
| 96 | # details. | ||||||||||||||||||||||||||||
| 97 | socktype => 'stream', | ||||||||||||||||||||||||||||
| 98 | host => delete $socks_params{host}, | ||||||||||||||||||||||||||||
| 99 | service => delete $socks_params{port} || 1080, | ||||||||||||||||||||||||||||
| 100 | )->then(sub { | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my ($sock) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # We're delegating most of the real work to the protocol here | ||||||||||||||||||||||||||||
| 104 | my $proto = Protocol::SOCKS::Client->new( | ||||||||||||||||||||||||||||
| 105 | version => 5, | ||||||||||||||||||||||||||||
| 106 | writer => sub { | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $sock->write(shift) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 108 | }, | ||||||||||||||||||||||||||||
| 109 | 0 | %socks_params, | |||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # Proxy any read traffic into our protocol handler. | ||||||||||||||||||||||||||||
| 113 | # Once we've negotiated the stream, we'll take over. | ||||||||||||||||||||||||||||
| 114 | $stream->configure( | ||||||||||||||||||||||||||||
| 115 | handle => $sock, | ||||||||||||||||||||||||||||
| 116 | on_read => sub { | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | my ($stream, $buffref, $eof) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 118 | 0 | $proto->on_read($buffref); | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | if($eof) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | $stream->close; | |||||||||||||||||||||||||||
| 121 | 0 | $f->fail('connect' => 'something broke'); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 0 | ); | |||||||||||||||||||||||||||
| 125 | 0 | $loop->add($stream); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Version and auth header goes first | ||||||||||||||||||||||||||||
| 128 | 0 | $stream->write($proto->init_packet); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Push the read handler - if we were | ||||||||||||||||||||||||||||
| 131 | # given a prefab stream there may be a | ||||||||||||||||||||||||||||
| 132 | # read handler in place already | ||||||||||||||||||||||||||||
| 133 | # We're ready now - let's start the auth | ||||||||||||||||||||||||||||
| 134 | # process. | ||||||||||||||||||||||||||||
| 135 | $proto->auth( | ||||||||||||||||||||||||||||
| 136 | )->then(sub { | ||||||||||||||||||||||||||||
| 137 | $proto->connect( | ||||||||||||||||||||||||||||
| 138 | ATYPE_IPV4, | ||||||||||||||||||||||||||||
| 139 | $params{host}, | ||||||||||||||||||||||||||||
| 140 | $params{service}, | ||||||||||||||||||||||||||||
| 141 | )->transform( | ||||||||||||||||||||||||||||
| 142 | done => sub { | ||||||||||||||||||||||||||||
| 143 | 0 | ▶ | $loop->remove($stream); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 144 | 0 | $stream | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | ) | ||||||||||||||||||||||||||||
| 147 | 0 | ▶ | }) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | }); | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $socks_params{'port'} || 1080
MC/DC0%0/1
delete $socks_params{'port'} || 1080
delete $socks_params{'port'}
Truth table0%0/2
A
A= delete $socks_params{'port'}
| |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | $f->on_done($on_done) if $on_done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | $f->on_fail(sub { | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | $on_socks_error->($_[0]) if defined $_[1] and $_[1] eq "socks"; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
Condition0%0/3
defined $_[1] and $_[1] eq "socks"
MC/DC0%0/2
defined $_[1] and $_[1] eq "socks"
defined $_[1]
$_[1] eq "socks"
Truth table0%0/3
A and B
A= defined $_[1]B= $_[1] eq "socks"
| |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | }) if $on_socks_error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | $f->on_ready(sub { undef $f });# unless defined wantarray; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 155 | 0 | return $f; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 1 | 1; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | __END__ | ||||||||||||||||||||||||||||