-
Notifications
You must be signed in to change notification settings - Fork 3
/
sudo-prompt.js
executable file
·206 lines (183 loc) · 5.68 KB
/
sudo-prompt.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
/**
* @name: sudo-prompt
* @description: 调用系统弹窗组件申请权限执行脚本或命令
*/
const { app } = require('electron');
const child = require('child_process');
const fs = require('fs');
const path = require('path');
const inPath = require(path.join(__dirname, 'in-path'));
class SudoPrompt {
constructor(myApp) {
this.bins = [
'/usr/bin/pkexec',
'/usr/bin/gksu',
];
this.bin = null;
this.password = null;
this.pathRuntime = path.join((myApp || app).getPath('appData'), 'electronux/runtime');
this.passwordFile = path.join(this.pathRuntime, 'password.conf');
}
// 识别系统权限弹窗获取程序 //
getBin() {
if (this.bin) return this.bin;
for (let i = 0; i < this.bins.length; i += 1) {
if (fs.existsSync(this.bins[i])) {
this.bin = this.bins[i];
break;
}
}
return this.bin;
}
// 设置密码 //
setPassword(passwd) {
if (passwd) {
this.password = passwd;
fs.writeFileSync(this.passwordFile, passwd, {
encoding: 'utf8',
flag: 'w',
});
}
}
// 从文件中读取用户密码 //
readPassword() {
let password;
if (!this.password) {
password = fs.readFileSync(this.passwordFile);
password = password.toString().trim();
this.password = password;
}
return this.password;
}
// 检查用户用户密码是否已经设置 //
checkPassword() {
let password = fs.readFileSync(this.passwordFile);
password = password.trim();
if (password) return true;
return false;
}
/**
* [exec 执行一个命令]
* @param { [String] } command [命令]
* @param { [Array | String] } params [参数数组]
* @param { [Object] } options [exec可定制的参数]
* @return { Promise } [返回Promise对象]
*/
async exec(_command, _params, _options) {
const self = this;
self.getBin();
const params = Array.isArray(_params) ? _params.join(' ') : _params;
const options = (typeof (_options) === 'object') ? _options : {};
const command = `${self.bin} ${_command} ${params}`;
return new Promise(async (resolve, reject) => {
child.exec(command, options, (_err, _stdout, _stderr) => {
if (_err || _stderr) {
const err = !_err ? _stderr : _err;
reject(err);
} else {
resolve(_stdout);
}
});
});
}
/**
* [execFile 执行一个脚本文件]
* @param { [String] } path [脚本路径]
* @param { [Array | String] } params [参数数组]
* @param { [Object] } options [exec可定制的参数]
* @return { Promise } [返回Promise对象]
*/
async execFile(_path, _params, _options) {
const self = this;
self.getBin();
const params = Array.isArray(_params) ? _params.join(' ') : _params;
const options = (typeof (_options) === 'object') ? _options : {};
const command = `${self.bin} ${_path} ${params}`;
return new Promise(async (resolve, reject) => {
child.exec(command, options, (_err, _stdout, _stderr) => {
if (_err || _stderr) {
const err = !_err ? _stderr : _err;
reject(err);
} else {
resolve(_stdout);
}
});
});
}
/**
* [exec 执行一个命令]
* @param { [String] } command [命令]
* @param { [Array] } params [参数数组]
* @param { [Object] } options [exec可定制的参数]
* @return { Promise } [返回Promise对象]
*/
async spawn({
_command, _params, _options, _stdout, _stderr, _close,
}) {
const self = this;
self.getBin();
const params = Array.isArray(_params) ? _params : [_params];
params.unshift(_command);
const options = (typeof (_options) === 'object') ? _options : {};
const command = self.bin;
const childSpawn = child.spawn(command, params, options);
// data output
childSpawn.stdout.on('data', (d) => {
_stdout(d.toString());
});
// err output
childSpawn.stderr.on('data', (d) => {
console.log('sudo stderr: ', d.toString());
_stderr(d.toString());
});
// process exit
childSpawn.on('close', (code) => {
console.log('sudo close: ', code);
_close(code);
});
}
/**
* [spawnWithPasswd 使用保存的密码直接执行命令]
* @param { [String] } command [命令]
* @param { [Array] } params [参数数组]
* @param { [Object] } options [exec可定制的参数]
* @return { Promise } [返回Promise对象]
*/
async spawnWithPasswd({
_command, _params, _options, _stdout, _stderr, _close,
}) {
const self = this;
const prompt = '<::sudo-password::>';
self.readPassword();
if (!self.password) {
_close && (_close(1));
return;
}
const params = Array.isArray(_params) ? _params : [_params];
// params.unshift(_command);
const options = (typeof (_options) === 'object') ? _options : {};
const command = ['-S', '-k', '-p', prompt].concat(_command).concat(params);
const sudo = inPath('sudo');
const childSpawn = child.spawn(sudo, command, options);
// data output
childSpawn.stdout.on('data', (d) => {
(_stdout) && (_stdout(d.toString()));
});
// err output
childSpawn.stderr.on('data', (d) => {
console.log('spawn stderr-> ', d.toString());
const message = d.toString().trim();
if (message === prompt) {
childSpawn.stdin.write(`${self.password}\n`);
} else {
childSpawn.stdin.write('Y\n');
}
});
// process close
childSpawn.on('close', (code) => {
console.log('spawn close-> ', code);
(_close) && (_close(code));
});
}
}
module.exports = SudoPrompt;