ssh-client-wrapper 3.4.1 → 3.4.2
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/.mocharc.json +3 -0
- package/README.md +13 -0
- package/lib/agent.js +2 -1
- package/lib/fork.js +5 -1
- package/lib/pathConvert.js +126 -0
- package/lib/rsyncExec.js +21 -13
- package/lib/sshExec.js +5 -1
- package/lib/util.js +31 -53
- package/lib/winRsync.js +159 -0
- package/package.json +4 -4
package/.mocharc.json
ADDED
package/README.md
CHANGED
|
@@ -108,6 +108,19 @@ Downloads files or directories from the remote host using `rsync`.
|
|
|
108
108
|
* `replaceRetryableExitCodes` (boolean, optional): If `true`, `retryableExitCodes` replaces the built-in default instead of adding to it. Defaults to `false`.
|
|
109
109
|
* Returns: `Promise<void>`
|
|
110
110
|
|
|
111
|
+
### rsync on Windows
|
|
112
|
+
|
|
113
|
+
On Windows, `.send()` / `.recv()` work with the `rsync.exe` of MSYS2 (or Cygwin) found in `PATH`. The wrapper detects it by `msys-2.0.dll` (or `cygwin1.dll`) next to `rsync.exe` and then:
|
|
114
|
+
|
|
115
|
+
* uses the `ssh.exe` next to `rsync.exe` for the rsync transport (it does not work with the OpenSSH of Windows or Git for Windows),
|
|
116
|
+
* converts local paths and file paths in the ssh options (`D:\dir` -> `/d/dir`),
|
|
117
|
+
* passes `-F <profile>\.ssh\config` and `-oUserKnownHostsFile=<profile>\.ssh\known_hosts` explicitly (the ssh of MSYS2 takes its home from the MSYS2 user database, not from the Windows profile). Give your own `-F` or `UserKnownHostsFile` in `sshOpt` to override.
|
|
118
|
+
|
|
119
|
+
Limitations on Windows:
|
|
120
|
+
|
|
121
|
+
* `~` and relative `Include` paths in the ssh config are **not resolved** for the ssh run by rsync (they are resolved against the MSYS2 home, not the Windows profile), although they work for the ssh run by the wrapper itself. `Include` ignores a missing file silently, so use absolute paths (e.g. `C:/Users/you/.ssh/conf.d/*`) in the config, or put the setting in the config file directly.
|
|
122
|
+
* The ssh-agent of Windows (named pipe) cannot be used by the ssh of MSYS2.
|
|
123
|
+
|
|
111
124
|
### Remote paths of `.send()` / `.recv()`
|
|
112
125
|
|
|
113
126
|
A remote path (`dst` of `.send()`, `src` of `.recv()`) is interpreted by the login shell of the remote host, whatever the version or the implementation of the local `rsync` is:
|
package/lib/agent.js
CHANGED
|
@@ -6,7 +6,7 @@ import { promisify } from "node:util";
|
|
|
6
6
|
import { setTimeout as setTimeoutPromise } from "node:timers/promises";
|
|
7
7
|
import Debug from "debug";
|
|
8
8
|
import { spawn } from "node-pty";
|
|
9
|
-
import { rePhPrompt, resolveExecutable } from "./util.js";
|
|
9
|
+
import { rePhPrompt, resolveExecutable, releasePty } from "./util.js";
|
|
10
10
|
|
|
11
11
|
const debug = Debug("sshClientWrapper:debug:agent");
|
|
12
12
|
const debugVerbose = Debug("sshClientWrapper:verbose:agent");
|
|
@@ -324,6 +324,7 @@ function addKey(sock, keyFile, phCallback, ttl) {
|
|
|
324
324
|
});
|
|
325
325
|
|
|
326
326
|
pty.onExit(({ exitCode })=>{
|
|
327
|
+
releasePty(pty);
|
|
327
328
|
if (settled) {
|
|
328
329
|
return;
|
|
329
330
|
}
|
package/lib/fork.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import Debug from "debug";
|
|
2
2
|
import { spawn } from "node-pty";
|
|
3
3
|
|
|
4
|
-
import { resolveExecutable, rePwPrompt, rePhPrompt, reNewHostPrompt, reRemoteHostIdentificationHasChanged, buff2String, sendPty, watchDogTimer } from "./util.js";
|
|
4
|
+
import { resolveExecutable, rePwPrompt, rePhPrompt, reNewHostPrompt, reRemoteHostIdentificationHasChanged, buff2String, sendPty, watchDogTimer, releasePty } from "./util.js";
|
|
5
5
|
|
|
6
6
|
const debug = Debug("sshClientWrapper:debug:fork");
|
|
7
7
|
const debugVerbose = Debug("sshClientWrapper:verbose:fork");
|
|
@@ -79,6 +79,9 @@ export function createMasterPty(hostInfo) {
|
|
|
79
79
|
hostInfo.masterPty.kill();
|
|
80
80
|
}
|
|
81
81
|
const ptyProcess = spawn(nodePtyOpt.shell, [], nodePtyOpt);
|
|
82
|
+
ptyProcess.onExit(()=>{
|
|
83
|
+
releasePty(ptyProcess);
|
|
84
|
+
});
|
|
82
85
|
hostInfo.masterPty = ptyProcess;
|
|
83
86
|
}
|
|
84
87
|
|
|
@@ -130,6 +133,7 @@ export async function fork(hostInfo, cmd, args, timeout, outputCallback, retryab
|
|
|
130
133
|
}
|
|
131
134
|
ptyProcess.onExit(({ exitCode, signal })=>{
|
|
132
135
|
debug("onExit", exitCode, signal);
|
|
136
|
+
releasePty(ptyProcess);
|
|
133
137
|
|
|
134
138
|
if (ac !== null) {
|
|
135
139
|
ac.abort();
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Conversion between Windows paths and the path form of MSYS2/Cygwin programs (rsync.exe, ssh.exe of MSYS2).
|
|
5
|
+
* MSYS2 rsync takes "D:\dir" for a remote host named "D", so local paths must be given as "/d/dir".
|
|
6
|
+
*
|
|
7
|
+
* These are pure string functions: they do not depend on process.platform, the file system or the working directory,
|
|
8
|
+
* so that they can be used for the local side (client) and for a remote side which is Windows (server) alike.
|
|
9
|
+
* Only local paths must be converted. Never pass a remote spec ("host:path") to them.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const win32 = path.win32;
|
|
13
|
+
const DRIVE_ROOT = /^([A-Za-z]):[\\/]$/;
|
|
14
|
+
const DRIVE_ONLY = /^[A-Za-z]:$/;
|
|
15
|
+
const CYGDRIVE_PREFIX = /^\/cygdrive(?=\/|$)/;
|
|
16
|
+
const MSYS_DRIVE = /^\/([A-Za-z])(?=\/|$)/;
|
|
17
|
+
|
|
18
|
+
const DEFAULT_STYLE = "msys";
|
|
19
|
+
const STYLES = ["msys", "cygwin"];
|
|
20
|
+
|
|
21
|
+
const assertString = (p)=>{
|
|
22
|
+
if (typeof p !== "string") {
|
|
23
|
+
throw new TypeError(`path must be a string: ${typeof p}`);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
const assertStyle = (style)=>{
|
|
27
|
+
if (!STYLES.includes(style)) {
|
|
28
|
+
throw new RangeError(`style must be one of ${STYLES.join(", ")}: ${style}`);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const slash = (p)=>{return p.replace(/\\/g, "/");};
|
|
32
|
+
const backslash = (p)=>{return p.replace(/\//g, "\\");};
|
|
33
|
+
|
|
34
|
+
//"\\?\D:\x" -> "D:\x", "\\?\UNC\host\share\x" -> "\\host\share\x"
|
|
35
|
+
const stripLongPathPrefix = (p)=>{
|
|
36
|
+
if (/^\\\\[?.]\\UNC\\/i.test(p)) {
|
|
37
|
+
return `\\\\${p.slice(8)}`;
|
|
38
|
+
}
|
|
39
|
+
if (/^\\\\[?.]\\[A-Za-z]:/.test(p)) {
|
|
40
|
+
return p.slice(4);
|
|
41
|
+
}
|
|
42
|
+
return p;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* check if the path is an absolute Windows path with a drive letter ("D:\x", "D:/x", "D:") or an UNC path ("\\host\share")
|
|
47
|
+
* @param {string} p - path
|
|
48
|
+
* @returns {boolean} - true if the path is a Windows path which needs conversion for MSYS2 programs
|
|
49
|
+
*/
|
|
50
|
+
export function isWindowsPath(p) {
|
|
51
|
+
assertString(p);
|
|
52
|
+
const root = win32.parse(stripLongPathPrefix(p)).root;
|
|
53
|
+
return DRIVE_ONLY.test(root) || DRIVE_ROOT.test(root) || /^\\\\[^\\]+\\[^\\]+\\?$/.test(root);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* convert a Windows path to the path form of MSYS2 (or Cygwin) programs
|
|
58
|
+
* - "D:\a\b" and "D:/a/b" -> "/d/a/b" (cygwin style: "/cygdrive/d/a/b")
|
|
59
|
+
* - "\\host\share\a" -> "//host/share/a"
|
|
60
|
+
* - "a\b\" -> "a/b/" (relative paths just get slashes; a trailing slash is kept because rsync gives it a meaning)
|
|
61
|
+
* - "/d/a" and "/usr/bin" are returned as they are (so that the function is idempotent)
|
|
62
|
+
* @param {string} p - local path
|
|
63
|
+
* @param {object} [options] - options
|
|
64
|
+
* @param {string} [options.style] - "msys" (default) or "cygwin"
|
|
65
|
+
* @returns {string} - converted path
|
|
66
|
+
* @throws {RangeError} - if the path is relative to the current directory of a drive ("D:a")
|
|
67
|
+
*/
|
|
68
|
+
export function toMsysPath(p, options = {}) {
|
|
69
|
+
assertString(p);
|
|
70
|
+
const { style = DEFAULT_STYLE } = options;
|
|
71
|
+
assertStyle(style);
|
|
72
|
+
const src = stripLongPathPrefix(p);
|
|
73
|
+
const { root } = win32.parse(src);
|
|
74
|
+
const rest = src.slice(root.length);
|
|
75
|
+
if (DRIVE_ONLY.test(root)) {
|
|
76
|
+
throw new RangeError(`can not convert a drive relative path: ${p}`);
|
|
77
|
+
}
|
|
78
|
+
const m = DRIVE_ROOT.exec(root);
|
|
79
|
+
if (m) {
|
|
80
|
+
const drive = m[1].toLowerCase();
|
|
81
|
+
const prefix = style === "cygwin" ? `/cygdrive/${drive}` : `/${drive}`;
|
|
82
|
+
return rest === "" ? `${prefix}/` : `${prefix}/${slash(rest)}`;
|
|
83
|
+
}
|
|
84
|
+
return slash(src);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* convert a path of MSYS2 (or Cygwin) programs to a Windows path
|
|
89
|
+
* - "/d/a/b" and "/cygdrive/d/a/b" -> "D:\a\b"
|
|
90
|
+
* - "//host/share/a" -> "\\host\share\a"
|
|
91
|
+
* - "a/b" -> "a\b"
|
|
92
|
+
* - other absolute paths ("/usr/bin") live in the MSYS2 root: they are converted if options.root is given, otherwise an error is thrown
|
|
93
|
+
* - "/c" cannot be told from a directory named "c" in the MSYS2 root: it is always taken as the drive C
|
|
94
|
+
* @param {string} p - path of MSYS2 programs
|
|
95
|
+
* @param {object} [options] - options
|
|
96
|
+
* @param {string} [options.root] - Windows path of the MSYS2 root ("C:\msys64"), used for absolute paths without a drive
|
|
97
|
+
* @returns {string} - Windows path
|
|
98
|
+
* @throws {RangeError} - if the path is in the MSYS2 root and options.root is not given
|
|
99
|
+
*/
|
|
100
|
+
export function fromMsysPath(p, options = {}) {
|
|
101
|
+
assertString(p);
|
|
102
|
+
const { root } = options;
|
|
103
|
+
if (root !== undefined) {
|
|
104
|
+
assertString(root);
|
|
105
|
+
}
|
|
106
|
+
if (p.startsWith("//") && !p.startsWith("///")) {
|
|
107
|
+
return backslash(p);
|
|
108
|
+
}
|
|
109
|
+
if (!p.startsWith("/")) {
|
|
110
|
+
return backslash(p);
|
|
111
|
+
}
|
|
112
|
+
const cyg = CYGDRIVE_PREFIX.exec(p);
|
|
113
|
+
const body = cyg ? p.slice(cyg[0].length) : p;
|
|
114
|
+
const m = MSYS_DRIVE.exec(body);
|
|
115
|
+
if (m) {
|
|
116
|
+
const rest = body.slice(m[0].length);
|
|
117
|
+
return `${m[1].toUpperCase()}:\\${backslash(rest.replace(/^\//, ""))}`;
|
|
118
|
+
}
|
|
119
|
+
if (cyg) {
|
|
120
|
+
throw new RangeError(`not a drive path: ${p}`);
|
|
121
|
+
}
|
|
122
|
+
if (root === undefined) {
|
|
123
|
+
throw new RangeError(`the path is in the MSYS2 root, options.root is needed: ${p}`);
|
|
124
|
+
}
|
|
125
|
+
return win32.join(root, backslash(p));
|
|
126
|
+
}
|
package/lib/rsyncExec.js
CHANGED
|
@@ -6,7 +6,8 @@ import { glob, hasMagic } from "glob";
|
|
|
6
6
|
import { connect, retryWrapper, sshExec } from "./sshExec.js";
|
|
7
7
|
import { fork } from "./fork.js";
|
|
8
8
|
|
|
9
|
-
import { sshCmd,
|
|
9
|
+
import { sshCmd, rsyncCmd, getSshOption, resolveRetryableExitCodes } from "./util.js";
|
|
10
|
+
import { getLocalRsync, toRsyncPath, makeRshCommand } from "./winRsync.js";
|
|
10
11
|
import { expandRemotePath, formatRemoteArg, hasLiteralRemoteArgs, shellQuote } from "./remotePath.js";
|
|
11
12
|
|
|
12
13
|
const debug = Debug("sshClientWrapper:debug:rsyncExec");
|
|
@@ -14,7 +15,7 @@ const debugVerbose = Debug("sshClientWrapper:verbose:rsyncExec");
|
|
|
14
15
|
|
|
15
16
|
export async function checkRsyncVersion(timeout) {
|
|
16
17
|
const rt = {};
|
|
17
|
-
await fork(null,
|
|
18
|
+
await fork(null, rsyncCmd, ["--version"], timeout, (output)=>{
|
|
18
19
|
const result = /^rsync *version *(\d+)\.(\d+)\.(\d+).*$/m.exec(output);
|
|
19
20
|
|
|
20
21
|
if (result) {
|
|
@@ -40,15 +41,17 @@ export async function checkRsyncVersion(timeout) {
|
|
|
40
41
|
*/
|
|
41
42
|
export async function send(hostInfo, argSrc, dst, rsyncOpt, timeout, retryableExitCodes, replaceRetryableExitCodes) {
|
|
42
43
|
await connect(hostInfo);
|
|
44
|
+
//on win32 a backslash is a path separator, not an escape character
|
|
45
|
+
const globOpt = process.platform === "win32" ? { windowsPathsNoEscape: true } : {};
|
|
43
46
|
const promises = await Promise.allSettled(argSrc.map((e)=>{
|
|
44
|
-
return glob(e);
|
|
47
|
+
return glob(e, globOpt);
|
|
45
48
|
}));
|
|
46
49
|
const src = promises
|
|
47
50
|
.map((e, i)=>{
|
|
48
51
|
if (e.status !== "fulfilled") {
|
|
49
52
|
return null;
|
|
50
53
|
}
|
|
51
|
-
if (argSrc[i].endsWith("/")) {
|
|
54
|
+
if (argSrc[i].endsWith("/") || (process.platform === "win32" && argSrc[i].endsWith("\\"))) {
|
|
52
55
|
return `${e.value}/`;
|
|
53
56
|
}
|
|
54
57
|
return e.value;
|
|
@@ -66,7 +69,9 @@ export async function send(hostInfo, argSrc, dst, rsyncOpt, timeout, retryableEx
|
|
|
66
69
|
}
|
|
67
70
|
|
|
68
71
|
const sshArgs = getSshOption(hostInfo, true);
|
|
69
|
-
|
|
72
|
+
//rsync.exe of MSYS2 or Cygwin needs its own ssh.exe and takes local paths as /d/dir
|
|
73
|
+
const localRsync = getLocalRsync();
|
|
74
|
+
const rshOpt = ["-e", makeRshCommand(sshCmd, sshArgs, localRsync)];
|
|
70
75
|
|
|
71
76
|
//the remote path is interpreted by the remote shell (~, $VAR, {a,b}, quotes and escapes)
|
|
72
77
|
const remoteDsts = await expandRemotePath(hostInfo, dst);
|
|
@@ -76,7 +81,9 @@ export async function send(hostInfo, argSrc, dst, rsyncOpt, timeout, retryableEx
|
|
|
76
81
|
const [remoteDst] = remoteDsts;
|
|
77
82
|
const literal = await hasLiteralRemoteArgsOfLocalRsync(hostInfo);
|
|
78
83
|
|
|
79
|
-
const args = ["-avv", "--copy-unsafe-links", ...rshOpt, ...rsyncOpt, ...src
|
|
84
|
+
const args = ["-avv", "--copy-unsafe-links", ...rshOpt, ...rsyncOpt, ...src.map((e)=>{
|
|
85
|
+
return toRsyncPath(e, localRsync);
|
|
86
|
+
}), `${hostInfo.host}:${formatRemoteArg(remoteDst, literal)}`];
|
|
80
87
|
let remoteDir;
|
|
81
88
|
if (remoteDst.endsWith("/")) {
|
|
82
89
|
remoteDir = remoteDst;
|
|
@@ -91,9 +98,9 @@ export async function send(hostInfo, argSrc, dst, rsyncOpt, timeout, retryableEx
|
|
|
91
98
|
await sshExec(hostInfo, `mkdir -p ${shellQuote(remoteDir)}`, 0, debugVerbose);
|
|
92
99
|
}
|
|
93
100
|
debug(`send ${src} to ${hostInfo.host}:${remoteDst}`);
|
|
94
|
-
debugVerbose(`send: ${
|
|
101
|
+
debugVerbose(`send: ${rsyncCmd} ${args.join(" ")}`);
|
|
95
102
|
const exitCodes = resolveRetryableExitCodes(hostInfo, retryableExitCodes, replaceRetryableExitCodes);
|
|
96
|
-
return retryWrapper(fork.bind(null, hostInfo,
|
|
103
|
+
return retryWrapper(fork.bind(null, hostInfo, rsyncCmd, args, timeout, null, exitCodes), hostInfo);
|
|
97
104
|
}
|
|
98
105
|
|
|
99
106
|
/**
|
|
@@ -139,17 +146,18 @@ export async function recv(hostInfo, argSrc, dst, rsyncOpt, timeout, retryableEx
|
|
|
139
146
|
return `${hostInfo.host}:${formatRemoteArg(e, literal)}`;
|
|
140
147
|
});
|
|
141
148
|
const sshArgs = getSshOption(hostInfo, true);
|
|
142
|
-
const
|
|
143
|
-
const
|
|
149
|
+
const localRsync = getLocalRsync();
|
|
150
|
+
const rshOpt = ["-e", makeRshCommand(sshCmd, sshArgs, localRsync)];
|
|
151
|
+
const args = ["-avv", "--copy-unsafe-links", ...rshOpt, ...rsyncOpt, ...src, toRsyncPath(dst, localRsync)];
|
|
144
152
|
|
|
145
153
|
//make destination directory
|
|
146
|
-
const dstDir =
|
|
154
|
+
const dstDir = src.length > 1 || hasMagic(argSrc[0]) ? dst : path.dirname(dst);
|
|
147
155
|
await fs.mkdir(dstDir, { recursive: true });
|
|
148
156
|
|
|
149
157
|
debug(`recv ${src.join(" ")} to ${dst}`);
|
|
150
|
-
debugVerbose(`recv: ${
|
|
158
|
+
debugVerbose(`recv: ${rsyncCmd} ${args.join(" ")}`);
|
|
151
159
|
const exitCodes = resolveRetryableExitCodes(hostInfo, retryableExitCodes, replaceRetryableExitCodes);
|
|
152
|
-
return retryWrapper(fork.bind(null, hostInfo,
|
|
160
|
+
return retryWrapper(fork.bind(null, hostInfo, rsyncCmd, args, timeout, null, exitCodes), hostInfo);
|
|
153
161
|
}
|
|
154
162
|
|
|
155
163
|
/**
|
package/lib/sshExec.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import crypto from "crypto";
|
|
2
2
|
import { setTimeout as setTimeoutPromise } from "timers/promises";
|
|
3
3
|
import Debug from "debug";
|
|
4
|
-
import { sendPty, sshCmd, getSshOption, isControlMasterEnabled, sanityCheck, watchDogTimer, rePwPrompt, rePhPrompt } from "./util.js";
|
|
4
|
+
import { sendPty, sshCmd, getSshOption, getNoStdinOption, isControlMasterEnabled, sanityCheck, watchDogTimer, rePwPrompt, rePhPrompt } from "./util.js";
|
|
5
5
|
import { fork, createMasterPty, sshLoginCallback } from "./fork.js";
|
|
6
6
|
import { shouldUseAgent, ensureAgentForHost } from "./agent.js";
|
|
7
7
|
|
|
@@ -28,6 +28,7 @@ export async function sshExec(hostInfo, cmd, timeout, outputCallback, rcfile, pr
|
|
|
28
28
|
await connect(hostInfo);
|
|
29
29
|
debug("exec", cmd, "on remote server");
|
|
30
30
|
const args = getSshOption(hostInfo, false);
|
|
31
|
+
args.push(...getNoStdinOption());
|
|
31
32
|
if (rcfile) {
|
|
32
33
|
args.push(`. ${rcfile};`);
|
|
33
34
|
} else if (hostInfo.rcfile) {
|
|
@@ -118,6 +119,7 @@ export async function canConnect(hostInfo, timeout) {
|
|
|
118
119
|
async function existsMaster(hostInfo, argTimeout) {
|
|
119
120
|
debugVerbose("existsMaster called");
|
|
120
121
|
const args = getSshOption(hostInfo);
|
|
122
|
+
args.push(...getNoStdinOption());
|
|
121
123
|
args.push("-Ocheck");
|
|
122
124
|
|
|
123
125
|
let output = "";
|
|
@@ -154,6 +156,7 @@ async function verifyConnection(hostInfo, timeout) {
|
|
|
154
156
|
return;
|
|
155
157
|
}
|
|
156
158
|
const args = getSshOption(hostInfo);
|
|
159
|
+
args.push(...getNoStdinOption());
|
|
157
160
|
args.push(`echo ${crypto.randomUUID()}`);
|
|
158
161
|
|
|
159
162
|
let output = "";
|
|
@@ -342,6 +345,7 @@ export async function disconnect(hostInfo) {
|
|
|
342
345
|
try {
|
|
343
346
|
if (await existsMaster(hostInfo)) {
|
|
344
347
|
const args = getSshOption(hostInfo);
|
|
348
|
+
args.push(...getNoStdinOption());
|
|
345
349
|
args.push("-Oexit");
|
|
346
350
|
|
|
347
351
|
let output = "";
|
package/lib/util.js
CHANGED
|
@@ -10,7 +10,6 @@ const debug = Debug("sshClientWrapper:debug:util");
|
|
|
10
10
|
|
|
11
11
|
export const sshCmd = "ssh";
|
|
12
12
|
export const rsyncCmd = "rsync";
|
|
13
|
-
export const gokrazyRsyncCmd = "gokr-rsync";
|
|
14
13
|
export const acceptableRsyncRetrunCodes = [10, 11, 12, 13, 14];
|
|
15
14
|
|
|
16
15
|
/**
|
|
@@ -248,59 +247,17 @@ export const isControlMasterEnabled = (hostInfo)=>{
|
|
|
248
247
|
};
|
|
249
248
|
|
|
250
249
|
/**
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
* @param {
|
|
258
|
-
* @returns {
|
|
250
|
+
* ssh options which keep ssh.exe from using the console input of its pty.
|
|
251
|
+
* On Windows, ssh.exe sometimes (about 0.6 % of the starts) stops during its start-up when its stdin is the
|
|
252
|
+
* console input of a ConPTY, and never exits. With stdin from NUL (-n) it never stopped in 1500 starts.
|
|
253
|
+
* -n is only for ssh processes which are started through a pty and need no stdin (not for "ssh -e" of rsync,
|
|
254
|
+
* which talks the rsync protocol through stdin, and not for the interactive "expect" session).
|
|
255
|
+
* Password and passphrase prompts are read from the console, so they still work with -n.
|
|
256
|
+
* @param {string} [platform] - process.platform
|
|
257
|
+
* @returns {string[]} - ["-n"] on win32, otherwise an empty array
|
|
259
258
|
*/
|
|
260
|
-
export const
|
|
261
|
-
|
|
262
|
-
if (platform === "win32") {
|
|
263
|
-
return resolveExecutable(cmd, opt) !== cmd;
|
|
264
|
-
}
|
|
265
|
-
const isExecutable = opt.isExecutable ?? ((p)=>{
|
|
266
|
-
try {
|
|
267
|
-
fs.accessSync(p, fs.constants.X_OK);
|
|
268
|
-
return fs.statSync(p).isFile();
|
|
269
|
-
} catch {
|
|
270
|
-
return false;
|
|
271
|
-
}
|
|
272
|
-
});
|
|
273
|
-
return (env.PATH ?? "").split(":").filter(Boolean)
|
|
274
|
-
.some((dir)=>{
|
|
275
|
-
return isExecutable(path.posix.join(dir, cmd));
|
|
276
|
-
});
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* find the rsync command to be used
|
|
281
|
-
* `rsync` if it is found in PATH, otherwise `gokr-rsync` (gokrazy/rsync) if it is found, otherwise `rsync`.
|
|
282
|
-
* @param {object} opt - options for commandExists (for tests)
|
|
283
|
-
* @returns {string} - command name
|
|
284
|
-
*/
|
|
285
|
-
export const findRsyncCmd = (opt = {})=>{
|
|
286
|
-
for (const candidate of [rsyncCmd, gokrazyRsyncCmd]) {
|
|
287
|
-
if (commandExists(candidate, opt)) {
|
|
288
|
-
debug(`use ${candidate} as rsync`);
|
|
289
|
-
return candidate;
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
return rsyncCmd;
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
let rsyncCommand = null;
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* the rsync command to be used (cached after the first call)
|
|
299
|
-
* @returns {string} - command name
|
|
300
|
-
*/
|
|
301
|
-
export const getRsyncCmd = ()=>{
|
|
302
|
-
rsyncCommand ??= findRsyncCmd();
|
|
303
|
-
return rsyncCommand;
|
|
259
|
+
export const getNoStdinOption = (platform = process.platform)=>{
|
|
260
|
+
return platform === "win32" ? ["-n"] : [];
|
|
304
261
|
};
|
|
305
262
|
|
|
306
263
|
/**
|
|
@@ -444,6 +401,27 @@ export const sendPty = async (pty, message, logger, label)=>{
|
|
|
444
401
|
pty.write(message);
|
|
445
402
|
};
|
|
446
403
|
|
|
404
|
+
/**
|
|
405
|
+
* close the input pipe of a pty which has exited, so that its conhost.exe can go away (win32 only).
|
|
406
|
+
*
|
|
407
|
+
* node-pty (ConPTY) never closes the write end of the console input pipe, neither when the child exits nor in kill().
|
|
408
|
+
* The headless conhost.exe of the pty stays alive as long as that pipe is open, so every pty leaks one conhost.exe
|
|
409
|
+
* until the node process ends. After about 1600 of them the system runs out of resources
|
|
410
|
+
* (rsync.exe fails with 0xC000012D / 0xC0000142, node dies with "Out of memory").
|
|
411
|
+
* This touches a private field of node-pty; it does nothing if the field is not there.
|
|
412
|
+
* @param {object} pty - pty object returned by node-pty spawn()
|
|
413
|
+
* @param {string} [platform] - platform name (injectable for tests)
|
|
414
|
+
*/
|
|
415
|
+
export const releasePty = (pty, platform = process.platform)=>{
|
|
416
|
+
if (platform !== "win32") {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
const inSocket = pty?._agent?._inSocket;
|
|
420
|
+
if (inSocket && !inSocket.destroyed && typeof inSocket.destroy === "function") {
|
|
421
|
+
inSocket.destroy();
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
|
|
447
425
|
/**
|
|
448
426
|
* set watch dog timer
|
|
449
427
|
* @param {number} timeout - timeout (in sec.)
|
package/lib/winRsync.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import os from "os";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import Debug from "debug";
|
|
5
|
+
|
|
6
|
+
import { resolveExecutable, rsyncCmd } from "./util.js";
|
|
7
|
+
import { toMsysPath } from "./pathConvert.js";
|
|
8
|
+
|
|
9
|
+
const debug = Debug("sshClientWrapper:debug:winRsync");
|
|
10
|
+
|
|
11
|
+
//runtime of the rsync builds which take "/d/dir" (MSYS2) or "/cygdrive/d/dir" (Cygwin), but not "D:\dir" ("D" is taken as a host name)
|
|
12
|
+
const RUNTIMES = [
|
|
13
|
+
{ dll: "msys-2.0.dll", style: "msys" },
|
|
14
|
+
{ dll: "cygwin1.dll", style: "cygwin" }
|
|
15
|
+
];
|
|
16
|
+
const defaultIsDir = (p)=>{
|
|
17
|
+
return fs.statSync(p, { throwIfNoEntry: false })?.isDirectory() === true;
|
|
18
|
+
};
|
|
19
|
+
const defaultReadFile = (p)=>{
|
|
20
|
+
try {
|
|
21
|
+
return fs.readFileSync(p, "utf8");
|
|
22
|
+
} catch {
|
|
23
|
+
return "";
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
const defaultIsFile = (p)=>{
|
|
27
|
+
return fs.statSync(p, { throwIfNoEntry: false })?.isFile() === true;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* look at the directory of the local rsync and tell how it wants paths and which ssh it must be paired with.
|
|
32
|
+
* rsync.exe of MSYS2 or Cygwin needs its ssh.exe of the same distribution (it does not work with the OpenSSH of Windows or Git for Windows),
|
|
33
|
+
* and takes local paths in the MSYS2/Cygwin form.
|
|
34
|
+
* @param {object} [opt] - options (platform, rsyncPath and isFile are injectable for tests)
|
|
35
|
+
* @param {string} [opt.platform] - platform name
|
|
36
|
+
* @param {string} [opt.rsyncPath] - path of rsync executable. default is the one found in PATH
|
|
37
|
+
* @param {Function} [opt.isFile] - function which checks if given path is a file
|
|
38
|
+
* @returns {object|null} - { style: "msys"|"cygwin", ssh: path of ssh.exe next to rsync.exe or null }, null if the local rsync takes Windows paths or the platform is not win32
|
|
39
|
+
*/
|
|
40
|
+
export const detectLocalRsync = ({ platform = process.platform, rsyncPath, isFile = defaultIsFile } = {})=>{
|
|
41
|
+
if (platform !== "win32") {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
const exe = rsyncPath ?? resolveExecutable(rsyncCmd, { platform, isFile });
|
|
45
|
+
if (!/[\\/]/.test(exe)) {
|
|
46
|
+
debug("rsync not found in PATH");
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const dir = path.win32.dirname(exe);
|
|
50
|
+
for (const { dll, style } of RUNTIMES) {
|
|
51
|
+
if (isFile(path.win32.join(dir, dll))) {
|
|
52
|
+
const ssh = path.win32.join(dir, "ssh.exe");
|
|
53
|
+
debug(`${exe} is a ${style} build`);
|
|
54
|
+
return { style, ssh: isFile(ssh) ? ssh : null };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
let cached;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* detectLocalRsync() of the rsync in PATH, remembered for the life of the process
|
|
64
|
+
* @returns {object|null} - see detectLocalRsync
|
|
65
|
+
*/
|
|
66
|
+
export const getLocalRsync = ()=>{
|
|
67
|
+
if (cached === undefined) {
|
|
68
|
+
cached = detectLocalRsync();
|
|
69
|
+
}
|
|
70
|
+
return cached;
|
|
71
|
+
};
|
|
72
|
+
export const resetLocalRsync = ()=>{
|
|
73
|
+
cached = undefined;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* convert a local path to the form the local rsync takes
|
|
78
|
+
* @param {string} p - local path
|
|
79
|
+
* @param {object|null} local - return value of detectLocalRsync
|
|
80
|
+
* @returns {string} - path to be put into arguments of rsync
|
|
81
|
+
*/
|
|
82
|
+
export const toRsyncPath = (p, local)=>{
|
|
83
|
+
if (!local) {
|
|
84
|
+
return p;
|
|
85
|
+
}
|
|
86
|
+
return toMsysPath(p, { style: local.style });
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const quoteIfNeeded = (s)=>{
|
|
90
|
+
return /\s/.test(s) ? `"${s}"` : s;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const hasSshOption = (args, re)=>{
|
|
94
|
+
return args.some((e)=>{
|
|
95
|
+
return typeof e === "string" && re.test(e);
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* ssh.exe of MSYS2/Cygwin takes its home from the MSYS2 user database (/home/user), not from the profile of Windows.
|
|
101
|
+
* so ~/.ssh/config and known_hosts of the Windows profile (which ssh.exe of Windows and scw itself use) are not seen.
|
|
102
|
+
* give them explicitly as absolute paths.
|
|
103
|
+
* limitation: "~" and relative "Include" in the config file are resolved against the MSYS2 home, not the Windows profile.
|
|
104
|
+
* @param {string[]} sshArgs - ssh options
|
|
105
|
+
* @param {object} [opt] - options (injectable for tests)
|
|
106
|
+
* @param {string} [opt.home] - home directory of the Windows profile
|
|
107
|
+
* @param {Function} [opt.isFile] - function which checks if given path is a file
|
|
108
|
+
* @param {Function} [opt.isDir] - function which checks if given path is a directory
|
|
109
|
+
* @param {Function} [opt.readFile] - function which returns the text of given file
|
|
110
|
+
* @returns {string[]} - options to be added
|
|
111
|
+
*/
|
|
112
|
+
export const homeSshOptions = (sshArgs, { home = os.homedir(), isFile = defaultIsFile, isDir = defaultIsDir, readFile = defaultReadFile } = {})=>{
|
|
113
|
+
const added = [];
|
|
114
|
+
const sshDir = path.win32.join(home, ".ssh");
|
|
115
|
+
const config = path.win32.join(sshDir, "config");
|
|
116
|
+
if (!hasSshOption(sshArgs, /^-F/) && isFile(config)) {
|
|
117
|
+
added.push("-F", config);
|
|
118
|
+
const text = readFile(config);
|
|
119
|
+
if (/^\s*Include\s/im.test(text) || /^\s*[^#\s].*~/m.test(text)) {
|
|
120
|
+
debug(`WARNING: "~" or "Include" in ${config} is resolved against the MSYS2 home by rsync's ssh and may be ignored`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (!hasSshOption(sshArgs, /^-oUserKnownHostsFile=/i) && isDir(sshDir)) {
|
|
124
|
+
added.push(`-oUserKnownHostsFile=${path.win32.join(sshDir, "known_hosts")}`);
|
|
125
|
+
}
|
|
126
|
+
return added;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* make the value of "rsync -e" : the ssh command line (ssh executable and options)
|
|
131
|
+
* for MSYS2/Cygwin rsync, the ssh.exe next to rsync.exe is used, file paths in the options are converted
|
|
132
|
+
* and config/known_hosts of the Windows profile are given explicitly.
|
|
133
|
+
* @param {string} sshExe - name of ssh command
|
|
134
|
+
* @param {string[]} sshArgs - ssh options
|
|
135
|
+
* @param {object|null} local - return value of detectLocalRsync
|
|
136
|
+
* @param {object} [homeOpt] - options for homeSshOptions (for tests)
|
|
137
|
+
* @returns {string} - value for "-e"
|
|
138
|
+
*/
|
|
139
|
+
export const makeRshCommand = (sshExe, sshArgs, local, homeOpt)=>{
|
|
140
|
+
if (!local) {
|
|
141
|
+
return `${sshExe} ${sshArgs.join(" ")}`;
|
|
142
|
+
}
|
|
143
|
+
const withHome = [...sshArgs, ...homeSshOptions(sshArgs, homeOpt)];
|
|
144
|
+
const converted = withHome.map((e, i, a)=>{
|
|
145
|
+
if (typeof e !== "string") {
|
|
146
|
+
return e;
|
|
147
|
+
}
|
|
148
|
+
if (a[i - 1] === "-i" || a[i - 1] === "-F") {
|
|
149
|
+
return quoteIfNeeded(toRsyncPath(e, local));
|
|
150
|
+
}
|
|
151
|
+
const m = /^(-o(?:ControlPath|UserKnownHostsFile)=)(.*)$/i.exec(e);
|
|
152
|
+
if (m) {
|
|
153
|
+
return `${m[1]}${quoteIfNeeded(toRsyncPath(m[2], local))}`;
|
|
154
|
+
}
|
|
155
|
+
return e;
|
|
156
|
+
});
|
|
157
|
+
const exe = local.ssh ? quoteIfNeeded(toRsyncPath(local.ssh, local)) : sshExe;
|
|
158
|
+
return `${exe} ${converted.join(" ")}`;
|
|
159
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ssh-client-wrapper",
|
|
3
|
-
"version": "3.4.
|
|
3
|
+
"version": "3.4.2",
|
|
4
4
|
"description": "wrapper library for open ssh client and rsync",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ssh",
|
|
@@ -37,13 +37,13 @@
|
|
|
37
37
|
"*.js": "eslint --fix"
|
|
38
38
|
},
|
|
39
39
|
"overrides": {
|
|
40
|
-
"@semantic-release/npm": "
|
|
40
|
+
"@semantic-release/npm": "$@semantic-release/npm"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"ajv": "^8.17.1",
|
|
44
44
|
"ajv-keywords": "^5.1.0",
|
|
45
45
|
"debug": "^4.4.3",
|
|
46
|
-
"glob": "^
|
|
46
|
+
"glob": "^15.0.0",
|
|
47
47
|
"node-pty": "^1.1.0"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"@semantic-release/github": "^11.0.4",
|
|
54
54
|
"@semantic-release/npm": "^13.1.0",
|
|
55
55
|
"@stylistic/eslint-plugin": "^5.2.3",
|
|
56
|
-
"c8": "^
|
|
56
|
+
"c8": "^12.0.0",
|
|
57
57
|
"chai": "^5.3.1",
|
|
58
58
|
"chai-as-promised": "^8.0.1",
|
|
59
59
|
"eslint-plugin-chai-expect": "^3.1.0",
|