1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
package Mojo::IOLoop::Client;
use Mojo::Base 'Mojo::EventEmitter';
use Errno 'EINPROGRESS';
use IO::Socket::INET;
use Mojo::IOLoop;
use Scalar::Util 'weaken';
use Socket qw(IPPROTO_TCP SO_ERROR TCP_NODELAY);
# IPv6 support requires IO::Socket::IP
use constant IPV6 => $ENV{MOJO_NO_IPV6}
? 0
: eval 'use IO::Socket::IP 0.20 (); 1';
# TLS support requires IO::Socket::SSL
use constant TLS => $ENV{MOJO_NO_TLS}
? 0
: eval 'use IO::Socket::SSL 1.84 (); 1';
use constant TLS_READ => TLS ? IO::Socket::SSL::SSL_WANT_READ() : 0;
use constant TLS_WRITE => TLS ? IO::Socket::SSL::SSL_WANT_WRITE() : 0;
# SOCKS support requires IO::Socket::Socks
use constant SOCKS => $ENV{MOJO_NO_SOCKS}
? 0
: eval 'use IO::Socket::Socks 0.64 (); 1';
use constant SOCKS_READ => SOCKS ? IO::Socket::Socks::SOCKS_WANT_READ() : 0;
use constant SOCKS_WRITE => SOCKS ? IO::Socket::Socks::SOCKS_WANT_WRITE() : 0;
has reactor => sub { Mojo::IOLoop->singleton->reactor };
sub DESTROY { shift->_cleanup }
sub connect {
my $self = shift;
my $args = ref $_[0] ? $_[0] : {@_};
weaken $self;
$self->reactor->next_tick(sub { $self && $self->_connect($args) });
}
sub _cleanup {
my $self = shift;
return $self unless my $reactor = $self->reactor;
$self->{$_} && $reactor->remove(delete $self->{$_}) for qw(timer handle);
return $self;
}
sub _connect {
my ($self, $args) = @_;
my $handle;
my $reactor = $self->reactor;
my $address = $args->{socks_address} || ($args->{address} ||= 'localhost');
my $port = $args->{socks_port} || $args->{port} || ($args->{tls} ? 443 : 80);
unless ($handle = $self->{handle} = $args->{handle}) {
my %options = (
Blocking => 0,
PeerAddr => $address eq 'localhost' ? '127.0.0.1' : $address,
PeerPort => $port
);
$options{LocalAddr} = $args->{local_address} if $args->{local_address};
$options{PeerAddr} =~ s/[\[\]]//g if $options{PeerAddr};
my $class = IPV6 ? 'IO::Socket::IP' : 'IO::Socket::INET';
return $self->emit(error => "Can't connect: $@")
unless $self->{handle} = $handle = $class->new(%options);
}
$handle->blocking(0);
# Timeout
$self->{timer} = $reactor->timer($args->{timeout} || 10,
sub { $self->emit(error => 'Connect timeout') });
# Wait for handle to become writable
weaken $self;
$reactor->io($handle => sub { $self->_ready($args) })->watch($handle, 0, 1);
}
sub _ready {
my ($self, $args) = @_;
# Retry or handle exceptions
my $handle = $self->{handle};
return $! == EINPROGRESS ? undef : $self->emit(error => $!)
if $handle->isa('IO::Socket::IP') && !$handle->connect;
return $self->emit(error => $! = $handle->sockopt(SO_ERROR))
unless $handle->connected;
# Disable Nagle's algorithm
setsockopt $handle, IPPROTO_TCP, TCP_NODELAY, 1;
$self->_try_socks($args);
}
sub _socks {
my ($self, $args) = @_;
# Connected
my $handle = $self->{handle};
return $self->_try_tls($args) if $handle->ready;
# Switch between reading and writing
my $err = $IO::Socket::Socks::SOCKS_ERROR;
if ($err == SOCKS_READ) { $self->reactor->watch($handle, 1, 0) }
elsif ($err == SOCKS_WRITE) { $self->reactor->watch($handle, 1, 1) }
else { $self->emit(error => $err) }
}
sub _tls {
my $self = shift;
# Connected
my $handle = $self->{handle};
return $self->_cleanup->emit(connect => $handle) if $handle->connect_SSL;
# Switch between reading and writing
my $err = $IO::Socket::SSL::SSL_ERROR;
if ($err == TLS_READ) { $self->reactor->watch($handle, 1, 0) }
elsif ($err == TLS_WRITE) { $self->reactor->watch($handle, 1, 1) }
}
sub _try_socks {
my ($self, $args) = @_;
my $handle = $self->{handle};
return $self->_try_tls($args) unless $args->{socks_address};
return $self->emit(
error => 'IO::Socket::Socks 0.64 required for SOCKS support')
unless SOCKS;
my %options
= (ConnectAddr => $args->{address}, ConnectPort => $args->{port});
@options{qw(AuthType Username Password)}
= ('userpass', @$args{qw(socks_user socks_pass)})
if $args->{socks_user};
my $reactor = $self->reactor;
$reactor->remove($handle);
return $self->emit(error => 'SOCKS upgrade failed')
unless IO::Socket::Socks->start_SOCKS($handle, %options);
weaken $self;
$reactor->io($handle => sub { $self->_socks($args) })->watch($handle, 0, 1);
}
sub _try_tls {
my ($self, $args) = @_;
my $handle = $self->{handle};
return $self->_cleanup->emit(connect => $handle)
if !$args->{tls} || $handle->isa('IO::Socket::SSL');
return $self->emit(error => 'IO::Socket::SSL 1.84 required for TLS support')
unless TLS;
# Upgrade
weaken $self;
my %options = (
SSL_ca_file => $args->{tls_ca}
&& -T $args->{tls_ca} ? $args->{tls_ca} : undef,
SSL_cert_file => $args->{tls_cert},
SSL_error_trap => sub { $self->emit(error => $_[1]) },
SSL_hostname => IO::Socket::SSL->can_client_sni ? $args->{address} : '',
SSL_key_file => $args->{tls_key},
SSL_startHandshake => 0,
SSL_verify_mode => $args->{tls_ca} ? 0x01 : 0x00,
SSL_verifycn_name => $args->{address},
SSL_verifycn_scheme => $args->{tls_ca} ? 'http' : undef
);
my $reactor = $self->reactor;
$reactor->remove($handle);
return $self->emit(error => 'TLS upgrade failed')
unless IO::Socket::SSL->start_SSL($handle, %options);
$reactor->io($handle => sub { $self->_tls })->watch($handle, 0, 1);
}
1;
=encoding utf8
=head1 NAME
Mojo::IOLoop::Client - Non-blocking TCP client
=head1 SYNOPSIS
use Mojo::IOLoop::Client;
# Create socket connection
my $client = Mojo::IOLoop::Client->new;
$client->on(connect => sub {
my ($client, $handle) = @_;
...
});
$client->on(error => sub {
my ($client, $err) = @_;
...
});
$client->connect(address => 'example.com', port => 80);
# Start reactor if necessary
$client->reactor->start unless $client->reactor->is_running;
=head1 DESCRIPTION
L<Mojo::IOLoop::Client> opens TCP connections for L<Mojo::IOLoop>.
=head1 EVENTS
L<Mojo::IOLoop::Client> inherits all events from L<Mojo::EventEmitter> and can
emit the following new ones.
=head2 connect
$client->on(connect => sub {
my ($client, $handle) = @_;
...
});
Emitted once the connection is established.
=head2 error
$client->on(error => sub {
my ($client, $err) = @_;
...
});
Emitted if an error occurs on the connection, fatal if unhandled.
=head1 ATTRIBUTES
L<Mojo::IOLoop::Client> implements the following attributes.
=head2 reactor
my $reactor = $client->reactor;
$client = $client->reactor(Mojo::Reactor::Poll->new);
Low-level event reactor, defaults to the C<reactor> attribute value of the
global L<Mojo::IOLoop> singleton.
=head1 METHODS
L<Mojo::IOLoop::Client> inherits all methods from L<Mojo::EventEmitter> and
implements the following new ones.
=head2 connect
$client->connect(address => '127.0.0.1', port => 3000);
Open a socket connection to a remote host. Note that TLS support depends on
L<IO::Socket::SSL> (1.84+) and IPv6 support on L<IO::Socket::IP> (0.20+).
These options are currently available:
=over 2
=item address
address => 'mojolicio.us'
Address or host name of the peer to connect to, defaults to C<localhost>.
=item handle
handle => $handle
Use an already prepared handle.
=item local_address
local_address => '127.0.0.1'
Local address to bind to.
=item port
port => 80
Port to connect to, defaults to C<80> or C<443> with C<tls> option.
=item socks_address
socks_address => '127.0.0.1'
Address or host name of SOCKS5 proxy server to use for connection.
=item socks_pass
socks_pass => 'secr3t'
Password to use for SOCKS5 authentication.
=item socks_port
socks_port => 9050
Port of SOCKS5 proxy server to use for connection.
=item socks_user
socks_user => 'sri'
Username to use for SOCKS5 authentication.
=item timeout
timeout => 15
Maximum amount of time in seconds establishing connection may take before
getting canceled, defaults to C<10>.
=item tls
tls => 1
Enable TLS.
=item tls_ca
tls_ca => '/etc/tls/ca.crt'
Path to TLS certificate authority file. Also activates hostname verification.
=item tls_cert
tls_cert => '/etc/tls/client.crt'
Path to the TLS certificate file.
=item tls_key
tls_key => '/etc/tls/client.key'
Path to the TLS key file.
=back
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|