Simplify data transmission

This commit is contained in:
Kovid Goyal 2022-02-24 08:32:30 +05:30
parent 3c28a4f723
commit a71e7d7eb1
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 25 additions and 39 deletions

View File

@ -39,7 +39,6 @@ def make_tarfile(hostname: str = '', shell_integration_dest: str = DEFAULT_SHELL
def get_ssh_data(msg: str, shell_integration_dest: str = DEFAULT_SHELL_INTEGRATION_DEST) -> Iterator[bytes]: def get_ssh_data(msg: str, shell_integration_dest: str = DEFAULT_SHELL_INTEGRATION_DEST) -> Iterator[bytes]:
yield b"KITTY_SSH_DATA_START\n"
try: try:
hostname, pwfilename, pw = msg.split(':', 2) hostname, pwfilename, pw = msg.split(':', 2)
except Exception: except Exception:
@ -58,12 +57,9 @@ def get_ssh_data(msg: str, shell_integration_dest: str = DEFAULT_SHELL_INTEGRATI
yield b' error while gathering ssh data\n' yield b' error while gathering ssh data\n'
else: else:
from base64 import standard_b64encode from base64 import standard_b64encode
encoded_data = memoryview(standard_b64encode(data)) encoded_data = standard_b64encode(data)
while encoded_data: yield f"{len(encoded_data)}\n".encode('ascii')
yield encoded_data[:2048] yield encoded_data
yield b'\n'
encoded_data = encoded_data[2048:]
yield b"KITTY_SSH_DATA_END\n"
def safe_remove(x: str) -> None: def safe_remove(x: str) -> None:

View File

@ -42,6 +42,10 @@ print(' '.join(map(str, buf)))'''), lines=13, cols=77)
t('ssh -p 33 main', port=33) t('ssh -p 33 main', port=33)
def test_ssh_bootstrap_script(self): def test_ssh_bootstrap_script(self):
# test simple untar with only sh
with tempfile.TemporaryDirectory() as tdir:
self.check_bootstrap('sh', tdir, 'sh', SHELL_INTEGRATION_VALUE='')
ok_login_shell = '' ok_login_shell = ''
for sh in ('dash', 'zsh', 'bash', 'posh', 'sh'): for sh in ('dash', 'zsh', 'bash', 'posh', 'sh'):
q = shutil.which(sh) q = shutil.which(sh)

View File

@ -3,18 +3,17 @@
# Distributed under terms of the GPLv3 license. # Distributed under terms of the GPLv3 license.
# read the transmitted data from STDIN # read the transmitted data from STDIN
saved_tty_settings=$(command stty -g)
command stty raw -echo
encoded_data_file=$(mktemp)
cleanup_on_bootstrap_exit() { cleanup_on_bootstrap_exit() {
[ ! -z "$encoded_data_file" ] && command rm -f "$encoded_data_file"
[ ! -z "$saved_tty_settings" ] && command stty "$saved_tty_settings" [ ! -z "$saved_tty_settings" ] && command stty "$saved_tty_settings"
} }
trap 'cleanup_on_bootstrap_exit' EXIT
die() { echo "$*" >/dev/stderr; cleanup_on_bootstrap_exit; exit 1; } die() { echo "$*" >/dev/stderr; cleanup_on_bootstrap_exit; exit 1; }
debug() { printf "\033P@kitty-print|%s\033\\" "$(printf "%s" "debug: $1" | base64)"; } debug() { printf "\033P@kitty-print|%s\033\\" "$(printf "%s" "debug: $1" | base64)"; }
saved_tty_settings=$(command stty -g)
command stty raw min 1 time 0 || die "stty not available"
trap 'cleanup_on_bootstrap_exit' EXIT
data_started="n" data_started="n"
data_complete="n" data_complete="n"
pending_data="" pending_data=""
@ -34,38 +33,25 @@ data_password="DATA_PASSWORD"
password_filename="PASSWORD_FILENAME" password_filename="PASSWORD_FILENAME"
pending_data="" pending_data=""
data_complete="n" data_complete="n"
printf "\033P@kitty-ssh|%s:%s:%s\033\\" "$hostname" "$password_filename" "$data_password"
while [ "$data_complete" = "n" ]; do printf "\033P@kitty-ssh|%s:%s:%s\033\\" "$hostname" "$password_filename" "$data_password"
IFS= read -r line || die "Incomplete ssh data"; IFS= read -r size || die "Failed to read data size";
case "$line" in case "$size" in
*"KITTY_SSH_DATA_START") " "*)
prefix=$(command expr "$line" : "\(.*\)KITTY_SSH_DATA_START") die "$size";
pending_data="$pending_data$prefix"
data_started="y";
;; ;;
"KITTY_SSH_DATA_END") "")
data_complete="y"; die "Got empty size";
;;
*)
if [ "$data_started" = "y" ]; then
printf "%s" "$line" >> "$encoded_data_file"
else
pending_data="$pending_data$line\n"
fi
;; ;;
esac esac
done command head -c "$size" < /dev/stdin | command base64 -d | command tar xjf - --no-same-owner -C "$HOME"
rc="$?"
command stty "$saved_tty_settings" command stty "$saved_tty_settings"
saved_tty_settings="" saved_tty_settings=""
if [ "$rc" != "0" ]; then die "Failed to extract data transmitted by ssh kitten over the TTY device"; fi
if [ -n "$pending_data" ]; then if [ -n "$pending_data" ]; then
printf "\033P@kitty-echo|%s\033\\" "$(printf "%s" "$pending_data" | base64)" printf "\033P@kitty-echo|%s\033\\" "$(printf "%s" "$pending_data" | base64)"
fi fi
command base64 -d < "$encoded_data_file" | command tar xjf - --no-same-owner -C "$HOME"
rc=$?
command rm -f "$encoded_data_file"
encoded_data_file=""
if [ "$rc" != "0" ]; then die "Failed to extract data transmitted by ssh kitten over the TTY device"; fi
if [ ! -f "$HOME/.terminfo/kitty.terminfo" ]; then die "Extracted data transmitted by ssh kitten is incomplete"; fi if [ ! -f "$HOME/.terminfo/kitty.terminfo" ]; then die "Extracted data transmitted by ssh kitten is incomplete"; fi
# export TERMINFO # export TERMINFO