zettcode-ssh 0.5.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ """Remote coding tools over system OpenSSH."""
2
+
3
+ from .plugin import SSHPlugin
4
+
5
+ __version__ = "0.5.2"
6
+ __all__ = ["SSHPlugin"]
zettcode/ssh/config.py ADDED
@@ -0,0 +1,74 @@
1
+ """Discover selectable literal aliases without executing SSH configuration directives."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import glob
6
+ import shlex
7
+ from pathlib import Path
8
+
9
+ from .transport import HOST
10
+
11
+ MAX_FILES = 64
12
+ MAX_BYTES = 256 * 1024
13
+ MAX_ALIASES = 200
14
+
15
+
16
+ def host_aliases(config: Path | None = None) -> tuple[str, ...]:
17
+ """Read literal Host aliases and bounded Includes; ignore wildcard/negated patterns and Match sections.
18
+
19
+ Authentication and connection settings remain OpenSSH's responsibility. Match exec is never executed.
20
+ """
21
+ root = config if config is not None else Path.home() / ".ssh" / "config"
22
+ aliases: dict[str, None] = {}
23
+ visited: set[Path] = set()
24
+ remaining = MAX_BYTES
25
+
26
+ def read(path: Path, matching: bool = False) -> bool:
27
+ nonlocal remaining
28
+ try:
29
+ path = path.expanduser().resolve()
30
+ if path in visited or len(visited) >= MAX_FILES or remaining <= 0:
31
+ return matching
32
+ visited.add(path)
33
+ with path.open("rb") as handle:
34
+ data = handle.read(remaining + 1)
35
+ if len(data) > remaining:
36
+ remaining = 0
37
+ return matching
38
+ remaining -= len(data)
39
+ lines = data.decode("utf-8").splitlines()
40
+ except (OSError, UnicodeError, RuntimeError):
41
+ return matching
42
+ for line in lines:
43
+ try:
44
+ words = shlex.split(line, comments=True)
45
+ except ValueError:
46
+ continue
47
+ if not words:
48
+ continue
49
+ key, _, inline = words[0].partition("=")
50
+ args = ([inline] if inline else []) + words[1:]
51
+ if args and args[0] == "=":
52
+ args = args[1:]
53
+ key = key.lower()
54
+ if key == "match":
55
+ matching = True
56
+ elif key == "host":
57
+ matching = False
58
+ for alias in args:
59
+ if len(aliases) < MAX_ALIASES and len(alias) <= 255 and HOST.fullmatch(alias):
60
+ aliases.setdefault(alias, None)
61
+ elif key == "include" and not matching:
62
+ for expression in args:
63
+ candidate = Path(expression).expanduser()
64
+ if not candidate.is_absolute():
65
+ candidate = root.expanduser().parent / candidate
66
+ # Limit expansion as well as reads; never traverse an unbounded Include tree.
67
+ for index, included in enumerate(glob.iglob(str(candidate))):
68
+ if index >= MAX_FILES:
69
+ break
70
+ matching = read(Path(included), matching)
71
+ return matching
72
+
73
+ read(root)
74
+ return tuple(aliases)