-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.js
311 lines (287 loc) · 10.4 KB
/
utils.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
const os = require('os');
const fs = require('fs');
const http = require('http');
const md5 = require('./libs/md5');
const vm = require('vm');
const {
Worker,
isMainThread,
parentPort,
workerData
} = require('worker_threads');
const logger = require('./libs/log');
// const {
// runTime
// } = require('./server');
module.exports = {
//runTime:runTime.common,
toast: function (msg, code) {
utools.showNotification(msg, 'main'); //
},
//获取内网ip
getLocalIp: function () {
var map = [];
var nif = os.networkInterfaces();
this.log('nif:', nif);
runTime.localIp = '';
var level = 0;
for (let i in nif) {
for (let ii in nif[i]) {
if(nif[i][ii].family.toLowerCase() != 'ipv4' || nif[i][ii].internal)continue;
var l = 0;
if(!runTime.localIp) l = 1;
if(!/^(utun|v)/i.test(i)) l=2;
if(/^192\.168/.test( nif[i][ii].address)) l=3;
if(/^(以太网|en|eth|wlan)/i.test(i)) l=4;
if(nif[i][ii].address == runTime.settings.localIp) l=5;
if(l > level){
level = l;this.log(level);
runTime.localIp = nif[i][ii].address;
if(l==5)return runTime.localIp;
}
}
}
if (runTime.settings.localIp)
this.toast('未找到指定IP:' + runTime.settings.localIp);
return runTime.localIp;
},
clearFeatures: function () {
//移除动态加载的功能
var f = utools.getFeatures();
for (let i in f) {
if (/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.test(f[i].code)) {
utools.removeFeature(f[i].code);
}
}
},
addFeature: function (ip, name, id) {
utools.setFeature({
"code": "" + ip,
"explain": `发送给:主机名(IP)`,
// "icon": "res/xxx.png",
// "icon": "data:image/png;base64,xxx...",
// "platform": ["win32", "darwin", "linux"]
"cmds": [
{
"type": "over",
"label": `发送给:${name}(${ip})`,
// 排除的正则 (可选)
//"exclude":"/xxx/i",
// 长度限制(主输入框中的字符不少于) (可选)
"minLength": 1,
// 长度限制(不多于) (可选)
//"maxLength": 1
},
{
"type": "files",
"label": `发送给:${name}(${ip})`,
// 支持file或directory (可选)
"fileType": "file",
// 文件名称正则匹配 (可选)
//"match": "/xxx/",
// 数量限制(不少于) (可选)
"minNum": 1,
// 数量限制(不多于) (可选)
"maxNum": 1
},
{
// 类型,可能的值(img, files, regex, over)
"type": "img",
// 文字说明,在搜索列表中出现(必须)
"label": `发送给:${name}(${ip})`,
},
]
});
runTime.hosts[ip] = {
ip:ip,
hostName:name,
nativeId:id,
};
},
detectDevice: function (_ipSeg) {
var _this = this;
//this.clearFeatures();
var localId = utools.getLocalId();
var localIp = _this.getLocalIp();
var ipSeg = localIp.split('.');
if (typeof _ipSeg == 'undefined') {
ipSeg = ipSeg[0] + '.' + ipSeg[1] + '.' + ipSeg[2];
} else if (/^\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(_ipSeg)) {
ipSeg = _ipSeg;
} else {
return;
}
console.log(ipSeg);
_this.log(ipSeg);
for (let i = 0; i < 256; i++) {
var ip = ipSeg + '.' + i;
(function (ip) {
//console.log(ip, '-', new Date().getTime());
if (ip == localIp) return;
const req = http.get(`http://${ip}:${runTime.settings.targetPort}/detect`, {
headers: {
'cmd': 'detect',
'ip': localIp,
'id': localId,
'name': encodeURIComponent(runTime.settings.name),
'findingCode': runTime.settings.findingCode.code
},
timeout: 3500,
}, (res) => {
console.log(ip);
console.log('res.headers:', res.headers);
_this.log('detectDevice:' + ip);
_this.log('res.headers:', res.headers);
if (!res.headers.id) return;
_this.addFeature(ip, decodeURIComponent(res.headers.name), res.headers.id);
res.resume();
if (i == 255) // && typeof _ipSeg != 'undefined'
_this.toast(ipSeg + '.0~255 扫描完毕!');
}).on('timeout', () => {
// 必须监听 timeout 事件 并中止请求 否则请求参数中的 timeout 没有效果
req.destroy();
}).on('error', (err) => {
utools.removeFeature(ip);
if (i == 255) //&& typeof _ipSeg != 'undefined'
_this.toast(ipSeg + '.0~255 扫描完毕!');
});
})(ip);
}
if (typeof _ipSeg == 'undefined' && runTime.settings.otherIpSeg != '')
this.detectDevice(runTime.settings.otherIpSeg);
},
detectDevice5: function (_ipSeg) {
var _this = this;
//this.clearFeatures();
var localId = utools.getLocalId();
var localIp = _this.getLocalIp();
var ipSeg = localIp.split('.');
ipSeg = ipSeg[0] + '.' + ipSeg[1];
console.log(ipSeg);
for (let j = 0; j < 256; j++) {
for (let i = 0; i < 256; i++) {
var ip = ipSeg + '.' + j + '.' + i;
(function (ip) {
//console.log(ip, '-', new Date().getTime());
const req = http.get(`http://${ip}:8891/detect`, {
//todo 头部增加暗号
headers: {
'ip': localIp,
'id': localId,
'name': runTime.settings.name,
'findingCode': runTime.settings.findingCode.code
},
timeout: 100,
}, (res) => {
console.log(ip);
console.log('res:', res);
if (ip == localIp) return;
_this.addFeature(ip, res.headers.name, res.headers.id);
res.resume();
}).on('timeout', () => {
req.destroy();
}).on('error', (err) => {
utools.removeFeature(ip);
if (i == 255)
console.log(ip, '-', new Date().getTime());
});
})(ip);
}
}
},
detectDevice4: function () {
if (isMainThread) {
const worker = new Worker('./detect.js');
worker.on('message', function (data) {
console.log('message:', data)
});
}
},
detectDevice3: function () {
var _this = this;
this.clearFeatures();
var localIp = _this.getLocalIp();
var ipSeg = localIp.split('.');
ipSeg.pop();
ipSeg.pop();
var ips = [];
const context = {
ips: [],
ipSeg: ipSeg,
localIp: localIp,
http: http,
utools: utools
};
vm.createContext(context); //return;
code = `for (let j = 0; j < 256; j++) {
for (let i = 0; i < 256; i++) {
var ip = ipSeg.join('.') + '.' + j + '.' + i;
(function (ip) {
http.get('http://'+ip+':8891/detect', {
headers: {
'ip': localIp,
'id': utools.getLocalId()
}
}, (res) => {
console.log(ip);
console.log('res:', res);
if (ip == localIp) return;
ips.push(ip);
_this.addFeature(ip, res.headers.id);
res.resume();
}).on('error', (err) => {});
})(ip);
}
}`;
try {
vm.runInContext(code, context);
console.log('vm:', context);
} catch (e) {
console.log(e);
}
return ips;
},
getPlatform: function () {
if (utools.isMacOs()) {
console.log('mac');
return runTime.platform = 'mac';
}
if (utools.isWindows()) {
console.log('win');
return runTime.platform = 'win';
}
if (utools.isLinux()) {
console.log('linux');
return runTime.platform = 'linux';
}
},
md5(str) {
return md5(str);
},
log(...logs) {
console.log(runTime.settings.log,...logs);
if (!runTime.settings.log) return;
logger.log(`[${new Date().toLocaleString()}]`, ...logs);
},
log1(str, obj) {
let path = utools.getPath('documents') + '/jflower.log';
fs.open(path, 'a', (err, fd) => {
if (err) {
console.log(err);
return false;
}
if (typeof obj == 'undefined') obj = '';
str = `[${new Date().toLocaleString()}]${str.toString()}${obj.toString()}\n`;
fs.write(fd, str, (err) => {
console.log(err);
});
});
},
checkFileExists(path ,name){
if(fs.existsSync(path+name)){
//如果文件存在则重命名文件
return this.checkFileExists(path ,'1_'+name);
}
return name;
}
}