Subject: | Various fixes: FQDN, preëxisting 'handle' |
The attached patch fixes the following issues:
* Deconfigure $stream's on_read after setup
* Slightly neater sequencing; always remember to $loop->remove($stream) even after failure
* Only add/remove $stream to loop if we constructed it; stream given by 'handle' argument should already be a member
* Use ATYPE_FQDN except for hostnames that look a bit like IPv4 addresses (see also RT124730)
With these in place, a small additional patch to Net::Async::HTTP allows the latter to use NaSOCKS as a proxy.
--
Paul Evans
Subject: | rt.patch |
=== modified file 'lib/Net/Async/SOCKS.pm'
--- lib/Net/Async/SOCKS.pm 2018-03-09 19:07:50 +0000
+++ lib/Net/Async/SOCKS.pm 2018-03-09 19:17:32 +0000
@@ -77,6 +77,10 @@
my $stream = delete $params{handle} || IO::Async::Stream->new;
+ # If 'handle' is already given then it will already be a member of the
+ # Loop
+ my $must_add = not defined $stream->loop;
+
$stream->isa( "IO::Async::Stream" ) or
croak "Can only SOCKS_connect a handle instance of IO::Async::Stream";
@@ -122,7 +126,7 @@
}
}
);
- $loop->add($stream);
+ $loop->add($stream) if $must_add;
# Version and auth header goes first
$stream->write($proto->init_packet);
@@ -134,18 +138,19 @@
# process.
$proto->auth(
)->then(sub {
+ my $host = $params{host};
+
$proto->connect(
- ATYPE_IPV4,
- $params{host},
+ $host =~ m/^\d{1,3}(\.\d{1,3}){3}+$/ ? ATYPE_IPV4 : ATYPE_FQDN,
+ $host,
$params{service},
- )->transform(
- done => sub {
- $loop->remove($stream);
- $stream
- }
- )
+ );
})
- });
+ })->on_ready(sub {
+ $loop->remove($stream) if $must_add;
+ $stream->configure(on_read => undef);
+ })->then_done($stream);
+
$f->on_done($on_done) if $on_done;
$f->on_fail(sub {
$on_socks_error->($_[0]) if defined $_[1] and $_[1] eq "socks";