-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
177 lines (153 loc) · 4.98 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
const { app, BrowserWindow, Menu, ipcMain, shell } = require('electron')
const path = require('path')
const isDev = false
//const isMac = process.platform === 'darwin'
const dimensions = [370, 370] // width, height
const childProcessSpawn = require('child_process').spawn
var currentProgress = -1
var childProcess
var mainWindow
// Create main window
function createMainWindow() {
mainWindow = new BrowserWindow({
title: 'Social Surge',
icon: path.join(__dirname, './icon.png'),
width: isDev ? dimensions[0] + 500 : dimensions[0], // extend window for dev console
height: dimensions[1],
frame: false,
resizable: false,
webPreferences: {
contextIsolation: true,
nodeIntegration: false,
preload: path.join(__dirname, './preload.js'),
},
})
// open dev tools if not in production environment
if (isDev) {
mainWindow.webContents.openDevTools()
}
mainWindow.loadFile(path.join(__dirname, './renderer/index.html'))
}
// Menu template
const menu = [
{
role: 'fileMenu',
},
]
// App is ready
app.whenReady().then(() => {
createMainWindow()
// Implement Menu
const mainMenu = Menu.buildFromTemplate(menu)
Menu.setApplicationMenu(mainMenu)
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createMainWindow()
}
})
})
// exit
ipcMain.on('exit', () => {
app.quit()
})
// minimize
ipcMain.on('minimize-window', () => {
mainWindow.minimize()
})
// quit if all windows are closed
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
// opens chromium link in user's default browser
ipcMain.on('open-chromium-link', () => {
shell.openExternal(
'https://www.chromium.org/getting-involved/download-chromium/'
)
})
// cancel run button was clicked
ipcMain.on('run-complete', async (event) => {
cleanupRun()
})
// sets icon progress bar value and saves to global variable
function saveAndSetProgress(value) {
currentProgress = value
mainWindow.setProgressBar(value)
}
function cleanupRun() {
// reset progress bar
saveAndSetProgress(-1)
// go to next page
mainWindow.webContents.send('run-complete')
// terminate child puppeteer process
childProcess.kill()
// clean up environment variables
process.env.SEARCHSTRING = null
process.env.VIEWCOUNT = null
process.env.MINVIEWS = null
process.env.MAXVIEWS = null
process.env.WORKERCOUNT = null
process.env.CHROMIUMPATH = null
process.env.PROXIES = null
process.env.SUCCESSES = 0
}
// start run and setup stdout listeners for child_process thread
ipcMain.on('run-start', async (event, runInfo) => {
console.log(`recieved: ${runInfo.chromiumPath}`)
process.env.SEARCHSTRING = String(runInfo.searchString)
process.env.VIEWCOUNT = Number(runInfo.viewCount)
process.env.MINVIEWS = Number(runInfo.minViewS)
process.env.MAXVIEWS = Number(runInfo.maxViewS)
process.env.WORKERCOUNT = Number(runInfo.workerCount)
process.env.CHROMIUMPATH = String(runInfo.chromiumPath)
console.log(`setting: ${process.env.CHROMIUMPATH}`)
process.env.PROXIES = runInfo.proxies
process.env.SUCCESSES = 0
currentProgress = 0
childProcess = childProcessSpawn('node', [
path.join(__dirname, 'childThread.js'),
])
const onData = async (data) => {
childOutput = data.toString()
sendStdoutMessage('[Child] ' + childOutput)
// Check if we have hit our desired number of views
if (childOutput.includes('all views completed')) {
cleanupRun()
childProcess.stdout.removeListener('data', onData) // Remove the event listener
return
}
// parse responses from child process
dataArray = childOutput.split(' ')
if (dataArray[0].trim() === 'started') {
mainWindow.webContents.send(
'individual-view-start',
dataArray[1]
)
return
} else if (dataArray[0].trim() === 'completed') {
ipAddress = dataArray[1].trim()
viewResult = dataArray[2].trim() === 'false' ? false : true
viewTimeMs = Number(dataArray[3].trim())
}
mainWindow.webContents.send(
'individual-result',
ipAddress,
viewResult,
viewTimeMs
)
// Update icon progress bar
saveAndSetProgress(
currentProgress + viewResult / Number(process.env.VIEWCOUNT)
)
}
childProcess.stdout.on('data', onData)
// Listen for data on stderr (errors)
childProcess.stderr.on('data', (data) => {
sendStdoutMessage('[Child Error] ' + data.toString())
})
})
function sendStdoutMessage(message) {
console.log(message) // Log the message to the main process console
mainWindow.webContents.send('stdout-message', message) // Send the message to the renderer process
}