Skip to content

Instantly share code, notes, and snippets.

@mindc
Last active September 19, 2017 10:39
Show Gist options
  • Save mindc/49c9eb31a32bdc5d9e0570ed054e1307 to your computer and use it in GitHub Desktop.
Save mindc/49c9eb31a32bdc5d9e0570ed054e1307 to your computer and use it in GitHub Desktop.
Perl version of stunnel, using IO::Async
#!/usr/bin/perl
=pod
=encoding utf8
=head1 AUTHOR
Paweł Feruś <null@mindc.net>
=cut
use strict;
use warnings;
use IO::Async::Loop;
use IO::Async::SSL;
my $accept = [ '127.0.0.1' => 443 ];
my $connect = [ '127.0.0.1' => 80 ];
my $loop = IO::Async::Loop->new;
$loop->listen(
on_stream => sub {
my ( $local ) = @_;
$loop->connect(
host => $connect->[0],
service => $connect->[1],
socktype => 'stream',
on_stream => sub {
my ( $stream ) = @_;
$stream->configure(
on_read => sub {
my ( undef, $buffref, $eol ) = @_;
return if $eol;
return unless $$buffref;
$local->write( $$buffref );
$$buffref = "";
return 0;
},
on_closed => sub {
$local->close;
}
);
$local->configure(
on_read => sub {
my ( undef, $buffref, $eol ) = @_;
return if $eol;
return unless $$buffref;
$stream->write( $$buffref );
$$buffref = "";
return 0;
},
on_closed => sub {
$stream->close;
}
);
$loop->add( $stream );
$loop->add( $local );
}
)->get;
},
host => $accept->[0],
service => $accept->[1],
socktype => 'stream',
extensions => [qw( SSL )],
SSL_key_file => 'private.pem',
SSL_ca_file => 'ca.crt',
SSL_cert_file => 'private.crt',
SSL_server => 1,
)->get;
$loop->run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment