dsh-linked-workspace 0.18.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.en.md +6 -0
- package/README.md +6 -0
- package/docs/API.md +14 -6
- package/docs/VERIFY.md +16 -0
- package/lib/client.js +490 -1
- package/lib/index.js +613 -16
- package/lib/ssh.js +108 -15
- package/package.json +1 -1
package/lib/ssh.js
CHANGED
|
@@ -57,11 +57,29 @@ const STATUS_BY_CODE = Object.freeze({
|
|
|
57
57
|
FS_NOT_FOUND: 404,
|
|
58
58
|
TIMEOUT: 408,
|
|
59
59
|
HOST_KEY_MISMATCH: 409,
|
|
60
|
+
HOST_KEY_UNKNOWN: 409,
|
|
60
61
|
FS_IS_DIR: 409,
|
|
61
62
|
FS_NOT_DIR: 409,
|
|
62
63
|
INTERNAL: 500,
|
|
63
64
|
});
|
|
64
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Decode a command's output with the encoding the remote host actually uses.
|
|
68
|
+
*
|
|
69
|
+
* Most servers speak UTF-8, but a Windows OpenSSH session (or a shell with a
|
|
70
|
+
* legacy code page) answers in GBK, which arrives as mojibake under UTF-8.
|
|
71
|
+
* `TextDecoder` handles both without pulling in another dependency.
|
|
72
|
+
*/
|
|
73
|
+
export function decodeOutput(bundle, encoding = 'utf8') {
|
|
74
|
+
const buffer = Buffer.isBuffer(bundle) ? bundle : Buffer.from(bundle === undefined || bundle === null ? '' : String(bundle), 'utf8');
|
|
75
|
+
if (encoding !== 'gbk') return buffer.toString('utf8');
|
|
76
|
+
try {
|
|
77
|
+
return new TextDecoder('gbk').decode(buffer);
|
|
78
|
+
} catch {
|
|
79
|
+
return buffer.toString('utf8');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
65
83
|
/** Raw error codes that mean "the connection is gone", not "the operation failed". */
|
|
66
84
|
const CONNECTION_ERROR_CODES = new Set([
|
|
67
85
|
'ECONNRESET',
|
|
@@ -868,6 +886,43 @@ async function readKnownHostsStore(file) {
|
|
|
868
886
|
return { version: KNOWN_HOSTS_VERSION, byAuthority };
|
|
869
887
|
}
|
|
870
888
|
|
|
889
|
+
/**
|
|
890
|
+
* Read every remembered host key, with the authorities a user can act on.
|
|
891
|
+
*
|
|
892
|
+
* The plugin shows these in settings so a changed key can be forgotten without
|
|
893
|
+
* hand-editing `known_hosts.json`.
|
|
894
|
+
*/
|
|
895
|
+
export async function listKnownHosts(file) {
|
|
896
|
+
const store = await readKnownHostsStore(file);
|
|
897
|
+
return Object.keys(store.byAuthority)
|
|
898
|
+
.sort()
|
|
899
|
+
.map((authority) => ({
|
|
900
|
+
authority,
|
|
901
|
+
fingerprint: store.byAuthority[authority].fingerprint,
|
|
902
|
+
firstSeen: store.byAuthority[authority].firstSeen || 0,
|
|
903
|
+
}));
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
/**
|
|
907
|
+
* Forget the remembered key for one authority.
|
|
908
|
+
*
|
|
909
|
+
* Returns true when an entry was removed. The next connection is then a normal
|
|
910
|
+
* trust-on-first-use decision again (or a refusal while the policy is strict).
|
|
911
|
+
*/
|
|
912
|
+
export async function forgetKnownHost(file, authority) {
|
|
913
|
+
const wanted = String(authority || '').trim();
|
|
914
|
+
if (!wanted) throw new SshError('BAD_REQUEST', '需要提供 authority(host:port)');
|
|
915
|
+
let removed = false;
|
|
916
|
+
await withFileLock(file, async () => {
|
|
917
|
+
const store = await readKnownHostsStore(file);
|
|
918
|
+
if (!store.byAuthority[wanted]) return;
|
|
919
|
+
delete store.byAuthority[wanted];
|
|
920
|
+
await writeKnownHostsStore(file, store);
|
|
921
|
+
removed = true;
|
|
922
|
+
});
|
|
923
|
+
return removed;
|
|
924
|
+
}
|
|
925
|
+
|
|
871
926
|
async function writeKnownHostsStore(file, store) {
|
|
872
927
|
const dir = path.dirname(file);
|
|
873
928
|
await fs.mkdir(dir, { recursive: true });
|
|
@@ -915,6 +970,7 @@ export class RemoteHost {
|
|
|
915
970
|
#sftpCreating = null;
|
|
916
971
|
#homeDir = null;
|
|
917
972
|
#hostKeyMismatch = null;
|
|
973
|
+
#hostKeyUnknown = null;
|
|
918
974
|
#generation = 0;
|
|
919
975
|
#logger;
|
|
920
976
|
#dataDir;
|
|
@@ -933,7 +989,7 @@ export class RemoteHost {
|
|
|
933
989
|
* }} options
|
|
934
990
|
*/
|
|
935
991
|
constructor(options = {}) {
|
|
936
|
-
const { host, knownHostsFile, connectTimeoutMs, logger, dataDir, credentialFile, onLost } = options || {};
|
|
992
|
+
const { host, knownHostsFile, connectTimeoutMs, logger, dataDir, credentialFile, onLost, hostKeyPolicy } = options || {};
|
|
937
993
|
if (!isPlainObject(host) || !asString(host.host).trim()) {
|
|
938
994
|
throw new SshError('INTERNAL', 'RemoteHost requires a host record with a non-empty "host" field');
|
|
939
995
|
}
|
|
@@ -949,6 +1005,7 @@ export class RemoteHost {
|
|
|
949
1005
|
|
|
950
1006
|
this.host = hostRecord;
|
|
951
1007
|
this.knownHostsFile = knownHostsFile;
|
|
1008
|
+
this.hostKeyPolicy = typeof hostKeyPolicy === 'function' || typeof hostKeyPolicy === 'string' ? hostKeyPolicy : 'tofu';
|
|
952
1009
|
const timeout = Number(connectTimeoutMs);
|
|
953
1010
|
this.connectTimeoutMs = Number.isFinite(timeout) && timeout > 0 ? timeout : DEFAULT_CONNECT_TIMEOUT_MS;
|
|
954
1011
|
this.#logger = typeof logger === 'function' ? logger : null;
|
|
@@ -1010,6 +1067,7 @@ export class RemoteHost {
|
|
|
1010
1067
|
const auth = await this.#resolveAuth();
|
|
1011
1068
|
const knownHosts = await readKnownHostsStore(this.knownHostsFile);
|
|
1012
1069
|
this.#hostKeyMismatch = null;
|
|
1070
|
+
this.#hostKeyUnknown = null;
|
|
1013
1071
|
|
|
1014
1072
|
const client = new ssh2.Client();
|
|
1015
1073
|
this.#generation += 1;
|
|
@@ -1135,23 +1193,42 @@ export class RemoteHost {
|
|
|
1135
1193
|
}
|
|
1136
1194
|
|
|
1137
1195
|
#classifyConnectError(raw) {
|
|
1138
|
-
|
|
1139
|
-
const
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1196
|
+
const earlyMessage = String((raw && raw.message) || raw || 'connection failed');
|
|
1197
|
+
const earlyLevel = raw && raw.level;
|
|
1198
|
+
// The host-key refusals must be recognised before the "already an SshError"
|
|
1199
|
+
// shortcut below: ssh2 reports the denial as a handshake error, which the
|
|
1200
|
+
// SFTP/exec layer has usually converted into a generic INTERNAL SshError by
|
|
1201
|
+
// the time it reaches us. Our own flag is the authoritative signal.
|
|
1202
|
+
const hostKeyRefusal = raw instanceof SshError
|
|
1203
|
+
|| /host denied/i.test(earlyMessage)
|
|
1204
|
+
|| /verification failed/i.test(earlyMessage)
|
|
1205
|
+
|| earlyLevel === 'handshake';
|
|
1206
|
+
if (this.#hostKeyUnknown && hostKeyRefusal) {
|
|
1207
|
+
return new SshError(
|
|
1208
|
+
'HOST_KEY_UNKNOWN',
|
|
1209
|
+
[
|
|
1210
|
+
`Host key for ${this.authority} is not known yet and the policy is "strict":`,
|
|
1211
|
+
`the server presented ${this.#hostKeyUnknown.actual}.`,
|
|
1212
|
+
`Either switch the host key policy back to "tofu" or trust this key on purpose.`,
|
|
1213
|
+
].join(' '),
|
|
1214
|
+
{ cause: raw },
|
|
1215
|
+
);
|
|
1216
|
+
}
|
|
1217
|
+
if (this.#hostKeyMismatch && hostKeyRefusal) {
|
|
1145
1218
|
return new SshError(
|
|
1146
1219
|
'HOST_KEY_MISMATCH',
|
|
1147
1220
|
[
|
|
1148
1221
|
`Host key mismatch for ${this.authority}:`,
|
|
1149
|
-
`expected ${
|
|
1150
|
-
`If this change is expected,
|
|
1222
|
+
`expected ${this.#hostKeyMismatch.expected} but the server presented ${this.#hostKeyMismatch.actual}.`,
|
|
1223
|
+
`If this change is expected, forget the stored key in Settings → Linked workspaces and reconnect.`,
|
|
1151
1224
|
].join(' '),
|
|
1152
1225
|
{ cause: raw },
|
|
1153
1226
|
);
|
|
1154
1227
|
}
|
|
1228
|
+
if (raw instanceof SshError) return raw;
|
|
1229
|
+
const message = earlyMessage;
|
|
1230
|
+
const level = earlyLevel;
|
|
1231
|
+
const code = raw && raw.code;
|
|
1155
1232
|
if (
|
|
1156
1233
|
/all configured authentication methods failed/i.test(message) ||
|
|
1157
1234
|
/cannot parse privatekey/i.test(message) ||
|
|
@@ -1271,6 +1348,12 @@ export class RemoteHost {
|
|
|
1271
1348
|
|
|
1272
1349
|
// -- host key trust -------------------------------------------------------
|
|
1273
1350
|
|
|
1351
|
+
/** The policy can change while the plugin runs, so it is read every time. */
|
|
1352
|
+
#hostKeyPolicy() {
|
|
1353
|
+
const value = typeof this.hostKeyPolicy === 'function' ? this.hostKeyPolicy() : this.hostKeyPolicy;
|
|
1354
|
+
return value === 'strict' ? 'strict' : 'tofu';
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1274
1357
|
#verifyHostKey(store, hashedKey, callback) {
|
|
1275
1358
|
let answered = false;
|
|
1276
1359
|
const answer = (permitted) => {
|
|
@@ -1283,6 +1366,15 @@ export class RemoteHost {
|
|
|
1283
1366
|
const entry = store.byAuthority[this.authority];
|
|
1284
1367
|
|
|
1285
1368
|
if (!entry) {
|
|
1369
|
+
// `strict` refuses anything we have not seen before, so a man-in-the-middle
|
|
1370
|
+
// cannot win the first connection either. The default (`tofu`) keeps the
|
|
1371
|
+
// usual behaviour of trusting the first key and remembering it.
|
|
1372
|
+
if (this.#hostKeyPolicy() === 'strict') {
|
|
1373
|
+
this.#hostKeyUnknown = { actual: fingerprint };
|
|
1374
|
+
this.#log('error', `Refusing unknown host key for ${this.authority} (policy: strict): ${fingerprint}`);
|
|
1375
|
+
answer(false);
|
|
1376
|
+
return;
|
|
1377
|
+
}
|
|
1286
1378
|
this.#log('info', `Trusting new host key for ${this.authority}: ${fingerprint}`);
|
|
1287
1379
|
this.#rememberHostKey(fingerprint).then(
|
|
1288
1380
|
() => answer(true),
|
|
@@ -1426,6 +1518,7 @@ export class RemoteHost {
|
|
|
1426
1518
|
const requestedTimeout = Number(opts.timeoutMs);
|
|
1427
1519
|
const timeoutMs = Number.isFinite(requestedTimeout) && requestedTimeout > 0 ? Math.floor(requestedTimeout) : defaultTimeoutMs;
|
|
1428
1520
|
const signal = opts.signal && typeof opts.signal.addEventListener === 'function' ? opts.signal : null;
|
|
1521
|
+
const encoding = opts.encoding === 'gbk' ? 'gbk' : 'utf8';
|
|
1429
1522
|
const shortCommand = commandText.length > 120 ? `${commandText.slice(0, 117)}...` : commandText;
|
|
1430
1523
|
const label = `exec ${JSON.stringify(shortCommand)}`;
|
|
1431
1524
|
|
|
@@ -1530,8 +1623,8 @@ export class RemoteHost {
|
|
|
1530
1623
|
channel.on('error', (streamErr) => settle(streamErr));
|
|
1531
1624
|
channel.on('close', (code) => {
|
|
1532
1625
|
settle(null, {
|
|
1533
|
-
stdout: Buffer.concat(stdout)
|
|
1534
|
-
stderr: Buffer.concat(stderr)
|
|
1626
|
+
stdout: decodeOutput(Buffer.concat(stdout), encoding),
|
|
1627
|
+
stderr: decodeOutput(Buffer.concat(stderr), encoding),
|
|
1535
1628
|
code: typeof code === 'number' ? code : 0,
|
|
1536
1629
|
});
|
|
1537
1630
|
});
|
|
@@ -1560,7 +1653,7 @@ export class RemoteHost {
|
|
|
1560
1653
|
}
|
|
1561
1654
|
}
|
|
1562
1655
|
|
|
1563
|
-
async #exec(command) {
|
|
1656
|
+
async #exec(command, encoding = 'utf8') {
|
|
1564
1657
|
return this.#retry(`exec ${JSON.stringify(command)}`, async () => {
|
|
1565
1658
|
const client = await this.#readyClient();
|
|
1566
1659
|
return new Promise((resolve, reject) => {
|
|
@@ -1586,8 +1679,8 @@ export class RemoteHost {
|
|
|
1586
1679
|
finish(null, {
|
|
1587
1680
|
code: typeof code === 'number' ? code : 0,
|
|
1588
1681
|
signal: signal || null,
|
|
1589
|
-
stdout: Buffer.concat(stdout)
|
|
1590
|
-
stderr: Buffer.concat(stderr)
|
|
1682
|
+
stdout: decodeOutput(Buffer.concat(stdout), encoding),
|
|
1683
|
+
stderr: decodeOutput(Buffer.concat(stderr), encoding),
|
|
1591
1684
|
});
|
|
1592
1685
|
});
|
|
1593
1686
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-linked-workspace",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"description": "Linked local ⇄ remote workspaces and remote workspaces for DeepSeek Harness: theme-aware split editor, file-type icons, and agent tools that edit remote files directly.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|