-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
executable file
·208 lines (184 loc) · 5.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
var inpath = require('inpath').sync;
var pidof = require('pidof');
var sudo = inpath('sudo');
var isWin = (process.platform == 'win32');
var messages = [
'PID is null',
'Password is invalid'
];
function sudoCommand(command, password, withResult, callback) {
password = password || '';
withResult = withResult === undefined ? true : withResult;
// callback = callback || function() {
// };
if (typeof callback != 'function') {
callback = function() {
}
}
var error = null;
var pid = '';
var result = '';
var prompt = '#sudo-js-passwd#';
var prompts = 0;
var args = ['-S', '-k', '-p', prompt].concat(command);
var spawnProcess = spawn(sudo, args, {stdio: 'pipe'});
var bin = command.filter(function(i) {
return i.indexOf('-') !== 0;
})[0];
spawnProcess.stdout.on('data', function(data) {
result += "\n"+ data.toString();
});
if (withResult) {
spawnProcess.on('close', function(code) {
callback(error, pid, result);
});
}
function waitForStartup(err, _pid) {
if (err) {
throw new Error('Couldn\'t start '+ bin);
}
if (_pid || spawnProcess.exitCode !== null) {
error = null;
pid = _pid;
if (!withResult) {
try {
callback(error, pid, result);
} catch (e) {
}
}
} else {
setTimeout(function() {
pidof(bin, waitForStartup);
}, 100);
}
}
pidof(bin, waitForStartup);
spawnProcess.stderr.on("data", function (data) {
data.toString().trim().split('\n').forEach(function(line) {
if (line === prompt) {
if (++prompts > 1) {
callback(true, {code: 1, msg: messages[1]}, result);
spawnProcess.stdin.write("\n\n\n\n");
} else {
spawnProcess.stdin.write(password + "\n");
}
}
if (errorFound(line)) {
if(error === null) {
error = ''
}
error += data.toString().trim() // In case script throws multiple errors
// error = new Error(data.toString().trim())
}
});
});
}
function errorFound(line) {
const errors = ['E: Invalid operation', 'could not be found', 'command not found', 'Invalid subcommand'];
return errors.filter(err => line.includes(err)).length ? true : false
}
function sudoCommandForWindows(command, withResult, callback) {
var bin = command[0];
command.splice(0, 1);
var result = '';
var task = spawn(bin, command, {
shell: true
});
task.stderr.on('data', function(data) {
data = data.toString();
if (data.trim() != '' && withResult) {
result += "\n"+ data;
}
});
task.stdout.on('data', function(data) {
data = data.toString();
if (data.trim() != '' && withResult) {
result += "\n"+ data;
}
});
task.on('close', function(code) {
callback(null, null, result);
});
task.on('error', function(code) {
callback(true, null, result);
});
}
module.exports = {
password: '',
setPassword: function(password) {
this.password = password;
},
check: function(callback) {
if (isWin) {
// next update
callback(true);
} else {
sudoCommand(['ls'], this.password, false, (function(i) {
return function (err) {
if (!i++) {
callback(!err)
}
}
})(0));
}
},
exec: function(command, options, callback) {
var self = this;
if (typeof options == 'function') {
callback = options;
options = {};
}
if (typeof options !== 'object') {
options = {};
}
if (typeof callback !== 'function') {
callback = function() {
}
}
if (!Array.isArray(command)) {
command = [command];
}
if (isWin) {
// exec(command.join(' '), function(err, stdout, stderr) {
// callback(err, {}, stdout.toString());
// });
// change method to spawn
sudoCommandForWindows(command, options.withResult, callback);
} else {
if (options.check === true || options.check === undefined) {
this.check(function(valid) {
if (valid) {
sudoCommand(command, self.password,
options.withResult, callback);
} else {
callback(true, {code: 1, msg: messages[1]}, '');
}
});
} else {
sudoCommand(command, self.password, options.withResult, callback);
}
}
},
killByPid: function(pid, callback) {
if (pid) {
pid = pid.toString();
if (isWin) {
this.exec(["tskill", pid], callback);
} else {
this.exec(["kill", "-9", pid], callback);
}
}
},
killByName: function(name, callback) {
var self = this;
pidof(name, function(err, pid) {
if (pid) {
self.killByPid(pid, callback);
} else {
callback(true, {code: 0, msg: messages[0]}, '');
}
});
}
}