X-Git-Url: https://git.distorted.org.uk/~mdw/termux-packages/blobdiff_plain/188d120104c4b1a2d1d5234a7f8de9f6e8fe9a3d..02baa030aa6273c97592f6790499f8892cb59223:/packages/mosh/mosh.cc diff --git a/packages/mosh/mosh.cc b/packages/mosh/mosh.cc index 9f0352e6..33e0b583 100644 --- a/packages/mosh/mosh.cc +++ b/packages/mosh/mosh.cc @@ -85,6 +85,7 @@ static const char *usage_format = " --predict=adaptive local echo for slower links [default]\n" "-a --predict=always use local echo even on fast links\n" "-n --predict=never never use local echo\n" +"-6 use IPv6 only\n" "\n" "-p NUM --port=NUM server-side UDP port\n" "\n" @@ -95,6 +96,8 @@ static const char *usage_format = " (example: \"ssh -p 2222\")\n" " (default: \"ssh\")\n" "\n" +" --no-init do not send terminal initialization string\n" +"\n" " --help this message\n" " --version version and copyright information\n" "\n" @@ -162,24 +165,26 @@ int main( int argc, char *argv[] ) string server = "mosh-server"; string ssh = "ssh"; string predict, port_request, ssh_port; - int help=0, version=0, fake_proxy=0; + int help=0, version=0, fake_proxy=0, term_init=1; + int force_ipv6 = 0; static struct option long_options[] = { { "client", required_argument, 0, 'c' }, { "server", required_argument, 0, 's' }, + { "no-init", no_argument, &term_init, 0 }, { "predict", required_argument, 0, 'r' }, { "port", required_argument, 0, 'p' }, { "ssh-port", required_argument, 0, 'P' }, { "ssh", required_argument, 0, 'S' }, { "help", no_argument, &help, 1 }, { "version", no_argument, &version, 1 }, - { "fake-proxy!", no_argument, &fake_proxy, 1 }, + { "fake-proxy", no_argument, &fake_proxy, 1 }, { 0, 0, 0, 0 } }; while ( 1 ) { int option_index = 0; - int c = getopt_long( argc, argv, "anp:P:", + int c = getopt_long( argc, argv, "6anp:P:", long_options, &option_index ); if ( c == -1 ) { break; @@ -213,6 +218,9 @@ int main( int argc, char *argv[] ) case 'n': predict = "never"; break; + case '6': + force_ipv6 = true; + break; default: die( usage_format, argv[0] ); } @@ -259,6 +267,9 @@ int main( int argc, char *argv[] ) memset( &hints, 0, sizeof( hints ) ); hints.ai_socktype = SOCK_STREAM; + if (force_ipv6) { + hints.ai_family = AF_INET6; + } if ( ( rv = getaddrinfo( host.c_str(), port.c_str(), @@ -271,6 +282,9 @@ int main( int argc, char *argv[] ) } int try_family = AF_INET; + if (force_ipv6) { + try_family = AF_INET6; + } // loop through all the results and connect to the first we can for ( p = servinfo; p != NULL || try_family == AF_INET; p = p->ai_next ) { if(p == NULL && try_family == AF_INET) { // start over and try AF_INET6 @@ -315,17 +329,18 @@ int main( int argc, char *argv[] ) freeaddrinfo( servinfo ); // all done with this structure int pid = fork(); - if ( pid == -1 ) { - die( "%s: fork: %d", argv[0], errno ); - } + if ( pid == -1 ) die( "%s: fork: %d", argv[0], errno ); if ( pid == 0 ) { - cat( sockfd, 1 ); + close( STDIN_FILENO ); + cat( sockfd, STDOUT_FILENO ); shutdown( sockfd, 0 ); exit( 0 ); } signal( SIGHUP, SIG_IGN ); - cat( 0, sockfd ); - shutdown( sockfd, 1 ); + close( STDOUT_FILENO ); + cat( STDIN_FILENO, sockfd ); + shutdown( sockfd, SHUT_WR /* = 1 */ ); + close( STDIN_FILENO ); waitpid( pid, NULL, 0 ); exit( 0 ); } @@ -338,25 +353,9 @@ int main( int argc, char *argv[] ) char **command = &argv[optind]; int commands = argc - optind; - string color_invocation = client + " -c"; - FILE *color_file = popen( color_invocation.c_str(), "r" ); - if ( !color_file ) { - die( "%s: popen: %d", argv[0], errno ); - } char *buf = NULL; size_t buf_sz = 0; ssize_t n; - if ( ( n = getline( &buf, &buf_sz, color_file ) ) < 0 ) { - die( "%s: Can't count colors: %d", argv[0], errno ); - } - string colors = string( buf, n ); - pclose( color_file ); - - if ( !colors.size() || - colors.find_first_not_of( "0123456789" ) != string::npos || - atoi( colors.c_str() ) < 0 ) { - colors = "0"; - } int pty, pty_slave; struct winsize ws; @@ -369,9 +368,7 @@ int main( int argc, char *argv[] ) } int pid = fork(); - if ( pid == -1 ) { - die( "%s: fork: %d", argv[0], errno ); - } + if ( pid == -1 ) die( "%s: fork: %d", argv[0], errno ); if ( pid == 0 ) { close( pty ); if ( -1 == dup2( pty_slave, 1 ) || @@ -382,34 +379,57 @@ int main( int argc, char *argv[] ) vector server_args; server_args.push_back( "new" ); - server_args.push_back( "-s" ); server_args.push_back( "-c" ); - server_args.push_back( colors ); + server_args.push_back( "256" ); + server_args.push_back( "-s" ); if ( port_request.size() ) { server_args.push_back( "-p" ); server_args.push_back( port_request ); } + + for (char const* env_name : { + "LANG", "LANGUAGE", "LC_CTYPE", "LC_NUMERIC", + "LC_TIME", "LC_COLLATE", "LC_MONETARY", "LC_MESSAGES", "LC_PAPER", + "LC_NAME", "LC_ADDRESS", "LC_TELEPHONE", "LC_MEASUREMENT", + "LC_IDENTIFICATION", "LC_ALL" }) { + char* env_value = getenv(env_name); + if (env_value) { + server_args.push_back("-l"); + server_args.push_back(string(env_name) + "=" + env_value); + } + } + if ( commands ) { + server_args.push_back( "--" ); server_args.insert( server_args.end(), command, command + commands ); } + string quoted_self = shell_quote_string( string( argv[0] ) ); string quoted_server_args = shell_quote( server_args ); fflush( stdout ); - string proxy_arg = "ProxyCommand=" + quoted_self + " --fake-proxy -- %h %p"; + string proxy_arg = "ProxyCommand=" + quoted_self; + if (force_ipv6) { + proxy_arg += " -6"; + } + proxy_arg += " --fake-proxy -- %h %p"; string ssh_remote_command = server + " " + quoted_server_args; vector ssh_args; + ssh_args.push_back( "-n" ); + ssh_args.push_back( "-tt" ); ssh_args.push_back( "-S" ); ssh_args.push_back( "none" ); ssh_args.push_back( "-o" ); ssh_args.push_back( proxy_arg ); - ssh_args.push_back( "-t" ); ssh_args.push_back( userhost ); if ( ssh_port.size() ) { ssh_args.push_back( "-p" ); ssh_args.push_back( ssh_port ); } + if ( force_ipv6 ) { + ssh_args.push_back( "-6" ); + } ssh_args.push_back( "--" ); ssh_args.push_back( ssh_remote_command ); @@ -463,5 +483,6 @@ int main( int argc, char *argv[] ) setenv( "MOSH_KEY", key.c_str(), 1 ); setenv( "MOSH_PREDICTION_DISPLAY", predict.c_str(), 1 ); + if (!term_init) setenv( "MOSH_NO_TERM_INIT", "1", 1 ); execlp( client.c_str(), client.c_str(), ip.c_str(), port.c_str(), (char *)NULL ); }