-
Notifications
You must be signed in to change notification settings - Fork 123
/
main.js
486 lines (363 loc) · 12.6 KB
/
main.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Modules to control application life and create native browser window
const electron = require('electron')
const path = require('path')
const duoxiancheng = require('./duoxiancheng_v1')
const queryContract = require("./dealContract")
const schedule = require('node-schedule');
const { dialog } = require('electron')
const punk = require('./btcord')
const ipcMain = electron.ipcMain
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const confighelper = require("./confighandle")
let mainWindow = null
const qianggou =new duoxiancheng.EthereumManager();
const configFilePath = "./taskconfig.json"
const configData = confighelper.readConfigFile(configFilePath);
const tasklist =[] //用来管理任务队列,新增任务,删除任务的是,要从这里面增加删除。
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1900,
height: 1000,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
contextIsolation: false
}
})
// and load the index.html of the app.
mainWindow.loadFile('./web/index.html')
duoxiancheng.setmainWindow(mainWindow);
punk.setmainWindow(mainWindow);
//const mainMenu = Menu.buildFromTemplate(menuTemplate);
//Menu.setApplicationMenu(mainMenu);
// Open the DevTools.
//mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
//app.commandLine.appendSwitch('proxy-server', 'socks5://127.0.0.1:10808');
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
var bInitTask = false;
async function alltask()
{
if(bInitTask)
{
return
}
bInitTask = true;
//启动 task
for(var i=0;i<configData.length;i++)
{
var data = configData[i];
var renwuj =await taskcreate(data);
}
}
function gettime(exectime) {
var result = [true];
let schedule = null;
var nowexectime = Number(exectime)
switch (nowexectime) {
case 60000: // 1 minute
schedule = '*/1 * * * *';
break;
case 3600000: // 1 hour
schedule = '0 * * * *';
break;
case 86400000: // 1 day
schedule = '0 0 * * *';
break;
case 604800000: // 1 week
schedule = '0 0 * * 0';
break;
case 2628000000: // 1 month (approx.)
schedule = '0 0 1 * *';
break;
case 31536000000: // 1 year (approx.)
schedule = '0 0 1 1 *';
break;
// 在这里添加更多的case,根据您的需求
default:
console.log('Invalid exectime value');
;
}
if (schedule != null) {
result.push(schedule)
}
else {
result[0] = false;
}
return result;
}
const taskcreate = async (data) => {
timeresult = gettime(data.exectime)
if(timeresult[0])
{
const j = schedule.scheduleJob(timeresult[1], async () => {
//脚本地址
data = confighelper.getElementById(configData,data.id);
if(data == null || Number(data.rematimes)<1)
{
return;
}
filename = data.scriptpath
try {
const dynamicModule = require(filename);
if (typeof dynamicModule.start === 'function') {
await dynamicModule.start(data);
} else {
console.error(`Error: The file ${filename} does not have a start function.`);
}
data.rematimes = Number(data.rematimes)-1;
data.lastExecTime = formatDate( new Date());
confighelper.updateOrAddTask(configData,data);
confighelper.writeConfigFile(configFilePath, configData);
//给页面发送通知
mainWindow.webContents.send("info:taskInit", { configData});
clearModuleCache(filename);
} catch (error) {
clearModuleCache(filename);
console.error(`Error loading and executing file: ${filename}`, error);
}
})
tasklist.push({"id":data.id,"renwuj":j})
return j;
}
}
async function addTask(taskData) {
// Update configData and save it to the file
// Create a new task and add it to the tasklist
const newTask = await taskcreate(taskData);
}
const eventListener = async () => {
ipcMain.on('info:initweb3', async (e, value) => {
console.log("info:initweb3");
console.log(value)
result = qianggou.initWeb3(value)
mainWindow.webContents.send("info:initweb3", { result });
})
ipcMain.on('info:qianggou', async (e, value) => {
console.log("info:qianggou");
console.log(value)
result = qianggou.qianggou(value)
mainWindow.webContents.send("info:qianggou", { result });
})
ipcMain.on('info:test', async (e, value) => {
console.log("info:test");
console.log(value)
result = qianggou.test(value)
mainWindow.webContents.send("info:test", { result });
})
ipcMain.on('info:queryabi', async (e, value) => {
console.log("info:queryabi");
console.log(value)
result = await queryContract.start(value)
mainWindow.webContents.send("info:queryabi", { result });
})
ipcMain.on('info:abishiyong', async (e, value) => {
console.log("info:abishiyong");
console.log(value)
result = qianggou.abishiyong(value)
//result = await queryContract.abishiyong(value)
//mainWindow.webContents.send("info:abishiyong", { result });
})
ipcMain.on('info:accounts', async (e, value) => {
console.log("info:accounts");
console.log(value)
result = qianggou.accounts();
console.log(result);
mainWindow.webContents.send("info:accounts", { result });
})
ipcMain.on('info:creatpriatenumber', async (e, value) => {
console.log("info:creatpriatenumber");
console.log(value)
result = qianggou.creatpriatenumber(value);
mainWindow.webContents.send("info:creatpriatenumber", { result });
})
ipcMain.on('info:importprikey', async (e, value) => {
console.log("info:importprikey");
filename = await dialog.showOpenDialog({ properties: ['openFile'] });
if (filename.canceled == false) {
qianggou.importprikey(filename.filePaths);
}
//result =
//mainWindow.webContents.send("info:importprikey", { });
})
ipcMain.on('info:exportprikey', async (e, value) => {
console.log("info:exportprikey");
filename = await dialog.showOpenDialog({ properties: ['openDirectory'] });
if (filename.canceled == false) {
qianggou.exportprikey(filename.filePaths);
}
//result =
//mainWindow.webContents.send("info:importprikey", { });
})
ipcMain.on('info:pilianglingshui', async (e, value) => {
console.log("info:pilianglingshui");
filename = await dialog.showOpenDialog({ properties: ['openFile'] });
if (filename.canceled == false) {
try {
const dynamicModule = require(filename.filePaths[0]);
if (typeof dynamicModule.start === 'function') {
dynamicModule.start(value);
} else {
console.error(`Error: The file ${filename} does not have a start function.`);
}
clearModuleCache(filename.filePaths[0]);
} catch (error) {
clearModuleCache(filename.filePaths[0]);
console.error(`Error loading and executing file: ${filename}`, error);
}
}
})
ipcMain.on('info:selectpath', async (e, value) => {
console.log("info:selectpath");
filename = await dialog.showOpenDialog({ properties: ['openFile'] });
if (filename.canceled == false) {
value.filepath = filename.filePaths[0]
mainWindow.webContents.send("info:selectpath", value);
}
else
{
}
})
ipcMain.on('info:getmaxgasprice', async (e, value) => {
console.log("info:getmaxgasprice");
qianggou.CalcGasPrice();
})
ipcMain.on('info:boxcheck', async (e, value) => {
console.log("info:boxcheck");
qianggou.boxcheck(value);
})
ipcMain.on('info:loadlocalABI', async (e, value) => {
console.log("info:loadlocalABI");
filename = await dialog.showOpenDialog({ properties: ['openFile'] });
if (filename.canceled == false) {
result = await queryContract.loadlocalABI(filename.filePaths[0])
mainWindow.webContents.send("info:queryabi", { result });
}
})
var renwu = null;
//启动定时监控,关闭定时监控
ipcMain.on('info:startgetmaxgasprice', async (e, value) => {
console.log("info:startgetmaxgasprice");
qianggou.CalcGasPrice();
//开启定时任务
if (renwu == null) {
renwu = task1()
}
else {
}
})
ipcMain.on('info:mytest', async (e, value) => {
console.log("info:mytest");
qianggou.mytest();
})
const task1 = async ()=>{
//每分钟的1-10秒都会触发,其它通配符依次类推
//console.log("123111");
//console.log(value);
//console.log(count);
const j = schedule.scheduleJob('*/5 * * * * *', async ()=>{
await qianggou.CalcGasPrice();
})
return j;
}
ipcMain.on('info:getbtccookiepath', async (e, value) => {
console.log("info:getbtccookiepath");
filename = await dialog.showOpenDialog({ properties: ['openFile'] });
if (filename.canceled == false) {
mainWindow.webContents.send("info:getbtccookiepath", { "filename":filename.filePaths });
}
})
ipcMain.on('info:taskInit', async (e, value) => {
console.log("info:taskInit");
alltask()
mainWindow.webContents.send("info:taskInit", { configData});
})
ipcMain.on('info:taskedit', async (e, value) => {
console.log("info:taskedit");
var result = confighelper.updateOrAddTask(configData, value.editdata);
confighelper.writeConfigFile(configFilePath, configData);
if(result)
{
//说明新增了任务
await taskcreate(value.editdata);
}
else
{
//说明修改了任务,需要删除旧任务,然后增加新任务
const taskIndex = tasklist.findIndex((task) => task.id === value.editdata.id);
if (taskIndex !== -1) {
tasklist[taskIndex].renwuj.cancel();
tasklist.splice(taskIndex, 1);
}
await taskcreate(value.editdata);
}
//mainWindow.webContents.send("info:taskInit", { configData});
})
ipcMain.on('info:taskdelete', async (e, value) => {
console.log("info:taskdelete");
confighelper.deleteTaskById(configData, value.id);
confighelper.writeConfigFile(configFilePath, configData);
//任务队列需要取消之前的任务
const taskIndex = tasklist.findIndex((task) => task.id === value.id);
if (taskIndex !== -1) {
tasklist[taskIndex].renwuj.cancel();
tasklist.splice(taskIndex, 1);
}
//mainWindow.webContents.send("info:taskInit", { configData});
})
ipcMain.on('info:btcsetting', async (e, value) => {
console.log("info:btcsetting");
punk.setInit(value)
})
ipcMain.on('info:punkimgdownload', async (e, value) => {
console.log("info:punkimgdownload");
punk.punkimgdownload()
})
ipcMain.on('info:punkselectblockinfo', async (e, value) => {
console.log("info:punkselectblockinfo");
punk.punkselectblockinfo()
})
ipcMain.on('info:punkidselect', async (e, value) => {
console.log("info:punkidselect");
var info = punk.punkidselect(value.punkid)
mainWindow.webContents.send("info:getbtccookiepath", { info });
})
}
function clearModuleCache(modulePath) {
try {
const resolvedPath = require.resolve(modulePath);
delete require.cache[resolvedPath];
} catch (error) {
console.error(`Error clearing module cache for: ${modulePath}`, error);
}
}
eventListener();
function formatDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
}