From 8f46e184262aebb3a420571de73520d60ccdfbd0 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 24 May 2020 23:27:34 +0200 Subject: [PATCH] Add --force-adb-forward Add a command-line option to force "adb forward", without attempting "adb reverse" first. This is especially useful for using SSH tunnels without enabling remote port forwarding. --- README.md | 16 ++++++++++++++++ app/scrcpy.1 | 4 ++++ app/src/cli.c | 10 ++++++++++ app/src/scrcpy.c | 1 + app/src/scrcpy.h | 2 ++ app/src/server.c | 22 ++++++++++++++-------- app/src/server.h | 1 + 7 files changed, 48 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ab66827f..3f4db9fa 100644 --- a/README.md +++ b/README.md @@ -289,6 +289,22 @@ From another terminal: scrcpy ``` +To avoid enabling remote port forwarding, you could force a forward connection +instead (notice the `-L` instead of `-R`): + +```bash +adb kill-server # kill the local adb server on 5037 +ssh -CN -L5037:localhost:5037 -L27183:localhost:27183 your_remote_computer +# keep this open +``` + +From another terminal: + +```bash +scrcpy --force-adb-forwrad +``` + + Like for wireless connections, it may be useful to reduce quality: ``` diff --git a/app/scrcpy.1 b/app/scrcpy.1 index 3bbdbffe..1020a2fb 100644 --- a/app/scrcpy.1 +++ b/app/scrcpy.1 @@ -52,6 +52,10 @@ The list of possible display ids can be listed by "adb shell dumpsys display" Default is 0. +.TP +.B \-\-force\-adb\-forward +Do not attempt to use "adb reverse" to connect to the device. + .TP .B \-f, \-\-fullscreen Start in fullscreen. diff --git a/app/src/cli.c b/app/src/cli.c index b19d9a58..a0c17c1a 100644 --- a/app/src/cli.c +++ b/app/src/cli.c @@ -54,6 +54,10 @@ scrcpy_print_usage(const char *arg0) { "\n" " Default is 0.\n" "\n" + " --force-adb-forward\n" + " Do not attempt to use \"adb reverse\" to connect to the\n" + " the device.\n" + "\n" " -f, --fullscreen\n" " Start in fullscreen.\n" "\n" @@ -516,6 +520,7 @@ guess_record_format(const char *filename) { #define OPT_RENDER_DRIVER 1016 #define OPT_NO_MIPMAPS 1017 #define OPT_CODEC_OPTIONS 1018 +#define OPT_FORCE_ADB_FORWARD 1019 bool scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) { @@ -525,6 +530,8 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) { {"codec-options", required_argument, NULL, OPT_CODEC_OPTIONS}, {"crop", required_argument, NULL, OPT_CROP}, {"display", required_argument, NULL, OPT_DISPLAY_ID}, + {"force-adb-forward", no_argument, NULL, + OPT_FORCE_ADB_FORWARD}, {"fullscreen", no_argument, NULL, 'f'}, {"help", no_argument, NULL, 'h'}, {"lock-video-orientation", required_argument, NULL, @@ -701,6 +708,9 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) { case OPT_CODEC_OPTIONS: opts->codec_options = optarg; break; + case OPT_FORCE_ADB_FORWARD: + opts->force_adb_forward = true; + break; default: // getopt prints the error message on stderr return false; diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 7a873391..67ebf8c0 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -305,6 +305,7 @@ scrcpy(const struct scrcpy_options *options) { .show_touches = options->show_touches, .stay_awake = options->stay_awake, .codec_options = options->codec_options, + .force_adb_forward = options->force_adb_forward, }; if (!server_start(&server, options->serial, ¶ms)) { return false; diff --git a/app/src/scrcpy.h b/app/src/scrcpy.h index 8d324378..70d99433 100644 --- a/app/src/scrcpy.h +++ b/app/src/scrcpy.h @@ -42,6 +42,7 @@ struct scrcpy_options { bool window_borderless; bool mipmaps; bool stay_awake; + bool force_adb_forward; }; #define SCRCPY_OPTIONS_DEFAULT { \ @@ -79,6 +80,7 @@ struct scrcpy_options { .window_borderless = false, \ .mipmaps = true, \ .stay_awake = false, \ + .force_adb_forward = false, \ } bool diff --git a/app/src/server.c b/app/src/server.c index 5ec2441c..fb498d63 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -217,15 +217,20 @@ enable_tunnel_forward_any_port(struct server *server, } static bool -enable_tunnel_any_port(struct server *server, struct port_range port_range) { - if (enable_tunnel_reverse_any_port(server, port_range)) { - return true; +enable_tunnel_any_port(struct server *server, struct port_range port_range, + bool force_adb_forward) { + if (!force_adb_forward) { + // Attempt to use "adb reverse" + if (enable_tunnel_reverse_any_port(server, port_range)) { + return true; + } + + // if "adb reverse" does not work (e.g. over "adb connect"), it + // fallbacks to "adb forward", so the app socket is the client + + LOGW("'adb reverse' failed, fallback to 'adb forward'"); } - // if "adb reverse" does not work (e.g. over "adb connect"), it fallbacks to - // "adb forward", so the app socket is the client - - LOGW("'adb reverse' failed, fallback to 'adb forward'"); return enable_tunnel_forward_any_port(server, port_range); } @@ -384,7 +389,8 @@ server_start(struct server *server, const char *serial, goto error1; } - if (!enable_tunnel_any_port(server, params->port_range)) { + if (!enable_tunnel_any_port(server, params->port_range, + params->force_adb_forward)) { goto error1; } diff --git a/app/src/server.h b/app/src/server.h index ff7acbdb..2215d817 100644 --- a/app/src/server.h +++ b/app/src/server.h @@ -56,6 +56,7 @@ struct server_params { uint16_t display_id; bool show_touches; bool stay_awake; + bool force_adb_forward; }; // init default values