Developer-Dashboard
view release on metacpan or search on metacpan
lib/Developer/Dashboard/Web/Server.pm view on Meta::CPAN
for my $state (@client_states) {
_ssl_frontend_client_finish($state);
}
if ( !$self->_close_ssl_frontend_handle($listener) ) {
if ( !defined $frontend_close_error ) {
$frontend_close_error = "Unable to close SSL frontend listener: $!";
}
}
my @routine_errors = @client_errors;
_stop_ssl_backend( $backend_pid, \%reaped_children );
die "SSL frontend client handler failed: $routine_errors[0]" if @routine_errors;
die $frontend_close_error if defined $frontend_close_error;
return 1;
}
# _close_ssl_frontend_handle($handle)
# Closes one SSL frontend socket handle and returns the close result unchanged.
# Input: open socket or filehandle reference owned by the SSL frontend.
# Output: true when closed successfully, false when Perl reports a close error.
sub _close_ssl_frontend_handle {
my ( $self, $handle ) = @_;
return close $handle;
}
# _wait_for_ssl_backend_ready($daemon, $pid)
# Waits until the isolated SSL backend completes a loopback TLS handshake or reports
# a startup failure, preventing public requests from racing backend startup.
# Input: daemon descriptor and GoRoutine-managed backend process identifier.
# Output: true when the backend listener accepts a connection; dies on failure.
sub _wait_for_ssl_backend_ready {
my ( $self, $daemon, $pid ) = @_;
my $deadline = time() + 45;
while ( time() < $deadline ) {
if ( Developer::Dashboard::GoRoutine::process_exited($pid) ) {
my $status = Developer::Dashboard::GoRoutine::wait_process($pid);
$SSL_BACKEND_REAPED{$pid} = 1;
die "SSL backend exited before becoming ready (status $status)";
}
my $backend = IO::Socket::SSL->new(
PeerAddr => $daemon->internal_sockhost,
PeerPort => $daemon->internal_sockport,
Proto => 'tcp',
SSL_verify_mode => 0,
SSL_hostname => 'localhost',
Timeout => 1,
);
if ($backend) {
close $backend or die "Unable to close SSL backend readiness probe: $!";
return 1;
}
sleep 0.05;
}
_stop_ssl_backend($pid);
die 'Timed out waiting for SSL backend listener readiness';
}
# _ssl_backend_command($daemon)
# Builds the staged-core command that runs the isolated SSL Starman backend,
# resolving the core dispatcher beside the current helper wrapper.
# Input: daemon descriptor with public and internal endpoints.
# Output: argv array reference safe for GoRoutine's native process API.
sub _ssl_backend_command {
my ( $self, $daemon ) = @_;
if ( ref( $self->{ssl_backend_command_builder} ) eq 'CODE' ) {
my $command = $self->{ssl_backend_command_builder}->(
$daemon->internal_sockhost,
$daemon->internal_sockport,
$self->{workers},
);
die 'SSL backend command builder must return an argv array reference'
if ref($command) ne 'ARRAY' || !@{$command};
return $command;
}
my $running_helper = $ENV{DEVELOPER_DASHBOARD_RUNNING_HELPER};
die 'Missing private Dashboard helper for SSL backend process'
if !defined $running_helper || $running_helper eq '';
my $helper = File::Spec->catfile( dirname($running_helper), '_dashboard-core' );
return [
$^X, $helper, 'web-ssl-backend-foreground',
'--host', $daemon->internal_sockhost,
'--port', $daemon->internal_sockport,
'--workers', $self->{workers},
];
}
# _run_ssl_backend_process($daemon)
# Runs the internal SSL PSGI backend inside the forked child process and
# returns its exit code to the immediate caller.
# Input: daemon descriptor object.
# Output: numeric process exit code.
sub _run_ssl_backend_process {
my ( $self, $daemon ) = @_;
# Mark this process as the internal backend behind the SSL front-proxy so
# authorize_request never auto-grants loopback admin (every connection here
# arrives from the proxy's loopback socket). Starman workers fork from here
# and inherit the flag.
local $ENV{DEVELOPER_DASHBOARD_SSL_PROXIED} = 1;
my $runner = $self->_build_runner($daemon);
my $app = $self->psgi_app;
$runner->run($app);
return 0;
}
# _open_ssl_frontend_listener_or_die(%args)
# Opens the public SSL frontend listener or stops the backend child and dies
# with an explicit bind error when the public socket cannot be reserved.
# Input: daemon descriptor, backend pid integer, and reaped-child hash ref.
# Output: bound listener socket handle.
sub _open_ssl_frontend_listener_or_die {
my ( $self, %args ) = @_;
my $daemon = $args{daemon} || die 'Missing SSL frontend daemon descriptor';
my $backend_pid = $args{backend_pid};
my $reaped_children = $args{reaped_children};
my $listener = IO::Socket::INET->new(
LocalAddr => $daemon->sockhost,
LocalPort => $daemon->sockport,
Proto => 'tcp',
ReuseAddr => 1,
Listen => 128,
);
lib/Developer/Dashboard/Web/Server.pm view on Meta::CPAN
system(@verify_cmd);
};
return 0 if $verify_exit != 0;
return 0 if $verify_stdout !~ /\:\s+OK\s*\z/ && $verify_stderr !~ /\:\s+OK\s*\z/;
}
return 1;
}
# get_ssl_cert_paths()
# Returns the paths to the self-signed certificate and key files.
# Input: none. The current certificate profile is whatever generate_self_signed_cert()
# most recently prepared for the active dashboard runtime.
# Output: list of (cert_path, key_path) or dies if files do not exist.
sub get_ssl_cert_paths {
my ( undef, $cert_dir ) = _ssl_certificate_directory();
my $cert_file = File::Spec->catfile($cert_dir, 'server.crt');
my $key_file = File::Spec->catfile($cert_dir, 'server.key');
die "Certificate file not found: $cert_file" if !-f $cert_file;
die "Key file not found: $key_file" if !-f $key_file;
return ($cert_file, $key_file);
}
1;
__END__
=head1 NAME
Developer::Dashboard::Web::Server - PSGI server bridge for Developer Dashboard
=head1 SYNOPSIS
my $server = Developer::Dashboard::Web::Server->new(app => $app, paths => $paths);
$server->run;
=head1 DESCRIPTION
This module reserves the local listen address, builds the Dancer2 PSGI app,
passes the path registry so skill C<lib/Dashboard.pm> extensions can load,
and runs it under Starman through Plack::Runner.
=head1 METHODS
=head2 new, run, start_daemon, listening_url, serve_daemon, psgi_app, _build_runner, _serve_ssl_frontend, _close_ssl_frontend_handle, _ssl_backend_command, _wait_for_ssl_backend_ready, _handle_ssl_frontend_client, _default_headers, _ssl_certificate_d...
Construct and run the local PSGI web server with optional SSL/HTTPS support.
When C<ssl => 1> is passed to new(), generates or refreshes self-signed certificates in C<~/.developer-dashboard/certs/> with SAN coverage for C<localhost>, C<127.0.0.1>, C<::1>, the concrete non-wildcard bind host, and any configured extra aliases/I...
=head1 SSL SUPPORT
The public front-proxy owns the external listener. It starts the internal
Starman server with the private C<web-ssl-backend-foreground> helper using an
argument vector, not a shell command. The command targets the staged
C<_dashboard-core> dispatcher beside the running helper rather than reusing
the command wrapper named by C<DEVELOPER_DASHBOARD_RUNNING_HELPER>; wrappers
prepend their own command name and would otherwise recursively start SSL
frontends. The helper reconstructs the ordinary application and serves only on
the reserved internal endpoint. Startup is not considered ready until a TLS
handshake succeeds. Readiness checks detect exited-but-unreaped child
processes, and premature helper exit and timeout are reported as errors.
Each accepted client socket is handed to a GoRoutine worker with shared socket,
daemon descriptor, and minimal host/SAN state. Workers use bounded readiness
waits so shutdown cancellation also works when a client remains idle. The
front-end cancels and joins workers before stopping and waiting for the backend,
and surfaces worker failures unless shutdown itself requested cancellation.
Socket close errors are retained while workers and the managed backend are
cleaned up, then reported so a failed close cannot strand the backend process.
The worker contract depends on GoRoutine's supported shared filehandle and
reference types; it does not claim that arbitrary opaque native pointers can
be shared.
=head2 _close_ssl_frontend_handle($handle)
Closes one SSL frontend socket or filehandle and returns Perl's close result
unchanged. The SSL frontend uses this helper for worker sockets, listener-side
socket release, and the public listener so each close error can be preserved
while shutdown finishes. The input is an open socket or filehandle reference;
the output is true on success or false when Perl reports a close error.
Pass C<ssl => 1> to the new() constructor to enable HTTPS:
my $server = Developer::Dashboard::Web::Server->new(
app => $app,
ssl => 1,
);
$server->run;
Self-signed certificates are generated automatically in C<~/.developer-dashboard/certs/> and reused on subsequent runs when they already match the expected browser-safe localhost/loopback profile plus any configured SAN aliases or IP literals. Older ...
Both C<generate_self_signed_cert> and C<get_ssl_cert_paths> locate that directory through C<_ssl_certificate_directory>, which asks C<Developer::Dashboard::PathRegistry> for the home runtime layer. The registry resolves the home directory from C<HOME...
=head1 HTTPS REDIRECT TRUST BOUNDARY
The public listener binds C<0.0.0.0> by default, so the plain-HTTP redirect it
serves on the SSL port is reachable from the network and its C<Location> header
must never be built from client-controlled input. Two values reach that header
and both are validated:
=over 4
=item *
The B<authority> comes from the request C<Host> header only when that header
parses as a valid host with an optional in-range port B<and> names an authority
this server can legitimately answer for: a loopback IP literal, or one of the
names its own certificate covers (C<localhost>, C<127.0.0.1>, C<::1>, the
concrete non-wildcard bind host, and every entry in
C<web.ssl_subject_alt_names>). Anything else is discarded and the redirect uses
the server-derived listen authority instead. The accepted authority is rebuilt
from the parsed parts, so no byte of the original header survives into the
header value.
=item *
The B<target> is only reused when it is an origin-form path. A target is
concatenated directly after the authority, so an authority-form target such as
C<@evil.com/> would demote the real host to userinfo and hand the redirect to
( run in 0.723 second using v1.01-cache-2.11-cpan-eb134014566 )