Request for support for socks proxy in Mojo::UserAgent

234 views
Skip to first unread message

Alex

unread,
May 30, 2011, 9:17:05 AM5/30/11
to Mojolicious
How can I use mojo with socks proxy?

For example in LWP::UserAgent/WWW::Mechanize it works after:
LWP::Protocol::implementor( http =>
'LWP::Protocol::http::SocksChain' );
LWP::Protocol::implementor( https =>
'LWP::Protocol::https::SocksChain' );

See no way in mojo right now.

sri

unread,
May 30, 2011, 9:56:00 AM5/30/11
to Mojolicious
I think at the moment you would have to handle the connection
management yourself, using for example the on_start callback of
Mojo::UserAgent.

https://github.com/kraih/mojo/blob/master/t/mojolicious/websocket_lite_app.t#L210

Suggestions for making this easier in the future would be very much
appreciated btw.

--
sebastian

Alex

unread,
May 30, 2011, 3:20:40 PM5/30/11
to Mojolicious
Probably I can do it with:
1. wrapping Net::SC module functionality in socket interface expected
through $tx->connection($socket);
2. handling callback like on_start, on_message to prepare/analyze
additional params through socks proxy
But quick look into start/_start_tx sub make me worried if it would be
easy :)

In LWP::UserAgent LWP::Protocol is used as middle layer, with almost
single interface method:
request
I guess this interface can be extended with:
* on_start
* on_read
* on_write
* on_*
in async environment.
Plus easy way to pass control back to event machine.

On May 30, 5:56 pm, sri <kra...@googlemail.com> wrote:
> I think at the moment you would have to handle the connection
> management yourself, using for example the on_start callback of
> Mojo::UserAgent.
>
>    https://github.com/kraih/mojo/blob/master/t/mojolicious/websocket_lit...

Alex

unread,
May 31, 2011, 5:02:44 AM5/31/11
to Mojolicious
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 :(

sri

unread,
May 31, 2011, 6:14:09 AM5/31/11
to Mojolicious
Here's another example for what i was talking about.

use 5.14.0;
use Mojo::UserAgent;
use IO::Socket::INET;

my $ua = Mojo::UserAgent->new;
$ua->on_start(sub {
my ($ua, $tx) = @_;
my $host = $tx->req->url->host;
my $port = $tx->req->url->port || 80;
my $socket = IO::Socket::INET->new(PeerAddr => $host, PeerPort =>
$port);
$tx->connection($socket);
$tx->keep_alive(0);
});

say $ua->get('mojolicio.us')->res->body;

--
sebastian

sri

unread,
May 31, 2011, 6:33:54 AM5/31/11
to Mojolicious
And for future reference, a complete SOCKS example.

use 5.14.0;
use Mojo::UserAgent;
use Net::SC;

# Without SOCKS proxy
say Mojo::UserAgent->new->get('whatismyip.org')->res->body;

# With SOCKS proxy
my $ua = Mojo::UserAgent->new;
$ua->on_start(
sub {
my ($ua, $tx) = @_;
my $host = $tx->req->url->host;
my $port = $tx->req->url->port || 80;
my $sc = Net::SC->new(
Chain_Len => 1,
Debug => 0,
Chain_File_Data => ['202.4.155.234:1080'],
Auto_Save => 0,
Restore_Type => 0,
);
my $rc = $sc->connect($host, $port);
die "Can't connect: " . socks_error($rc) unless SOCKS_OKAY ==
$rc;
$tx->connection($sc->sh);
$tx->keep_alive(0);
}
);
say $ua->get('whatismyip.org')->res->body;

--
sebastian

Alex

unread,
May 31, 2011, 7:01:16 AM5/31/11
to Mojolicious
Cool, thanks.

Olexandr

unread,
Sep 5, 2012, 8:13:44 AM9/5/12
to mojol...@googlegroups.com, kra...@googlemail.com
All these examples work only with mojolicious < v2. and current UserAgent have no on_start sub. Can someone show actual SOCKS proxy example?
Reply all
Reply to author
Forward
0 new messages