-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
119 lines (102 loc) · 2.93 KB
/
index.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
const { app, BrowserWindow, ipcMain, Tray, Menu } = require('electron');
const AutoLaunch = require('auto-launch');
const path = require('path');
const axios = require('axios');
const { machineIdSync } = require('node-machine-id');
const fs = require('fs');
const { configureNetwork, revertNetwork } = require('./proxy');
const os = require('os');
let mainWindow;
let tray = null;
let isProxyRunning = false;
let pingInterval;
const SERVER_URL = 'https://codeshare.me/programs/network-control-panel';
const DEVICE_ID_FILE = path.join(os.homedir(), '.device_id');
let DEVICE_ID;
if (fs.existsSync(DEVICE_ID_FILE)) {
DEVICE_ID = fs.readFileSync(DEVICE_ID_FILE, 'utf8');
} else {
DEVICE_ID = machineIdSync();
fs.writeFileSync(DEVICE_ID_FILE, DEVICE_ID, 'utf8');
}
const autoLauncher = new AutoLaunch({
name: 'Network Control Panel By @umutxyp',
path: app.getPath('exe'),
});
autoLauncher.isEnabled().then((isEnabled) => {
if (!isEnabled) {
autoLauncher.enable();
}
}).catch((err) => console.error('AutoLaunch Error:', err));
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 400,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
icon: path.join(__dirname, 'icon.ico'),
title: "Network Control Panel By @umutxyp",
frame: false,
});
mainWindow.loadFile('index.html');
mainWindow.setMenu(null);
mainWindow.on('close', (event) => {
event.preventDefault();
mainWindow.hide();
});
}
function sendPing() {
axios.post(`${SERVER_URL}/ping`, {
deviceId: DEVICE_ID,
timestamp: new Date(),
})
}
function sendDisconnect() {
axios.post(`${SERVER_URL}/disconnect`, {
deviceId: DEVICE_ID,
timestamp: new Date(),
})
}
app.whenReady().then(() => {
createWindow();
tray = new Tray(path.join(__dirname, 'icon.ico'));
tray.setToolTip('Network Control @umutxyp');
tray.setContextMenu(Menu.buildFromTemplate([
{
label: 'Open',
click: () => mainWindow.show(),
},
{
label: 'Exit',
click: () => {
sendDisconnect();
revertNetwork('77.88.8.8', '77.88.8.1');
app.quit();
},
},
]));
pingInterval = setInterval(sendPing, 10000);
ipcMain.emit('toggle-proxy', 'start');
});
app.on('before-quit', () => {
clearInterval(pingInterval);
revertNetwork('77.88.8.8', '77.88.8.1');
sendDisconnect();
});
ipcMain.on('toggle-proxy', (event, action) => {
if (action === 'start') {
if (isProxyRunning) {
event.reply('proxy-status', 'already running');
} else {
configureNetwork('77.88.8.8', '77.88.8.1');
isProxyRunning = true;
event.reply('proxy-status', 'started');
}
} else if (action === 'stop') {
revertNetwork('77.88.8.8', '77.88.8.1');
isProxyRunning = false;
event.reply('proxy-status', 'stopped');
}
});