Last active
September 19, 2017 10:39
-
-
Save mindc/49c9eb31a32bdc5d9e0570ed054e1307 to your computer and use it in GitHub Desktop.
Perl version of stunnel, using IO::Async
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
=pod | |
=encoding utf8 | |
=head1 AUTHOR | |
Paweł Feruś <[email protected]> | |
=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