Skip to content

Instantly share code, notes, and snippets.

@brianmed
Created June 18, 2015 13:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmed/cea6f8e86dfa97f96b55 to your computer and use it in GitHub Desktop.
Save brianmed/cea6f8e86dfa97f96b55 to your computer and use it in GitHub Desktop.
Attempt at SSL proxy in Mojolicious / Perl
use Mojo::Base -strict;
use Mojo::UserAgent;
use Mojo::Server::Daemon;
my $ua = Mojo::UserAgent->new;
my $daemon = Mojo::Server::Daemon->new(
listen => ['http://*:3001']
)->unsubscribe('request');
$daemon->on(request => sub {
my ($daemon, $tx) = @_;
state $remote;
if ("CONNECT" eq $tx->req->method) {
my $handle = $daemon->ioloop->{connections}{$tx->connection}{stream}{handle};
$remote = Mojo::IOLoop->client({address => $tx->req->url->host, port => $tx->req->url->port} => sub {
my ($loop, $err, $stream) = @_;
$DB::single = 1;
die($err) if $err;
$stream->on(close => sub {
warn("CLOSE");
});
$stream->on(error => sub {
my ($stream, $err) = @_;
warn("ERROR: $err");
});
$stream->on(timeout => sub {
warn("TIMEOUT");
});
$stream->on(read => sub {
my ($stream, $bytes) = @_;
print($handle $bytes);
});
$tx->resume;
});
$tx->res->code(200);
$tx->res->message("Connection established");
$tx->res->headers->add("Proxy-agent" => 'Mojo/6.0 (Linux)');
$tx->on(finish => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
$reactor->io($handle =>
sub {
my ($reactor, $writable) = @_;
unless ($writable) {
$DB::single = 1;
while (my $line = $handle->getline) {
Mojo::IOLoop->stream($remote)->write($line);
}
}
}
)->watch($handle, 1, 1);
});
}
});
$daemon->run;
### Usage:
###
### use Mojo::Base -strict;
### use Mojo::UserAgent;
###
### my $ua = Mojo::UserAgent->new;
### $ua->proxy->https('http://127.0.0.1:3001');
###
### my $tx = $ua->get('https://www.google.com');
### say $tx->res->to_string;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment