dsh-remote-go 0.1.0 → 0.3.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/README.md +64 -95
- package/{client.js → dist/client.js} +7805 -5486
- package/dist/index.js +4 -0
- package/dist/src/admission.js +46 -0
- package/dist/src/api.js +442 -0
- package/dist/src/atomic-json.js +235 -0
- package/dist/src/bootstrap.js +106 -0
- package/dist/src/connect/attach.js +43 -0
- package/dist/src/connect/bootstrap.js +14 -0
- package/dist/src/connect/failures.js +44 -0
- package/dist/src/connect/guards.js +31 -0
- package/dist/src/connect/launch.js +80 -0
- package/dist/src/connect/links.js +70 -0
- package/dist/src/connect/ownership.js +220 -0
- package/dist/src/connect/pipeline.js +473 -0
- package/dist/src/connect/reattach.js +215 -0
- package/dist/src/connect/registry.js +11 -0
- package/dist/src/connect/repair.js +126 -0
- package/dist/src/connect/restart.js +316 -0
- package/dist/src/connect/stop.js +198 -0
- package/dist/src/connect/transport.js +109 -0
- package/dist/src/connect/verify.js +39 -0
- package/dist/src/connect/watchdog.js +76 -0
- package/dist/src/connect-log.js +162 -0
- package/dist/src/connect-progress.js +142 -0
- package/dist/src/constants.js +67 -0
- package/dist/src/context.js +117 -0
- package/dist/src/diagnostics.js +102 -0
- package/dist/src/download-jobs.js +230 -0
- package/dist/src/errors.js +243 -0
- package/dist/src/events.js +390 -0
- package/dist/src/host-lifecycle.js +504 -0
- package/dist/src/host-registry.js +232 -0
- package/dist/src/host-runtime.js +86 -0
- package/dist/src/host-view.js +75 -0
- package/dist/src/index.js +216 -0
- package/dist/src/kernel-adapter.js +434 -0
- package/dist/src/key-files.js +52 -0
- package/dist/src/known-hosts.js +125 -0
- package/dist/src/layout.js +43 -0
- package/dist/src/local-download.js +380 -0
- package/dist/src/local-ssh.js +30 -0
- package/dist/src/local-version.js +101 -0
- package/dist/src/node/download.js +160 -0
- package/dist/src/node-runtime.js +346 -0
- package/dist/src/probe.js +96 -0
- package/dist/src/prompts.js +270 -0
- package/dist/src/proxy/cookies.js +136 -0
- package/dist/src/proxy/headers.js +262 -0
- package/dist/src/proxy/http.js +142 -0
- package/dist/src/proxy/index.js +135 -0
- package/dist/src/proxy/limits.js +8 -0
- package/dist/src/proxy/routes.js +39 -0
- package/dist/src/proxy/upgrade.js +175 -0
- package/dist/src/redact.js +104 -0
- package/dist/src/remote-files.js +72 -0
- package/dist/src/remote-path.js +123 -0
- package/dist/src/remote-proc.js +339 -0
- package/dist/src/remote-web/registry.js +147 -0
- package/dist/src/remote-web/resolve.js +310 -0
- package/dist/src/remote-web/start.js +227 -0
- package/dist/src/remote-web/stop.js +93 -0
- package/dist/src/request-trust.js +87 -0
- package/dist/src/runtime/dispatch.js +310 -0
- package/dist/src/runtime/effects.js +91 -0
- package/dist/src/runtime/events.js +35 -0
- package/dist/src/runtime/fsm.js +120 -0
- package/dist/src/runtime/rules.js +565 -0
- package/dist/src/secrets.js +142 -0
- package/dist/src/service.js +406 -0
- package/dist/src/settings.js +144 -0
- package/dist/src/ssh-config.js +276 -0
- package/dist/src/ssh-failures.js +122 -0
- package/dist/src/ssh-port.js +4 -0
- package/dist/src/ssh-sftp.js +297 -0
- package/dist/src/ssh.js +477 -0
- package/dist/src/store.js +191 -0
- package/dist/src/tool-shared.js +181 -0
- package/dist/src/tools-exec-policy.js +167 -0
- package/dist/src/tools-exec.js +371 -0
- package/dist/src/tools-report.js +424 -0
- package/dist/src/tools.js +411 -0
- package/dist/src/tunnel.js +129 -0
- package/dist/src/util.js +149 -0
- package/dist/src/version.js +114 -0
- package/dist/src/watchdog-bus.js +29 -0
- package/dist/src/watchdog.js +156 -0
- package/index.js +5 -4
- package/locale/en.json +2 -2
- package/locale/zh.json +2 -2
- package/package.json +40 -21
- package/vendor/THIRD_PARTY_NOTICES.md +27 -1
- package/src/api.js +0 -340
- package/src/atomic-json.js +0 -208
- package/src/bootstrap.js +0 -100
- package/src/connect-log.js +0 -39
- package/src/connect-pipeline.js +0 -463
- package/src/connect-progress.js +0 -135
- package/src/constants.js +0 -31
- package/src/diagnostics.js +0 -157
- package/src/download-jobs.js +0 -217
- package/src/errors.js +0 -316
- package/src/host-lifecycle.js +0 -187
- package/src/host-registry.js +0 -145
- package/src/host-runtime.js +0 -53
- package/src/host-view.js +0 -51
- package/src/index.js +0 -162
- package/src/key-files.js +0 -49
- package/src/known-hosts.js +0 -94
- package/src/layout.js +0 -35
- package/src/local-download.js +0 -162
- package/src/local-ssh.js +0 -27
- package/src/local-version.js +0 -88
- package/src/node-runtime.js +0 -521
- package/src/probe.js +0 -89
- package/src/prompts.js +0 -132
- package/src/redact.js +0 -54
- package/src/remote-files.js +0 -71
- package/src/remote-path.js +0 -73
- package/src/remote-proc.js +0 -208
- package/src/remote-web.js +0 -300
- package/src/secrets.js +0 -79
- package/src/service.js +0 -250
- package/src/settings.js +0 -135
- package/src/ssh-config.js +0 -271
- package/src/ssh.js +0 -652
- package/src/store.js +0 -143
- package/src/tunnel.js +0 -73
- package/src/util.js +0 -153
- package/src/version.js +0 -103
- package/src/webapp.js +0 -28
- package/vendor/ssh2-entry.mjs +0 -12
- /package/{vendor → dist/vendor}/ssh2.bundle.cjs +0 -0
package/src/settings.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Plugin settings: defaults, validation and sanitization; unknown or invalid fields fall back to defaults instead of failing activation.
|
|
3
|
-
*/
|
|
4
|
-
import { DEFAULT_IDLE_MINUTES, DEFAULT_KEY_PATH, DEFAULT_REMOTE_PROFILE, SSH_READY_TIMEOUT_MS } from './constants.js'
|
|
5
|
-
import { clamp, deepMerge, isPlainObject, parseSshDestination } from './util.js'
|
|
6
|
-
|
|
7
|
-
export function defaultSettings() {
|
|
8
|
-
return {
|
|
9
|
-
defaultPort: 22,
|
|
10
|
-
// Only a key file (or a one-time password) is supported: there is no SSH
|
|
11
|
-
// agent path any more, so the default is the key file the wizard pre-fills.
|
|
12
|
-
defaultAuth: 'key',
|
|
13
|
-
defaultKeyPath: DEFAULT_KEY_PATH,
|
|
14
|
-
connectTimeoutSec: Math.round(SSH_READY_TIMEOUT_MS / 1000),
|
|
15
|
-
autoReconnect: { enabled: true, maxAttempts: 5, baseDelayMs: 800 },
|
|
16
|
-
hostKeyPolicy: 'first-use',
|
|
17
|
-
saveTokens: false,
|
|
18
|
-
language: 'auto',
|
|
19
|
-
logLevel: 'info',
|
|
20
|
-
bootstrap: {
|
|
21
|
-
profile: DEFAULT_REMOTE_PROFILE,
|
|
22
|
-
},
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* A profile name DSH can boot: one safe path segment, and never `desktop`
|
|
28
|
-
* (the Electron app reserves that profile and the CLI refuses it).
|
|
29
|
-
*/
|
|
30
|
-
function normalizeProfileName(value) {
|
|
31
|
-
const text = typeof value === 'string' ? value.trim() : ''
|
|
32
|
-
if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/.test(text)) return null
|
|
33
|
-
if (text.toLowerCase() === 'desktop') return null
|
|
34
|
-
return text
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function oneOf(value, allowed, fallback) {
|
|
38
|
-
return allowed.includes(value) ? value : fallback
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function numberIn(value, min, max, fallback) {
|
|
42
|
-
const num = typeof value === 'number' ? value : Number(value)
|
|
43
|
-
if (!Number.isFinite(num)) return fallback
|
|
44
|
-
return clamp(Math.round(num), min, max)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function boolean(value, fallback) {
|
|
48
|
-
return typeof value === 'boolean' ? value : fallback
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function sanitizeSettings(raw) {
|
|
52
|
-
const defaults = defaultSettings()
|
|
53
|
-
if (!isPlainObject(raw)) return defaults
|
|
54
|
-
const input = deepMerge(defaults, raw)
|
|
55
|
-
const autoReconnect = isPlainObject(input.autoReconnect) ? input.autoReconnect : defaults.autoReconnect
|
|
56
|
-
const bootstrap = isPlainObject(input.bootstrap) ? input.bootstrap : defaults.bootstrap
|
|
57
|
-
const profile = normalizeProfileName(bootstrap.profile) ?? defaults.bootstrap.profile
|
|
58
|
-
return {
|
|
59
|
-
defaultPort: numberIn(input.defaultPort, 1, 65535, defaults.defaultPort),
|
|
60
|
-
// The retired "agent" choice migrates to key-file auth instead of silently
|
|
61
|
-
// resetting, because key files are the only non-interactive option left.
|
|
62
|
-
defaultAuth:
|
|
63
|
-
input.defaultAuth === 'agent' ? 'key' : oneOf(input.defaultAuth, ['key', 'password'], defaults.defaultAuth),
|
|
64
|
-
defaultKeyPath:
|
|
65
|
-
typeof input.defaultKeyPath === 'string' && input.defaultKeyPath.trim() !== ''
|
|
66
|
-
? input.defaultKeyPath.trim()
|
|
67
|
-
: defaults.defaultKeyPath,
|
|
68
|
-
connectTimeoutSec: numberIn(input.connectTimeoutSec, 5, 300, defaults.connectTimeoutSec),
|
|
69
|
-
autoReconnect: {
|
|
70
|
-
enabled: boolean(autoReconnect.enabled, defaults.autoReconnect.enabled),
|
|
71
|
-
maxAttempts: numberIn(autoReconnect.maxAttempts, 0, 20, defaults.autoReconnect.maxAttempts),
|
|
72
|
-
baseDelayMs: numberIn(autoReconnect.baseDelayMs, 100, 30_000, defaults.autoReconnect.baseDelayMs),
|
|
73
|
-
},
|
|
74
|
-
hostKeyPolicy: oneOf(input.hostKeyPolicy, ['first-use', 'strict'], defaults.hostKeyPolicy),
|
|
75
|
-
saveTokens: boolean(input.saveTokens, defaults.saveTokens),
|
|
76
|
-
language: oneOf(input.language, ['auto', 'zh', 'en'], defaults.language),
|
|
77
|
-
logLevel: oneOf(input.logLevel, ['error', 'warn', 'info', 'debug'], defaults.logLevel),
|
|
78
|
-
bootstrap: {
|
|
79
|
-
profile,
|
|
80
|
-
},
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function sanitizeHostEntry(raw, options = {}) {
|
|
85
|
-
const settings = options.settings ?? defaultSettings()
|
|
86
|
-
const existing = options.existing ?? null
|
|
87
|
-
if (!isPlainObject(raw)) throw new Error('host entry must be an object')
|
|
88
|
-
const name = typeof raw.name === 'string' && raw.name.trim() !== '' ? raw.name.trim().slice(0, 120) : ''
|
|
89
|
-
const address = typeof raw.address === 'string' ? raw.address.trim() : ''
|
|
90
|
-
if (address === '') throw new Error('address is required')
|
|
91
|
-
const destination = parseSshDestination(address)
|
|
92
|
-
if (!destination.host) throw new Error('address is invalid')
|
|
93
|
-
const userInput = typeof raw.username === 'string' ? raw.username.trim() : ''
|
|
94
|
-
const username = userInput || existing?.username || destination.user || ''
|
|
95
|
-
const authType =
|
|
96
|
-
raw.auth && isPlainObject(raw.auth)
|
|
97
|
-
? raw.auth.type === 'agent'
|
|
98
|
-
? 'key'
|
|
99
|
-
: oneOf(raw.auth.type, ['key', 'password'], settings.defaultAuth)
|
|
100
|
-
: (existing?.auth?.type ?? settings.defaultAuth)
|
|
101
|
-
const keyPathInput =
|
|
102
|
-
raw.auth && isPlainObject(raw.auth) && typeof raw.auth.keyPath === 'string' ? raw.auth.keyPath.trim() : ''
|
|
103
|
-
const keyPath = keyPathInput || existing?.auth?.keyPath || (authType === 'key' ? settings.defaultKeyPath : '')
|
|
104
|
-
const keepAlive = oneOf(raw.keepAlive, ['keep', 'stop', 'idle'], existing?.keepAlive ?? 'keep')
|
|
105
|
-
const hostKeyPolicy = oneOf(
|
|
106
|
-
raw.hostKeyPolicy,
|
|
107
|
-
['first-use', 'strict'],
|
|
108
|
-
existing?.hostKeyPolicy ?? settings.hostKeyPolicy,
|
|
109
|
-
)
|
|
110
|
-
return {
|
|
111
|
-
id: typeof raw.id === 'string' && raw.id !== '' ? raw.id : (existing?.id ?? undefined),
|
|
112
|
-
name: name || destination.host || destination.alias || 'remote',
|
|
113
|
-
address,
|
|
114
|
-
port: numberIn(raw.port ?? destination.port ?? existing?.port, 1, 65535, settings.defaultPort),
|
|
115
|
-
username: username || null,
|
|
116
|
-
auth: { type: authType, ...(keyPath ? { keyPath } : {}) },
|
|
117
|
-
jumpHost:
|
|
118
|
-
typeof raw.jumpHost === 'string'
|
|
119
|
-
? raw.jumpHost.trim() !== ''
|
|
120
|
-
? raw.jumpHost.trim()
|
|
121
|
-
: null
|
|
122
|
-
: (existing?.jumpHost ?? null),
|
|
123
|
-
keepAlive,
|
|
124
|
-
idleMinutes: numberIn(raw.idleMinutes ?? existing?.idleMinutes, 1, 24 * 60, DEFAULT_IDLE_MINUTES),
|
|
125
|
-
autoReconnect: boolean(raw.autoReconnect, existing?.autoReconnect ?? settings.autoReconnect.enabled),
|
|
126
|
-
hostKeyPolicy,
|
|
127
|
-
pinnedFingerprint:
|
|
128
|
-
typeof raw.pinnedFingerprint === 'string' && raw.pinnedFingerprint.startsWith('SHA256:')
|
|
129
|
-
? raw.pinnedFingerprint
|
|
130
|
-
: (existing?.pinnedFingerprint ?? null),
|
|
131
|
-
createdAt: existing?.createdAt ?? new Date().toISOString(),
|
|
132
|
-
updatedAt: new Date().toISOString(),
|
|
133
|
-
lastOpenedAt: existing?.lastOpenedAt ?? null,
|
|
134
|
-
}
|
|
135
|
-
}
|
package/src/ssh-config.js
DELETED
|
@@ -1,271 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Read-only `~/.ssh/config` parsing and alias discovery for the subset `ssh_config(5)` defines; `Include`, `Match` and `ProxyCommand` are reported, not guessed.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const KNOWN_OPTIONS = new Set([
|
|
6
|
-
'hostname',
|
|
7
|
-
'user',
|
|
8
|
-
'port',
|
|
9
|
-
'identityfile',
|
|
10
|
-
'certificatefile',
|
|
11
|
-
'proxyjump',
|
|
12
|
-
'proxycommand',
|
|
13
|
-
'serveraliveinterval',
|
|
14
|
-
'serveralivecountmax',
|
|
15
|
-
'connecttimeout',
|
|
16
|
-
'identitiesonly',
|
|
17
|
-
'preferredauthentications',
|
|
18
|
-
])
|
|
19
|
-
|
|
20
|
-
function parseLine(line) {
|
|
21
|
-
const trimmed = line.trim()
|
|
22
|
-
if (trimmed === '' || trimmed.startsWith('#')) return null
|
|
23
|
-
const eq = trimmed.indexOf('=')
|
|
24
|
-
const space = trimmed.search(/\s/)
|
|
25
|
-
let keyword
|
|
26
|
-
let value
|
|
27
|
-
if (eq >= 0 && (space < 0 || eq < space)) {
|
|
28
|
-
keyword = trimmed.slice(0, eq)
|
|
29
|
-
value = trimmed.slice(eq + 1)
|
|
30
|
-
} else if (space >= 0) {
|
|
31
|
-
keyword = trimmed.slice(0, space)
|
|
32
|
-
value = trimmed.slice(space + 1)
|
|
33
|
-
} else {
|
|
34
|
-
keyword = trimmed
|
|
35
|
-
value = ''
|
|
36
|
-
}
|
|
37
|
-
value = value.trim()
|
|
38
|
-
if (
|
|
39
|
-
(value.startsWith('"') && value.endsWith('"') && value.length >= 2) ||
|
|
40
|
-
(value.startsWith("'") && value.endsWith("'") && value.length >= 2)
|
|
41
|
-
) {
|
|
42
|
-
value = value.slice(1, -1)
|
|
43
|
-
}
|
|
44
|
-
return { keyword: keyword.trim(), value }
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function splitPatterns(value) {
|
|
48
|
-
const patterns = []
|
|
49
|
-
const negated = []
|
|
50
|
-
for (const token of value.split(/\s+/)) {
|
|
51
|
-
if (token === '') continue
|
|
52
|
-
if (token.startsWith('!')) negated.push(token.slice(1))
|
|
53
|
-
else patterns.push(token)
|
|
54
|
-
}
|
|
55
|
-
return { patterns, negated }
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function parseSshConfig(text) {
|
|
59
|
-
const entries = []
|
|
60
|
-
const globals = {}
|
|
61
|
-
const unsupported = []
|
|
62
|
-
const malformed = []
|
|
63
|
-
let current = null
|
|
64
|
-
|
|
65
|
-
const lines = String(text ?? '').split(/\r?\n/)
|
|
66
|
-
for (let index = 0; index < lines.length; index += 1) {
|
|
67
|
-
const lineNo = index + 1
|
|
68
|
-
const parsed = parseLine(lines[index])
|
|
69
|
-
if (!parsed) continue
|
|
70
|
-
const keyword = parsed.keyword.toLowerCase()
|
|
71
|
-
if (keyword === 'host') {
|
|
72
|
-
const { patterns, negated } = splitPatterns(parsed.value)
|
|
73
|
-
current = { patterns, negated, options: {}, line: lineNo }
|
|
74
|
-
if (patterns.length === 0) malformed.push({ line: lineNo, text: lines[index] })
|
|
75
|
-
else entries.push(current)
|
|
76
|
-
continue
|
|
77
|
-
}
|
|
78
|
-
if (keyword === 'match') {
|
|
79
|
-
unsupported.push({ line: lineNo, keyword: 'Match' })
|
|
80
|
-
// Options inside a Match block only apply under conditions we cannot
|
|
81
|
-
// evaluate; skip them until the next Host block.
|
|
82
|
-
current = { patterns: ['__dsh_remote_go_match_block__'], negated: [], options: {}, line: lineNo }
|
|
83
|
-
entries.push(current)
|
|
84
|
-
continue
|
|
85
|
-
}
|
|
86
|
-
if (keyword === 'include') {
|
|
87
|
-
unsupported.push({ line: lineNo, keyword: 'Include' })
|
|
88
|
-
continue
|
|
89
|
-
}
|
|
90
|
-
if (!KNOWN_OPTIONS.has(keyword)) {
|
|
91
|
-
// Unknown-but-harmless options are ignored; only flag keywords that would
|
|
92
|
-
// change meaning if misread.
|
|
93
|
-
if (keyword === 'proxycommand' || keyword === 'certificatefile')
|
|
94
|
-
unsupported.push({ line: lineNo, keyword: parsed.keyword })
|
|
95
|
-
continue
|
|
96
|
-
}
|
|
97
|
-
if (parsed.value === '') {
|
|
98
|
-
malformed.push({ line: lineNo, text: lines[index] })
|
|
99
|
-
continue
|
|
100
|
-
}
|
|
101
|
-
if (current) {
|
|
102
|
-
current.options[keyword] = current.options[keyword] ?? []
|
|
103
|
-
current.options[keyword].push(parsed.value)
|
|
104
|
-
} else {
|
|
105
|
-
globals[keyword] = globals[keyword] ?? []
|
|
106
|
-
globals[keyword].push(parsed.value)
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
return { entries, globals, unsupported, malformed }
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function matchHostPattern(pattern, value) {
|
|
113
|
-
if (pattern === '*') return true
|
|
114
|
-
const escaped = pattern
|
|
115
|
-
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
|
|
116
|
-
.replace(/\*/g, '.*')
|
|
117
|
-
.replace(/\?/g, '.')
|
|
118
|
-
return new RegExp(`^${escaped}$`, 'i').test(value)
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function entryMatches(entry, alias) {
|
|
122
|
-
if (entry.patterns.includes('__dsh_remote_go_match_block__')) return false
|
|
123
|
-
const positive = entry.patterns.some((pattern) => matchHostPattern(pattern, alias))
|
|
124
|
-
if (!positive) return false
|
|
125
|
-
return !entry.negated.some((pattern) => matchHostPattern(pattern, alias))
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const ADDITIVE_OPTIONS = new Set(['identityfile', 'certificatefile'])
|
|
129
|
-
|
|
130
|
-
function effectiveOptions(globals, entries, alias, tokens) {
|
|
131
|
-
const out = {}
|
|
132
|
-
const apply = (/** @type {Record<string, string[]>} */ options) => {
|
|
133
|
-
for (const [key, values] of Object.entries(options)) {
|
|
134
|
-
const expanded = values.map((value) => expandTokens(value, tokens))
|
|
135
|
-
if (ADDITIVE_OPTIONS.has(key)) {
|
|
136
|
-
out[key] = [...(out[key] ?? []), ...expanded]
|
|
137
|
-
continue
|
|
138
|
-
}
|
|
139
|
-
if (out[key] !== undefined) continue
|
|
140
|
-
out[key] = expanded
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
apply(globals)
|
|
144
|
-
for (const entry of entries) {
|
|
145
|
-
if (entryMatches(entry, alias)) apply(entry.options)
|
|
146
|
-
}
|
|
147
|
-
return out
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export function expandTokens(value, tokens) {
|
|
151
|
-
return String(value).replace(/%([hprd%])/g, (match, token) => {
|
|
152
|
-
if (token === '%') return '%'
|
|
153
|
-
return tokens[token] ?? match
|
|
154
|
-
})
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function expandTilde(value, home) {
|
|
158
|
-
if (value === '~') return home
|
|
159
|
-
if (value.startsWith('~/')) return `${home.replace(/[\\/]+$/, '')}/${value.slice(2)}`
|
|
160
|
-
return value
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export function resolveSshAlias(parsed, alias, options = {}) {
|
|
164
|
-
const home = options.home ?? '~'
|
|
165
|
-
const baseTokens = { h: alias, r: '~', p: '22', d: home }
|
|
166
|
-
const firstPass = effectiveOptions(parsed.globals, parsed.entries, alias, baseTokens)
|
|
167
|
-
const host = firstPass.hostname?.[0] ?? alias
|
|
168
|
-
const user = firstPass.user?.[0] ?? null
|
|
169
|
-
const rawPort = firstPass.port?.[0] ?? '22'
|
|
170
|
-
const port = /^\d+$/.test(rawPort) ? Number(rawPort) : 22
|
|
171
|
-
const tokens = { h: host, r: user ?? '~', p: String(port), d: home }
|
|
172
|
-
const resolved = effectiveOptions(parsed.globals, parsed.entries, alias, tokens)
|
|
173
|
-
const identityFiles = (resolved.identityfile ?? []).map((value) => expandTilde(value, home))
|
|
174
|
-
const proxyJump = resolved.proxyjump?.[0] ?? null
|
|
175
|
-
return {
|
|
176
|
-
alias,
|
|
177
|
-
host,
|
|
178
|
-
user,
|
|
179
|
-
port,
|
|
180
|
-
identityFiles,
|
|
181
|
-
proxyJump,
|
|
182
|
-
source: 'config',
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* List concrete, manually-dialable aliases from the config. Wildcard patterns
|
|
188
|
-
* (`*`, `?`) and negations are skipped because they cannot be resolved without
|
|
189
|
-
* a concrete target.
|
|
190
|
-
*/
|
|
191
|
-
export function discoverSshAliases(parsed, options = {}) {
|
|
192
|
-
const out = []
|
|
193
|
-
const seen = new Set()
|
|
194
|
-
for (const entry of parsed.entries) {
|
|
195
|
-
for (const pattern of entry.patterns) {
|
|
196
|
-
if (pattern.includes('*') || pattern.includes('?') || pattern.startsWith('__dsh_remote_go_')) continue
|
|
197
|
-
if (seen.has(pattern)) continue
|
|
198
|
-
seen.add(pattern)
|
|
199
|
-
const resolved = resolveSshAlias(parsed, pattern, options)
|
|
200
|
-
out.push({ alias: pattern, host: resolved.host, user: resolved.user, port: resolved.port, source: 'config' })
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
return out
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
export function resolveDestination(destination, options = {}) {
|
|
207
|
-
const text = String(destination ?? '').trim()
|
|
208
|
-
const literal = parseDestinationLiteral(text)
|
|
209
|
-
const parsed = parseSshConfig(options.configText ?? '')
|
|
210
|
-
const home = options.home ?? '~'
|
|
211
|
-
const aliases = discoverSshAliases(parsed, { home })
|
|
212
|
-
const alias = aliases.find((item) => item.alias === text)
|
|
213
|
-
if (alias) {
|
|
214
|
-
const resolved = resolveSshAlias(parsed, text, { home })
|
|
215
|
-
return {
|
|
216
|
-
input: text,
|
|
217
|
-
alias: text,
|
|
218
|
-
host: resolved.host,
|
|
219
|
-
user: literal.user ?? resolved.user,
|
|
220
|
-
port: literal.port ?? resolved.port,
|
|
221
|
-
identityFiles: resolved.identityFiles,
|
|
222
|
-
proxyJump: resolved.proxyJump,
|
|
223
|
-
source: 'config',
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
// Literal destination: inherit globals (e.g. a global IdentityFile) but not
|
|
227
|
-
// Host blocks that do not match the literal text.
|
|
228
|
-
const tokens = { h: literal.host, r: literal.user ?? '~', p: String(literal.port ?? 22), d: home }
|
|
229
|
-
const globals = effectiveOptions(parsed.globals, [], text, tokens)
|
|
230
|
-
const identityFiles = (globals.identityfile ?? []).map((value) => expandTilde(value, home))
|
|
231
|
-
return {
|
|
232
|
-
input: text,
|
|
233
|
-
alias: null,
|
|
234
|
-
host: literal.host,
|
|
235
|
-
user: literal.user,
|
|
236
|
-
port: literal.port ?? 22,
|
|
237
|
-
identityFiles,
|
|
238
|
-
proxyJump: globals.proxyjump?.[0] ?? null,
|
|
239
|
-
source: 'manual',
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function parseDestinationLiteral(text) {
|
|
244
|
-
let user = null
|
|
245
|
-
let rest = text
|
|
246
|
-
const at = rest.lastIndexOf('@')
|
|
247
|
-
if (at >= 0) {
|
|
248
|
-
user = rest.slice(0, at)
|
|
249
|
-
rest = rest.slice(at + 1)
|
|
250
|
-
}
|
|
251
|
-
let host = rest
|
|
252
|
-
let port = null
|
|
253
|
-
if (rest.startsWith('[')) {
|
|
254
|
-
const close = rest.indexOf(']')
|
|
255
|
-
if (close > 0) {
|
|
256
|
-
host = rest.slice(1, close)
|
|
257
|
-
const tail = rest.slice(close + 1)
|
|
258
|
-
if (/^:\d+$/.test(tail)) port = Number(tail.slice(1))
|
|
259
|
-
}
|
|
260
|
-
} else {
|
|
261
|
-
const colon = rest.lastIndexOf(':')
|
|
262
|
-
if (colon > 0 && rest.indexOf(':') === colon) {
|
|
263
|
-
const maybePort = rest.slice(colon + 1)
|
|
264
|
-
if (/^\d{1,5}$/.test(maybePort)) {
|
|
265
|
-
host = rest.slice(0, colon)
|
|
266
|
-
port = Number(maybePort)
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
return { user: user || null, host, port }
|
|
271
|
-
}
|