diff --git a/perl/Net/Server/Coro.pm b/perl/Net/Server/Coro.pm index cf8633b..204849c 100644 --- a/perl/Net/Server/Coro.pm +++ b/perl/Net/Server/Coro.pm @@ -46,7 +46,8 @@ sub post_bind_hook { my $self = shift; my $prop = $self->{server}; delete $prop->{select}; - $prop->{sock} = [ map { make_coro_socket($_) } @{ $prop->{sock} } ]; + + $prop->{sock} = [ map { make_coro_socket($_, $self->server_cert, $self->server_key) } @{ $prop->{sock} } ]; } =head2 make_coro_socket SOCKET @@ -58,14 +59,19 @@ it into a L object. sub make_coro_socket { my $socket = shift; + my ($server_cert, $server_key) = @_; + my @extra; if ( $socket->isa("IO::Socket::SSL") ) { $socket = bless $socket, "Net::Server::Proto::TCP"; @extra = ( expects_ssl => 1 ); } + $socket = Net::Server::Proto::Coro->new_from_fh( $socket, forward_class => ref($socket), + server_cert => $server_cert, + server_key => $server_key, @extra ); return $socket; diff --git a/perl/Net/Server/Proto/Coro.pm b/perl/Net/Server/Proto/Coro.pm index ad10d80..a8c7275 100644 --- a/perl/Net/Server/Proto/Coro.pm +++ b/perl/Net/Server/Proto/Coro.pm @@ -27,7 +27,9 @@ sub accept { my $socket = $self->new_from_fh( $fh, forward_class => tied( ${$self} )->[7], - expects_ssl => tied( ${$self} )->[9] + expects_ssl => tied( ${$self} )->[9], + server_cert => tied( ${$self} )->[11], + server_key => tied( ${$self} )->[12], ); return wantarray ? ( $socket, $peername ) : $socket; } @@ -47,7 +49,7 @@ sub is_ssl { return $self->[10] ? 1 : 0; } -sub start_SSL { Net::Server::Proto::Coro::FH::start_SSL( tied ${$_[0]} ); } +sub start_SSL { Net::Server::Proto::Coro::FH::start_SSL( tied ${$_[0]}, $_[1], $_[2]) } sub read { Net::Server::Proto::Coro::FH::READ ( tied ${$_[0]}, $_[1], $_[2], $_[3]) } sub sysread { Net::Server::Proto::Coro::FH::READ ( tied ${$_[0]}, $_[1], $_[2], $_[3]) } sub syswrite { Net::Server::Proto::Coro::FH::WRITE ( tied ${$_[0]}, $_[1], $_[2], $_[3]) } @@ -71,6 +73,8 @@ sub TIEHANDLE { my $self = $class->SUPER::TIEHANDLE(%arg); $self->[9] = $arg{expects_ssl}; $self->[10] = undef; + $self->[11] = $arg{server_cert}; + $self->[12] = $arg{server_key}; return $self; } @@ -102,8 +106,9 @@ sub READ_UNTIL { } sub READ { + return Coro::Handle::FH::READ(@_) unless $_[0][9]; - $_[0]->force_close and return unless $_[0][10] or $_[0]->start_SSL(); + $_[0]->force_close and return unless $_[0][10] or $_[0]->start_SSL($_[0][11], $_[0][12]); my $len = $_[2]; my $ofs = $_[3]; @@ -126,7 +131,7 @@ sub READ { sub READLINE { return Coro::Handle::FH::READLINE(@_) unless $_[0][9]; - $_[0]->force_close and return unless $_[0][10] or $_[0]->start_SSL(); + $_[0]->force_close and return unless $_[0][10] or $_[0]->start_SSL($_[0][11], $_[0][12]); my $irs = $_[1] || $/; my $stop = sub { @@ -145,7 +150,7 @@ sub READLINE { sub WRITE { return Coro::Handle::FH::WRITE(@_) unless $_[0][9]; - $_[0]->force_close and return unless $_[0][10] or $_[0]->start_SSL(); + $_[0]->force_close and return unless $_[0][10] or $_[0]->start_SSL($_[0][11], $_[0][12]); my $len = defined $_[2] ? $_[2] : length $_[1]; my $ofs = $_[3] || 0; @@ -225,6 +230,8 @@ use vars qw/$CONTEXT/; sub start_SSL { my $ctx; $_[0][9] = 1; + my $server_cert = $_[1] || "certs/server-cert.pem"; + my $server_key = $_[2] || "certs/server-key.pem"; unless ($CONTEXT) { $ctx = $CONTEXT = Net::SSLeay::CTX_new; @@ -232,10 +239,10 @@ sub start_SSL { Net::SSLeay::CTX_set_mode( $ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_ENABLE_PARTIAL_WRITE ); - Net::SSLeay::CTX_use_PrivateKey_file( $ctx, "certs/server-key.pem", + Net::SSLeay::CTX_use_PrivateKey_file( $ctx, $server_key, Net::SSLeay::FILETYPE_PEM() ); Net::SSLeay::CTX_use_certificate_chain_file( $ctx, - "certs/server-cert.pem" ); + $server_cert ); } $ctx = $CONTEXT; $_[0][11] = $ctx;