forked from mmick66/electrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
259 lines (231 loc) · 6.38 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
import { SocksClient } from 'socks'
import electron, { ipcMain, session } from 'electron'
import path from 'path'
import url from 'url'
import 'babel-polyfill'
import fs from 'fs'
// const axios = require('axios');
const { autoUpdater, AppUpdater } = require('electron-updater')
// const { Storage } = require('./engine.js')
// //Basic flags
autoUpdater.autoDownload = false
autoUpdater.autoInstallOnAppQuit = true
const app = electron.app
const BrowserWindow = electron.BrowserWindow
// require('update-electron-app')()
let mainWindow
class Storage {
fileName
constructor(fileN) {
this.fileName = fileN
if (!fs.existsSync(fileN)) {
fs.writeFileSync(fileN, '')
}
}
async get() {
let pth = this.fileName
if (fs.existsSync(pth)) {
let rawdata = fs.readFileSync(pth)
if (rawdata != '') {
let data = JSON.parse(rawdata)
return data
} else return null
} else {
return null
}
}
// async getA() {
// let pth = this.fileName
// if (fs.existsSync(pth)) {
// let rawdata = fs.readFileSync(pth)
// if (rawdata != '') {
// let data = Object.entries(JSON.parse(rawdata))
// return data
// } else return null
// } else {
// return null
// }
// }
async set(value) {
let pth = this.fileName
let data = []
if (fs.existsSync(pth)) {
let rawdata = fs.readFileSync(pth)
if (rawdata != '') data = JSON.parse(rawdata)
}
if (data.length >= 200) {
data.splice(0, value.length)
}
data = data.concat(value)
fs.writeFileSync(pth, JSON.stringify(data))
return true
}
}
const storage = new Storage('proxy.json')
const createWindow = () => {
mainWindow = new BrowserWindow({
icon: path.join(__dirname, 'icons/icon.ico'),
width: 414,
height: 736,
webPreferences: {
nodeIntegration: true,
preload: path.join(__dirname, 'preload.js'),
},
})
// session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => {
// details.requestHeaders['User-Agent'] =
// '5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1788.0'
// callback({ cancel: false, requestHeaders: details.requestHeaders })
// })
autoUpdater.checkForUpdates()
mainWindow.setHasShadow(true)
// mainWindow.setAlwaysOnTop(true)
mainWindow.setMenuBarVisibility(false)
// mainWindow.webContents.openDevTools()
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true,
})
)
// mainWindow.loadURL('http://localhost:3000')
mainWindow.on('closed', () => {
mainWindow = null
})
/*New Update Available*/
autoUpdater.on('update-available', async (info) => {
showMessage(`Update available. Current version ${app.getVersion()}`)
let pth = await autoUpdater.downloadUpdate()
showMessage(pth)
})
autoUpdater.on('update-not-available', info => {
showMessage(`No update available. Current version ${app.getVersion()}`)
})
/*Download Completion Message*/
autoUpdater.on('update-downloaded', info => {
showMessage(`Update downloaded. Current version ${app.getVersion()}`)
})
function showMessage(message) {
mainWindow.webContents.send('updateMessage', message)
}
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})
async function connect(options) {
const { proxy } = options
try {
const info = await SocksClient.createConnection(options)
console.log('did connect')
return { sucess: true, proxy }
// TODO://
// info.socket.write(
// 'GET /json HTTP/1.1\nHost: ip-api.com/?fields=status,message,country,isp,mobile,proxy,hosting'
// )
// return new Promise((resolve, reject) => {
// info.socket.on('data', data => {
// resolve(data.toString())
// })
// info.socket.on('error', err => {
// reject(err)
// })
// })
} catch (err) {
console.log('did not connect')
return { sucess: false, proxy }
}
}
async function SocksEngine({ host, port }) {
const options = {
proxy: {
host, // ipv4 or ipv6 or hostname
port: Number(port),
type: 5, // Proxy version (4 or 5)
},
command: 'connect', // SOCKS command (createConnection factory function only supports the connect command)
destination: {
host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5.
port: 80,
},
// Optional fields
timeout: 30000,
}
try {
const data = await connect(options)
return data
} catch (err) {
console.log('did not connect')
return { sucess: false, proxy: options.proxy }
// Handle errors
}
}
// ipcMain.on('proxys', async (event, arg) => {
// try {
// const proxys = JSON.parse(arg)
// const result = await Promise.(
// proxys.map(value => SocksEngine(value))
// )
// event.reply('result', result)
// return true
// } catch (error) {
// console.log(error)
// event.reply('error', 'server error, Testing operation not completed')
// }
// })
ipcMain.on('proxys', async (event, arg) => {
try {
const proxys = JSON.parse(arg)
const promises = proxys.map(value => SocksEngine(value))
const batchNumber = 5
for (let i = 0; i < promises.length; i += batchNumber) {
const batch = promises.slice(i, i + batchNumber)
Promise.allSettled(batch).then(value => {
event.reply('result', value)
})
}
await Promise.allSettled(promises)
event.reply('result', true)
return true
} catch (error) {
console.log(error)
event.reply('error', 'server error, Testing operation not completed')
}
})
ipcMain.handle('save', async (event, arg) => {
try {
const proxys = JSON.parse(arg)
// const now = new Date()
// // // Format the date as YYYY-MM-DD
// // const year = now.getFullYear()
// // const month = String(now.getMonth() + 1).padStart(2, '0')
// // const day = String(now.getDate()).padStart(2, '0')
// // const seconds = String(now.getSeconds()).padStart(2, 0)
// const formattedDate = now.toISOString() //`${year}-${month}-${day}-${seconds}`
// log(formattedDate)
await storage.set(proxys)
return true
} catch (error) {
console.log(error)
// event.reply('error', 'server error, Saving operation not completed')
return false
}
})
ipcMain.handle('retrive', async event => {
try {
const result = await storage.get()
return result
} catch (error) {
console.log(error)
// event.reply('error', 'server error, Saving operation not completed')
return false
}
})