I get it working with:
--snip
#!/usr/bin/env perl
use strict;
use Net::SC;
use Mojo::IOLoop;
use Mojo::UserAgent;
use Mojo::Message::Response;
use Test::More;
my $ip = Mojo::UserAgent->new()
->get('
http://www.whatismyip.org/')
->res->body;
my $sc = Net::SC->new(
Chain_Len => 1,
Debug => 0,
# Chain_File_Data => ['127.0.0.1:9050:::4'],
Chain_File_Data => ['202.4.155.234:1080'], # free
public socks proxy China, Bejing
Auto_Save => 0,
Restore_Type => 0,
);
my $rc = $sc->connect( '
www.whatismyip.org', 80 );
die "Can't connect: ".socks_error($rc) unless SOCKS_OKAY == $rc;
my $ip_tor;
my $id = Mojo::IOLoop->connect(
handle => $sc->sh,
on_connect => sub {
my ($self, $id) = @_;
# raw
# $self->write($id, "GET / HTTP/1.1\r\n\r\n");
my $tx = Mojo::UserAgent->build_tx(GET => 'http://
www.whatismyip.org');
$self->write($id, $tx->req->to_string);
},
on_read => sub {
my ($self, $id, $chunk) = @_;
# DEBUG: can be called on part of response?
# in which case save chunks somewhere
# raw
# ($ip_tor) = $chunk =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(.
\d{1,3}+)?)/;
my $res = Mojo::Message::Response->new();
$res->parse($chunk);
# $res->dom() is available
($ip_tor) = $res->body =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.
\d{1,3}(.\d{1,3}+)?)/;
chomp $ip_tor;
},
on_close => sub {
Mojo::IOLoop->stop;
},
);
Mojo::IOLoop->start;
isnt( $ip, $ip_tor, "check ips '$ip' VS '$ip_tor'" );
done_testing();
# D'oh!!!
--snap
I'm sending it here for the sake of information.
Though, I see no easy way to do it through Mojo::UserAgent :(