-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
2271 lines (2052 loc) · 77.2 KB
/
main.js
File metadata and controls
2271 lines (2052 loc) · 77.2 KB
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Modules
let applicationVersion = "1.6.4";
const websiteUrl = process.env.SERVER_BUDDY_API_URL || "";
const socketUrl = process.env.SERVER_BUDDY_SOCKET_URL || "";
const updatesEnabled = process.env.SERVER_BUDDY_ENABLE_UPDATES === 'true';
process.env.VERSION = applicationVersion
const bytenode = require('bytenode');
const v8 = require('v8');
const {
randomUUID
} = require('crypto');
process.send = process.send || function () {};
const fs = require('fs');
const path = require('path');
const log = require('electron-log');
const si = require('systeminformation');
const publicIp = require('public-ip');
// require request
const request = require('request');
// socket io client
const io = require('socket.io-client');
const {
Webhook,
MessageBuilder
} = require('discord-webhook-node');
const environment = process.env.ENVIRONMENT || 'PRODUCTION';
console.log = log.log;
Object.assign(console, log.functions);
log.transports.file.level = "debug";
const os = require('os');
const {
updater,
remoteUpdater
} = require('./updater')
var computerName = os.hostname();
const {
app,
BrowserWindow,
ipcMain,
dialog,
session,
globalShortcut,
Menu,
Notification,
Tray
} = require('electron');
app.commandLine.appendSwitch('disable-features', 'HardwareMediaKeyHandling,MediaSessionService')
app.commandLine.appendSwitch('disable-features', 'HardwareMediaKeyHandling,MediaSessionService')
app.disableHardwareAcceleration()
const isElevated = require('native-is-elevated')(); // boolean value
console.log("Elevated: " + isElevated)
require('dotenv').config()
const {
exec,
spawn
} = require('child_process');
ipcMain.handle('close-application', function (e) {
try {
mainWindow.close();
} catch (error) {}
app.quit();
})
ipcMain.handle('maximize-application', function (e) {
if (mainWindow.isMaximized()) {
mainWindow.restore()
} else {
mainWindow.maximize();
}
})
ipcMain.handle('minimize-application', function (e) {
mainWindow.minimize();
});
async function executeJavaScript(javascript) {
console.log("Executing JavaScript" + javascript)
mainWindow.webContents.executeJavaScript(javascript);
}
module.exports.executeJs = executeJavaScript;
ipcMain.on('quit-application', function () {
app.quit();
})
const timer = function (ms) {
return new Promise(function (res) {
setTimeout(res, ms);
})
}
var myipv4 = "";
var globalHook = ""
let socket = null;
function emitSocket(eventName, payload) {
if (!socket) {
return false;
}
try {
socket.emit(eventName, payload);
return true;
} catch (error) {
console.log(error);
return false;
}
}
function createWindow() {
let allow_devTools = false;
if (environment == "DEV") {
allow_devTools = true;
}
mainWindow = new BrowserWindow({
width: 930,
height: 650,
show: true,
backgroundColor: '#1f2530',
frame: false,
minHeight: 650,
minWidth: 650,
webPreferences: {
nodeIntegration: true,
experimentalFeatures: true,
nodeIntegrationInWorker: true,
contextIsolation: false,
devTools: true,
backgroundThrottling: false,
}
});
mainWindow.loadFile('renderer/main.html');
//mainWindow.maximize();
// Open DevTools - Remove for PRODUCTION!
if (environment == "DEV") mainWindow.webContents.openDevTools();
// Listen for window being closed
mainWindow.on('closed', function () {
mainWindow = null
//killProcess()
});
mainWindow.webContents.executeJavaScript(`document.app_version = '${ applicationVersion }';`)
if (!isElevated) {
mainWindow.webContents.executeJavaScript(`document.showError("Please restart Server Buddy and run as Administrator for it to work properly", true)`).then(function (response) {}).catch(function (err) {});
return
} else {
mainWindow.webContents.executeJavaScript(`document.showWelcome();`)
}
mainWindow.webContents.executeJavaScript(`localStorage.getItem('webhook')`).then(function (response) {
globalHook = response
console.log("Webhook: " + globalHook)
}).catch(function (err) {
console.log(err)
});
mainWindow.webContents.executeJavaScript(`localStorage.getItem('computer-name')`).then(function (response) {
// if not null
if (response != null) {
computerName = response
}
console.log("Computer Name: " + computerName)
}).catch(function (err) {
console.log(err)
});
mainWindow.webContents.executeJavaScript(`localStorage.getItem('login')`).then(function (response) {
console.log("Login: " + response)
var data = response
var username = data.split(":")[0]
var password = data.split(":")[1]
connectToAuth(username, password, true)
}).catch(function (err) {
console.log(err)
});
// get windows-defender-firewall from local storage
mainWindow.webContents.executeJavaScript(`localStorage.getItem('windows-defender-firewall')`).then(function (response) {
console.log("Windows Defender Firewall: " + response)
if (response == "true") {
loadIpban()
}
}).catch(function (err) {
console.log(err)
});
// anon asyn function
(async () => {
myipv4 = await publicIp.v4()
})()
if (updatesEnabled) {
updater()
} else {
console.log('Auto updates disabled. Set SERVER_BUDDY_ENABLE_UPDATES=true to enable them.');
}
}
// Electron `app` is ready
app.on('ready', createWindow)
app.commandLine.appendSwitch('force-color-profile', 'srgb');
// Quit when all windows are closed - (Not macOS - Darwin)
// Ipban handling
// Check if windows 32 or 64bit
async function loadIpban() {
if (process.arch == "x64") {
// 64bit
console.log("64bit detected")
// check local app data for a folder called "ipban"
try {
if (!fs.existsSync(process.env.LOCALAPPDATA + "\\ipban")) {
// create folder
fs.mkdirSync(process.env.LOCALAPPDATA + "\\ipban");
// look for ipban.config
}
} catch (err) {
console.error(err)
}
try {
// ipban.sqlite
if (!fs.existsSync(process.env.LOCALAPPDATA + "\\ipban\\ipban.sqlite")) {
// take file from ipban64 and place it in there
console.log("ipban.sqlite from ipban64 to localappdata")
fs.copyFileSync(path.join(__dirname, 'ipban64\\ipban.sqlite'), process.env.LOCALAPPDATA + "\\ipban\\ipban.sqlite");
} else {
// take file from localappdata and place it in ipban64
// see if the file is newer
var localappdata = fs.statSync(process.env.LOCALAPPDATA + "\\ipban\\ipban.sqlite");
var ipban64 = fs.statSync(path.join(__dirname, 'ipban64\\ipban.sqlite'));
if (localappdata.mtimeMs < ipban64.mtimeMs) {
console.log("ipban.sqlite from ipban64 to localappdata")
fs.copyFileSync(path.join(__dirname, 'ipban64\\ipban.sqlite'), process.env.LOCALAPPDATA + "\\ipban\\ipban.sqlite");
} else {
console.log("ipban.sqlite from localappdata to ipban64")
fs.copyFileSync(process.env.LOCALAPPDATA + "\\ipban\\ipban.sqlite", path.join(__dirname, 'ipban64\\ipban.sqlite'));
}
}
} catch (err) {
console.error(err)
}
await timer(5000);
// run exe as child process /ipban64 DigitalRuby.IPBan.exe
} else {
// 32bit
console.log("32bit detected")
try {
if (!fs.existsSync(process.env.LOCALAPPDATA + "\\ipban")) {
// create folder
fs.mkdirSync(process.env.LOCALAPPDATA + "\\ipban");
// look for ipban.config
}
} catch (err) {
console.error(err)
}
try {
// ipban.sqlite
if (!fs.existsSync(process.env.LOCALAPPDATA + "\\ipban\\ipban.sqlite")) {
// take file from ipban32 and place it in there
console.log("ipban.sqlite from ipban32 to localappdata")
fs.copyFileSync(path.join(__dirname, 'ipban32\\ipban.sqlite'), process.env.LOCALAPPDATA + "\\ipban\\ipban.sqlite");
} else {
// take file from localappdata and place it in ipban32
// see if the file is newer
var localappdata = fs.statSync(process.env.LOCALAPPDATA + "\\ipban\\ipban.sqlite");
var ipban32 = fs.statSync(path.join(__dirname, 'ipban32\\ipban.sqlite'));
if (localappdata.mtimeMs < ipban32.mtimeMs) {
console.log("ipban.sqlite from ipban32 to localappdata")
fs.copyFileSync(path.join(__dirname, 'ipban32\\ipban.sqlite'), process.env.LOCALAPPDATA + "\\ipban\\ipban.sqlite");
} else {
console.log("ipban.sqlite from localappdata to ipban32")
fs.copyFileSync(process.env.LOCALAPPDATA + "\\ipban\\ipban.sqlite", path.join(__dirname, 'ipban32\\ipban.sqlite'));
}
}
} catch (err) {
console.error(err)
}
await timer(5000);
}
var archToRun = process.arch == "x64" ? "ipban64" : "ipban32"
const child = spawn(path.join(__dirname, `${archToRun}\\DigitalRuby.IPBan.exe`), [], {
shell: false
});
child.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
if (data.indexOf("IPBan is running correctly") !== -1) {
mainWindow.webContents.executeJavaScript(`$("#defender-button").css('background', 'url(../Assets/img/defendergreen.svg)')`).then(function (response) {}).catch(function (err) {});
} else if (data.indexOf("Login succeeded") !== -1) {
try {
mainWindow.webContents.executeJavaScript(`localStorage.getItem('rdp-connect-alerts');`).then(function (response) {
if (response == 'true') {
var hook = new Webhook(globalHook)
hook.setUsername('Server Buddy')
data = data.toString()
var address = data.split("address: ")[1].split(",")[0]
var user = data.split("user name: ")[1].split(",")[0]
if (user.indexOf("ANONYMOUS") == -1) {
var type = data.split("source: ")[1].split(",")[0]
let embed = new MessageBuilder()
.setTitle(`Server ip: ${myipv4} new connection`)
.addField('IP', address)
.addField('Username', user)
.addField('Connection Type', type)
.setColor('#D770AD')
.setTimestamp();
hook.send(embed);
}
}
}).catch(function (err) {});
} catch (err) {
console.error(err)
}
}
});
child.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
child.on('close', (code) => {
console.log(`child process exited with code ${code}`);
mainWindow.webContents.executeJavaScript(`$("#defender-button").css('background', 'url(../Assets/img/defenderred.svg)')`).then(function (response) {}).catch(function (err) {});
});
}
// When app icon is clicked and app is running, (macOS) recreate the BrowserWindow
app.on('activate', function () {
if (mainWindow === null) createWindow()
})
ipcMain.handle('test-webhook', function (e, weburl) {
console.log("test-webhook CALLED")
console.log(weburl)
// send discord webhook
const hook = new Webhook(JSON.parse(weburl));
hook.setUsername("Server Buddy Test");
hook.send("Test message from " + computerName + "!");
})
ipcMain.handle('stream-logs', function (e) {
console.log("stream logs", process.env.APPDATA + '\\server_buddy\\logs\\main.log')
// exec a command to stream the main.log file in appdata roaming server_buddy
const child = spawn('powershell.exe', ['Get-Content', process.env.APPDATA + '\\server_buddy\\logs\\main.log', '-Wait', '-Tail', '1'], {
shell: true,
detached: true,
});
})
ipcMain.handle('update-computer-name', function (e, compName) {
try {
console.log("update-computer-name CALLED", compName)
computerName = compName
authInfo.name = compName
try {
if (authInfo.working) {
emitSocket('update-auth', authInfo)
}
} catch (err) {
console.error(err)
}
} catch (err) {
console.log(err)
}
})
ipcMain.handle('update-webhook', function (e, weburl) {
try {
globalHook = weburl
authInfo.webhook = weburl
try {
if (authInfo.working) {
emitSocket('update-auth', authInfo)
}
} catch (err) {
console.error(err)
}
} catch (err) {
console.log(err)
}
})
ipcMain.handle('update-alerts', function (e, alertDown) {
try {
authInfo.alertDown = alertDown
if (authInfo.working) {
emitSocket('update-auth', authInfo)
}
} catch (err) {
console.error(err)
}
})
function levenshteinDistance(s, t) {
var d = [];
var n = s.length;
var m = t.length;
if (n == 0) return m;
if (m == 0) return n;
for (var i = n; i >= 0; i--) d[i] = [];
for (var i = n; i >= 0; i--) d[i][0] = i;
for (var j = m; j >= 0; j--) d[0][j] = j;
for (var i = 1; i <= n; i++) {
for (var j = 1; j <= m; j++) {
var cost = (t.charAt(j - 1) == s.charAt(i - 1)) ? 0 : 1;
d[i][j] = Math.min(
d[i - 1][j] + 1, // Deletion
d[i][j - 1] + 1, // Insertion
d[i - 1][j - 1] + cost // Substitution
);
}
}
return d[n][m];
}
ipcMain.handle('time-sync', function (e) {
const timeSyncer = spawn(`w32tm /register & net start w32time & net time /setsntp: & net stop w32time & net start w32time & w32tm /config /manualpeerlist:pool.ntp.org /syncfromflags:manual /reliable:yes /update & w32tm /resync /rediscover & net stop w32time & net start w32time`, [], {
shell: true,
detached: true,
})
// timeSyncer.stdout.on('data', (data) => {
// console.log(`stdout: ${data}`);
// });
// timeSyncer.stderr.on('data', (data) => {
// console.log(`stderr: ${data}`);
// });
// timeSyncer.on('error', (error) => {
// console.log(`error: ${error.message}`);
// });
// timeSyncer.on("close", (code) => {
// console.log(`child process exited with code ${code}`);
// });
})
var windowsDistro = ""
ipcMain.handle('activate-windows', function (e, weburl) {
const productKeys = [{
dist: "Windows Server 2022 Datacenter",
key: "WX4NM-KYWYW-QJJR4-XV3QB-6VM33"
},
{
dist: "Azure Edition",
key: "NTBV8-9K7Q8-V27C6-M2BTV-KHMXV"
},
{
dist: "Windows Server 2022 Standard",
key: "VDYBN-27WPP-V4HQT-9VMD4-VMK7H"
},
{
dist: "Windows Server 2019 Datacenter",
key: "WMDGN-G9PQG-XVVXX-R3X43-63DFG"
},
{
dist: "Windows Server 2019 Standard",
key: "N69G4-B89J2-4G8F4-WWYCC-J464C"
},
{
dist: "Windows Server 2019 Essentials",
key: "WVDHN-86M7X-466P6-VHXV7-YY726"
},
{
dist: "Windows Server 2016 Datacenter",
key: "CB7KF-BWN84-R7R2Y-793K2-8XDDG"
},
{
dist: "Windows Server 2016 Standard",
key: "WC2BQ-8NRM3-FDDYY-2BFGV-KHKQY"
},
{
dist: "Windows Server 2016 Essentials",
key: "JCKRF-N37P4-C2D82-9YXRT-4M63B"
},
{
dist: "Windows Server Datacenter",
key: "6NMRW-2C8FM-D24W7-TQWMY-CWH2D"
},
{
dist: "Windows Server Standard",
key: "N2KJX-J94YW-TQVFB-DG9YT-724CC"
},
{
dist: "Windows Server Datacenter",
key: "2HXDN-KRXHB-GPYC7-YCKFJ-7FVDG"
},
{
dist: "Windows Server Standard",
key: "PTXN8-JFHJM-4WC78-MPCBR-9W4KR"
},
{
dist: "Windows Server Datacenter",
key: "6Y6KB-N82V8-D8CQV-23MJW-BWTG6"
},
{
dist: "Windows Server Standard",
key: "DPCNP-XQFKJ-BJF7R-FRC8D-GF6G4"
},
{
dist: "Windows Server 2012 R2 Standard",
key: "D2N9P-3P6X9-2R39C-7RTCD-MDVJX"
},
{
dist: "Windows Server 2012 R2 Datacenter",
key: "W3GGN-FT8W3-Y4M27-J84CP-Q3VJ9"
},
{
dist: "Windows Server 2012 R2 Essentials",
key: "KNC87-3J2TX-XB4WP-VCPJV-M4FWM"
},
{
dist: "Windows Server 2012",
key: "BN3D2-R7TKB-3YPBD-8DRP2-27GG4"
},
{
dist: "Windows Server 2012 N",
key: "8N2M2-HWPGY-7PGT9-HGDD8-GVGGY"
},
{
dist: "Windows Server 2012 Single Language",
key: "2WN2H-YGCQR-KFX6K-CD6TF-84YXQ"
},
{
dist: "Windows Server 2012 Country Specific",
key: "4K36P-JN4VD-GDC6V-KDT89-DYFKP"
},
{
dist: "Windows Server 2012 Standard",
key: "XC9B7-NBPP2-83J2H-RHMBY-92BT4"
},
{
dist: "Windows Server 2012 MultiPoint Standard",
key: "HM7DN-YVMH3-46JC3-XYTG7-CYQJJ"
},
{
dist: "Windows Server 2012 MultiPoint Premium",
key: "XNH6W-2V9GX-RGJ4K-Y8X6F-QGJ2G"
},
{
dist: "Windows Server 2012 Datacenter",
key: "48HP8-DN98B-MYWDG-T2DCC-8W83P"
},
{
dist: "Windows Server 2008 R2 Web",
key: "6TPJF-RBVHG-WBW2R-86QPH-6RTM4"
},
{
dist: "Windows Server 2008 R2 HPC edition",
key: "TT8MH-CG224-D3D7Q-498W2-9QCTX"
},
{
dist: "Windows Server 2008 R2 Standard",
key: "YC6KT-GKW9T-YTKYR-T4X34-R7VHC"
},
{
dist: "Windows Server 2008 R2 Enterprise",
key: "489J6-VHDMP-X63PK-3K798-CPX3Y"
},
{
dist: "Windows Server 2008 R2 Datacenter",
key: "74YFP-3QFB3-KQT8W-PMXWJ-7M648"
},
{
dist: "Windows Server 2008 R2 for Itanium-based Systems",
key: "GT63C-RJFQ3-4GMB6-BRFB9-CB83V"
},
{
dist: "Windows Web Server 2008",
key: "WYR28-R7TFJ-3X2YQ-YCY4H-M249D"
},
{
dist: "Windows Server 2008 Standard",
key: "TM24T-X9RMF-VWXK6-X8JC9-BFGM2"
},
{
dist: "Windows Server 2008 Standard without Hyper-V",
key: "W7VD6-7JFBR-RX26B-YKQ3Y-6FFFJ"
},
{
dist: "Windows Server 2008 Enterprise",
key: "YQGMW-MPWTJ-34KDK-48M3W-X4Q6V"
},
{
dist: "Windows Server 2008 Enterprise without Hyper-V",
key: "39BXF-X8Q23-P2WWT-38T2F-G3FPG"
},
{
dist: "Windows Server 2008 HPC",
key: "RCTX3-KWVHP-BR6TB-RB6DM-6X7HP"
},
{
dist: "Windows Server 2008 Datacenter",
key: "7M67G-PC374-GR742-YH8V4-TCBY3"
}
]
try {
console.log("Current Distro: " + windowsDistro)
var closestDist;
var closetKey;
var minDist = Number.MAX_VALUE;
// find closest match in product key list dist to var windowsDistro
for (var i = 0; i < productKeys.length; i++) {
var dist = productKeys[i].dist;
var key = productKeys[i].key;
var distDiff = levenshteinDistance(windowsDistro, dist);
if (distDiff < minDist) {
minDist = distDiff;
closestDist = dist;
closetKey = key;
}
}
console.log("The closest match is: " + closestDist);
mainWindow.webContents.executeJavaScript(`document.showQuestion("PLEASE READ CAREFULLY" ,"Make sure the full distribution after the year matches! Does **${windowsDistro}** match **${closestDist}**? ", "Yes", "No")`).then((result) => {
console.log(result)
if (result == true) {
console.log("Found Match")
// open powershell and run dism /online /get-targeteditions
// exec dism /online /get-currentedition and log output
exec('dism /online /get-currentedition', {
'shell': 'powershell.exe'
}, (err, stdout, stderr) => {
console.log(stdout)
if (stderr) {
console.log("Error: " + stderr)
mainWindow.webContents.executeJavaScript(`document.showError(${stderr}, true)`)
}
if (stdout.toLowerCase().indexOf("eval") !== -1) {
console.log("Found Eval Version")
// lets switch it to standard
// dism /online /Set-Edition:ServerStandard /ProductKey:DBGBW-NPF86-BJVTX-K3WKJ-MTB6V /AcceptEula
// send success message with confirm
mainWindow.webContents.executeJavaScript(`document.showSuccess("A popup should appear and load for a while (30 mins possibly) **restart computer once it asks you to** THEN Hit this Activate Windows button again!", true)`)
const setEditionShell = spawn(`dism /online /Set-Edition:ServerStandard /ProductKey:${closetKey} /AcceptEula`, [], {
shell: true,
detached: true,
})
setEditionShell.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
setEditionShell.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
setEditionShell.on('error', (error) => {
console.log(`error: ${error.message}`);
});
setEditionShell.on("close", (code) => {
console.log(`child process exited with code ${code}`);
});
} else if (stdout.toLowerCase().indexOf("error") !== -1) {
mainWindow.webContents.executeJavaScript(`document.showError(${stdout}, true)`)
} else {
// Found version that can be upgraded
console.log("Found Version that can be upgraded")
const activateEditionShell = spawn(`slmgr /ipk ${closetKey} & slmgr /skms kms.digiboy.ir & slmgr /ato`, [], {
shell: true,
detached: true,
})
activateEditionShell.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
activateEditionShell.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
activateEditionShell.on('error', (error) => {
console.log(`error: ${error.message}`);
});
activateEditionShell.on("close", (code) => {
console.log(`child process exited with code ${code}`);
});
// wait for 30 mins
setTimeout(() => {
let dialog_options = {
title: 'Restart PC',
message: `Please restart your computer`,
detail: `After your computer restarts, please hit the Activate Windows button again`,
};
dialog.showMessageBox(dialog_options);
}, 1800000)
}
});
} else {
console.log("No Match")
mainWindow.webContents.executeJavaScript(`document.showError("Contact an admin for support with OS: ${windowsDistro}", true)`)
}
}).catch((err) => {
console.log(err)
})
} catch (err) {
console.log(err)
}
})
var rdpUser = ""
ipcMain.handle('get-rdp-user', function () {
console.log("get-rdp-user CALLED")
// exec command
exec('$env:UserName', {
'shell': 'powershell.exe'
}, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
console.log(`Got user: ${stdout}`)
// make only 1 line
stdout = stdout.replace(/(\r\n|\n|\r)/gm, "");
// parse default user
var user = stdout
rdpUser = user
mainWindow.webContents.executeJavaScript(`$("#rdp-user").text( ${ JSON.stringify(user) });`).then(function (response) {}).catch(function (err) {});
});
})
ipcMain.handle('get-system-info', function () {
console.log("get-system-info CALLED")
si.cpu().then(function (data) {
console.log(data)
// mainWindow.webContents.executeJavaScript to update manufactuer
mainWindow.webContents.executeJavaScript(`$("#cpu-manufacturer").text( ${ JSON.stringify(data.manufacturer) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#cpu-brand").text( ${ JSON.stringify(data.brand) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#cpu-cores").text( ${ JSON.stringify(data.physicalCores) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#cpu-threads").text( ${ JSON.stringify(data.cores) });`).then(function (response) {}).catch(function (err) {});
// cpu speed
mainWindow.webContents.executeJavaScript(`$("#cpu-speed").text( ${ JSON.stringify(data.speed) });`).then(function (response) {}).catch(function (err) {});
// cpu speed min
mainWindow.webContents.executeJavaScript(`$("#cpu-speed-min").text( ${ JSON.stringify(data.speedMin) });`).then(function (response) {}).catch(function (err) {});
// cpu speed max
mainWindow.webContents.executeJavaScript(`$("#cpu-speed-max").text( ${ JSON.stringify(data.speedMax) });`).then(function (response) {}).catch(function (err) {});
})
si.osInfo().then(function (data) {
console.log(data)
// if distro contains the word Microsoft, remove the word Microsoft
if (data.distro.toLowerCase().indexOf("microsoft") !== -1) {
data.distro = data.distro.replace("Microsoft ", "")
}
windowsDistro = data.distro
mainWindow.webContents.executeJavaScript(`$("#os-distro").text( ${ JSON.stringify(data.distro) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#os-release").text( ${ JSON.stringify(data.release) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#os-arch").text( ${ JSON.stringify(data.arch) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#os-kernel").text( ${ JSON.stringify(data.kernel) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#os-servicepack").text( ${ JSON.stringify(data.servicepack) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#os-hypervisor").text( ${ JSON.stringify(data.hypervisor) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#os-uefi").text( ${ JSON.stringify(data.uefi) });`).then(function (response) {}).catch(function (err) {});
})
si.mem().then(function (data) {
console.log(data)
// convert bytes to GB
data.total = (data.total / 1073741824).toFixed(2)
// round up
data.total = Math.ceil(data.total)
mainWindow.webContents.executeJavaScript(`$("#ram-total").text( ${ JSON.stringify(data.total + "GB") });`).then(function (response) {}).catch(function (err) {});
})
try {
si.memLayout().then(function (data) {
console.log(data)
data = data[0]
mainWindow.webContents.executeJavaScript(`$("#ram-manufacturer").text( ${ JSON.stringify(data.manufacturer) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#ram-speed").text( ${ JSON.stringify(data.clockSpeed) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#ram-type").text( ${ JSON.stringify(data.type) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#ram-ecc").text( ${ JSON.stringify(data.ecc) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#ram-part-number").text( ${ JSON.stringify(data.partNum) });`).then(function (response) {}).catch(function (err) {});
})
} catch (err) {
console.log(err)
}
si.networkInterfaces().then(function (data) {
console.log(data)
// for loop through data and fund operstate 'up'
for (var i = 0; i < data.length; i++) {
if (data[i].operstate == "up") {
// set data to data[i]
data = data[i]
break
}
}
//console.log(data)
mainWindow.webContents.executeJavaScript(`$("#net-interface").text( ${ JSON.stringify(data.iface) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#net-mac").text( ${ JSON.stringify(data.mac) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#net-name").text( ${ JSON.stringify(data.ifaceName) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#net-type").text( ${ JSON.stringify(data.type) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#net-virtual").text( ${ JSON.stringify(data.virtual) });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#net-speed").text( ${ JSON.stringify(data.speed+ "Mbps") });`).then(function (response) {}).catch(function (err) {});
mainWindow.webContents.executeJavaScript(`$("#net-dhcp").text( ${ JSON.stringify(data.dhcp) });`).then(function (response) {}).catch(function (err) {});
})
})
var rdpPort = "3389"
ipcMain.handle('get-rdp-port', function () {
console.log("get-rdp-port CALLED")
// exec command
exec(`get-ItemProperty -Path 'HKLM:\\System\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp\\'-name portnumber`, {
'shell': 'powershell.exe'
}, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
// find number in string
var port = JSON.stringify("Port: " + stdout.split("PortNumber")[1].split(":")[1].split("\r")[0])
rdpPort = stdout.split("PortNumber")[1].split(":")[1].split("\r")[0].trim()
authInfo.rdpPort = rdpPort
try {
emitSocket('update-auth', authInfo)
} catch (err) {
console.error(err)
}
console.log(`Got port: ${port}`);
mainWindow.webContents.executeJavaScript(`$("#rdp-port").text( ${ port });`).then(function (response) {}).catch(function (err) {});
});
})
ipcMain.handle('firewall-whitelist', function (e, ip) {
console.log("firewall-whitelist CALLED")
console.log(ip)
// exec command
exec(`New-NetFirewallRule -DisplayName "Allow ${ip}" -Direction Inbound -Action Allow -RemoteAddress ${ip}`, {
'shell': 'powershell.exe'
}, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
console.log(err)
mainWindow.webContents.executeJavaScript(`document.showError(${JSON.stringify(err.message)}, true)`).then(function (response) {}).catch(function (err) {});
return;
}
// the *entire* stdout and stderr (buffered)
// find number in string
console.log(stdout);
if (stdout.indexOf("successfully from the store")) {
mainWindow.webContents.executeJavaScript(`document.showSuccess("Added ${ip} to the whitelist")`).then(function (response) {}).catch(function (err) {});
} else {
mainWindow.webContents.executeJavaScript(`document.showError("Failed to add ${ip} to the whitelist")`).then(function (response) {}).catch(function (err) {});
}
});
})
ipcMain.handle('firewall-blacklist', function (e, ip) {
console.log("firewall-blacklist CALLED")
console.log(ip)
// exec command
exec(`New-NetFirewallRule -DisplayName "Block ${ip}" -Direction Inbound -Action Block -RemoteAddress ${ip}`, {
'shell': 'powershell.exe'
}, (err, stdout, stderr) => {
if (err) {
console.log(err)
mainWindow.webContents.executeJavaScript(`document.showError(${JSON.stringify(err.message)}, true)`).then(function (response) {}).catch(function (err) {});
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
// find number in string
console.log(stdout);
if (stdout.indexOf("successfully from the store")) {
mainWindow.webContents.executeJavaScript(`document.showSuccess("Added ${ip} to the blacklist")`).then(function (response) {}).catch(function (err) {});
} else {
mainWindow.webContents.executeJavaScript(`document.showError("Failed to add ${ip} to the blacklist")`).then(function (response) {}).catch(function (err) {});
}
});
})
ipcMain.handle('rdp-change-password', function (e, newPass) {
console.log("rdp-change-password CALLED")
console.log(`net user ${rdpUser} ${newPass}`)
// exec command
// remove new line from string
exec(`net user ${rdpUser} ${newPass}`, {
'shell': 'powershell.exe'
}, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
console.log(err)
mainWindow.webContents.executeJavaScript(`document.showError(${JSON.stringify(err.message)}, true)`).then(function (response) {}).catch(function (err) {});
return;
}
// the *entire* stdout and stderr (buffered)
// find number in string
console.log("stout", stdout);
console.log("strerr", stderr);
if (authInfo.working) {
mainWindow.webContents.executeJavaScript(`document.showSuccess("Password Changed, Check your email for backup info (Might be in Spam)", true);`).then(function (response) {}).catch(function (err) {});
var options = {
'method': 'POST',
'url': `${websiteUrl}/server-rdp-change`,
'headers': {
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'email': authInfo.email,
'password': authInfo.password,
'server_ip': authInfo.ip,
'server_name': authInfo.name,
'rdp_port': authInfo.rdpPort,
'rdp_password': newPass,
}
};
request(options, function (error, response) {
console.log(response.body);
if (error) console.log(error);
});
} else {
mainWindow.webContents.executeJavaScript(`document.showSuccess("Password changed. You are not logged in, so no backup email will be sent.", true);`).then(function (response) {}).catch(function (err) {});
}
});
})
ipcMain.handle('rdp-change-port', function (e, newPort) {
console.log("rdp-change-port CALLED")
console.log(newPort)
// exec command to see if port is in use
exec(`Get-NetTCPConnection | where Localport -eq ${newPort} | select Localport,OwningProcess`, {
'shell': 'powershell.exe'
}, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
console.log(err)
mainWindow.webContents.executeJavaScript(`document.showError(${JSON.stringify(err.message)}), true`).then(function (response) {}).catch(function (err) {});
return;
}
if (stdout == "") {
// port is not in use
exec(`netsh advfirewall firewall add rule name="Allow RDP" dir=in action=allow protocol=TCP localport=${newPort}`, {
'shell': 'powershell.exe'
}, (err, stdout, stderr) => {
if (stdout.indexOf("Ok.") !== -1) {
console.log("Port is not in use")
exec(`Set-ItemProperty -Path 'HKLM:\\System\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp\\'-name portnumber -value ${newPort}`, {
'shell': 'powershell.exe'
}, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
mainWindow.webContents.executeJavaScript(`document.showError(${JSON.stringify(err.message)}), true`).then(function (response) {}).catch(function (err) {});
console.log(err)
return;
}
// the *entire* stdout and stderr (buffered)
// find number in string
console.log(stdout);
console.log(stderr);
if (stdout !== "" || stderr !== "") {
mainWindow.webContents.executeJavaScript(`document.showError("Port might not have changed, see app logs", true);`).then(function (response) {}).catch(function (err) {});
} else {
authInfo.rdpPort = newPort
try {
emitSocket('update-auth', authInfo)
} catch (err) {
console.error(err)
}
if (authInfo.working == true) {
mainWindow.webContents.executeJavaScript(`document.showSuccess("Port Changed, Check your email for backup info (Might be in Spam)", true);`).then(function (response) {}).catch(function (err) {});
var options = {
'method': 'POST',
'url': `${websiteUrl}/server-rdp-change`,
'headers': {
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'email': authInfo.email,
'password': authInfo.password,
'server_ip': authInfo.ip,
'server_name': authInfo.name,
'rdp_port': newPort,
}
};
request(options, function (error, response) {
console.log(response.body);
if (error) console.log(error);
});
} else {
mainWindow.webContents.executeJavaScript(`document.showSuccess("Port changed. You are not logged in, so no backup email will be sent.", true);`).then(function (response) {}).catch(function (err) {});