diff --git a/tests/phone-ssh b/tests/phone-ssh index 407b13fc4..7f162abad 100755 --- a/tests/phone-ssh +++ b/tests/phone-ssh @@ -28,6 +28,15 @@ unset OMAMOBILE_SSH_HOST OMAMOBILE_SSH_KEY OMAMOBILE_SSH_KNOWN_HOSTS OMAMOBILE_S tools/phone-ssh 'printf "%s" "$HOME"' >"$scratch/default" grep -Fx root@172.16.42.1 "$scratch/default" >/dev/null grep -Fx 'printf "%s" "$HOME"' "$scratch/default" >/dev/null +grep -Fx ConnectTimeout=6 "$scratch/default" >/dev/null +OMAMOBILE_SSH_CONNECT_TIMEOUT=19 tools/phone-ssh true >"$scratch/timeout-override" +grep -Fx ConnectTimeout=19 "$scratch/timeout-override" >/dev/null +for bad_timeout in 0 -1 1.5 invalid 999x; do + if OMAMOBILE_SSH_CONNECT_TIMEOUT=$bad_timeout tools/phone-ssh true >"$scratch/bad-timeout" 2>/dev/null; then + echo 'not ok - invalid connection timeout accepted'; exit 1 + fi + [[ ! -s $scratch/bad-timeout ]] +done export OMAMOBILE_SSH_HOST='root@fe80::1234%usb-test' export OMAMOBILE_SSH_KEY="$scratch/key with spaces" printf 'fake identity\n' >"$OMAMOBILE_SSH_KEY" @@ -383,16 +392,25 @@ class RelayTests(unittest.TestCase): (family, socket.SOCK_STREAM, socket.IPPROTO_TCP, "", destination) ]) as resolve, patch.object(proxy.socket, "socket", return_value=sock), \ patch.object(proxy, "relay") as relay: - proxy.main(["--relay", "usb-test", address]) + proxy.main(["--relay", "usb-test", address, "19"]) resolve.assert_called_once_with(address, 22, type=socket.SOCK_STREAM, flags=socket.AI_NUMERICHOST) self.assertEqual(sock.method_calls[:4], [ unittest.mock.call.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, b"usb-test\0"), - unittest.mock.call.settimeout(6), unittest.mock.call.connect(destination), + unittest.mock.call.settimeout(19), unittest.mock.call.connect(destination), unittest.mock.call.settimeout(None), ]) relay.assert_called_once_with(sock) + def test_proxy_command_passes_timeout_to_relay(self): + with patch.object(proxy, "__file__", "/tmp/phone-ssh-proxy"): + from unittest.mock import patch as mock_patch + import contextlib, io + output = io.StringIO() + with contextlib.redirect_stdout(output): + proxy.main(["--command", "usb-test", "192.0.2.1", "17"]) + self.assertIn("--relay usb-test 192.0.2.1 17", output.getvalue()) + def test_binding_failure_cannot_fall_back_to_unbound_connect(self): from unittest.mock import MagicMock sock = MagicMock() diff --git a/tools/phone-ssh b/tools/phone-ssh index 4233b9e03..a58026ff8 100755 --- a/tools/phone-ssh +++ b/tools/phone-ssh @@ -13,6 +13,7 @@ # OMAMOBILE_SSH_HOST_KEY_ALIAS selects the name checked in that file, so # native and recovery systems can keep distinct pins at the same USB address. # OMAMOBILE_SSH_INTERFACE selects a direct interface for this socket only. +# OMAMOBILE_SSH_CONNECT_TIMEOUT sets the connection timeout in positive seconds (default 6). # With explicit CONFIG=/dev/null, the default USB address automatically uses # its unique main-table link route, avoiding VPN policy-route interception. # Set INTERFACE=off to opt out. Other SSH configs retain their own transports. @@ -41,6 +42,10 @@ known=${OMAMOBILE_SSH_KNOWN_HOSTS:-} alias=${OMAMOBILE_SSH_HOST_KEY_ALIAS:-} config=${OMAMOBILE_SSH_CONFIG:-} interface=${OMAMOBILE_SSH_INTERFACE:-auto} +connect_timeout=${OMAMOBILE_SSH_CONNECT_TIMEOUT:-6} +[[ $connect_timeout =~ ^[1-9][0-9]*$ ]] || { + echo 'phone-ssh: OMAMOBILE_SSH_CONNECT_TIMEOUT must be a positive integer' >&2; exit 1; +} proxy="" proxy_helper=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)/phone-ssh-proxy case $interface in @@ -56,7 +61,7 @@ case $interface in } ;; esac if [[ -n $interface ]]; then - proxy=$(python3 "$proxy_helper" --command "$interface" "${target##*@}") + proxy=$(python3 "$proxy_helper" --command "$interface" "${target##*@}" "$connect_timeout") fi [[ -z $alias || ( $alias != -* && $alias =~ ^[a-zA-Z0-9._:-]+$ ) ]] || { echo 'phone-ssh: invalid OMAMOBILE_SSH_HOST_KEY_ALIAS' >&2; exit 1; @@ -122,7 +127,8 @@ PYCONTEXT # short policy hash leaves room for OpenSSH's temporary 17-byte suffix. path="${XDG_RUNTIME_DIR:-/tmp}/omamobile-ssh-$context-%C" fi -opts=(-o LogLevel=ERROR -o ConnectTimeout=6) +# A slow USB link setup can delay the first handshake. +opts=(-o LogLevel=ERROR -o "ConnectTimeout=$connect_timeout") [[ -z $proxy ]] || opts+=(-o "ProxyCommand=$proxy") [[ -z $config ]] || opts+=(-F "$config") if [[ -n $known ]]; then diff --git a/tools/phone-ssh-proxy b/tools/phone-ssh-proxy index 0dec812ef..a7704bd80 100755 --- a/tools/phone-ssh-proxy +++ b/tools/phone-ssh-proxy @@ -82,14 +82,17 @@ def main(args): if args == ["--discover"]: print(discover()) return - if len(args) != 3 or args[0] not in ("--command", "--relay"): - raise ValueError("usage: phone-ssh-proxy --discover | --command|--relay INTERFACE IP") + if len(args) not in (3, 4) or args[0] not in ("--command", "--relay"): + raise ValueError("usage: phone-ssh-proxy --discover | --command INTERFACE IP [TIMEOUT] | --relay INTERFACE IP [TIMEOUT]") interface = interface_name(args[1]) address = str(ipaddress.ip_address(args[2])) + timeout = int(args[3]) if len(args) == 4 and args[3].isdigit() and int(args[3]) > 0 else 6 + if len(args) == 4 and (not args[3].isdigit() or int(args[3]) < 1): + raise ValueError("invalid connection timeout") if args[0] == "--command": # ProxyCommand is parsed by both OpenSSH (% tokens) and a shell. # Embed validated literal arguments, not expanded %h/%p tokens. - command = [sys.executable, str(Path(__file__).resolve()), "--relay", interface, address] + command = [sys.executable, str(Path(__file__).resolve()), "--relay", interface, address, str(timeout)] print(shlex.join(command).replace("%", "%%")) return family, kind, proto, _, destination = socket.getaddrinfo( @@ -97,7 +100,7 @@ def main(args): )[0] with socket.socket(family, kind, proto) as sock: sock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, interface.encode() + b"\0") - sock.settimeout(6) + sock.settimeout(timeout) sock.connect(destination) sock.settimeout(None) relay(sock)