App-FileSync
view release on metacpan or search on metacpan
lib/App/FileSync/Transport/SSH.pm view on Meta::CPAN
package App::FileSync::Transport::SSH;
# Runs filesync-server on the server host over one ssh connection and speaks
# the wire protocol on its pipes. The ssh argument list is built here and
# never passed through a local shell, and every value that reaches the remote
# shell is restricted to characters that are inert there, so a repo or client
# id cannot become a command.
use App::FileSync::Platform;
use strict;
use warnings;
# Not :all - this class has a close method, and autodie replaces a package's
# subs named after the builtins it wraps.
use autodie qw(open);
use File::Temp ();
use List::Util qw(any);
use Readonly;
use Sub::Private { mode => 'enforce' };
use App::FileSync qw(validate_client_id validate_repo_name);
use App::FileSync::Args qw(named);
use App::FileSync::Constants qw($PROTOCOL_VERSION $ROLE_CLIENT @ROLES $SERVER_PROGRAM);
use App::FileSync::I18N qw(throw);
use App::FileSync::IO qw(read_text);
use App::FileSync::Process;
use version; our $VERSION = version->declare('0.002.1');
# Kept for compatibility: the protocol version sent as --proto.
our $PROTO = $PROTOCOL_VERSION;
# Options every session uses: no prompts, keepalives so a dead link is
# noticed, and a bounded connect.
Readonly my @SSH_OPTIONS => (
'-T',
'-o', 'BatchMode=yes',
'-o', 'ServerAliveInterval=30',
'-o', 'ConnectTimeout=10',
);
# Host key policies a repo may choose.
Readonly my @HOST_KEY_CHECKING => qw(yes accept-new);
# The highest TCP port.
Readonly::Scalar my $MAX_PORT => 65_535;
# Characters that would end or split an argument.
Readonly my $UNSAFE_ARG => qr/[\0\n]/;
# user@host, host, or a bracketed IPv6 literal. Both the user and the host
# start with a letter or digit, so neither can be read as an ssh option.
Readonly my $SERVER => qr/\A(?:[A-Za-z0-9][A-Za-z0-9._-]*@)?
(?:[A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?|\[[0-9A-Fa-f:]+\])\z/x;
# A remote program path: only characters inert in a POSIX shell, and not
# starting with '-'.
Readonly my $PROGRAM => qr{\A[A-Za-z0-9._/][A-Za-z0-9._/-]*\z};
=encoding utf8
=head1 NAME
App::FileSync::Transport::SSH - run the server over ssh
=head1 SYNOPSIS
my $transport = App::FileSync::Transport::SSH->new(
server => 'filesync@files.example.com', repo => 'docs', client => 'laptop',
identity => '~/.ssh/filesync_ed25519',
);
...
$transport->close or warn $transport->error_text;
=head1 METHODS
=head2 new
Purpose: start C<filesync-server> on the server host for one session.
Args: C<server> (C<user@host>, C<host> or C<user@[v6]>), C<repo>, C<client>,
optional C<role> (default C<client>), C<server_command> (default
C<filesync-server>), C<host_key_checking> (C<yes>, the default, or
C<accept-new>), C<known_hosts>, C<identity>, C<port>, C<command> (the local
ssh program), C<extra_args> (arrayref of extra ssh arguments).
Returns: the transport.
Side Effects: runs C<ssh ... SERVER PROGRAM --repo R --client C --role X
--proto 2>. ssh's standard error goes to a temporary file, never the
protocol pipe, and is available from L</error_text>. On Windows a C<.cmd> or
C<.bat> ssh wrapper is started through C<COMSPEC>, since CreateProcess cannot
start one directly.
Usage:
my $transport = App::FileSync::Transport::SSH->new(%args);
=head3 EXAMPLE
( run in 1.072 second using v1.01-cache-2.11-cpan-234fa220139 )