dsh-ssh-compute 0.0.0-stage → 1.2.3
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/LICENSE +21 -0
- package/README.md +262 -2
- package/client.js +1058 -0
- package/cordis.patch.yml +17 -0
- package/icon.svg +7 -0
- package/index.js +578 -0
- package/lib/config.js +307 -0
- package/lib/glob.js +189 -0
- package/lib/guard.js +169 -0
- package/lib/keys.js +101 -0
- package/lib/remote.js +642 -0
- package/lib/schema.js +108 -0
- package/lib/shell.js +69 -0
- package/lib/ssh.js +312 -0
- package/lib/sshconfig.js +256 -0
- package/lib/transfer.js +336 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +67 -4
package/lib/keys.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Private-key resolution.
|
|
2
|
+
//
|
|
3
|
+
// Three sources, in order: key material pasted into the settings, a configured
|
|
4
|
+
// path, and finally whatever this machine already has in `~/.ssh`. An entry
|
|
5
|
+
// that resolves to nothing passes no `-i` at all, which is the right answer for
|
|
6
|
+
// a machine whose keys live in `~/.ssh/config` or an agent.
|
|
7
|
+
|
|
8
|
+
import { createHash } from 'node:crypto'
|
|
9
|
+
import { chmodSync, existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
|
|
10
|
+
import { tmpdir } from 'node:os'
|
|
11
|
+
import { join } from 'node:path'
|
|
12
|
+
|
|
13
|
+
import { discoverPrivateKeys, expandHome } from './config.js'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A per-mount holder for the key files this plugin writes itself.
|
|
17
|
+
*
|
|
18
|
+
* Pasted key material has to become a file before ssh can use it. The file
|
|
19
|
+
* lives in the system temp directory, is named after a digest so two servers
|
|
20
|
+
* sharing one key share one file, and is removed when the plugin unloads.
|
|
21
|
+
* @returns the store: `fileFor(server)` and `dispose()`.
|
|
22
|
+
*/
|
|
23
|
+
export function createKeyStore() {
|
|
24
|
+
const directory = join(tmpdir(), 'dsh-ssh-compute')
|
|
25
|
+
/** absolute path -> nothing; a Set so two entries can own the same file. */
|
|
26
|
+
const written = new Set()
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Describe what a server will authenticate with, without touching the disk
|
|
30
|
+
* beyond what `fileFor` already does. The detail page shows this.
|
|
31
|
+
* @param server - one normalized entry.
|
|
32
|
+
* @returns a short label and the path when there is one.
|
|
33
|
+
*/
|
|
34
|
+
const describe = (server) => {
|
|
35
|
+
if (server.keyText !== '') return { source: 'inline', path: undefined, count: 1 }
|
|
36
|
+
if (server.keyPath !== '') return { source: 'path', path: expandHome(server.keyPath), count: 1 }
|
|
37
|
+
const detected = discoverPrivateKeys()
|
|
38
|
+
return detected.length === 0
|
|
39
|
+
? { source: 'default', path: undefined, count: 0 }
|
|
40
|
+
: { source: 'detected', path: detected[0], count: detected.length }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
describe,
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The `-i` paths for one server, or an empty list to let ssh decide.
|
|
48
|
+
*
|
|
49
|
+
* More than one is normal: a machine commonly holds two keys and only one of
|
|
50
|
+
* them is authorised on a given server. Passing a single `-i` would suppress
|
|
51
|
+
* ssh's own identity list and turn that into `Permission denied (publickey)`
|
|
52
|
+
* for a key that works.
|
|
53
|
+
* @param server - one normalized entry.
|
|
54
|
+
* @returns existing private-key files, most preferred first.
|
|
55
|
+
*/
|
|
56
|
+
fileFor(server) {
|
|
57
|
+
let resolved = []
|
|
58
|
+
if (server.keyText !== '') {
|
|
59
|
+
const digest = createHash('sha256').update(server.keyText).digest('hex').slice(0, 16)
|
|
60
|
+
const target = join(directory, `${digest}.key`)
|
|
61
|
+
if (!written.has(target)) {
|
|
62
|
+
mkdirSync(directory, { recursive: true })
|
|
63
|
+
const body = server.keyText.endsWith('\n') ? server.keyText : `${server.keyText}\n`
|
|
64
|
+
writeFileSync(target, body, { mode: 0o600 })
|
|
65
|
+
try {
|
|
66
|
+
// Windows ignores the POSIX mode; the deep temp directory and the
|
|
67
|
+
// per-user profile ACL are what protect it there.
|
|
68
|
+
chmodSync(target, 0o600)
|
|
69
|
+
} catch {
|
|
70
|
+
// Nothing to do: the write already succeeded with the mode above.
|
|
71
|
+
}
|
|
72
|
+
written.add(target)
|
|
73
|
+
}
|
|
74
|
+
resolved = [target]
|
|
75
|
+
} else if (server.keyPath !== '') {
|
|
76
|
+
const expanded = expandHome(server.keyPath)
|
|
77
|
+
if (!existsSync(expanded)) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
`dsh-ssh-compute: the key file for server "${server.name}" does not exist: ${expanded}`,
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
resolved = [expanded]
|
|
83
|
+
} else {
|
|
84
|
+
resolved = discoverPrivateKeys()
|
|
85
|
+
}
|
|
86
|
+
return resolved
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
/** Remove the files this mount wrote. Configured keys are never touched. */
|
|
90
|
+
dispose() {
|
|
91
|
+
for (const target of written) {
|
|
92
|
+
try {
|
|
93
|
+
rmSync(target, { force: true })
|
|
94
|
+
} catch {
|
|
95
|
+
// A leftover temp file is harmless; removing it again next run is enough.
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
written.clear()
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
}
|