Reduce bootstrap script length by removing comments and indents

dropbear has 9000 bytes limit on ssh arguments length.
This commit is contained in:
pagedown 2022-03-16 01:03:51 +08:00
parent 91a17e3f0c
commit 150bf1a5b0
No known key found for this signature in database
GPG Key ID: E921CF18AC8FF6EB
3 changed files with 19 additions and 6 deletions

View File

@ -223,13 +223,17 @@ def safe_remove(x: str) -> None:
os.remove(x) os.remove(x)
def prepare_script(ans: str, replacements: Dict[str, str]) -> str: def prepare_script(ans: str, replacements: Dict[str, str], script_type: str) -> str:
for k in ('EXEC_CMD',): for k in ('EXEC_CMD',):
replacements[k] = replacements.get(k, '') replacements[k] = replacements.get(k, '')
def sub(m: 're.Match[str]') -> str: def sub(m: 're.Match[str]') -> str:
return replacements[m.group()] return replacements[m.group()]
if script_type == 'sh':
# Remove comments and indents. The dropbear SSH server has 9000 bytes limit on ssh arguments length.
# Needs to be trimmed before replacing EXEC_CMD to avoid affecting the indentation of user commands.
ans = re.sub(r'^\s*#.*\n|^\s*', '', ans, flags=re.MULTILINE)
return re.sub('|'.join(fr'\b{k}\b' for k in replacements), sub, ans) return re.sub('|'.join(fr'\b{k}\b' for k in replacements), sub, ans)
@ -269,7 +273,7 @@ def bootstrap_script(
if request_data: if request_data:
sd.update(sensitive_data) sd.update(sensitive_data)
replacements.update(sensitive_data) replacements.update(sensitive_data)
return prepare_script(ans, sd), replacements, shm return prepare_script(ans, sd, script_type), replacements, shm
def get_ssh_cli() -> Tuple[Set[str], Set[str]]: def get_ssh_cli() -> Tuple[Set[str], Set[str]]:

View File

@ -10,7 +10,8 @@ from functools import lru_cache
from kittens.ssh.config import load_config from kittens.ssh.config import load_config
from kittens.ssh.main import ( from kittens.ssh.main import (
bootstrap_script, get_connection_data, wrap_bootstrap_script bootstrap_script, get_connection_data, get_remote_command,
wrap_bootstrap_script
) )
from kittens.ssh.options.types import Options as SSHOptions from kittens.ssh.options.types import Options as SSHOptions
from kittens.ssh.options.utils import DELETE_ENV_VAR from kittens.ssh.options.utils import DELETE_ENV_VAR
@ -78,6 +79,11 @@ print(' '.join(map(str, buf)))'''), lines=13, cols=77)
self.ae(parse(conf, '1').env, {'a': 'c', 'b': 'b'}) self.ae(parse(conf, '1').env, {'a': 'c', 'b': 'b'})
self.ae(parse(conf, '2').env, {'a': 'c', 'b': 'b'}) self.ae(parse(conf, '2').env, {'a': 'c', 'b': 'b'})
def test_ssh_bootstrap_sh_cmd_limit(self):
rcmd, _, _ = get_remote_command([], SSHOptions({'interpreter': 'sh'}))
# dropbear has a 9000 bytes maximum command length limit
self.assertLessEqual(sum(len(x) for x in rcmd), 9000)
@property @property
@lru_cache() @lru_cache()
def all_possible_sh(self): def all_possible_sh(self):

View File

@ -274,7 +274,8 @@ exec_zsh_with_integration() {
export ZDOTDIR="$shell_integration_dir/zsh" export ZDOTDIR="$shell_integration_dir/zsh"
exec "$login_shell" "-l" exec "$login_shell" "-l"
fi fi
unset KITTY_ORIG_ZDOTDIR # ensure this is not propagated # ensure this is not propagated
unset KITTY_ORIG_ZDOTDIR
} }
exec_fish_with_integration() { exec_fish_with_integration() {
@ -313,8 +314,10 @@ exec_with_shell_integration() {
} }
execute_sh_with_posix_env() { execute_sh_with_posix_env() {
[ "$shell_name" = "sh" ] || return # only for sh as that is likely to be POSIX compliant # only for sh as that is likely to be POSIX compliant
command "$login_shell" -l -c ":" > /dev/null 2> /dev/null && return # sh supports -l so use that [ "$shell_name" = "sh" ] || return
# sh supports -l so use that
command "$login_shell" -l -c ":" > /dev/null 2> /dev/null && return
[ -z "$shell_integration_dir" ] && die "Could not read data over tty ssh kitten cannot function" [ -z "$shell_integration_dir" ] && die "Could not read data over tty ssh kitten cannot function"
sh_dir="$shell_integration_dir/sh" sh_dir="$shell_integration_dir/sh"
command mkdir -p "$sh_dir" || die "Creating $sh_dir failed" command mkdir -p "$sh_dir" || die "Creating $sh_dir failed"