@andy.ian/dsh-homing 2.0.2 → 2.1.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.
Files changed (138) hide show
  1. package/bin/restart-daemon.mjs +3 -0
  2. package/bin/restart.mjs +3 -0
  3. package/index.js +785 -29
  4. package/lib/devices.mjs +0 -1
  5. package/lib/files-page.mjs +10 -18
  6. package/lib/files.mjs +39 -27
  7. package/package.json +8 -9
  8. package/restart/client.mjs +184 -0
  9. package/restart/config.mjs +187 -0
  10. package/restart/engine.mjs +93 -0
  11. package/restart/face.mjs +62 -0
  12. package/restart/runner.mjs +184 -0
  13. package/restart/service.mjs +91 -0
  14. package/restart-daemon/README.md +66 -0
  15. package/restart-daemon/cli.mjs +321 -0
  16. package/restart-daemon/lib/detach.mjs +126 -0
  17. package/restart-daemon/lib/process-tree.mjs +69 -0
  18. package/restart-daemon/lib/restart-flow.mjs +111 -0
  19. package/restart-daemon/lib/update-state.mjs +72 -0
  20. package/restart-daemon/main.mjs +691 -0
  21. package/bin/supervisor.mjs +0 -488
  22. package/bin/watchdog.mjs +0 -144
  23. package/lib/vendor/node_modules/ssh2/examples/server-chat.js +0 -238
  24. package/lib/vendor/node_modules/ssh2/examples/sftp-server-download-only.js +0 -134
  25. package/lib/vendor/node_modules/ssh2/test/common.js +0 -316
  26. package/lib/vendor/node_modules/ssh2/test/fixtures/bad_rsa_private_key +0 -26
  27. package/lib/vendor/node_modules/ssh2/test/fixtures/https_cert.pem +0 -33
  28. package/lib/vendor/node_modules/ssh2/test/fixtures/https_key.pem +0 -52
  29. package/lib/vendor/node_modules/ssh2/test/fixtures/id_dsa +0 -12
  30. package/lib/vendor/node_modules/ssh2/test/fixtures/id_ecdsa +0 -5
  31. package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa +0 -15
  32. package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa.ppk +0 -26
  33. package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa_enc +0 -30
  34. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa +0 -21
  35. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.pub +0 -1
  36. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.pub.result +0 -7
  37. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.result +0 -7
  38. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc +0 -22
  39. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.pub +0 -1
  40. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.pub.result +0 -7
  41. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.result +0 -7
  42. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm +0 -23
  43. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.pub +0 -1
  44. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.pub.result +0 -7
  45. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.result +0 -7
  46. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa +0 -9
  47. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.pub +0 -1
  48. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.pub.result +0 -7
  49. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.result +0 -7
  50. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc +0 -10
  51. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.pub +0 -1
  52. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.pub.result +0 -8
  53. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.result +0 -7
  54. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm +0 -10
  55. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.pub +0 -1
  56. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.pub.result +0 -8
  57. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.result +0 -7
  58. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519 +0 -7
  59. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.pub +0 -1
  60. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.pub.result +0 -7
  61. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.result +0 -7
  62. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa +0 -27
  63. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.pub +0 -1
  64. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.pub.result +0 -7
  65. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.result +0 -7
  66. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc +0 -28
  67. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.pub +0 -1
  68. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.pub.result +0 -8
  69. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.result +0 -7
  70. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm +0 -29
  71. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.pub +0 -1
  72. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.pub.result +0 -8
  73. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.result +0 -7
  74. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa +0 -12
  75. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.pub +0 -1
  76. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.pub.result +0 -7
  77. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.result +0 -7
  78. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc +0 -15
  79. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.pub +0 -1
  80. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.pub.result +0 -8
  81. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.result +0 -7
  82. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa +0 -5
  83. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.pub +0 -1
  84. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.pub.result +0 -7
  85. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.result +0 -7
  86. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc +0 -8
  87. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.pub +0 -1
  88. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.pub.result +0 -8
  89. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.result +0 -7
  90. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa +0 -27
  91. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.pub +0 -1
  92. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.pub.result +0 -7
  93. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.result +0 -7
  94. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc +0 -30
  95. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.pub +0 -1
  96. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.pub.result +0 -8
  97. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.result +0 -7
  98. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256 +0 -54
  99. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.pub +0 -1
  100. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.pub.result +0 -6
  101. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.result +0 -6
  102. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_dsa_enc +0 -17
  103. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_dsa_enc.result +0 -7
  104. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa +0 -26
  105. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa.result +0 -7
  106. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa_enc +0 -18
  107. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa_enc.result +0 -7
  108. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa.pub +0 -9
  109. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa.pub.result +0 -7
  110. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa2.pub +0 -10
  111. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa2.pub.result +0 -7
  112. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa3.pub +0 -11
  113. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa3.pub.result +0 -7
  114. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa4.pub +0 -11
  115. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa4.pub.result +0 -7
  116. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa5.pub +0 -8
  117. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa5.pub.result +0 -7
  118. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa6.pub +0 -13
  119. package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa6.pub.result +0 -7
  120. package/lib/vendor/node_modules/ssh2/test/fixtures/openssh_new_rsa +0 -27
  121. package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_dsa_key +0 -12
  122. package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_ecdsa_key +0 -5
  123. package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_rsa_key +0 -15
  124. package/lib/vendor/node_modules/ssh2/test/test-exec.js +0 -578
  125. package/lib/vendor/node_modules/ssh2/test/test-integration-openssh.js +0 -486
  126. package/lib/vendor/node_modules/ssh2/test/test-keygen.js +0 -60
  127. package/lib/vendor/node_modules/ssh2/test/test-misc-client-server.js +0 -1460
  128. package/lib/vendor/node_modules/ssh2/test/test-openssh.js +0 -261
  129. package/lib/vendor/node_modules/ssh2/test/test-protocol-crypto.js +0 -631
  130. package/lib/vendor/node_modules/ssh2/test/test-protocol-keyparser.js +0 -177
  131. package/lib/vendor/node_modules/ssh2/test/test-server-hostkeys.js +0 -138
  132. package/lib/vendor/node_modules/ssh2/test/test-sftp.js +0 -842
  133. package/lib/vendor/node_modules/ssh2/test/test-shell.js +0 -109
  134. package/lib/vendor/node_modules/ssh2/test/test-userauth-agent-openssh.js +0 -110
  135. package/lib/vendor/node_modules/ssh2/test/test-userauth-agent.js +0 -171
  136. package/lib/vendor/node_modules/ssh2/test/test-userauth.js +0 -611
  137. package/lib/vendor/node_modules/ssh2/test/test-worker-imports.js +0 -25
  138. package/lib/vendor/node_modules/ssh2/test/test.js +0 -20
@@ -1,238 +0,0 @@
1
- // **BEFORE RUNNING THIS SCRIPT:**
2
- // 1. The server portion is best run on non-Windows systems because they have
3
- // terminfo databases which are needed to properly work with different
4
- // terminal types of client connections
5
- // 2. Install `blessed`: `npm install blessed`
6
- // 3. Create a server host key in this same directory and name it `host.key`
7
- 'use strict';
8
-
9
- const { readFileSync } = require('fs');
10
-
11
- const blessed = require('blessed');
12
- const { Server } = require('ssh2');
13
-
14
- const RE_SPECIAL =
15
- // eslint-disable-next-line no-control-regex
16
- /[\x00-\x1F\x7F]+|(?:\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K])/g;
17
- const MAX_MSG_LEN = 128;
18
- const MAX_NAME_LEN = 10;
19
- const PROMPT_NAME = `Enter a nickname to use (max ${MAX_NAME_LEN} chars): `;
20
-
21
- const users = [];
22
-
23
- function formatMessage(msg, output) {
24
- output.parseTags = true;
25
- msg = output._parseTags(msg);
26
- output.parseTags = false;
27
- return msg;
28
- }
29
-
30
- function userBroadcast(msg, source) {
31
- const sourceMsg = `> ${msg}`;
32
- const name = `{cyan-fg}{bold}${source.name}{/}`;
33
- msg = `: ${msg}`;
34
- for (const user of users) {
35
- const output = user.output;
36
- if (source === user)
37
- output.add(sourceMsg);
38
- else
39
- output.add(formatMessage(name, output) + msg);
40
- }
41
- }
42
-
43
- function localMessage(msg, source) {
44
- const output = source.output;
45
- output.add(formatMessage(msg, output));
46
- }
47
-
48
- function noop(v) {}
49
-
50
- new Server({
51
- hostKeys: [readFileSync('host.key')],
52
- }, (client) => {
53
- let stream;
54
- let name;
55
-
56
- client.on('authentication', (ctx) => {
57
- let nick = ctx.username;
58
- let prompt = PROMPT_NAME;
59
- let lowered;
60
-
61
- // Try to use username as nickname
62
- if (nick.length > 0 && nick.length <= MAX_NAME_LEN) {
63
- lowered = nick.toLowerCase();
64
- let ok = true;
65
- for (const user of users) {
66
- if (user.name.toLowerCase() === lowered) {
67
- ok = false;
68
- prompt = `That nickname is already in use.\n${PROMPT_NAME}`;
69
- break;
70
- }
71
- }
72
- if (ok) {
73
- name = nick;
74
- return ctx.accept();
75
- }
76
- } else if (nick.length === 0) {
77
- prompt = 'A nickname is required.\n' + PROMPT_NAME;
78
- } else {
79
- prompt = 'That nickname is too long.\n' + PROMPT_NAME;
80
- }
81
-
82
- if (ctx.method !== 'keyboard-interactive')
83
- return ctx.reject(['keyboard-interactive']);
84
-
85
- ctx.prompt(prompt, function retryPrompt(answers) {
86
- if (answers.length === 0)
87
- return ctx.reject(['keyboard-interactive']);
88
- nick = answers[0];
89
- if (nick.length > MAX_NAME_LEN) {
90
- return ctx.prompt(`That nickname is too long.\n${PROMPT_NAME}`,
91
- retryPrompt);
92
- } else if (nick.length === 0) {
93
- return ctx.prompt(`A nickname is required.\n${PROMPT_NAME}`,
94
- retryPrompt);
95
- }
96
- lowered = nick.toLowerCase();
97
- for (const user of users) {
98
- if (user.name.toLowerCase() === lowered) {
99
- return ctx.prompt(`That nickname is already in use.\n${PROMPT_NAME}`,
100
- retryPrompt);
101
- }
102
- }
103
- name = nick;
104
- ctx.accept();
105
- });
106
- }).on('ready', () => {
107
- let rows;
108
- let cols;
109
- let term;
110
- client.once('session', (accept, reject) => {
111
- accept().once('pty', (accept, reject, info) => {
112
- rows = info.rows;
113
- cols = info.cols;
114
- term = info.term;
115
- accept && accept();
116
- }).on('window-change', (accept, reject, info) => {
117
- rows = info.rows;
118
- cols = info.cols;
119
- if (stream) {
120
- stream.rows = rows;
121
- stream.columns = cols;
122
- stream.emit('resize');
123
- }
124
- accept && accept();
125
- }).once('shell', (accept, reject) => {
126
- stream = accept();
127
- users.push(stream);
128
-
129
- stream.name = name;
130
- stream.rows = rows || 24;
131
- stream.columns = cols || 80;
132
- stream.isTTY = true;
133
- stream.setRawMode = noop;
134
- stream.on('error', noop);
135
-
136
- const screen = new blessed.screen({
137
- autoPadding: true,
138
- smartCSR: true,
139
- program: new blessed.program({
140
- input: stream,
141
- output: stream
142
- }),
143
- terminal: term || 'ansi'
144
- });
145
-
146
- screen.title = 'SSH Chatting as ' + name;
147
- // Disable local echo
148
- screen.program.attr('invisible', true);
149
-
150
- const output = stream.output = new blessed.log({
151
- screen: screen,
152
- top: 0,
153
- left: 0,
154
- width: '100%',
155
- bottom: 2,
156
- scrollOnInput: true
157
- });
158
- screen.append(output);
159
-
160
- screen.append(new blessed.box({
161
- screen: screen,
162
- height: 1,
163
- bottom: 1,
164
- left: 0,
165
- width: '100%',
166
- type: 'line',
167
- ch: '='
168
- }));
169
-
170
- const input = new blessed.textbox({
171
- screen: screen,
172
- bottom: 0,
173
- height: 1,
174
- width: '100%',
175
- inputOnFocus: true
176
- });
177
- screen.append(input);
178
-
179
- input.focus();
180
-
181
- // Local greetings
182
- localMessage('{blue-bg}{white-fg}{bold}Welcome to SSH Chat!{/}\n'
183
- + 'There are {bold}'
184
- + (users.length - 1)
185
- + '{/} other user(s) connected.\n'
186
- + 'Type /quit or /exit to exit the chat.',
187
- stream);
188
-
189
- // Let everyone else know that this user just joined
190
- for (const user of users) {
191
- const output = user.output;
192
- if (user === stream)
193
- continue;
194
- output.add(formatMessage('{green-fg}*** {bold}', output)
195
- + name
196
- + formatMessage('{/bold} has joined the chat{/}', output));
197
- }
198
-
199
- screen.render();
200
- // XXX This fake resize event is needed for some terminals in order to
201
- // have everything display correctly
202
- screen.program.emit('resize');
203
-
204
- // Read a line of input from the user
205
- input.on('submit', (line) => {
206
- input.clearValue();
207
- screen.render();
208
- if (!input.focused)
209
- input.focus();
210
- line = line.replace(RE_SPECIAL, '').trim();
211
- if (line.length > MAX_MSG_LEN)
212
- line = line.substring(0, MAX_MSG_LEN);
213
- if (line.length > 0) {
214
- if (line === '/quit' || line === '/exit')
215
- stream.end();
216
- else
217
- userBroadcast(line, stream);
218
- }
219
- });
220
- });
221
- });
222
- }).on('close', () => {
223
- if (stream !== undefined) {
224
- users.splice(users.indexOf(stream), 1);
225
- // Let everyone else know that this user just left
226
- for (const user of users) {
227
- const output = user.output;
228
- output.add(formatMessage('{magenta-fg}*** {bold}', output)
229
- + name
230
- + formatMessage('{/bold} has left the chat{/}', output));
231
- }
232
- }
233
- }).on('error', (err) => {
234
- // Ignore errors
235
- });
236
- }).listen(0, function() {
237
- console.log('Listening on port ' + this.address().port);
238
- });
@@ -1,134 +0,0 @@
1
- 'use strict';
2
-
3
- const { timingSafeEqual } = require('crypto');
4
- const { constants, readFileSync } = require('fs');
5
-
6
- const { Server, sftp: { OPEN_MODE, STATUS_CODE } } = require('ssh2');
7
-
8
- const allowedUser = Buffer.from('foo');
9
- const allowedPassword = Buffer.from('bar');
10
-
11
- function checkValue(input, allowed) {
12
- const autoReject = (input.length !== allowed.length);
13
- if (autoReject) {
14
- // Prevent leaking length information by always making a comparison with the
15
- // same input when lengths don't match what we expect ...
16
- allowed = input;
17
- }
18
- const isMatch = timingSafeEqual(input, allowed);
19
- return (!autoReject && isMatch);
20
- }
21
-
22
- new Server({
23
- hostKeys: [readFileSync('host.key')]
24
- }, (client) => {
25
- console.log('Client connected!');
26
-
27
- client.on('authentication', (ctx) => {
28
- let allowed = true;
29
- if (!checkValue(Buffer.from(ctx.username), allowedUser))
30
- allowed = false;
31
-
32
- switch (ctx.method) {
33
- case 'password':
34
- if (!checkValue(Buffer.from(ctx.password), allowedPassword))
35
- return ctx.reject();
36
- break;
37
- default:
38
- return ctx.reject();
39
- }
40
-
41
- if (allowed)
42
- ctx.accept();
43
- else
44
- ctx.reject();
45
- }).on('ready', () => {
46
- console.log('Client authenticated!');
47
-
48
- client.on('session', (accept, reject) => {
49
- const session = accept();
50
- session.on('sftp', (accept, reject) => {
51
- console.log('Client SFTP session');
52
-
53
- const openFiles = new Map();
54
- let handleCount = 0;
55
- const sftp = accept();
56
- sftp.on('OPEN', (reqid, filename, flags, attrs) => {
57
- // Only allow opening /tmp/foo.txt for writing
58
- if (filename !== '/tmp/foo.txt' || !(flags & OPEN_MODE.READ))
59
- return sftp.status(reqid, STATUS_CODE.FAILURE);
60
-
61
- // Create a fake handle to return to the client, this could easily
62
- // be a real file descriptor number for example if actually opening
63
- // the file on the disk
64
- const handle = Buffer.alloc(4);
65
- openFiles.set(handleCount, { read: false });
66
- handle.writeUInt32BE(handleCount++, 0, true);
67
-
68
- console.log('Opening file for read');
69
- sftp.handle(reqid, handle);
70
- }).on('READ', (reqid, handle, offset, length) => {
71
- let fnum;
72
- if (handle.length !== 4
73
- || !openFiles.has(fnum = handle.readUInt32BE(0, true))) {
74
- return sftp.status(reqid, STATUS_CODE.FAILURE);
75
- }
76
-
77
- // Fake the read
78
- const state = openFiles.get(fnum);
79
- if (state.read) {
80
- sftp.status(reqid, STATUS_CODE.EOF);
81
- } else {
82
- state.read = true;
83
-
84
- console.log(
85
- 'Read from file at offset %d, length %d', offset, length
86
- );
87
- sftp.data(reqid, 'bar');
88
- }
89
- }).on('CLOSE', (reqid, handle) => {
90
- let fnum;
91
- if (handle.length !== 4
92
- || !openFiles.has(fnum = handle.readUInt32BE(0))) {
93
- return sftp.status(reqid, STATUS_CODE.FAILURE);
94
- }
95
-
96
- openFiles.delete(fnum);
97
-
98
- console.log('Closing file');
99
- sftp.status(reqid, STATUS_CODE.OK);
100
- }).on('REALPATH', function(reqid, path) {
101
- const name = [{
102
- filename: '/tmp/foo.txt',
103
- longname: '-rwxrwxrwx 1 foo foo 3 Dec 8 2009 foo.txt',
104
- attrs: {}
105
- }];
106
- sftp.name(reqid, name);
107
- }).on('STAT', onSTAT)
108
- .on('LSTAT', onSTAT);
109
-
110
- function onSTAT(reqid, path) {
111
- if (path !== '/tmp/foo.txt')
112
- return sftp.status(reqid, STATUS_CODE.FAILURE);
113
-
114
- let mode = constants.S_IFREG; // Regular file
115
- mode |= constants.S_IRWXU; // Read, write, execute for user
116
- mode |= constants.S_IRWXG; // Read, write, execute for group
117
- mode |= constants.S_IRWXO; // Read, write, execute for other
118
- sftp.attrs(reqid, {
119
- mode: mode,
120
- uid: 0,
121
- gid: 0,
122
- size: 3,
123
- atime: Date.now(),
124
- mtime: Date.now(),
125
- });
126
- }
127
- });
128
- });
129
- }).on('close', () => {
130
- console.log('Client disconnected');
131
- });
132
- }).listen(0, '127.0.0.1', function() {
133
- console.log(`Listening on port ${this.address().port}`);
134
- });
@@ -1,316 +0,0 @@
1
- 'use strict';
2
-
3
- const assert = require('assert');
4
- const { readFileSync } = require('fs');
5
- const { join } = require('path');
6
- const { inspect } = require('util');
7
-
8
- const Client = require('../lib/client.js');
9
- const Server = require('../lib/server.js');
10
- const { parseKey } = require('../lib/protocol/keyParser.js');
11
-
12
- const mustCallChecks = [];
13
-
14
- const DEFAULT_TEST_TIMEOUT = 30 * 1000;
15
-
16
- function noop() {}
17
-
18
- function runCallChecks(exitCode) {
19
- if (exitCode !== 0) return;
20
-
21
- const failed = mustCallChecks.filter((context) => {
22
- if ('minimum' in context) {
23
- context.messageSegment = `at least ${context.minimum}`;
24
- return context.actual < context.minimum;
25
- }
26
- context.messageSegment = `exactly ${context.exact}`;
27
- return context.actual !== context.exact;
28
- });
29
-
30
- failed.forEach((context) => {
31
- console.error('Mismatched %s function calls. Expected %s, actual %d.',
32
- context.name,
33
- context.messageSegment,
34
- context.actual);
35
- console.error(context.stack.split('\n').slice(2).join('\n'));
36
- });
37
-
38
- if (failed.length)
39
- process.exit(1);
40
- }
41
-
42
- function mustCall(fn, exact) {
43
- return _mustCallInner(fn, exact, 'exact');
44
- }
45
-
46
- function mustCallAtLeast(fn, minimum) {
47
- return _mustCallInner(fn, minimum, 'minimum');
48
- }
49
-
50
- function _mustCallInner(fn, criteria = 1, field) {
51
- if (process._exiting)
52
- throw new Error('Cannot use common.mustCall*() in process exit handler');
53
-
54
- if (typeof fn === 'number') {
55
- criteria = fn;
56
- fn = noop;
57
- } else if (fn === undefined) {
58
- fn = noop;
59
- }
60
-
61
- if (typeof criteria !== 'number')
62
- throw new TypeError(`Invalid ${field} value: ${criteria}`);
63
-
64
- const context = {
65
- [field]: criteria,
66
- actual: 0,
67
- stack: inspect(new Error()),
68
- name: fn.name || '<anonymous>'
69
- };
70
-
71
- // Add the exit listener only once to avoid listener leak warnings
72
- if (mustCallChecks.length === 0)
73
- process.on('exit', runCallChecks);
74
-
75
- mustCallChecks.push(context);
76
-
77
- function wrapped(...args) {
78
- ++context.actual;
79
- return fn.call(this, ...args);
80
- }
81
- // TODO: remove origFn?
82
- wrapped.origFn = fn;
83
-
84
- return wrapped;
85
- }
86
-
87
- function getCallSite(top) {
88
- const originalStackFormatter = Error.prepareStackTrace;
89
- Error.prepareStackTrace = (err, stack) =>
90
- `${stack[0].getFileName()}:${stack[0].getLineNumber()}`;
91
- const err = new Error();
92
- Error.captureStackTrace(err, top);
93
- // With the V8 Error API, the stack is not formatted until it is accessed
94
- // eslint-disable-next-line no-unused-expressions
95
- err.stack;
96
- Error.prepareStackTrace = originalStackFormatter;
97
- return err.stack;
98
- }
99
-
100
- function mustNotCall(msg) {
101
- const callSite = getCallSite(mustNotCall);
102
- return function mustNotCall(...args) {
103
- args = args.map(inspect).join(', ');
104
- const argsInfo = (args.length > 0
105
- ? `\ncalled with arguments: ${args}`
106
- : '');
107
- assert.fail(
108
- `${msg || 'function should not have been called'} at ${callSite}`
109
- + argsInfo);
110
- };
111
- }
112
-
113
- function setup(title, configs) {
114
- const {
115
- client: clientCfg_,
116
- server: serverCfg_,
117
- allReady: allReady_,
118
- timeout: timeout_,
119
- debug,
120
- noForceClientReady,
121
- noForceServerReady,
122
- noClientError,
123
- noServerError,
124
- } = configs;
125
-
126
- // Make shallow copies of client/server configs to avoid mutating them when
127
- // multiple tests share the same config object reference
128
- let clientCfg;
129
- if (clientCfg_)
130
- clientCfg = { ...clientCfg_ };
131
- let serverCfg;
132
- if (serverCfg_)
133
- serverCfg = { ...serverCfg_ };
134
-
135
- let clientClose = false;
136
- let clientReady = false;
137
- let serverClose = false;
138
- let serverReady = false;
139
- const msg = (text) => {
140
- return `${title}: ${text}`;
141
- };
142
-
143
- const timeout = (typeof timeout_ === 'number'
144
- ? timeout_
145
- : DEFAULT_TEST_TIMEOUT);
146
-
147
- const allReady = (typeof allReady_ === 'function' ? allReady_ : undefined);
148
-
149
- if (debug) {
150
- if (clientCfg) {
151
- clientCfg.debug = (...args) => {
152
- console.log(`[${title}][CLIENT]`, ...args);
153
- };
154
- }
155
- if (serverCfg) {
156
- serverCfg.debug = (...args) => {
157
- console.log(`[${title}][SERVER]`, ...args);
158
- };
159
- }
160
- }
161
-
162
- let timer;
163
- let client;
164
- let clientReadyFn;
165
- let server;
166
- let serverReadyFn;
167
- if (clientCfg) {
168
- client = new Client();
169
- if (!noClientError)
170
- client.on('error', onError);
171
- clientReadyFn = (noForceClientReady ? onReady : mustCall(onReady));
172
- client.on('ready', clientReadyFn)
173
- .on('close', mustCall(onClose));
174
- } else {
175
- clientReady = clientClose = true;
176
- }
177
-
178
- if (serverCfg) {
179
- server = new Server(serverCfg);
180
- if (!noServerError)
181
- server.on('error', onError);
182
- serverReadyFn = (noForceServerReady ? onReady : mustCall(onReady));
183
- server.on('connection', mustCall((conn) => {
184
- if (!noServerError)
185
- conn.on('error', onError);
186
- conn.on('ready', serverReadyFn);
187
- server.close();
188
- })).on('close', mustCall(onClose));
189
- } else {
190
- serverReady = serverClose = true;
191
- }
192
-
193
- function onError(err) {
194
- const which = (this === client ? 'client' : 'server');
195
- assert(false, msg(`Unexpected ${which} error: ${err.stack}\n`));
196
- }
197
-
198
- function onReady() {
199
- if (this === client) {
200
- assert(!clientReady,
201
- msg('Received multiple ready events for client'));
202
- clientReady = true;
203
- } else {
204
- assert(!serverReady,
205
- msg('Received multiple ready events for server'));
206
- serverReady = true;
207
- }
208
- clientReady && serverReady && allReady && allReady();
209
- }
210
-
211
- function onClose() {
212
- if (this === client) {
213
- assert(!clientClose,
214
- msg('Received multiple close events for client'));
215
- clientClose = true;
216
- } else {
217
- assert(!serverClose,
218
- msg('Received multiple close events for server'));
219
- serverClose = true;
220
- }
221
- if (clientClose && serverClose)
222
- clearTimeout(timer);
223
- }
224
-
225
- process.nextTick(mustCall(() => {
226
- function connectClient() {
227
- if (clientCfg.sock) {
228
- clientCfg.sock.connect(server.address().port, 'localhost');
229
- } else {
230
- clientCfg.host = 'localhost';
231
- clientCfg.port = server.address().port;
232
- }
233
- try {
234
- client.connect(clientCfg);
235
- } catch (ex) {
236
- ex.message = msg(ex.message);
237
- throw ex;
238
- }
239
- }
240
-
241
- if (server) {
242
- server.listen(0, 'localhost', mustCall(() => {
243
- if (timeout >= 0) {
244
- timer = setTimeout(() => {
245
- assert(false, msg('Test timed out'));
246
- }, timeout);
247
- }
248
- if (client)
249
- connectClient();
250
- }));
251
- }
252
- }));
253
-
254
- return { client, server };
255
- }
256
-
257
- const FIXTURES_DIR = join(__dirname, 'fixtures');
258
- const fixture = (() => {
259
- const cache = new Map();
260
- return (file) => {
261
- const existing = cache.get(file);
262
- if (existing !== undefined)
263
- return existing;
264
-
265
- const result = readFileSync(join(FIXTURES_DIR, file));
266
- cache.set(file, result);
267
- return result;
268
- };
269
- })();
270
- const fixtureKey = (() => {
271
- const cache = new Map();
272
- return (file, passphrase, bypass) => {
273
- if (typeof passphrase === 'boolean') {
274
- bypass = passphrase;
275
- passphrase = undefined;
276
- }
277
- if (typeof bypass !== 'boolean' || !bypass) {
278
- const existing = cache.get(file);
279
- if (existing !== undefined)
280
- return existing;
281
- }
282
- const fullPath = join(FIXTURES_DIR, file);
283
- const raw = fixture(file);
284
- let key = parseKey(raw, passphrase);
285
- if (Array.isArray(key))
286
- key = key[0];
287
- const result = { key, raw, fullPath };
288
- cache.set(file, result);
289
- return result;
290
- };
291
- })();
292
-
293
- function setupSimple(debug, title) {
294
- const { client, server } = setup(title, {
295
- client: { username: 'Password User', password: '12345' },
296
- server: { hostKeys: [ fixtureKey('ssh_host_rsa_key').raw ] },
297
- debug,
298
- });
299
- server.on('connection', mustCall((conn) => {
300
- conn.on('authentication', mustCall((ctx) => {
301
- ctx.accept();
302
- }));
303
- }));
304
- return { client, server };
305
- }
306
-
307
- module.exports = {
308
- fixture,
309
- fixtureKey,
310
- FIXTURES_DIR,
311
- mustCall,
312
- mustCallAtLeast,
313
- mustNotCall,
314
- setup,
315
- setupSimple,
316
- };
@@ -1,26 +0,0 @@
1
- -----BEGIN RSA PRIVATE KEY-----
2
- MIIEpQIBAAKCAQEAz7MF4vhgw6HxNf3KtVf3VULTYgrRSlv+cCZdB1xxI1p/nGyu
3
- /eekUn5C+mGeDS488DX5ulzicxVpL7pamc/tFNcp91MrR7PiIMK2l+bwbZJubbLj
4
- DHhNcBklnFOSKxtmQRfuorGakpy/kXmIxF5of0xXGns6DlHRq9dGCJIXvrkqhcEb
5
- k4n2y4aV4VOiMHdo6FrFQVPzA8DlbJP2SjIFZ/0VdK7O7eiyiqV1p1xlbTQQ5rAX
6
- LdsshBn/GvoBOTCVupMXurn2582vgGh26Mmovj2QGzScMGUVttkMlnxUmKT/aQka
7
- mC0vR54QOW7lyWPjAitOV0qgmtGm3/cl7W7NjwIDAQABAoIBAFxH0C+951BEXWV9
8
- s1jLEqshG8YNxFtjcDLn+KFSoznv9Y7MgxtwlgPI8X1Jbe2xQ4X+lUwGBN7Y/nkk
9
- NSjtxwphZtXqb+pVs/yWRoZLJzunucSnnFVoBg/uPFWuk9zvOYlmVrKWcnT9i+fY
10
- tbl5sLgOdQzg/zRpidztssIQFti3o2jnpyrEGcepPWLkfCgqPfGmNv78BAIt/6iT
11
- zYDB4GMSq/LnPTIOFsIOvlkZg3RCcLWeAPRC+lvFQVY+M/uJL5WIbA5il1IMMKH7
12
- MULWpRO3lnb1JVrkZlBldK5uew6AN3tHDQOmg+C2JuIbOZ35J9dcnwsE+IptWWBj
13
- XiFRJCECgYEA8BeuufkslureqOycaPLMkqchMTue1OxbLJFvPN+dh/cW6Lng3b8+
14
- xAyzZrc0vccH/jl9WVHhIZ7TcKXDzSmmrtnZ/3m1c4gANGqIPwO+emL1ZzzkIKGd
15
- FrLeBZKP4TWry9kjg4cG1SKGpcB5ngJMPXUxMZNe74tC4Hk820PkFjcCgYEA3XXn
16
- ngRCgH9N1eKSD2daxxlBhTTSnTgjU+dDaDFQzPIhJCcS8HwyQBQmNTOSXXK9sShC
17
- fdXAsmiBby5WEBq/K5+cXeDG2ZlFLyPovEgTUrLgraw42PYs0+A8Ls7dFk7PuMez
18
- 3G2gUPkY039JiyXKfcog9/dIRfbWCwzQ6s7TV2kCgYEArsme81cahhgg1zvCNokk
19
- M1Omz2/HFt2nFpAeOmPVDGnu7Kh9sxGKgTF53bpclBh0kjiKL99zFYXKCoUzQYYk
20
- CcEhemLBnYUSGRbBb5arMfAfFfR3Y+YkNaUsC0SCqILpOfMvbo57g+ipu7ufDlA/
21
- 7rIFiUDvaVap7j909W+8egsCgYEAsuc/0DBixMmSyHl7QwRcmkC15HVSu32RVIOb
22
- ub01KAtmaH1EWJAMTCW64/mggOtjgI0kgeE/BSFVhsqo7eOdkhEj0db27OxbroRU
23
- zF1xdrpYtRRO7D6a4iLgm3OzuQS72+tASo8pFqDUxG6sq8NAvLOgRJE4ioSoT07w
24
- KvAgXRkCgYEAmWgcsX/BdNcKOteSDtPkys5NRtWCBz7Coxb+xXXoXz1FVegBolpY
25
- wXVePvXTIbU8VJOLunMyH5wpmMUiJbTX9v2o/yfpsH0ci4GaAeVtqpA=
26
- -----END RSA PRIVATE KEY-----