1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | module.exports = {
|
25 | getPass: getPass
|
26 | };
|
27 |
|
28 | const mod_tty = require('tty');
|
29 | const mod_fs = require('fs');
|
30 | const mod_assert = require('assert-plus');
|
31 |
|
32 | var BACKSPACE = String.fromCharCode(127);
|
33 | var CTRLC = '\u0003';
|
34 | var CTRLD = '\u0004';
|
35 |
|
36 | function getPass(opts, cb) {
|
37 | if (typeof (opts) === 'function' && cb === undefined) {
|
38 | cb = opts;
|
39 | opts = {};
|
40 | }
|
41 | mod_assert.object(opts, 'options');
|
42 | mod_assert.func(cb, 'callback');
|
43 |
|
44 | mod_assert.optionalString(opts.prompt, 'options.prompt');
|
45 | if (opts.prompt === undefined)
|
46 | opts.prompt = 'Password';
|
47 |
|
48 | openTTY(function (err, rfd, wfd, rtty, wtty) {
|
49 | if (err) {
|
50 | cb(err);
|
51 | return;
|
52 | }
|
53 |
|
54 | wtty.write(opts.prompt + ':');
|
55 | rtty.resume();
|
56 | rtty.setRawMode(true);
|
57 | rtty.resume();
|
58 | rtty.setEncoding('utf8');
|
59 |
|
60 | var pw = '';
|
61 | rtty.on('data', onData);
|
62 |
|
63 | function onData(data) {
|
64 | var str = data.toString('utf8');
|
65 | for (var i = 0; i < str.length; ++i) {
|
66 | var ch = str[i];
|
67 | switch (ch) {
|
68 | case '\r':
|
69 | case '\n':
|
70 | case CTRLD:
|
71 | cleanup();
|
72 | cb(null, pw);
|
73 | return;
|
74 | case CTRLC:
|
75 | cleanup();
|
76 | cb(new Error('Aborted'));
|
77 | return;
|
78 | case BACKSPACE:
|
79 | pw = pw.slice(0, pw.length - 1);
|
80 | break;
|
81 | default:
|
82 | pw += ch;
|
83 | break;
|
84 | }
|
85 | }
|
86 | }
|
87 |
|
88 | function cleanup() {
|
89 | wtty.write('\r\n');
|
90 | rtty.setRawMode(false);
|
91 | rtty.pause();
|
92 | rtty.removeListener('data', onData);
|
93 | if (wfd !== undefined && wfd !== rfd) {
|
94 | wtty.end();
|
95 | mod_fs.closeSync(wfd);
|
96 | }
|
97 | if (rfd !== undefined) {
|
98 | rtty.end();
|
99 | mod_fs.closeSync(rfd);
|
100 | }
|
101 | }
|
102 | });
|
103 | }
|
104 |
|
105 | function openTTY(cb) {
|
106 | mod_fs.open('/dev/tty', 'r+', function (err, rttyfd) {
|
107 | if ((err && (err.code === 'ENOENT' || err.code === 'EACCES')) ||
|
108 | (process.version.match(/^v0[.][0-8][.]/))) {
|
109 | cb(null, undefined, undefined, process.stdin,
|
110 | process.stdout);
|
111 | return;
|
112 | }
|
113 | var rtty = new mod_tty.ReadStream(rttyfd);
|
114 | mod_fs.open('/dev/tty', 'w+', function (err3, wttyfd) {
|
115 | var wtty = new mod_tty.WriteStream(wttyfd);
|
116 | if (err3) {
|
117 | cb(err3);
|
118 | return;
|
119 | }
|
120 | cb(null, rttyfd, wttyfd, rtty, wtty);
|
121 | });
|
122 | });
|
123 | }
|