Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions tests/phone-ssh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 8 additions & 2 deletions tools/phone-ssh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions tools/phone-ssh-proxy
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,25 @@ 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(
address, 22, type=socket.SOCK_STREAM, flags=socket.AI_NUMERICHOST,
)[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)
Expand Down