ssh-cli 0.4.1

Native Rust CLI that gives LLMs (Claude Code, Cursor, Windsurf) the ability to operate remote servers via SSH over stdin/stdout
Documentation
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
//! Cliente SSH real via `russh` 0.62.2.
//!
//! Conexão one-shot: TCP + handshake + auth (senha e/ou chave) + exec com
//! timeout, truncagem de saída e abort remoto best-effort.
//! Host keys: TOFU em `known_hosts` XDG (ver [`super::known_hosts`]).

use crate::erros::{ErroSshCli, ResultadoSshCli};
use secrecy::{ExposeSecret, SecretString};
use std::path::PathBuf;
use tokio::io::{AsyncRead, AsyncWrite};

/// Configuração de uma conexão SSH.
///
/// Construída a partir de um [`crate::vps::modelo::VpsRegistro`] no momento
/// da chamada. Auth: chave privada (preferida) e/ou senha.
#[derive(Clone)]
pub struct ConfiguracaoConexao {
    /// Hostname ou IP do servidor SSH.
    pub host: String,
    /// Porta TCP do servidor SSH (padrão 22).
    pub porta: u16,
    /// Nome de usuário SSH.
    pub usuario: String,
    /// Senha SSH (`SecretString` para zeroize automático); pode ser vazia se key-only.
    pub senha: SecretString,
    /// Caminho da chave privada OpenSSH (opcional).
    pub key_path: Option<String>,
    /// Passphrase da chave (opcional).
    pub key_passphrase: Option<SecretString>,
    /// Timeout total para conexão + handshake + autenticação + exec, em ms.
    pub timeout_ms: u64,
    /// Caminho do arquivo known_hosts (TOFU). `None` = always-trust (só testes).
    pub known_hosts_path: Option<PathBuf>,
    /// Se true, permite substituir fingerprint divergente.
    pub replace_host_key: bool,
}

impl std::fmt::Debug for ConfiguracaoConexao {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ConfiguracaoConexao")
            .field("host", &self.host)
            .field("porta", &self.porta)
            .field("usuario", &self.usuario)
            .field("senha", &"<redacted>")
            .field("key_path", &self.key_path)
            .field(
                "key_passphrase",
                &self.key_passphrase.as_ref().map(|_| "<redacted>"),
            )
            .field("timeout_ms", &self.timeout_ms)
            .field("known_hosts_path", &self.known_hosts_path)
            .field("replace_host_key", &self.replace_host_key)
            .finish()
    }
}

impl ConfiguracaoConexao {
    /// Valida os campos básicos da configuração.
    pub fn validar(&self) -> ResultadoSshCli<()> {
        if self.host.trim().is_empty() {
            return Err(ErroSshCli::ArgumentoInvalido(
                "host vazio em ConfiguracaoConexao".to_string(),
            ));
        }
        if self.porta == 0 {
            return Err(ErroSshCli::ArgumentoInvalido(
                "porta 0 inválida em ConfiguracaoConexao".to_string(),
            ));
        }
        if self.usuario.trim().is_empty() {
            return Err(ErroSshCli::ArgumentoInvalido(
                "usuário vazio em ConfiguracaoConexao".to_string(),
            ));
        }
        let tem_senha = !self.senha.expose_secret().is_empty();
        let tem_key = self.key_path.as_ref().is_some_and(|p| !p.trim().is_empty());
        if !tem_senha && !tem_key {
            return Err(ErroSshCli::ArgumentoInvalido(
                "auth exige senha ou key_path".to_string(),
            ));
        }
        Ok(())
    }
}

/// Saída da execução de um comando SSH remoto.
#[derive(Debug, Clone)]
pub struct SaidaExecucao {
    /// Stdout capturado (possivelmente truncado a `max_chars` codepoints).
    pub stdout: String,
    /// Stderr capturado (possivelmente truncado a `max_chars` codepoints).
    pub stderr: String,
    /// Código de saída. `None` quando o comando foi terminado por sinal ou timeout.
    pub exit_code: Option<i32>,
    /// `true` se `stdout` foi truncado em `max_chars`.
    pub truncado_stdout: bool,
    /// `true` se `stderr` foi truncado em `max_chars`.
    pub truncado_stderr: bool,
    /// Duração total da execução, em milissegundos.
    pub duracao_ms: u64,
}

/// Resultado de uma operação de transferência de arquivo via SCP.
#[derive(Debug, Clone)]
pub struct TransferenciaResultado {
    /// Número de bytes transferidos.
    pub bytes_transferidos: u64,
    /// Duração total em milissegundos.
    pub duracao_ms: u64,
}

/// Trunca uma string UTF-8 a no máximo `max_chars` codepoints.
///
/// Retorna `(string_truncada, truncou)`. Se `max_chars == 0` retorna string vazia.
/// Unicode-safe: opera sobre codepoints via `chars()`, nunca quebra no meio.
#[must_use]
pub fn truncar_utf8(conteudo: &str, max_chars: usize) -> (String, bool) {
    let total = conteudo.chars().count();
    if total <= max_chars {
        return (conteudo.to_string(), false);
    }
    let truncado: String = conteudo.chars().take(max_chars).collect();
    (truncado, true)
}

// =========================================================================
// Trait ClienteSshTrait para permitir mocks em teste.
// =========================================================================

use async_trait::async_trait;
use std::path::Path;

/// Stream bidirecional usado para tunnel SSH (direct-tcpip).
pub trait CanalTunel: AsyncRead + AsyncWrite + Unpin + Send {}

impl<T> CanalTunel for T where T: AsyncRead + AsyncWrite + Unpin + Send {}

/// Trait para cliente SSH que permite implementação real (russh) ou mock para testes.
///
/// Este trait abstrai as operações de conexão SSH para permitir testes unitários
/// sem necessidade de conexão de rede real.
#[async_trait]
pub trait ClienteSshTrait: Send + Sync + 'static {
    /// Conecta a um servidor SSH e autentica com as credenciais fornecidas.
    async fn conectar(cfg: ConfiguracaoConexao) -> Result<Box<Self>, ErroSshCli>
    where
        Self: Sized;

    /// Executa um comando shell remoto e retorna a saída capturada.
    ///
    /// `stdin_data`, se presente, é escrito no canal após o `exec` e antes do loop
    /// de leitura (GAP-SSH-SEC-001: senha sudo/su fora da argv remota).
    async fn executar_comando(
        &mut self,
        cmd: &str,
        max_chars: usize,
        stdin_data: Option<Vec<u8>>,
    ) -> Result<SaidaExecucao, ErroSshCli>;

    /// Faz upload de um arquivo local para o servidor remoto via SCP.
    async fn upload(
        &mut self,
        local: &Path,
        remote: &Path,
    ) -> Result<TransferenciaResultado, ErroSshCli>;

    /// Faz download de um arquivo remoto para o sistema local via SCP.
    async fn download(
        &mut self,
        remote: &Path,
        local: &Path,
    ) -> Result<TransferenciaResultado, ErroSshCli>;

    /// Abre um canal `direct-tcpip` para forwarding de tunnel.
    async fn abrir_canal_tunel(
        &self,
        host_remoto: &str,
        porta_remota: u16,
        endereco_origem: &str,
        porta_origem: u16,
    ) -> Result<Box<dyn CanalTunel>, ErroSshCli>;

    /// Encerra a conexão SSH de forma limpa.
    async fn desconectar(&self) -> Result<(), ErroSshCli>;
}

#[cfg(test)]
/// Mocks de cliente SSH usados em testes unitários.
pub mod mocks {
    use super::*;
    use mockall::mock;

    mock! {
        pub ClienteSsh {}

    #[async_trait]
    impl crate::ssh::cliente::ClienteSshTrait for ClienteSsh {
            async fn conectar(cfg: ConfiguracaoConexao) -> Result<Box<Self>, ErroSshCli>;
            async fn executar_comando(&mut self, cmd: &str, max_chars: usize, stdin_data: Option<Vec<u8>>) -> Result<SaidaExecucao, ErroSshCli>;
            async fn upload(&mut self, local: &Path, remote: &Path) -> Result<TransferenciaResultado, ErroSshCli>;
            async fn download(&mut self, remote: &Path, local: &Path) -> Result<TransferenciaResultado, ErroSshCli>;
            async fn abrir_canal_tunel(
                &self,
                host_remoto: &str,
                porta_remota: u16,
                endereco_origem: &str,
                porta_origem: u16,
            ) -> Result<Box<dyn CanalTunel>, ErroSshCli>;
            async fn desconectar(&self) -> Result<(), ErroSshCli>;
        }
    }
}

// =========================================================================
// Implementação SSH REAL (feature `ssh-real`).
// =========================================================================

#[cfg(feature = "ssh-real")]
mod real {
    use super::{
        CanalTunel, ClienteSshTrait, ConfiguracaoConexao, SaidaExecucao, TransferenciaResultado,
    };
    use crate::erros::{ErroSshCli, ResultadoSshCli};
    use async_trait::async_trait;
    use secrecy::ExposeSecret;
    use std::path::Path;
    use std::sync::Arc;
    use std::time::{Duration, Instant};

    /// Handler russh com TOFU em known_hosts (ou always-trust se path ausente).
    pub struct ManipuladorCliente {
        host: String,
        porta: u16,
        known_hosts_path: Option<std::path::PathBuf>,
        replace_host_key: bool,
        /// Erro de host key capturado (russh Error não carrega nosso tipo).
        host_key_rejeitada: bool,
        detalhe_host_key: Option<String>,
    }

    impl ManipuladorCliente {
        fn novo(cfg: &ConfiguracaoConexao) -> Self {
            Self {
                host: cfg.host.clone(),
                porta: cfg.porta,
                known_hosts_path: cfg.known_hosts_path.clone(),
                replace_host_key: cfg.replace_host_key,
                host_key_rejeitada: false,
                detalhe_host_key: None,
            }
        }
    }

    impl russh::client::Handler for ManipuladorCliente {
        type Error = russh::Error;

        async fn check_server_key(
            &mut self,
            chave_servidor: &russh::keys::ssh_key::PublicKey,
        ) -> Result<bool, Self::Error> {
            let fingerprint = format!(
                "{}",
                chave_servidor.fingerprint(russh::keys::HashAlg::Sha256)
            );

            let Some(path) = self.known_hosts_path.clone() else {
                tracing::warn!("known_hosts ausente: aceitando host key (modo teste)");
                return Ok(true);
            };

            let mut kh = match crate::ssh::known_hosts::KnownHosts::carregar(path) {
                Ok(k) => k,
                Err(e) => {
                    tracing::error!(erro = %e, "falha ao carregar known_hosts");
                    self.host_key_rejeitada = true;
                    self.detalhe_host_key = Some(e.to_string());
                    return Ok(false);
                }
            };

            match crate::ssh::known_hosts::verificar_tofu(
                &mut kh,
                &self.host,
                self.porta,
                &fingerprint,
                self.replace_host_key,
            ) {
                Ok(true) => Ok(true),
                Ok(false) => Ok(false),
                Err(e) => {
                    self.host_key_rejeitada = true;
                    self.detalhe_host_key = Some(e.to_string());
                    tracing::error!(erro = %e, "host key rejeitada");
                    Ok(false)
                }
            }
        }
    }

    /// Cliente SSH ativo com sessão autenticada.
    pub struct ClienteSsh {
        /// Sessão SSH autenticada para operações de baixo nível.
        pub sessao: russh::client::Handle<ManipuladorCliente>,
        cfg: ConfiguracaoConexao,
    }

    impl std::fmt::Debug for ClienteSsh {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.debug_struct("ClienteSsh")
                .field("host", &self.cfg.host)
                .field("porta", &self.cfg.porta)
                .field("usuario", &self.cfg.usuario)
                .field("timeout_ms", &self.cfg.timeout_ms)
                .finish()
        }
    }

    fn mapear_exit_status(exit_status: u32) -> i32 {
        i32::try_from(exit_status).unwrap_or(-1)
    }

    fn processar_mensagem_exec(
        msg: russh::ChannelMsg,
        stdout_bytes: &mut Vec<u8>,
        stderr_bytes: &mut Vec<u8>,
        exit_code: &mut Option<i32>,
    ) -> bool {
        use russh::ChannelMsg;

        match msg {
            ChannelMsg::Data { data } => {
                stdout_bytes.extend_from_slice(&data);
            }
            ChannelMsg::ExtendedData { data, ext } => {
                // ext == 1 → SSH_EXTENDED_DATA_STDERR (RFC 4254 §5.2).
                if ext == 1 {
                    stderr_bytes.extend_from_slice(&data);
                } else {
                    tracing::debug!(ext, "dados estendidos ignorados");
                }
            }
            ChannelMsg::ExitStatus { exit_status } => {
                // russh entrega como u32. Mantemos como i32 para acomodar
                // convenções Unix (shells podem emitir códigos como u8 em
                // wait-status; aqui já é o exit code aplicativo, 0..=255).
                *exit_code = Some(mapear_exit_status(exit_status));
                // NÃO retorna true: aguardar Eof/Close após ExitStatus.
            }
            ChannelMsg::ExitSignal {
                signal_name,
                core_dumped,
                error_message,
                ..
            } => {
                tracing::warn!(
                    ?signal_name,
                    core_dumped,
                    %error_message,
                    "processo remoto terminou por sinal"
                );
                // Sem exit_status → mantemos None.
            }
            ChannelMsg::Eof => {
                tracing::debug!("EOF no canal SSH");
            }
            ChannelMsg::Close => {
                tracing::debug!("canal SSH fechado pelo servidor");
                return true;
            }
            _ => {}
        }

        false
    }

    /// Byte de ACK/OK do protocolo SCP (também usado como terminador do payload).
    const SCP_OK: u8 = 0;

    /// Basename seguro para o wire SCP (sem path separators / control chars).
    fn basename_scp(nome_arquivo: &str) -> String {
        nome_arquivo
            .split(['/', '\\'])
            .next_back()
            .unwrap_or("file")
            .replace(['\n', '\r', '\0'], "_")
    }

    /// Header `C`-line do protocolo SCP (newline real `0x0a`, nunca `\\n` literal).
    #[cfg_attr(not(test), allow(dead_code))]
    fn formatar_header_upload_scp(tamanho: u64, nome_arquivo: &str) -> String {
        formatar_header_upload_scp_com_modo(0o644, tamanho, nome_arquivo)
    }

    /// Header `C` com mode octal (ex.: `0644`).
    fn formatar_header_upload_scp_com_modo(mode: u32, tamanho: u64, nome_arquivo: &str) -> String {
        let nome = basename_scp(nome_arquivo);
        let mode = mode & 0o7777;
        format!("C{mode:04o} {tamanho} {nome}\n")
    }

    /// Linha `T` do protocolo SCP (preserve times / `-p`).
    fn formatar_linha_t_scp(mtime_secs: u64, atime_secs: u64) -> String {
        format!("T{mtime_secs} 0 {atime_secs} 0\n")
    }

    /// Parse da linha `T mtime 0 atime 0`.
    fn parse_linha_t_scp(linha: &str) -> ResultadoSshCli<(u64, u64)> {
        let linha = linha.trim_end_matches(['\0', '\r', '\n']).trim();
        if !linha.starts_with('T') {
            return Err(ErroSshCli::CanalFalhou(format!(
                "linha T SCP inesperada: {linha}"
            )));
        }
        let resto = &linha[1..];
        let partes: Vec<&str> = resto.split_whitespace().collect();
        if partes.len() < 3 {
            return Err(ErroSshCli::CanalFalhou(format!(
                "linha T SCP mal formatada: {linha}"
            )));
        }
        let mtime: u64 = partes[0].parse().map_err(|_| {
            ErroSshCli::CanalFalhou(format!("mtime inválido na linha T: {}", partes[0]))
        })?;
        let atime: u64 = partes[2].parse().map_err(|_| {
            ErroSshCli::CanalFalhou(format!("atime inválido na linha T: {}", partes[2]))
        })?;
        Ok((mtime, atime))
    }

    /// Parse do header `C0mmm size name` → `(mode, tamanho)`.
    fn parse_header_scp(header: &str) -> ResultadoSshCli<(u32, u64)> {
        let header = header.trim_end_matches(['\0', '\r', '\n']).trim();

        if !header.starts_with('C') {
            return Err(ErroSshCli::CanalFalhou(format!(
                "header SCP inesperado: {}",
                header
            )));
        }

        let partes: Vec<&str> = header.split_whitespace().collect();
        if partes.len() < 3 {
            return Err(ErroSshCli::CanalFalhou(format!(
                "header SCP mal formatado: {}",
                header
            )));
        }

        // Campo mode: `C0644` (prefixo `C` + 4 dígitos octais).
        let mode_token = partes[0];
        if mode_token.len() < 2 {
            return Err(ErroSshCli::CanalFalhou(format!(
                "mode SCP ausente no header: {header}"
            )));
        }
        let mode_oct = &mode_token[1..];
        let mode: u32 = u32::from_str_radix(mode_oct, 8)
            .map_err(|_| ErroSshCli::CanalFalhou(format!("mode SCP inválido: {mode_oct}")))?;

        let tamanho = partes[1].parse().map_err(|_| {
            ErroSshCli::CanalFalhou(format!("tamanho inválido no header: {}", partes[1]))
        })?;
        Ok((mode & 0o7777, tamanho))
    }

    /// Mode octal para o header `C` a partir de metadata local.
    fn mode_scp_de_metadata(meta: &std::fs::Metadata) -> u32 {
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            meta.permissions().mode() & 0o7777
        }
        #[cfg(not(unix))]
        {
            let _ = meta;
            0o644
        }
    }

    /// Segundos epoch a partir de SystemTime (best-effort).
    fn system_time_secs(t: std::time::SystemTime) -> u64 {
        t.duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0)
    }

    /// Sufixo do arquivo temporário de download atômico (SCP-022).
    const SCP_PARTIAL_SUFFIX: &str = ".ssh-cli.partial";

    fn caminho_parcial_download(local: &std::path::Path) -> std::path::PathBuf {
        let mut p = local.as_os_str().to_os_string();
        p.push(SCP_PARTIAL_SUFFIX);
        std::path::PathBuf::from(p)
    }

    /// Interpreta o primeiro byte de status SCP: `0`=OK, `1`/`2`=erro (+ mensagem).
    fn interpretar_status_scp(bytes: &[u8]) -> ResultadoSshCli<()> {
        if bytes.is_empty() {
            return Err(ErroSshCli::CanalFalhou(
                "status SCP vazio (esperado ACK 0x00)".to_string(),
            ));
        }
        match bytes[0] {
            SCP_OK => Ok(()),
            1 | 2 => {
                let msg = String::from_utf8_lossy(&bytes[1..]).trim().to_string();
                Err(ErroSshCli::CanalFalhou(if msg.is_empty() {
                    format!("SCP rejeitou a transferência (status {})", bytes[0])
                } else {
                    format!("SCP: {msg}")
                }))
            }
            other => Err(ErroSshCli::CanalFalhou(format!(
                "status SCP inesperado: 0x{other:02x}"
            ))),
        }
    }

    /// Monta `scp -t[p]/-f[p]` com path remoto escapado para o shell remoto.
    ///
    /// OpenSSH: source (`-f`) só emite linha `T` e mode honesto com **`-p`**.
    /// Sink (`-t`) com `-p` aplica mode completo (sem mascarar umask sticky).
    /// Sempre usamos `-p` (SCP-023 bi-direcional).
    fn comando_scp_remoto(modo: &str, remote: &std::path::Path) -> String {
        let path = crate::ssh::packing::escapar_shell_single_quotes(&remote.display().to_string());
        // `modo` esperado: `-t` ou `-f` (sem `-p`); anexamos `p` explicitamente.
        let modo_p = if modo.contains('p') {
            modo.to_string()
        } else {
            format!("{modo}p")
        };
        // Path em single-quotes (sem `--` para máxima compatibilidade OpenSSH scp legado).
        format!("scp {modo_p} {path}")
    }

    /// Aplica mode POSIX do header `C` no arquivo local (best-effort no Unix).
    fn aplicar_mode_local(path: &std::path::Path, mode: u32) -> ResultadoSshCli<()> {
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let perms = std::fs::Permissions::from_mode(mode & 0o7777);
            std::fs::set_permissions(path, perms).map_err(ErroSshCli::Io)?;
        }
        #[cfg(not(unix))]
        {
            let _ = (path, mode);
        }
        Ok(())
    }

    /// Lê o próximo `ChannelMsg::Data` não vazio do canal SCP.
    async fn scp_ler_data<S>(canal: &mut russh::Channel<S>) -> ResultadoSshCli<Vec<u8>>
    where
        S: From<(russh::ChannelId, russh::ChannelMsg)> + Send + Sync + 'static,
    {
        use russh::ChannelMsg;
        loop {
            match canal.wait().await {
                Some(ChannelMsg::Data { data }) => {
                    if data.is_empty() {
                        continue;
                    }
                    return Ok(data.to_vec());
                }
                Some(ChannelMsg::ExtendedData { data, .. }) => {
                    if data.is_empty() {
                        continue;
                    }
                    let msg = String::from_utf8_lossy(data.as_ref()).trim().to_string();
                    return Err(ErroSshCli::CanalFalhou(format!("SCP stderr: {msg}")));
                }
                Some(ChannelMsg::ExitStatus { exit_status }) if exit_status != 0 => {
                    return Err(ErroSshCli::CanalFalhou(format!(
                        "scp encerrou com exit {exit_status}"
                    )));
                }
                Some(ChannelMsg::Close) | None => {
                    return Err(ErroSshCli::CanalFalhou(
                        "canal SCP fechou prematuramente".to_string(),
                    ));
                }
                _ => continue,
            }
        }
    }

    /// Aguarda ACK de status SCP (`0x00`) ou propaga erro `1`/`2`.
    async fn scp_aguardar_status<S>(canal: &mut russh::Channel<S>) -> ResultadoSshCli<()>
    where
        S: From<(russh::ChannelId, russh::ChannelMsg)> + Send + Sync + 'static,
    {
        let data = scp_ler_data(canal).await?;
        interpretar_status_scp(&data)
    }

    /// Lê bytes até incluir newline (header `C`/`T`) ou status de erro `1`/`2`.
    async fn scp_ler_ate_newline<S>(canal: &mut russh::Channel<S>) -> ResultadoSshCli<Vec<u8>>
    where
        S: From<(russh::ChannelId, russh::ChannelMsg)> + Send + Sync + 'static,
    {
        let mut buf = Vec::new();
        loop {
            let chunk = scp_ler_data(canal).await?;
            if buf.is_empty() && matches!(chunk.first().copied(), Some(1 | 2)) {
                return Ok(chunk);
            }
            buf.extend_from_slice(&chunk);
            if buf.contains(&b'\n') {
                return Ok(buf);
            }
            if buf.len() > 16_384 {
                return Err(ErroSshCli::CanalFalhou(
                    "header SCP excessivamente longo".to_string(),
                ));
            }
        }
    }

    impl ClienteSsh {
        /// Conecta e autentica. Todo o fluxo (TCP + handshake + auth) respeita
        /// o `timeout_ms` da configuração.
        ///
        /// # Erros
        /// - [`ErroSshCli::ArgumentoInvalido`] se a configuração for inválida.
        /// - [`ErroSshCli::TimeoutSsh`] se exceder o timeout total.
        /// - [`ErroSshCli::ConexaoFalhou`] em falhas TCP/handshake.
        /// - [`ErroSshCli::AutenticacaoFalhou`] se o servidor rejeitar senha/chave
        ///   (tente `--key`, `--password-stdin` ou `--key-passphrase-stdin`).
        pub async fn conectar(cfg: ConfiguracaoConexao) -> ResultadoSshCli<Self> {
            cfg.validar()?;

            let timeout = Duration::from_millis(cfg.timeout_ms);
            let host = cfg.host.clone();
            let porta = cfg.porta;
            let usuario = cfg.usuario.clone();
            let senha_segura = cfg.senha.clone();
            let key_path = cfg.key_path.clone();
            let key_passphrase = cfg.key_passphrase.clone();
            let handler = ManipuladorCliente::novo(&cfg);

            let config_cliente = Arc::new(russh::client::Config {
                inactivity_timeout: Some(timeout),
                ..Default::default()
            });

            tracing::info!(
                host = %host,
                porta,
                usuario = %usuario,
                timeout_ms = cfg.timeout_ms,
                tem_chave = key_path.is_some(),
                "iniciando conexão SSH"
            );

            let resultado_conexao = tokio::time::timeout(timeout, async move {
                let mut sessao = russh::client::connect(
                    config_cliente,
                    (host.as_str(), porta),
                    handler,
                )
                .await
                .map_err(|e| ErroSshCli::ConexaoFalhou(format!("falha TCP/handshake: {e}")))?;

                // Preferência: chave privada primeiro; fallback senha se ambas presentes.
                let mut autenticado = false;

                if let Some(ref kp) = key_path {
                    let pass = key_passphrase
                        .as_ref()
                        .map(|s| s.expose_secret().to_string());
                    let chave = russh::keys::load_secret_key(kp, pass.as_deref()).map_err(|e| {
                        ErroSshCli::AutenticacaoSsh(format!(
                            "falha ao carregar chave {kp}: {e}"
                        ))
                    })?;
                    let hash = sessao
                        .best_supported_rsa_hash()
                        .await
                        .map_err(|e| {
                            ErroSshCli::ConexaoFalhou(format!("rsa hash: {e}"))
                        })?
                        .flatten();
                    let auth = sessao
                        .authenticate_publickey(
                            usuario.clone(),
                            russh::keys::PrivateKeyWithHashAlg::new(Arc::new(chave), hash),
                        )
                        .await
                        .map_err(|e| {
                            ErroSshCli::ConexaoFalhou(format!("falha auth publickey: {e}"))
                        })?;
                    autenticado = auth.success();
                    if !autenticado {
                        tracing::warn!(host = %host, "auth por chave rejeitada; tentando senha se houver");
                    }
                }

                if !autenticado && !senha_segura.expose_secret().is_empty() {
                    let auth = sessao
                        .authenticate_password(usuario.clone(), senha_segura.expose_secret())
                        .await
                        .map_err(|e| {
                            ErroSshCli::ConexaoFalhou(format!("falha auth password: {e}"))
                        })?;
                    autenticado = auth.success();
                }

                if !autenticado {
                    tracing::warn!(host = %host, usuario = %usuario, "autenticação SSH rejeitada");
                    return Err(ErroSshCli::AutenticacaoFalhou);
                }

                Ok::<_, ErroSshCli>(sessao)
            })
            .await;

            let sessao = match resultado_conexao {
                Ok(Ok(s)) => s,
                Ok(Err(erro)) => return Err(erro),
                Err(_) => return Err(ErroSshCli::TimeoutSsh(cfg.timeout_ms)),
            };

            tracing::info!("conexão SSH autenticada com sucesso");

            Ok(Self { sessao, cfg })
        }

        /// Executa um comando shell remoto e captura stdout/stderr em paralelo.
        ///
        /// Trunca cada stream em `max_chars` codepoints UTF-8. Respeita o
        /// `timeout_ms` da configuração para a execução inteira.
        ///
        /// # Erros
        /// - [`ErroSshCli::CanalFalhou`] em falha ao abrir canal ou enviar `exec`.
        /// - [`ErroSshCli::TimeoutSsh`] se exceder o timeout.
        pub async fn executar_comando(
            &mut self,
            comando: &str,
            max_chars: usize,
            stdin_data: Option<Vec<u8>>,
        ) -> ResultadoSshCli<SaidaExecucao> {
            self.executar_comando_interno(comando, max_chars, true, stdin_data)
                .await
        }

        async fn executar_comando_interno(
            &mut self,
            comando: &str,
            max_chars: usize,
            abort_em_timeout: bool,
            stdin_data: Option<Vec<u8>>,
        ) -> ResultadoSshCli<SaidaExecucao> {
            let inicio = Instant::now();
            let timeout = Duration::from_millis(self.cfg.timeout_ms);

            let resultado = tokio::time::timeout(timeout, async {
                let mut canal = self
                    .sessao
                    .channel_open_session()
                    .await
                    .map_err(|e| ErroSshCli::CanalFalhou(format!("abrir sessão: {e}")))?;

                canal
                    .exec(true, comando)
                    .await
                    .map_err(|e| ErroSshCli::CanalFalhou(format!("exec: {e}")))?;

                // Senha sudo/su no stdin do canal — nunca na cmdline remota (SEC-001).
                if let Some(bytes) = stdin_data.as_ref() {
                    canal
                        .data(&bytes[..])
                        .await
                        .map_err(|e| ErroSshCli::CanalFalhou(format!("stdin canal: {e}")))?;
                    canal
                        .eof()
                        .await
                        .map_err(|e| ErroSshCli::CanalFalhou(format!("eof canal: {e}")))?;
                }

                let mut stdout_bytes: Vec<u8> = Vec::new();
                let mut stderr_bytes: Vec<u8> = Vec::new();
                let mut exit_code: Option<i32> = None;

                while let Some(msg) = canal.wait().await {
                    if processar_mensagem_exec(
                        msg,
                        &mut stdout_bytes,
                        &mut stderr_bytes,
                        &mut exit_code,
                    ) {
                        break;
                    }
                }

                Ok::<_, ErroSshCli>((stdout_bytes, stderr_bytes, exit_code))
            })
            .await;

            let (stdout_bytes, stderr_bytes, exit_code) = match resultado {
                Ok(Ok(t)) => t,
                Ok(Err(erro)) => return Err(erro),
                Err(_) => {
                    if abort_em_timeout {
                        if let Some(padrao) = crate::ssh::packing::padrao_abort_remoto(comando) {
                            let abort_cmd = crate::ssh::packing::empacotar_abort_pkill(&padrao);
                            tracing::warn!(
                                padrao = %padrao,
                                "timeout local; tentando abort remoto best-effort"
                            );
                            let _ = self.tentar_abort_remoto(&abort_cmd).await;
                        }
                    }
                    return Err(ErroSshCli::TimeoutSsh(self.cfg.timeout_ms));
                }
            };

            let stdout_str = String::from_utf8_lossy(&stdout_bytes).to_string();
            let stderr_str = String::from_utf8_lossy(&stderr_bytes).to_string();

            let (stdout_truncado, truncado_stdout) = super::truncar_utf8(&stdout_str, max_chars);
            let (stderr_truncado, truncado_stderr) = super::truncar_utf8(&stderr_str, max_chars);

            let duracao_ms = u64::try_from(inicio.elapsed().as_millis()).unwrap_or(u64::MAX);

            Ok(SaidaExecucao {
                stdout: stdout_truncado,
                stderr: stderr_truncado,
                exit_code,
                truncado_stdout,
                truncado_stderr,
                duracao_ms,
            })
        }

        /// Upload de arquivo local para remote via SCP (protocolo OpenSSH sink).
        ///
        /// One-shot: stream em chunks (sem carregar o arquivo inteiro em RAM).
        ///
        /// # Erros
        /// - [`ErroSshCli::ArquivoNaoEncontrado`] se o arquivo local não existir.
        /// - [`ErroSshCli::ArgumentoInvalido`] se o path local não for arquivo regular.
        /// - [`ErroSshCli::CanalFalhou`] em falha ao abrir canal SCP / status remoto.
        /// - [`ErroSshCli::TimeoutSsh`] se exceder o timeout.
        pub async fn upload(
            &mut self,
            local: &std::path::Path,
            remote: &std::path::Path,
        ) -> ResultadoSshCli<TransferenciaResultado> {
            use russh::ChannelMsg;
            use std::io::Read;
            use std::time::Instant;

            let local_str = local.display().to_string();

            if local.is_dir() {
                return Err(ErroSshCli::ArgumentoInvalido(crate::i18n::t(
                    crate::i18n::Mensagem::ScpUploadSomenteArquivo,
                )));
            }

            let metadados = std::fs::metadata(local).map_err(|e| {
                if e.kind() == std::io::ErrorKind::NotFound {
                    ErroSshCli::ArquivoNaoEncontrado(local_str.clone())
                } else {
                    ErroSshCli::Io(e)
                }
            })?;

            if !metadados.is_file() {
                return Err(ErroSshCli::ArgumentoInvalido(crate::i18n::t(
                    crate::i18n::Mensagem::ScpUploadSomenteArquivo,
                )));
            }

            let tamanho = metadados.len();
            let mode = mode_scp_de_metadata(&metadados);
            let mtime = metadados.modified().ok().map(system_time_secs).unwrap_or(0);
            let atime = metadados
                .accessed()
                .ok()
                .map(system_time_secs)
                .unwrap_or(mtime);
            let nome_arquivo = local.file_name().and_then(|n| n.to_str()).unwrap_or("file");

            let inicio = Instant::now();
            let timeout = Duration::from_millis(self.cfg.timeout_ms);

            let resultado =
                tokio::time::timeout(timeout, async {
                    if crate::signals::cancelado() {
                        return Err(ErroSshCli::ArgumentoInvalido(crate::i18n::t(
                            crate::i18n::Mensagem::OperacaoCancelada,
                        )));
                    }

                    let mut canal =
                        self.sessao.channel_open_session().await.map_err(|e| {
                            ErroSshCli::CanalFalhou(format!("abrir sessão SCP: {e}"))
                        })?;

                    let comando = comando_scp_remoto("-t", remote);
                    canal
                        .exec(true, comando.as_str())
                        .await
                        .map_err(|e| ErroSshCli::CanalFalhou(format!("exec SCP: {e}")))?;

                    // Sink remoto envia ACK (0x00) antes de aceitar o header.
                    scp_aguardar_status(&mut canal).await?;

                    // Preserve times (linha T) antes do header C.
                    let linha_t = formatar_linha_t_scp(mtime, atime);
                    canal
                        .data(linha_t.as_bytes())
                        .await
                        .map_err(|e| ErroSshCli::CanalFalhou(format!("enviar linha T SCP: {e}")))?;
                    scp_aguardar_status(&mut canal).await?;

                    let header = formatar_header_upload_scp_com_modo(mode, tamanho, nome_arquivo);
                    canal
                        .data(header.as_bytes())
                        .await
                        .map_err(|e| ErroSshCli::CanalFalhou(format!("enviar header SCP: {e}")))?;
                    scp_aguardar_status(&mut canal).await?;

                    // SCP-018: stream do disco em chunks (sem fs::read total).
                    let mut arquivo = std::fs::File::open(local).map_err(ErroSshCli::Io)?;
                    let mut buf = vec![0u8; 32_768];
                    loop {
                        if crate::signals::cancelado() {
                            return Err(ErroSshCli::ArgumentoInvalido(crate::i18n::t(
                                crate::i18n::Mensagem::OperacaoCancelada,
                            )));
                        }
                        let n = arquivo.read(&mut buf).map_err(ErroSshCli::Io)?;
                        if n == 0 {
                            break;
                        }
                        canal.data(&buf[..n]).await.map_err(|e| {
                            ErroSshCli::CanalFalhou(format!("enviar bloco SCP: {e}"))
                        })?;
                    }

                    // Terminador de arquivo = byte 0x00 (não data vazio).
                    canal
                        .data([SCP_OK].as_slice())
                        .await
                        .map_err(|e| ErroSshCli::CanalFalhou(format!("enviar EOF SCP: {e}")))?;
                    scp_aguardar_status(&mut canal).await?;

                    let _ = canal.eof().await;
                    while let Some(msg) = canal.wait().await {
                        if let ChannelMsg::Close = msg {
                            break;
                        }
                    }

                    Ok::<_, ErroSshCli>(())
                })
                .await;

            resultado.map_err(|_| ErroSshCli::TimeoutSsh(self.cfg.timeout_ms))??;

            let duracao_ms = u64::try_from(inicio.elapsed().as_millis()).unwrap_or(u64::MAX);

            Ok(TransferenciaResultado {
                bytes_transferidos: tamanho,
                duracao_ms,
            })
        }

        /// Download de arquivo remote para local via SCP (protocolo OpenSSH source).
        ///
        /// Escreve em `{local}.ssh-cli.partial` e faz rename atômico (SCP-022).
        ///
        /// # Erros
        /// - [`ErroSshCli::Io`] se não conseguir escrever o arquivo local.
        /// - [`ErroSshCli::CanalFalhou`] em falha ao abrir canal SCP / status remoto.
        /// - [`ErroSshCli::TimeoutSsh`] se exceder o timeout.
        pub async fn download(
            &mut self,
            remote: &std::path::Path,
            local: &std::path::Path,
        ) -> ResultadoSshCli<TransferenciaResultado> {
            use russh::ChannelMsg;
            use std::io::Write;
            use std::time::{Duration as StdDuration, Instant, UNIX_EPOCH};

            if local.is_dir() {
                return Err(ErroSshCli::ArgumentoInvalido(crate::i18n::t(
                    crate::i18n::Mensagem::ScpDownloadLocalNaoDiretorio,
                )));
            }

            let inicio = Instant::now();
            let timeout = Duration::from_millis(self.cfg.timeout_ms);
            let partial = caminho_parcial_download(local);

            let resultado = tokio::time::timeout(timeout, async {
                if crate::signals::cancelado() {
                    return Err(ErroSshCli::ArgumentoInvalido(crate::i18n::t(
                        crate::i18n::Mensagem::OperacaoCancelada,
                    )));
                }

                let mut canal = self
                    .sessao
                    .channel_open_session()
                    .await
                    .map_err(|e| ErroSshCli::CanalFalhou(format!("abrir sessão SCP: {e}")))?;

                let comando = comando_scp_remoto("-f", remote);
                canal
                    .exec(true, comando.as_str())
                    .await
                    .map_err(|e| ErroSshCli::CanalFalhou(format!("exec SCP: {e}")))?;

                // Source remoto só envia o header após o ACK inicial do sink local.
                canal
                    .data([SCP_OK].as_slice())
                    .await
                    .map_err(|e| ErroSshCli::CanalFalhou(format!("enviar ack inicial: {e}")))?;

                let mut times: Option<(u64, u64)> = None;
                let mut header_bytes = scp_ler_ate_newline(&mut canal).await?;
                // Erro remoto: status 1/2 no primeiro byte.
                if !header_bytes.is_empty() && matches!(header_bytes[0], 1 | 2) {
                    interpretar_status_scp(&header_bytes)?;
                }
                let mut header = String::from_utf8_lossy(&header_bytes).into_owned();
                // Linha T opcional (preserve times).
                if header.trim_start().starts_with('T') {
                    times = Some(parse_linha_t_scp(&header)?);
                    canal
                        .data([SCP_OK].as_slice())
                        .await
                        .map_err(|e| ErroSshCli::CanalFalhou(format!("enviar ack T: {e}")))?;
                    header_bytes = scp_ler_ate_newline(&mut canal).await?;
                    if !header_bytes.is_empty() && matches!(header_bytes[0], 1 | 2) {
                        interpretar_status_scp(&header_bytes)?;
                    }
                    header = String::from_utf8_lossy(&header_bytes).into_owned();
                }
                let (mode_remoto, tamanho) = parse_header_scp(&header)?;

                canal
                    .data([SCP_OK].as_slice())
                    .await
                    .map_err(|e| ErroSshCli::CanalFalhou(format!("enviar ack header: {e}")))?;

                if let Some(pai) = local.parent() {
                    if !pai.as_os_str().is_empty() {
                        std::fs::create_dir_all(pai)?;
                    }
                }

                // SCP-022: escrever no partial; rename só no sucesso.
                let mut arquivo = std::fs::File::create(&partial).map_err(ErroSshCli::Io)?;
                let mut recebidos: u64 = 0;
                let mut pendente: Vec<u8> = Vec::new();

                while recebidos < tamanho {
                    if crate::signals::cancelado() {
                        return Err(ErroSshCli::ArgumentoInvalido(crate::i18n::t(
                            crate::i18n::Mensagem::OperacaoCancelada,
                        )));
                    }
                    if pendente.is_empty() {
                        let chunk = scp_ler_data(&mut canal).await?;
                        pendente.extend_from_slice(&chunk);
                    }
                    let falta = (tamanho - recebidos) as usize;
                    let usar = falta.min(pendente.len());
                    arquivo
                        .write_all(&pendente[..usar])
                        .map_err(ErroSshCli::Io)?;
                    recebidos += usar as u64;
                    pendente.drain(..usar);
                }

                // Após payload, source envia 0x00 final (pode já estar em `pendente`).
                if pendente.is_empty() {
                    match scp_ler_data(&mut canal).await {
                        Ok(trail) => pendente.extend_from_slice(&trail),
                        Err(_) if recebidos == tamanho => {}
                        Err(e) => return Err(e),
                    }
                }
                if pendente.first() == Some(&SCP_OK) {
                    pendente.remove(0);
                } else if !pendente.is_empty() {
                    return Err(ErroSshCli::CanalFalhou(format!(
                        "terminador SCP inesperado após payload (0x{:02x})",
                        pendente[0]
                    )));
                }

                arquivo.flush().map_err(ErroSshCli::Io)?;
                let _ = arquivo.sync_data();
                drop(arquivo);

                canal
                    .data([SCP_OK].as_slice())
                    .await
                    .map_err(|e| ErroSshCli::CanalFalhou(format!("enviar ack final: {e}")))?;

                let _ = canal.eof().await;
                while let Some(msg) = canal.wait().await {
                    if matches!(msg, ChannelMsg::Close) {
                        break;
                    }
                }

                // SCP-022b: aplicar mode/times no partial ANTES do rename atômico.
                // Assim falha de metadados não deixa `local` com conteúdo de sucesso parcial.
                aplicar_mode_local(&partial, mode_remoto)?;
                if let Some((mtime, atime)) = times {
                    let mtime_st = UNIX_EPOCH + StdDuration::from_secs(mtime);
                    let atime_st = UNIX_EPOCH + StdDuration::from_secs(atime);
                    let ft = std::fs::FileTimes::new()
                        .set_modified(mtime_st)
                        .set_accessed(atime_st);
                    if let Ok(f) = std::fs::File::options().write(true).open(&partial) {
                        let _ = f.set_times(ft);
                    }
                }

                std::fs::rename(&partial, local).map_err(ErroSshCli::Io)?;
                // GraphRAG escrita atômica: fsync do diretório pai após rename (best-effort).
                if let Some(pai) = local.parent() {
                    if !pai.as_os_str().is_empty() {
                        if let Ok(dir) = std::fs::File::open(pai) {
                            let _ = dir.sync_all();
                        }
                    }
                }

                Ok::<_, ErroSshCli>(recebidos)
            })
            .await;

            match resultado {
                Ok(Ok(recebidos)) => {
                    let duracao_ms =
                        u64::try_from(inicio.elapsed().as_millis()).unwrap_or(u64::MAX);
                    Ok(TransferenciaResultado {
                        bytes_transferidos: recebidos,
                        duracao_ms,
                    })
                }
                Ok(Err(e)) => {
                    let _ = std::fs::remove_file(&partial);
                    // Se rename já ocorreu e algo falhou depois (fsync best-effort não falha),
                    // ainda removemos partial; `local` só existe após rename bem-sucedido.
                    Err(e)
                }
                Err(_) => {
                    let _ = std::fs::remove_file(&partial);
                    Err(ErroSshCli::TimeoutSsh(self.cfg.timeout_ms))
                }
            }
        }

        /// Abort remoto best-effort: reconecta com timeout curto e executa pkill.
        async fn tentar_abort_remoto(&self, abort_cmd: &str) -> ResultadoSshCli<()> {
            // Implementação inline (sem chamar executar_comando_interno) evita
            // recursão async detectada pelo compilador.
            let mut cfg_abort = self.cfg.clone();
            cfg_abort.timeout_ms = cfg_abort.timeout_ms.clamp(3_000, 10_000);
            let outro = match Self::conectar(cfg_abort).await {
                Ok(c) => c,
                Err(e) => {
                    tracing::debug!(erro = %e, "abort remoto não pôde reconectar");
                    return Err(e);
                }
            };
            let timeout = Duration::from_millis(outro.cfg.timeout_ms);
            let _ = tokio::time::timeout(timeout, async {
                let mut canal = outro
                    .sessao
                    .channel_open_session()
                    .await
                    .map_err(|e| ErroSshCli::CanalFalhou(format!("abort canal: {e}")))?;
                canal
                    .exec(true, abort_cmd)
                    .await
                    .map_err(|e| ErroSshCli::CanalFalhou(format!("abort exec: {e}")))?;
                while let Some(msg) = canal.wait().await {
                    if matches!(msg, russh::ChannelMsg::Close) {
                        break;
                    }
                }
                Ok::<(), ErroSshCli>(())
            })
            .await;
            let _ = outro.desconectar().await;
            Ok(())
        }

        /// Encerra a sessão SSH de forma limpa.
        ///
        /// # Erros
        /// Propaga falha se `disconnect` retornar erro do transporte.
        pub async fn desconectar(&self) -> ResultadoSshCli<()> {
            let resultado = self
                .sessao
                .disconnect(russh::Disconnect::ByApplication, "encerrando", "pt-BR")
                .await;
            match resultado {
                Ok(()) => {
                    tracing::info!("sessão SSH encerrada");
                    Ok(())
                }
                Err(e) => {
                    tracing::warn!(erro = %e, "falha ao encerrar sessão SSH");
                    Err(ErroSshCli::ConexaoFalhou(format!(
                        "falha ao desconectar: {e}"
                    )))
                }
            }
        }

        /// Abre canal direct-tcpip para forwarding SSH.
        pub async fn abrir_canal_tunel(
            &self,
            host_remoto: &str,
            porta_remota: u16,
            endereco_origem: &str,
            porta_origem: u16,
        ) -> ResultadoSshCli<Box<dyn CanalTunel>> {
            let canal = self
                .sessao
                .channel_open_direct_tcpip(
                    host_remoto.to_string(),
                    u32::from(porta_remota),
                    endereco_origem.to_string(),
                    u32::from(porta_origem),
                )
                .await
                .map_err(|e| {
                    ErroSshCli::CanalFalhou(format!(
                        "falha ao abrir canal direct-tcpip para {}:{}: {}",
                        host_remoto, porta_remota, e
                    ))
                })?;

            Ok(Box::new(canal.into_stream()))
        }
    }

    #[async_trait]
    impl ClienteSshTrait for ClienteSsh {
        async fn conectar(cfg: ConfiguracaoConexao) -> Result<Box<Self>, ErroSshCli> {
            Self::conectar(cfg).await.map(Box::new)
        }

        async fn executar_comando(
            &mut self,
            cmd: &str,
            max_chars: usize,
            stdin_data: Option<Vec<u8>>,
        ) -> Result<SaidaExecucao, ErroSshCli> {
            Self::executar_comando(self, cmd, max_chars, stdin_data).await
        }

        async fn upload(
            &mut self,
            local: &Path,
            remote: &Path,
        ) -> Result<TransferenciaResultado, ErroSshCli> {
            Self::upload(self, local, remote).await
        }

        async fn download(
            &mut self,
            remote: &Path,
            local: &Path,
        ) -> Result<TransferenciaResultado, ErroSshCli> {
            Self::download(self, remote, local).await
        }

        async fn abrir_canal_tunel(
            &self,
            host_remoto: &str,
            porta_remota: u16,
            endereco_origem: &str,
            porta_origem: u16,
        ) -> Result<Box<dyn CanalTunel>, ErroSshCli> {
            Self::abrir_canal_tunel(
                self,
                host_remoto,
                porta_remota,
                endereco_origem,
                porta_origem,
            )
            .await
        }

        async fn desconectar(&self) -> Result<(), ErroSshCli> {
            Self::desconectar(self).await
        }
    }

    #[cfg(test)]
    mod testes_real {
        use super::{
            caminho_parcial_download, comando_scp_remoto, formatar_header_upload_scp,
            formatar_header_upload_scp_com_modo, formatar_linha_t_scp, interpretar_status_scp,
            mapear_exit_status, parse_header_scp, parse_linha_t_scp, processar_mensagem_exec,
            SCP_PARTIAL_SUFFIX,
        };

        #[test]
        fn mapear_exit_status_normal() {
            assert_eq!(mapear_exit_status(0), 0);
            assert_eq!(mapear_exit_status(255), 255);
        }

        #[test]
        fn mapear_exit_status_overflow_retorna_menos_um() {
            assert_eq!(mapear_exit_status(u32::MAX), -1);
        }

        #[test]
        fn parse_header_scp_valido_retorna_mode_e_tamanho() {
            let (mode, tamanho) =
                parse_header_scp("C0644 42 arquivo.txt\n").expect("header válido");
            assert_eq!(mode, 0o644);
            assert_eq!(tamanho, 42);
            let (mode2, _) = parse_header_scp("C0600 1 x\n").expect("600");
            assert_eq!(mode2, 0o600);
        }

        #[test]
        fn parse_header_scp_invalido_retorna_erro() {
            assert!(parse_header_scp("ERRO").is_err());
            assert!(parse_header_scp("C0644 sem_tamanho").is_err());
            assert!(parse_header_scp("C0644 abc arquivo").is_err());
            assert!(parse_header_scp("Czzzz 1 x\n").is_err());
        }

        #[test]
        fn processar_mensagem_exec_trata_stdout_stderr_e_close() {
            let mut stdout = Vec::new();
            let mut stderr = Vec::new();
            let mut exit_code = None;

            let deve_parar = processar_mensagem_exec(
                russh::ChannelMsg::Data {
                    data: b"stdout".to_vec().into(),
                },
                &mut stdout,
                &mut stderr,
                &mut exit_code,
            );
            assert!(!deve_parar);
            assert_eq!(stdout, b"stdout");

            let deve_parar = processar_mensagem_exec(
                russh::ChannelMsg::ExtendedData {
                    data: b"stderr".to_vec().into(),
                    ext: 1,
                },
                &mut stdout,
                &mut stderr,
                &mut exit_code,
            );
            assert!(!deve_parar);
            assert_eq!(stderr, b"stderr");

            let _ = processar_mensagem_exec(
                russh::ChannelMsg::ExitStatus { exit_status: 17 },
                &mut stdout,
                &mut stderr,
                &mut exit_code,
            );
            assert_eq!(exit_code, Some(17));

            let deve_parar = processar_mensagem_exec(
                russh::ChannelMsg::Close,
                &mut stdout,
                &mut stderr,
                &mut exit_code,
            );
            assert!(deve_parar);
        }

        #[test]
        fn formatar_header_upload_scp_gera_formato_esperado() {
            let header = formatar_header_upload_scp(123, "arquivo.txt");
            // Wire protocol: newline real (0x0a), NÃO a sequência literal '\'+'n'.
            assert_eq!(header, "C0644 123 arquivo.txt\n");
            assert_eq!(header.as_bytes().last().copied(), Some(b'\n'));
            assert!(
                !header.as_bytes().windows(2).any(|w| w == *b"\\n"),
                "header não deve conter backslash-n literal"
            );
        }

        #[test]
        fn formatar_header_upload_scp_usa_basename() {
            let header = formatar_header_upload_scp(1, "/tmp/dir/nome.bin");
            assert_eq!(header, "C0644 1 nome.bin\n");
        }

        #[test]
        fn interpretar_status_scp_ok_e_erro() {
            assert!(interpretar_status_scp(&[0]).is_ok());
            assert!(interpretar_status_scp(&[1, b'f', b'a', b'i', b'l']).is_err());
            assert!(interpretar_status_scp(&[]).is_err());
        }

        #[test]
        fn comando_scp_remoto_escapa_path_e_usa_p() {
            let cmd = comando_scp_remoto("-t", std::path::Path::new("/tmp/a b.txt"));
            assert_eq!(cmd, "scp -tp '/tmp/a b.txt'");
            let cmd_f = comando_scp_remoto("-f", std::path::Path::new("/var/log/a.log"));
            assert_eq!(cmd_f, "scp -fp '/var/log/a.log'");
            // Idempotente se já contiver p.
            assert_eq!(
                comando_scp_remoto("-fp", std::path::Path::new("/x")),
                "scp -fp '/x'"
            );
        }

        #[test]
        fn formatar_linha_t_scp_formato() {
            let t = formatar_linha_t_scp(1_700_000_000, 1_700_000_001);
            assert_eq!(t, "T1700000000 0 1700000001 0\n");
            assert_eq!(t.as_bytes().last().copied(), Some(b'\n'));
        }

        #[test]
        fn parse_linha_t_scp_ok() {
            let (m, a) = parse_linha_t_scp("T100 0 200 0\n").expect("T ok");
            assert_eq!((m, a), (100, 200));
        }

        #[test]
        fn formatar_header_com_modo() {
            let h = formatar_header_upload_scp_com_modo(0o755, 10, "x.sh");
            assert_eq!(h, "C0755 10 x.sh\n");
        }

        #[test]
        fn caminho_parcial_download_sufixo() {
            let p = caminho_parcial_download(std::path::Path::new("/tmp/out.bin"));
            assert!(p.to_string_lossy().ends_with(SCP_PARTIAL_SUFFIX));
            assert!(p.to_string_lossy().contains("out.bin"));
        }

        #[test]
        fn processar_mensagem_exec_ignora_extendido_com_codigo_diferente_de_stderr() {
            let mut stdout = Vec::new();
            let mut stderr = Vec::new();
            let mut exit_code = None;

            let deve_parar = processar_mensagem_exec(
                russh::ChannelMsg::ExtendedData {
                    data: b"nao-e-stderr".to_vec().into(),
                    ext: 2,
                },
                &mut stdout,
                &mut stderr,
                &mut exit_code,
            );

            assert!(!deve_parar);
            assert!(stdout.is_empty());
            assert!(stderr.is_empty());
            assert!(exit_code.is_none());
        }

        #[test]
        fn processar_mensagem_exec_trata_exit_signal_e_eof_sem_encerrar_loop() {
            let mut stdout = Vec::new();
            let mut stderr = Vec::new();
            let mut exit_code = Some(7);

            let deve_parar_signal = processar_mensagem_exec(
                russh::ChannelMsg::ExitSignal {
                    signal_name: russh::Sig::TERM,
                    core_dumped: false,
                    error_message: "encerrado".to_string(),
                    lang_tag: "pt-BR".to_string(),
                },
                &mut stdout,
                &mut stderr,
                &mut exit_code,
            );

            let deve_parar_eof = processar_mensagem_exec(
                russh::ChannelMsg::Eof,
                &mut stdout,
                &mut stderr,
                &mut exit_code,
            );

            assert!(!deve_parar_signal);
            assert!(!deve_parar_eof);
            assert_eq!(exit_code, Some(7));
        }

        #[test]
        fn processar_mensagem_exec_ignora_variantes_sem_tratamento_especifico() {
            let mut stdout = Vec::new();
            let mut stderr = Vec::new();
            let mut exit_code = None;

            let deve_parar = processar_mensagem_exec(
                russh::ChannelMsg::WindowAdjusted { new_size: 2048 },
                &mut stdout,
                &mut stderr,
                &mut exit_code,
            );

            assert!(!deve_parar);
            assert!(stdout.is_empty());
            assert!(stderr.is_empty());
            assert!(exit_code.is_none());
        }
    }
}

#[cfg(feature = "ssh-real")]
pub use real::{ClienteSsh, ManipuladorCliente};

// =========================================================================
// Stub usado quando a feature `ssh-real` está DESATIVADA.
// =========================================================================

#[cfg(not(feature = "ssh-real"))]
mod stub {
    use super::{ConfiguracaoConexao, SaidaExecucao, TransferenciaResultado};
    use crate::erros::ErroSshCli;
    use crate::ssh::cliente::ClienteSshTrait;
    use async_trait::async_trait;
    use std::path::Path;

    /// Stub quando `ssh-real` está desativado: sempre retorna
    /// [`ErroSshCli::ConexaoFalhou`].
    #[derive(Debug)]
    pub struct ClienteSsh;

    #[async_trait]
    impl ClienteSshTrait for ClienteSsh {
        async fn conectar(_cfg: ConfiguracaoConexao) -> Result<Box<Self>, ErroSshCli> {
            Err(ErroSshCli::ConexaoFalhou(
                "feature `ssh-real` está desabilitada; recompile com --features ssh-real".into(),
            ))
        }

        async fn executar_comando(
            &mut self,
            _cmd: &str,
            _max_chars: usize,
            _stdin_data: Option<Vec<u8>>,
        ) -> Result<SaidaExecucao, ErroSshCli> {
            Err(ErroSshCli::CanalFalhou(
                "stub sem russh: feature `ssh-real` desabilitada".into(),
            ))
        }

        async fn upload(
            &mut self,
            _local: &Path,
            _remote: &Path,
        ) -> Result<TransferenciaResultado, ErroSshCli> {
            Err(ErroSshCli::CanalFalhou(
                "stub sem russh: feature `ssh-real` desabilitada".into(),
            ))
        }

        async fn download(
            &mut self,
            _remote: &Path,
            _local: &Path,
        ) -> Result<TransferenciaResultado, ErroSshCli> {
            Err(ErroSshCli::CanalFalhou(
                "stub sem russh: feature `ssh-real` desabilitada".into(),
            ))
        }

        async fn abrir_canal_tunel(
            &self,
            _host_remoto: &str,
            _porta_remota: u16,
            _endereco_origem: &str,
            _porta_origem: u16,
        ) -> Result<Box<dyn super::CanalTunel>, ErroSshCli> {
            Err(ErroSshCli::CanalFalhou(
                "stub sem russh: feature `ssh-real` desabilitada".into(),
            ))
        }

        async fn desconectar(&self) -> Result<(), ErroSshCli> {
            Ok(())
        }
    }
}

#[cfg(not(feature = "ssh-real"))]
pub use stub::ClienteSsh;

// =========================================================================
// Testes unitários (sem rede, sem feature gate).
// =========================================================================

#[cfg(test)]
mod testes {
    use super::*;
    use secrecy::SecretString;

    fn cfg_valida() -> ConfiguracaoConexao {
        ConfiguracaoConexao {
            host: "127.0.0.1".to_string(),
            porta: 22,
            usuario: "root".to_string(),
            senha: SecretString::from("senha-exemplo".to_string()),
            key_path: None,
            key_passphrase: None,
            timeout_ms: 5000,
            known_hosts_path: None,
            replace_host_key: false,
        }
    }

    #[test]
    fn validar_host_vazio_retorna_erro() {
        let mut c = cfg_valida();
        c.host = String::new();
        let r = c.validar();
        assert!(r.is_err());
        let msg = r.unwrap_err().to_string();
        assert!(msg.contains("host"));
    }

    #[test]
    fn validar_host_apenas_espacos_retorna_erro() {
        let mut c = cfg_valida();
        c.host = "   ".to_string();
        assert!(c.validar().is_err());
    }

    #[test]
    fn validar_porta_zero_retorna_erro() {
        let mut c = cfg_valida();
        c.porta = 0;
        let r = c.validar();
        assert!(r.is_err());
        let msg = r.unwrap_err().to_string();
        assert!(msg.contains("porta"));
    }

    #[test]
    fn validar_usuario_vazio_retorna_erro() {
        let mut c = cfg_valida();
        c.usuario = String::new();
        assert!(c.validar().is_err());
    }

    #[test]
    fn validar_configuracao_correta_retorna_ok() {
        assert!(cfg_valida().validar().is_ok());
    }

    #[test]
    fn debug_nao_expoe_senha() {
        let c = cfg_valida();
        let dbg = format!("{c:?}");
        assert!(!dbg.contains("senha-exemplo"));
        assert!(dbg.contains("redacted"));
    }

    #[test]
    fn truncar_utf8_nao_trunca_se_cabe() {
        let (s, t) = truncar_utf8("ola mundo", 100);
        assert_eq!(s, "ola mundo");
        assert!(!t);
    }

    #[test]
    fn truncar_utf8_trunca_string_grande_ascii() {
        let entrada: String = "a".repeat(200);
        let (s, t) = truncar_utf8(&entrada, 50);
        assert_eq!(s.chars().count(), 50);
        assert!(t);
    }

    #[test]
    fn truncar_utf8_preserva_grafemas_acentuados() {
        // 10 codepoints: "á" (1 char) * 10
        let entrada: String = "á".repeat(30);
        let (s, t) = truncar_utf8(&entrada, 10);
        assert_eq!(s.chars().count(), 10);
        // Cada 'á' ocupa 2 bytes em UTF-8 → 10 chars = 20 bytes
        assert_eq!(s.len(), 20);
        assert!(t);
        // Não corta no meio de byte
        assert!(s.chars().all(|c| c == 'á'));
    }

    #[test]
    fn truncar_utf8_com_emojis_nao_quebra() {
        let entrada = "🚀🔒🛡🔑✨🎉💎⚡🌟🔥🎨";
        let (s, t) = truncar_utf8(entrada, 5);
        assert_eq!(s.chars().count(), 5);
        assert!(t);
    }

    #[test]
    fn truncar_utf8_zero_retorna_vazio() {
        let (s, t) = truncar_utf8("abc", 0);
        assert_eq!(s, "");
        assert!(t);
    }

    #[test]
    fn saida_execucao_debug_nao_crasha() {
        let s = SaidaExecucao {
            stdout: "ok".into(),
            stderr: String::new(),
            exit_code: Some(0),
            truncado_stdout: false,
            truncado_stderr: false,
            duracao_ms: 42,
        };
        let _ = format!("{s:?}");
    }

    #[test]
    fn duracao_ms_tipo_compativel() {
        // Garantia estática de que instant elapsed cabe em u64.
        let fake: u64 = 1234;
        assert_eq!(fake, 1234_u64);
    }
}