-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_command.py
More file actions
548 lines (463 loc) · 20.3 KB
/
session_command.py
File metadata and controls
548 lines (463 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
"""
session_command.py - Costruisce i comandi shell per ogni tipo di sessione PCM.
Supporta: SSH, Telnet, SFTP, RDP, VNC, SSH Tunnel, Mosh, Seriale.
"""
import os
import shutil
import subprocess
from typing import Optional, Tuple
import config_manager
# ---------------------------------------------------------------------------
# Helper per il recupero dei percorsi personalizzati
# ---------------------------------------------------------------------------
def _get_tool(cmd_id: str) -> str:
"""
Recupera il comando da eseguire:
1. Controlla se l'utente ha impostato un percorso custom (tool_paths).
2. Altrimenti usa shutil.which per trovare il path assoluto.
3. Fallback sul nome del comando stesso.
"""
settings = config_manager.load_settings()
custom_paths = settings.get("tool_paths", {})
# Mappa gli alias: se PCM cerca "tigervnc", controlla se l'utente ha configurato "xtigervncviewer"
alias_map = {
"tigervnc": "xtigervncviewer",
"vncviewer": "xtigervncviewer"
}
lookup_id = alias_map.get(cmd_id, cmd_id)
if lookup_id in custom_paths and custom_paths[lookup_id].strip():
return custom_paths[lookup_id].strip()
if cmd_id in custom_paths and custom_paths[cmd_id].strip():
return custom_paths[cmd_id].strip()
return shutil.which(cmd_id) or cmd_id
def _tool_exists(cmd_id: str) -> bool:
"""Controlla se lo strumento esiste (sia esso custom o di sistema)."""
tool = _get_tool(cmd_id)
if os.path.isabs(tool) and os.path.exists(tool):
return True
return shutil.which(tool) is not None
# ---------------------------------------------------------------------------
# Builders pubblici
# ---------------------------------------------------------------------------
def build_command(profilo: dict) -> Tuple[Optional[str], str]:
"""Restituisce (comando, modalità)"""
proto = profilo.get("protocol", "ssh").lower()
if proto == "ssh":
mode = profilo.get("ssh_open_mode", "Terminale interno")
if mode.startswith("Terminale esterno"):
return _wrap_pre(_build_ssh(profilo), profilo), "ssh_term_ext"
return _wrap_pre(_build_ssh(profilo), profilo), "embedded"
elif proto == "mosh":
mode = profilo.get("ssh_open_mode", "Terminale interno")
if mode.startswith("Terminale esterno"):
return _wrap_pre(_build_mosh(profilo), profilo), "ssh_term_ext"
return _wrap_pre(_build_mosh(profilo), profilo), "embedded"
elif proto == "telnet":
return _wrap_pre(_build_telnet(profilo), profilo), "embedded"
elif proto == "sftp":
mode = profilo.get("sftp_open_mode", "Browser interno")
if mode.startswith("Browser esterno"):
return _build_sftp_uri(profilo, "browser_ext"), "sftp_external"
elif mode.startswith("Terminale interno"):
return _wrap_pre(_build_sftp_cli(profilo), profilo), "embedded"
elif mode.startswith("Terminale esterno"):
return _wrap_pre(_build_sftp_cli(profilo), profilo), "sftp_term_ext"
else:
return _build_sftp(profilo), "sftp_panel"
elif proto == "file_transfer":
ft_sub = profilo.get("ft_protocol", "SFTP").upper()
if ft_sub == "SFTP":
mode = profilo.get("sftp_open_mode", "Browser interno")
if mode.startswith("Browser esterno"):
return _build_sftp_uri(profilo, "browser_ext"), "sftp_external"
elif mode.startswith("Terminale interno"):
return _wrap_pre(_build_sftp_cli(profilo), profilo), "embedded"
elif mode.startswith("Terminale esterno"):
return _wrap_pre(_build_sftp_cli(profilo), profilo), "sftp_term_ext"
else:
return _build_sftp(profilo), "sftp_panel"
else:
# FTP / FTPS
mode = profilo.get("ftp_open_mode", "Browser interno")
if mode.startswith("Browser esterno"):
return _build_ftp(profilo, modalita="browser_ext"), "ftp_external"
elif mode.startswith("Terminale interno"):
return _wrap_pre(_build_ftp(profilo, modalita="term_int"), profilo), "embedded"
elif mode.startswith("Terminale esterno"):
return _wrap_pre(_build_ftp(profilo, modalita="term_ext"), profilo), "ftp_term_ext"
else:
return _build_ftp(profilo, modalita="browser_int"), "ftp_panel"
elif proto == "ftp":
mode = profilo.get("ftp_open_mode", "Browser interno")
if mode.startswith("Browser esterno"):
return _build_ftp(profilo, modalita="browser_ext"), "ftp_external"
elif mode.startswith("Terminale interno"):
return _wrap_pre(_build_ftp(profilo, modalita="term_int"), profilo), "embedded"
elif mode.startswith("Terminale esterno"):
return _wrap_pre(_build_ftp(profilo, modalita="term_ext"), profilo), "ftp_term_ext"
else:
return _build_ftp(profilo, modalita="browser_int"), "ftp_panel"
elif proto == "rdp":
mode = profilo.get("rdp_open_mode", "external")
if mode == "internal" or "intern" in mode.lower() or "panel" in mode.lower():
return None, "rdp_embedded"
return _build_rdp(profilo), "external"
elif proto == "vnc":
return _build_vnc(profilo), "external"
elif proto == "serial":
return _wrap_pre(_build_serial(profilo), profilo), "serial"
else:
return None, "embedded"
def _wrap_pre(cmd: Optional[str], profilo: dict) -> Optional[str]:
if not cmd:
return cmd
pre = profilo.get("pre_cmd", "").strip()
if not pre:
return cmd
pre_esc = pre.replace("'", r"'\''")
cmd_esc = cmd.replace("'", r"'\''")
return (
"bash -c '"
"echo \">>> Eseguo pre-connessione: " + pre_esc + "\"; "
+ pre_esc + " && "
"echo \">>> Pre-connessione completata. Connetto...\"; "
+ cmd_esc +
" || (echo \">>> Pre-connessione fallita. Connessione annullata.\"; sleep 5)'"
)
# ---------------------------------------------------------------------------
# Protocol Builders
# ---------------------------------------------------------------------------
def _build_ssh(p: dict) -> str:
host = p.get("host", "")
user = p.get("user", "")
port = p.get("port", "22")
pwd = p.get("password", "")
pkey = p.get("private_key", "")
scmd = p.get("startup_cmd", "")
args = [f"-p {port}", "-o StrictHostKeyChecking=accept-new", "-o ServerAliveInterval=15", "-o ServerAliveCountMax=3"]
if pkey and os.path.exists(pkey):
args.append(f"-i '{pkey}'")
if p.get("x11"): args.append("-X")
if p.get("compression"): args.append("-C")
if p.get("keepalive"):
args.append("-o ServerAliveInterval=60")
if p.get("jump_host"):
ju = f"{p.get('jump_user')}@" if p.get('jump_user') else ""
args.append(f"-J {ju}{p.get('jump_host')}:{p.get('jump_port', '22')}")
# Compatibilità SSH legacy (server datati, router, NAS, Cisco, ecc.)
if p.get("legacy_kex"):
args.append("-o KexAlgorithms=+diffie-hellman-group1-sha1,diffie-hellman-group14-sha1")
if p.get("legacy_cipher"):
args.append("-o Ciphers=+aes128-cbc,aes256-cbc,3des-cbc")
if p.get("legacy_hostkey"):
args.append("-o HostKeyAlgorithms=+ssh-rsa,ssh-dss")
if p.get("legacy_mac"):
args.append("-o MACs=+hmac-sha1,hmac-md5")
if p.get("legacy_pubkey"):
args.append("-o PubkeyAcceptedAlgorithms=+ssh-rsa")
args_str = " ".join(args)
target = f"{user}@{host}" if user else host
ssh_exe = _get_tool("ssh")
if pwd and not pkey:
if _tool_exists("sshpass"):
sshpass_exe = _get_tool("sshpass")
base = f"\"{sshpass_exe}\" -f \"$PCM_PWD_FIFO\" \"{ssh_exe}\" {args_str} {target}"
else:
base = f"\"{ssh_exe}\" {args_str} {target}"
else:
base = f"\"{ssh_exe}\" {args_str} {target}"
if scmd:
base += f" -t '{_esc(scmd)}; exec $SHELL -l'"
return base
def _build_telnet(p: dict) -> str:
host = p.get("host", "")
port = p.get("port", "23")
user = p.get("user", "")
telnet_exe = _get_tool("telnet")
if not _tool_exists("telnet"):
return f"bash -c 'echo \"telnet non trovato. Installa telnet.\"; sleep 5'"
cmd = f"\"{telnet_exe}\" {host} {port}"
if user:
cmd = f"\"{telnet_exe}\" -l {user} {host} {port}"
return cmd
def _build_sftp(p: dict) -> str:
host = p.get("host", "")
port = p.get("port", "22")
user = p.get("user", "")
return f"sftp://{user}@{host}:{port}"
def _build_ftp(p: dict, modalita: str = "browser_int") -> str:
host = p.get("host", "")
port = p.get("port", "21")
user = p.get("user", "")
pwd = p.get("password", "")
tls = p.get("ftp_tls", False) or p.get("ft_protocol", "").upper() == "FTPS"
schema = "ftps" if tls else "ftp"
uri = f"{schema}://{user}@{host}:{port}" if user else f"{schema}://{host}:{port}"
if modalita == "browser_ext":
for fm in ("nautilus", "thunar", "dolphin", "nemo", "pcmanfm", "xdg-open"):
if _tool_exists(fm):
return f"\"{_get_tool(fm)}\" '{uri}'"
return f"xdg-open '{uri}'"
if modalita in ("term_int", "term_ext"):
if _tool_exists("lftp"):
lftp_exe = _get_tool("lftp")
uri_cred = f"{schema}://{_esc(user)}:{_esc(pwd)}@{host}:{port}" if user and pwd else uri
return f"\"{lftp_exe}\" -e 'open {uri_cred}' {host}"
elif _tool_exists("ftp"):
ftp_exe = _get_tool("ftp")
if user and pwd:
return f"bash -c 'printf \"open {host} {port}\\nuser {_esc(user)} {_esc(pwd)}\\nbinary\\n\" | \"{ftp_exe}\" -n'"
return f"\"{ftp_exe}\" {host} {port}"
else:
return "bash -c 'echo \"lftp non trovato.\"; sleep 5'"
return uri
def _build_sftp_uri(p: dict, modalita: str = "browser_ext") -> str:
host = p.get("host", "")
port = p.get("port", "22")
user = p.get("user", "")
uri = f"sftp://{user}@{host}:{port}" if user else f"sftp://{host}:{port}"
if modalita == "browser_ext":
for fm in ("nautilus", "thunar", "dolphin", "nemo", "pcmanfm", "xdg-open"):
if _tool_exists(fm):
return f"\"{_get_tool(fm)}\" '{uri}'"
return f"xdg-open '{uri}'"
return uri
def _build_sftp_cli(p: dict) -> str:
host = p.get("host", "")
port = p.get("port", "22")
user = p.get("user", "")
pkey = p.get("private_key", "").strip()
pwd = p.get("password", "")
args = [f"-P {port}", "-o StrictHostKeyChecking=accept-new"]
if pkey and os.path.exists(pkey):
args.append(f"-i '{pkey}'")
args_str = " ".join(args)
target = f"{user}@{host}" if user else host
sftp_exe = _get_tool("sftp")
if pkey and os.path.exists(pkey):
return f"\"{sftp_exe}\" {args_str} {target}"
if pwd:
if _tool_exists("sshpass"):
return f"\"{_get_tool('sshpass')}\" -f \"$PCM_PWD_FIFO\" \"{sftp_exe}\" {args_str} {target}"
elif _tool_exists("lftp"):
uri_cred = f"sftp://{_esc(user)}:{_esc(pwd)}@{host}:{port}" if user else f"sftp://{host}:{port}"
return f"\"{_get_tool('lftp')}\" -e 'open {uri_cred}' {host}"
return f"\"{sftp_exe}\" {args_str} {target}"
def _build_rdp(p: dict) -> str:
host = p.get("host", "")
port = p.get("port", "3389")
user = p.get("user", "")
pwd = p.get("password", "")
domain = p.get("rdp_domain", "").strip()
client = p.get("rdp_client", "xfreerdp3")
exe = _get_tool(client)
if client in ("xfreerdp", "xfreerdp3"):
args = [f"/v:{host}:{port}"]
if user: args.append(f"/u:{user}")
if domain: args.append(f"/d:{domain}")
if pwd: args.append(f"/p:'{_esc(pwd)}'")
args.append("/cert:ignore")
if p.get("fullscreen"): args.append("/f")
if p.get("redirect_clipboard"): args.append("/clipboard")
if p.get("redirect_drives"): args.append("/drive:home,/home")
return f"\"{exe}\" {' '.join(args)}"
elif client == "rdesktop":
args = ["-a 16"]
if user: args.append(f"-u {user}")
if domain: args.append(f"-d {domain}")
if pwd: args.append(f"-p '{_esc(pwd)}'")
if p.get("fullscreen"): args.append("-f")
return f"\"{exe}\" {' '.join(args)} {host}:{port}"
return f"\"{exe}\" {host}:{port}"
def _build_vnc(p: dict) -> str:
host = p.get("host", "")
port = p.get("port", "5900")
pwd = p.get("password", "")
client = p.get("vnc_client", "vncviewer")
def _vnc_idx(val, default=0):
try:
return int(val)
except (ValueError, TypeError):
return default
depth = {0: 32, 1: 16, 2: 8}.get(_vnc_idx(p.get("vnc_color", 0), 0), 32)
qual = {0: 9, 1: 6, 2: 3}.get(_vnc_idx(p.get("vnc_quality", 2), 2), 6)
def _passwd_wrap(exe, extra=""):
return (
f"bash -c 'TMP=$(mktemp); "
f"printf '%s' '{_esc(pwd)}' | vncpasswd -f > \"$TMP\"; "
f'"{exe}" {extra}-passwd "$TMP" {host}::{port}; '
f"rm -f '$TMP''"
)
# Tool personalizzato
custom = _get_custom_tool("vnc", client)
if custom:
exe = custom["path"]
syntax = custom.get("syntax", "")
if syntax == "TigerVNC":
extra = f"-depth {depth} -quality {qual} "
if pwd:
return _passwd_wrap(exe, extra)
return f'"{exe}" {extra}{host}::{port}'
return f'"{exe}" {host}::{port}'
if client == "realvnc-viewer":
exe = _get_tool("realvnc-viewer")
if exe == "realvnc-viewer":
exe = _trova_realvnc()
qlevel = {9: "Full", 6: "Medium", 3: "Low"}.get(qual, "Full")
clevel = {32: "rgb888", 16: "rgb565", 8: "rgb332"}.get(depth, "rgb888")
extra = f"-Quality={qlevel} -ColorLevel={clevel} "
return f'"{exe}" {extra}{host}::{port}'
elif client in ("tigervnc", "xtigervncviewer"):
exe = _get_tool("xtigervncviewer")
extra = f"-depth {depth} -quality {qual} "
if pwd:
return _passwd_wrap(exe, extra)
return f'"{exe}" {extra}{host}::{port}'
elif client == "remmina":
return f'"{_get_tool("remmina")}" -c vnc://{host}:{port}'
elif client == "krdc":
return f'"{_get_tool("krdc")}" vnc://{host}:{port}'
else:
# vncviewer generico: può essere TigerVNC rinominato o altro.
# NON passare -depth/-quality: TigerVNC non li accetta (usa -FullColour
# e -CompressLevel internamente). Lasciamo solo host::port.
exe = _get_tool(client)
if pwd:
return (
f"bash -c 'TMP=$(mktemp); "
f"printf '%s' '{_esc(pwd)}' | vncpasswd -f > \"$TMP\" 2>/dev/null "
f"|| printf '%s' '{_esc(pwd)}' > \"$TMP\"; "
f'"{exe}" --PasswordFile="$TMP" {host}::{port} 2>/dev/null '
f'|| "{exe}" -passwd "$TMP" {host}::{port}; '
f"rm -f '$TMP''"
)
return f'"{exe}" {host}::{port}'
_REALVNC_PATHS = [
"/usr/bin/realvnc-viewer",
"/usr/bin/vncviewer-real",
"/opt/realvnc/VNC-Viewer/vncviewer",
"/opt/VNC/bin/vncviewer",
"/snap/bin/realvnc-viewer",
]
def _trova_realvnc() -> str:
found = shutil.which("realvnc-viewer")
if found: return found
for p in _REALVNC_PATHS:
if os.path.isfile(p) and os.access(p, os.X_OK): return p
return "realvnc-viewer"
def _build_mosh(p: dict) -> str:
host = p.get("host", "")
port = p.get("port", "22")
user = p.get("user", "")
pkey = p.get("private_key", "")
mosh_exe = _get_tool("mosh")
if not _tool_exists("mosh"):
return f"bash -c 'echo \"mosh non trovato.\"; sleep 5'"
if pkey and os.path.exists(pkey):
args = [f"--ssh='{_get_tool('ssh')} -p {port} -i {pkey}'"]
else:
args = [f"--ssh='{_get_tool('ssh')} -p {port}'"]
target = f"{user}@{host}" if user else host
return f"\"{mosh_exe}\" {' '.join(args)} {target}"
def _build_serial(p: dict) -> str:
device = p.get("device", "/dev/ttyUSB0")
baud = p.get("baud", "115200")
if _tool_exists("picocom"):
return f"\"{_get_tool('picocom')}\" -b {baud} {device}"
elif _tool_exists("minicom"):
return f"\"{_get_tool('minicom')}\" -b {baud} -D {device}"
elif _tool_exists("screen"):
return f"\"{_get_tool('screen')}\" {device} {baud}"
else:
return f"bash -c 'echo \"Nessun client seriale trovato.\"; sleep 5'"
def _esc(s: str) -> str:
return s.replace("'", "'\\''")
def check_dipendenze() -> dict:
tools = {
"ssh": _tool_exists("ssh"),
"sshpass": _tool_exists("sshpass"),
"xdotool": _tool_exists("xdotool"),
"xwininfo": _tool_exists("xwininfo"),
"mosh": _tool_exists("mosh"),
"telnet": _tool_exists("telnet"),
"picocom": _tool_exists("picocom"),
}
_terminali = ["xterm", "xfce4-terminal", "gnome-terminal", "konsole",
"alacritty", "kitty", "terminator", "wezterm",
"foot", "tilix", "lxterminal", "mate-terminal", "st"]
for t in _terminali: tools[t] = shutil.which(t) is not None
for c in ["xfreerdp3", "xfreerdp", "rdesktop"]: tools[c] = _tool_exists(c)
for c in ["vncviewer", "realvnc-viewer", "xtigervncviewer", "remmina", "krdc"]:
tools[c] = _tool_exists(c)
try:
import paramiko
tools["paramiko"] = True
except ImportError:
tools["paramiko"] = False
try:
import cryptography
tools["cryptography"] = True
except ImportError:
tools["cryptography"] = False
return tools
def check_dipendenze_categorizzate() -> dict:
"""Controlla le dipendenze categorizzandole per importanza."""
core_deps = {"ssh": _tool_exists("ssh"), "paramiko": True, "cryptography": True}
try:
import paramiko
core_deps["paramiko"] = True
except ImportError:
core_deps["paramiko"] = False
try:
import cryptography
core_deps["cryptography"] = True
except ImportError:
core_deps["cryptography"] = False
_terminali = ["xterm", "xfce4-terminal", "gnome-terminal", "konsole",
"alacritty", "kitty", "terminator", "wezterm",
"foot", "tilix", "lxterminal", "mate-terminal", "st"]
recommended_deps = {
"terminal": any(shutil.which(t) for t in _terminali),
}
optional_deps = {
"sshpass": _tool_exists("sshpass"),
"xwininfo": _tool_exists("xwininfo"),
"mosh": _tool_exists("mosh"),
"telnet": _tool_exists("telnet"),
"picocom": _tool_exists("picocom"),
"rdp_client": any(_tool_exists(c) for c in ["xfreerdp3", "xfreerdp", "rdesktop"]),
"vnc_client": any(_tool_exists(c) for c in ["vncviewer", "realvnc-viewer", "tigervnc", "remmina", "krdc"]),
}
return {
"core": core_deps,
"recommended": recommended_deps,
"optional": optional_deps,
"missing_core": [k for k, v in core_deps.items() if not v],
"missing_recommended": [k for k, v in recommended_deps.items() if not v],
}
def _get_custom_tool(category: str, label: str) -> dict | None:
"""Restituisce il dizionario {label, path, syntax} del tool personalizzato, o None."""
ct = config_manager.load_custom_tools()
for entry in ct.get(category, []):
if entry.get("label") == label:
return entry
return None
def installed_tools(category: str) -> list[str]:
_map = {
"terminal": ["xterm", "xfce4-terminal", "gnome-terminal", "konsole",
"alacritty", "kitty", "terminator", "wezterm",
"foot", "tilix", "lxterminal", "mate-terminal", "st"],
"rdp": ["xfreerdp3", "xfreerdp", "rdesktop"],
"vnc": ["vncviewer", "realvnc-viewer", "tigervnc", "remmina", "krdc"],
}
candidates = _map.get(category, [])
result = [t for t in candidates if shutil.which(t) or _tool_exists(t)]
# Aggiungi client personalizzati
if category in ("vnc", "rdp"):
ct = config_manager.load_custom_tools()
for entry in ct.get(category, []):
label = entry.get("label", "").strip()
path = entry.get("path", "").strip()
if label and path and label not in result:
result.append(label)
return result