-
Notifications
You must be signed in to change notification settings - Fork 2
/
tools.js
286 lines (283 loc) · 9.83 KB
/
tools.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const fs = require('fs');
const request = require('request');
const exec = require('child_process').exec;
const spawn = require('child_process').spawn;
const AutoLaunch = require('auto-launch');
const path = require('path');
const Winreg = require('winreg');
function local(string, list) {
return new Promise((yes, no) => {
try {
let child = spawn(string, list);
child.stdout.on("data", function (data) {
// console.log(data.toString('utf8'));
yes(data.toString('utf8'));
});
child.stderr.on("data", function (data) {
console.error(data.toString('utf8'));
no(data.toString('utf8'));
});
child.on("error", function (data) {
console.error(data.toString('utf8'));
no(data.toString('utf8'));
});
child.stdin.end(); //end input
} catch (e) {
no(e);
}
});
}
module.exports = {
parseConfigFile: function (name) {
let err, data = fs.readFileSync(name, 'utf8');
if (err) throw err;
return JSON.parse(data);
},
parseValue: function (data) {
if (data == "" || data == " ") {
return "";
}
if (data.charAt(0) == "\"" && data.charAt(data.length - 1) == "\"") {
return String(data.slice(1, data.length - 1))
}
if (data == "true") {
return true;
} else if (data == "false") {
return false;
}
let n = Number(data);
if (!isNaN(n)) {
return n;
}
return String(data)
},
saveToFile: function (file, data) {
try {
fs.writeFileSync(file, data);
return true;
} catch {
return false;
}
},
localProxy: function () {
return local("powershell",
["-Command",
"(Get-ItemProperty -Path 'HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings').proxyServer"
]);
},
setProxy: function (item, str) {
let regKey = new Winreg({
hive: Winreg.HKCU, // open registry hive HKEY_CURRENT_USER
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
});
if (str == undefined || str.length <= 0) {
console.log("disable");
regKey.set("ProxyEnable", Winreg.REG_DWORD, 0, (e) => {
if (e != undefined) {
item.label = e;
return
}
item.checked = false;
item.label = 'Set System Proxy';
});
return;
}
console.log("enable");
regKey.set("ProxyEnable", Winreg.REG_DWORD, 1, (e) => {
if (e != undefined) {
item.label = e;
return
}
item.label = 'Set System Proxy';
});
regKey.set("ProxyServer", Winreg.REG_SZ, str, (e) => {
if (e != undefined) {
item.label = e;
return
}
item.checked = true;
});
},
getProxy: function (func) {
let regKey = new Winreg({
hive: Winreg.HKCU, // open registry hive HKEY_CURRENT_USER
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
});
regKey.get("ProxyEnable", func);
},
openFile: function (path) {
exec(`start "" "${path}"`);
},
localF: local,
remoteVersion: function (func, firstRun) {
this.localProxy().then((p) => {
p = p.replace(/(\r\n|\n|\r)/gm, "");
let ip = p.split(":")[0];
let port = p.split(":")[1];
const options = {
url: 'https://api.github.com/repos/v2ray/v2ray-core/releases/latest',
headers: {
'User-Agent': 'Mozilla/5.0'
},
proxy: {
host: ip,
port: port
}
};
request(options, function (error, response, body) {
//console.log('error:', error); // Print the error if one occurred
if (error != null && !firstRun) {
alert(error);
return;
}
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
//console.log('body:', body); // Print the HTML for the Google homepage.
if (func != undefined) {
jData = JSON.parse(body);
let v = jData.tag_name.substring(1);
func(v, jData, options);
}
});
});
},
autoStart: function (pathName, set) {
let fileName = "";
if (fs.lstatSync(pathName).isDirectory()) {
let insides = fs.readdirSync(pathName);
for (let i = 0, item; item = insides[i]; i++) {
// do stuff with path
console.log(item);
if (item.match(/electron.*\.exe/g) != null || item.match(/v2ray.*\.exe/g) != null) {
fileName = path.join(pathName, item);
}
}
} else {
fileName = pathName;
}
let veAutoLauncher = new AutoLaunch({
name: 'v2rayE',
path: path.join(pathName),
});
if (set) {
veAutoLauncher.isEnabled()
.then(function (isEnabled) {
if (isEnabled) {
return;
}
veAutoLauncher.enable();
})
.catch(function (err) {
// handle error
console.log(err);
});
} else {
veAutoLauncher.isEnabled()
.then(function (isEnabled) {
if (!isEnabled) {
return;
}
veAutoLauncher.disable();
})
.catch(function (err) {
// handle error
console.log(err);
});
}
},
unzip: function (command, source, options, restartFunc) {
// exec(`start "" "${options.cwd}"`);
console.log(`${command} "${source}"`);
let child = spawn(`${command} "${source}"`, options);
let str = "";
child.stdout.on("data", function (data) {
console.log(data.toString('utf8'));
str += data.toString('utf8') + "\n";
});
child.stderr.on("data", function (data) {
console.error(data.toString('utf8'));
});
child.on("error", function (data) {
console.error(data.toString('utf8'));
exec(`start "" "${options.cwd}"`);
});
child.on("exit", () => {
restartFunc();
alert(str);
})
child.stdin.end();
/*exec(`${command} ${source}`, options, (e, stdo, stde) => {
restartFunc();
if (e != undefined) {
console.error(e);
alert(e);
exec(`start "" "${options.cwd}"`);
return;
}
alert(stdo);
});*/
/*try {
fs.createReadStream(source).on("error", (e) => {
alert(e);
}).pipe(unzip.Extract({
path: target
}).on("error", (e) => {
alert(e);
})).on("close", () => {
alert("unzip done!");
})
} catch (e) {
alert(e);
}*/
/*localC("powershell",
["-Command",
"Expand-Archive -Path .\\v2ray-windows-64.zip -DestinationPath .\\"
], path).then((info) => {
alert("unzip done");
}, (e) => {
console.error(e);
alert(`unzip error: ${e}`);
});*/
/*yauzl.open(source, {
lazyEntries: true
}, function (err, zipfile) {
if (err) alert(err);
zipfile.readEntry();
zipfile.on("entry", function (entry) {
let to = path.join(target, entry.fileName);
if (/\/$/.test(entry.fileName)) {
// Directory file names end with '/'.
// Note that entires for directories themselves are optional.
// An entry's fileName implicitly requires its parent directories to exist.
console.log(path.join(source, entry.fileName));
if (!fs.existsSync(to)) {
mkdirp(to, (err) => {
if (err) {
alert(err);
}
});
}
zipfile.readEntry();
} else {
// file entry
console.log(entry.fileName, to);
zipfile.readEntry();
zipfile.openReadStream(entry, function (err, readStream) {
if (err) alert(err);
readStream.on("end", function () {
zipfile.readEntry();
});
readStream.pipe(fs.createWriteStream(to));
});
}
})
.once("error", (e) => {
alert(e);
console.error(e);
zipfile.readEntry();
})
.once('close', () => {
alert("update done");
});;
});*/
}
};