Skip to content

Commit

Permalink
Use server's $SSH_CONNECTION if ProxyCommand isn't run.
Browse files Browse the repository at this point in the history
This is an alternate way to resolve mobile-shell#417 without affecting
backward compatibility.
  • Loading branch information
cgull committed Jan 17, 2016
1 parent 8200d5b commit acbdda2
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions scripts/mosh.pl
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ sub predict_check {
if ( $pid == 0 ) { # child
open(STDERR, ">&STDOUT") or die;

# Ask the server for its IP. The user's shell may not be
# Posix-compatible so invoke sh explicitly.
my $ssh_connection = "sh -c " .
shell_quote ( '[ -n "$SSH_CONNECTION" ] && printf "\nMOSH SSH_CONNECTION %s\n" "$SSH_CONNECTION"' ) .
";";

my @server = ( 'new' );

push @server, ( '-c', $colors );
Expand All @@ -299,10 +305,11 @@ sub predict_check {
die "Cannot exec $server: $!\n";
}
my $quoted_self = shell_quote( $0, "--family=$family" );
exec @ssh, '-S', 'none', '-o', "ProxyCommand=$quoted_self --fake-proxy -- %h %p", '-n', '-tt', $userhost, '--', "$server " . shell_quote( @server );
my @exec_argv = ( @ssh, '-S', 'none', '-o', "ProxyCommand=$quoted_self --fake-proxy -- %h %p", '-n', '-tt', $userhost, '--', $ssh_connection . " $server " . shell_quote( @server ) );
exec @exec_argv;
die "Cannot exec ssh: $!\n";
} else { # parent
my ( $ip, $port, $key );
my ( $ip, $sship, $port, $key );
my $bad_udp_port_warning = 0;
LINE: while ( <$pipe> ) {
chomp;
Expand All @@ -311,6 +318,13 @@ sub predict_check {
die "$0 error: detected attempt to redefine MOSH IP.\n";
}
( $ip ) = m{^MOSH IP (\S+)\s*$} or die "Bad MOSH IP string: $_\n";
} elsif ( m{^MOSH SSH_CONNECTION } ) {
my @words = split;
if ( scalar @words == 6 ) {
$sship = $words[4];
} else {
die "Bad MOSH SSH_CONNECTION string: $_\n";
}
} elsif ( m{^MOSH CONNECT } ) {
if ( ( $port, $key ) = m{^MOSH CONNECT (\d+?) ([A-Za-z0-9/+]{22})\s*$} ) {
last LINE;
Expand All @@ -328,7 +342,12 @@ sub predict_check {
close $pipe;

if ( not defined $ip ) {
if ( defined $sship ) {
warn "$0: Using remote IP address ${sship} from \$SSH_CONNECTION for hostname ${userhost}\n";
$ip = $sship;
} else {
die "$0: Did not find remote IP address (is SSH ProxyCommand disabled?).\n";
}
}

if ( not defined $key or not defined $port ) {
Expand Down

0 comments on commit acbdda2

Please sign in to comment.