T6: user auth (none/publickey/password), session channel, keepalive, SshSession facade

- Auth.ts: SERVICE_REQUEST handshake, none probe, publickey query + signed
  request (sig over string(session_id) || payload-without-signature;
  Ed25519 raw / ECDSA DER->mpint r||s / RSA PKCS1 SHA256/512), password
  fallback; skip-list wait loop (DISCONNECT/IGNORE/UNIMPLEMENTED/DEBUG/
  EXT_INFO/BANNER); msg60 dual meaning handled per phase
- Channel.ts: open/confirm, pty-req/shell/exec/window-change, chunked
  flow-controlled write, WINDOW_ADJUST at half local window, stderr via
  EXTENDED_DATA, exit-status, EOF/CLOSE lifecycle, per-channel promise
  slots (never overlaps transport.waitPacket)
- KeepAlive.ts: keepalive@openssh.com global request every 15s, dead
  after 3 unanswered probes
- SshSession.ts: state machine (IDLE..CLOSED), KEX with TOFU host key
  verify, post-auth dispatcher routes keepalive replies + channel packets,
  server channels/global requests refused, execCommand collects
  stdout/stderr/exitStatus
- Transport.ts: fix stale waitPacket timer rejecting a fresh waiter
  (clear timer on resolve/reject/re-arm) - latent since T3, amplified by
  the T6 auth loop
- DiagPage: username/password inputs + button 10 (connect -> auth ->
  exec 'echo haven-ok && uname -a')
This commit is contained in:
STRIX
2026-09-08 14:46:41 +08:00
parent 1b8f5caeff
commit 9346cbb646
6 changed files with 1345 additions and 2 deletions
+70
View File
@@ -19,6 +19,8 @@ import { ClientProposals } from '../ssh/Algorithms';
import { KexEngine, KexResult, parseExtInfo } from '../ssh/Kex';
import { verifyHostKeyTofu, InMemoryKnownHostStore, HostKeyDecider, HostKeyPrompt,
HostKeyDecision, HostKeyStatus } from '../ssh/HostKeyVerifier';
import { SshSession, SshSessionOptions, ExecResult } from '../ssh/SshSession';
import { AuthResult } from '../ssh/Auth';
import { bytesToUtf8, utf8ToBytes, hexToBytes, bytesToHex, bytesEqual, concatBytes, SshWriter } from '../ssh/SshWire';
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
@@ -28,6 +30,8 @@ import { BusinessError } from '@kit.BasicServicesKit';
struct DiagPage {
@State host: string = '';
@State port: string = '22';
@State username: string = '';
@State password: string = '';
@State logText: string = '';
@State busy: boolean = false;
private scroller: Scroller = new Scroller();
@@ -65,6 +69,23 @@ struct DiagPage {
}
.width('100%')
Row({ space: 8 }) {
TextInput({ placeholder: '用户名', text: this.username })
.layoutWeight(1)
.height(44)
.onChange((value: string) => {
this.username = value;
})
TextInput({ placeholder: '密码', text: this.password })
.layoutWeight(1)
.height(44)
.type(InputType.Password)
.onChange((value: string) => {
this.password = value;
})
}
.width('100%')
Column({ space: 8 }) {
Button('1. TCP 连接与 SSH Banner')
.width('100%')
@@ -111,6 +132,11 @@ struct DiagPage {
.onClick(() => {
this.runTest((): Promise<void> => this.testLiveKex());
})
Button('10. 认证 + 执行命令 (用户名/密码)')
.width('100%')
.onClick(() => {
this.runTest((): Promise<void> => this.testLiveAuth());
})
}
.width('100%')
@@ -751,4 +777,48 @@ struct DiagPage {
transport.close();
}
}
/** 10) Full session: connect → KEX (TOFU) → password auth → exec. */
private async testLiveAuth(): Promise<void> {
const host: string = this.host.trim();
if (host.length === 0) {
this.log('FAIL', '请先输入 SSH 服务器地址');
return;
}
const username: string = this.username.trim();
if (username.length === 0) {
this.log('FAIL', '请先输入用户名');
return;
}
const port: number = this.parsePort();
this.log('INFO', `连接 ${host}:${port} → KEX → 认证 → exec ...`);
const store: InMemoryKnownHostStore = new InMemoryKnownHostStore();
const decider: HostKeyDecider = {
decide: async (prompt: HostKeyPrompt): Promise<HostKeyDecision> => {
this.log('INFO', `主机密钥 [${prompt.status}] ${prompt.hostPort} (${prompt.keyAlgo})`);
this.log('INFO', `指纹: ${prompt.fingerprint}`);
return HostKeyDecision.ACCEPT_ONCE;
}
};
const options: SshSessionOptions = {
knownHostStore: store,
hostKeyDecider: decider
};
const session: SshSession = new SshSession(options);
try {
await session.connect(host, port);
this.log('INFO', 'KEX 完成,开始认证 ...');
const auth: AuthResult = await session.authenticate(username, this.password, null);
this.log('INFO', `认证成功 (方式: ${auth.method})`);
const result: ExecResult = await session.execCommand('echo haven-ok && uname -a', 15000);
this.log('INFO', `exitStatus: ${result.exitStatus}`);
this.log('INFO', `stdout: ${this.trunc(result.stdout, 200)}`);
if (result.stderr.length > 0) {
this.log('INFO', `stderr: ${this.trunc(result.stderr, 200)}`);
}
this.log('PASS', '完整会话流程通过 ✓');
} finally {
session.close();
}
}
}