-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
234 lines (174 loc) · 5.15 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
// Modules to control application life and create native browser window
const {app, BrowserWindow, screen,ipcMain,Tray,Menu} = require('electron')
const path = require('path')
const log = require('electron-log');
const jetpack = require('fs-jetpack').cwd(app.getAppPath());
var network = require('network');
const child_process = require('child_process');
const dialog = require('electron').dialog;
var sudo = require('sudo-prompt');
const AutoLaunch = require('auto-launch');
var options = {
name: 'SPIE'
};
//GLOBAL VARS
var mainWindow
var profileManager
let profileArr = JSON.parse(jetpack.read(path.join(__dirname+'/resources/config/profiles.txt')))
log.info(profileArr)
function createMainWindow (width,height) {
mainWindow = new BrowserWindow({
width: 600,
height: 400,
show:false,
frame: false,
alwaysOnTop:true,
x: width - 600,
y: height- 440,
webPreferences:{
nodeIntegration:true,
enableRemoteModule:true
}
})
mainWindow.loadFile('index.html')
//mainWindow.openDevTools()
network.get_interfaces_list(function(err, obj) {
mainWindow.webContents.send('load-interfaces',obj)
mainWindow.webContents.send('load-profiles',profileArr)
})
let tray = null;
// tray documentation at - https://github.com/electron/electron/blob/main/docs/api/menu-item.md
const template = [
{
label: 'SPIE IP-Changer',
enabled: false,
},
{
type: 'separator',
},
{
label: 'Öffnen', click: function () {
mainWindow.show();
},
},
{
label: 'Profile verwalten', click: function () {
createProfileManager();
},
},
{
type: 'separator',
},
{
label: 'Über',click:function() {
createAbout();
},
},
{
type: 'separator',
},
{
label: 'Beenden', click: function () {
app.quit();
},
},
];
const contextMenu = Menu.buildFromTemplate(template);
tray = new Tray(path.join(__dirname,'/resources/img/icon.ico'));
tray.setContextMenu(contextMenu);
tray.setToolTip('SPIE IP-Changer');
ipcMain.on('minimize-tray', (event,args) => {
if (tray) { return mainWindow.hide(); }
})
}
function createProfileManager () {
profileManager = new BrowserWindow({
width: 1000,
height: 700,
show:false,
frame: false,
webPreferences:{
nodeIntegration:true,
enableRemoteModule:true
}
})
profileManager.loadFile('profiles.html')
//profileManager.openDevTools()
profileManager.on('ready-to-show',function(){
profileManager.webContents.send('load-profile',profileArr)
profileManager.show()
})
profileManager.on('closed', function(){
mainWindow.show()
})
}
var aboutWin
function createAbout () {
aboutWin = new BrowserWindow({
width: 600,
height: 300,
show:true,
frame:false,
webPreferences:{
nodeIntegration:true,
enableRemoteModule:true
}
})
aboutWin.loadFile('about.html')
//aboutWin.openDevTools()
}
ipcMain.on('manage-profiles', (event,args) => {
event.returnValue = 'received'
log.info('received')
mainWindow.hide()
createProfileManager()
})
ipcMain.on('update-profiles', (event,args) => {
profileArr = args
jetpack.write(__dirname+'/resources/config/profiles.txt',JSON.stringify(args));
log.info(jetpack.read(path.join(__dirname+'/resources/config/profiles.txt')))
profileManager.webContents.send('load-profile',profileArr)
mainWindow.webContents.send('load-profiles',profileArr)
})
ipcMain.on('update-ip-address',(event,args) => {
log.info("Applying Profile:"+args.name+". To Interface: "+args.interface)
if(args.name == "dhcp"){
sudo.exec('netsh interface ipv4 set address name="'+args.interface+'" dhcp && netsh interface ip set dns "'+args.interface+'" dhcp', options,
function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
setTimeout(() => {
network.get_interfaces_list(function(err, obj) {
mainWindow.webContents.send('load-interfaces',obj)
mainWindow.webContents.send('load-profiles',profileArr)
})
}, 2000);
});
} else {
sudo.exec('netsh interface ipv4 set address name="'+args.interface+'" static '+args.ip+' '+args.netmask+' '+args.gateway+' && netsh interface ip set dns "'+args.interface+'" static '+args.dns1+' && netsh interface ip add dns "'+args.interface+'" static '+args.dns2+'', options,
function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
setTimeout(() => {
network.get_interfaces_list(function(err, obj) {
mainWindow.webContents.send('load-interfaces',obj)
mainWindow.webContents.send('load-profiles',profileArr)
})
}, 2000);
});
}
})
app.whenReady().then(() => {
let autoLaunch = new AutoLaunch({
name: 'IP-Changer',
path: app.getPath('exe'),
});
autoLaunch.isEnabled().then((isEnabled) => {
if (!isEnabled) autoLaunch.enable();
});
let display = screen.getPrimaryDisplay();
let width = display.bounds.width;
let height = display.bounds.height;
createMainWindow(width,height)
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})