-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_CraftCommand.bat
More file actions
970 lines (863 loc) · 33.5 KB
/
run_CraftCommand.bat
File metadata and controls
970 lines (863 loc) · 33.5 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
@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
cd /d "%~dp0."
:: --- CONSOLE SETUP ---
mode con cols=80 lines=42
color 0F
:: --- ANSI ESCAPE CODE SETUP ---
for /f %%a in ('echo prompt $E ^| cmd') do set "E=%%a"
set "R=%E%[0m"
set "CR=%E%[91m"
set "CG=%E%[92m"
set "CY=%E%[93m"
set "CC=%E%[96m"
set "CM=%E%[95m"
set "CW=%E%[97m"
set "CGY=%E%[90m"
set "CB=%E%[94m"
set "BOLD=%E%[1m"
:: --- ARGUMENT PARSING ---
if "%~1"=="--join" goto :HANDLE_JOIN
:: --- ONEDRIVE PROTECTION ---
set "ONEDRIVE_DETECTED=false"
echo "%CD%" | findstr /i "OneDrive" >nul
if !errorlevel! equ 0 set "ONEDRIVE_DETECTED=true"
if "%ONEDRIVE_DETECTED%"=="true" (
echo.
echo %CY%%BOLD%[WARNING]%R% %CY%OneDrive/Desktop Path Detected%R%
echo Running from these folders causes %BOLD%EPERM%R% installation errors.
echo.
echo %BOLD%Fix:%R% Move this folder to %CC%C:\CraftCommand%R%
echo.
pause
)
:: --- PRE-FLIGHT CHECKS ---
call :VALIDATE_ENVIRONMENT
if !errorlevel! neq 0 exit /b !errorlevel!
goto :MAIN_SETUP
:HANDLE_JOIN
set "PANEL_URL=%~2"
set "JOIN_TOKEN=%~3"
if "!PANEL_URL!"=="" goto :JOIN_ERROR
if "!JOIN_TOKEN!"=="" goto :JOIN_ERROR
echo.
echo %CC% ENROLLMENT %R% Initializing secure node join...
:: Fetch config via PowerShell
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"$url = '!PANEL_URL!/api/nodes/join-config/!JOIN_TOKEN!'; ^
try { ^
$r = Invoke-RestMethod -Uri $url -Method Get -TimeoutSec 10; ^
if ($r.error) { throw $r.error } ^
$r | ConvertTo-Json -Compress | Out-File -FilePath '%TEMP%\cc_join.json' -Encoding utf8; ^
} catch { ^
Write-Error $_; exit 1; ^
}"
if !errorlevel! neq 0 (
echo %CR% FAILED %R% Could not reach panel or token is invalid.
exit /b 1
)
:: Parse result
for /f "usebackq tokens=*" %%a in ("%TEMP%\cc_join.json") do set "CONFIG_JSON=%%a"
del "%TEMP%\cc_join.json" >nul 2>nul
:: Extract fields (Powershell helper)
for /f "usebackq tokens=*" %%a in (`powershell -NoProfile -Command "$j=Get-Content '%TEMP%\cc_join.json' -Raw | ConvertFrom-Json; $j.nodeId"`) do set "NODE_ID=%%a"
for /f "usebackq tokens=*" %%a in (`powershell -NoProfile -Command "$j=Get-Content '%TEMP%\cc_join.json' -Raw | ConvertFrom-Json; $j.nodeSecret"`) do set "NODE_SEC=%%a"
for /f "usebackq tokens=*" %%a in (`powershell -NoProfile -Command "$j=Get-Content '%TEMP%\cc_join.json' -Raw | ConvertFrom-Json; $j.panelUrl"`) do set "PANEL_URL_VAL=%%a"
if "!NODE_ID!"=="" (
echo %CR% FAILED %R% Enrollment data corrupted.
exit /b 1
)
:: Write .env
if not exist "agent" mkdir "agent"
echo PANEL_URL=!PANEL_URL_VAL!> "agent\.env"
echo NODE_ID=!NODE_ID!>> "agent\.env"
echo NODE_SECRET=!NODE_SEC!>> "agent\.env"
echo %CG% SUCCESS %R% Node enrolled: !NODE_ID!
echo %CGY% Starting agent... %R%
cd agent
if not exist "node_modules" call npm install
if not exist "dist" call npm run build
node dist/agent/src/index.js
exit /b 0
:JOIN_ERROR
echo.
echo %CR%%BOLD% ERROR %R% Missing arguments for --join.
echo Usage: %~nx0 --join ^<PANEL_URL^> ^<TOKEN^>
exit /b 1
:MAIN_SETUP
goto :VERSION_SYNC
:: ============================================================================
:: CRAFTCOMMAND — Platform Launcher
:: ============================================================================
:: --- UI HEADER: HERO CARD ---
:PRINT_HERO
echo.
echo %CC%%BOLD% __ __ __ __ %R%
echo %CC% ██████╗██████╗ █████╗ ███████╗████████╗ ██████╗ ██████╗ ███╗ ███╗███╗ ███╗ █████╗ ███╗ ██╗██████╗ %R%
echo %CC% ██╔════╝██╔══██╗██╔══██╗██╔════╝╚══██╔══╝ ██╔════╝██╔═══██╗████╗ ████║████╗ ████║██╔══██╗████╗ ██║██╔══██╗%R%
echo %CC% ██║ ██████╔╝███████║█████╗ ██║ ██║ ██║ ██║██╔████╔██║██╔████╔██║███████║██╔██╗ ██║██║ ██║%R%
echo %CC% ██║ ██╔══██╗██╔══██║██╔══╝ ██║ ██║ ██║ ██║██║╚██╔╝██║██║╚██╔╝██║██╔══██║██║╚██╗██║██║ ██║%R%
echo %CC% ╚██████╗██║ ██║██║ ██║██║ ██║ ╚██████╗╚██████╔╝██║ ╚═╝ ██║██║ ╚═╝ ██║██║ ██║██║ ╚████║██████╔╝%R%
echo %CC% ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═════╝ %R%
exit /b 0
:VALIDATE_ENVIRONMENT
set "NODE_OK=0"
where node >nul 2>nul
if !errorlevel! equ 0 set "NODE_OK=1"
if exist ".runtimes\node\node.exe" (
set "PATH=%CD%\.runtimes\node;!PATH!"
set "NODE_OK=1"
)
if "!NODE_OK!"=="1" (
:: Refresh path one last time to ensure NPM is visible
where npm >nul 2>nul
if !errorlevel! neq 0 (
set "PATH=%CD%\.runtimes\node;!PATH!"
)
call :CHECK_OPTIONAL_DEPENDENCIES
exit /b 0
)
cls
call :PRINT_HERO
echo.
echo %CY%%BOLD% DEPENDECY MISSING %R% Node.js is required to run CraftCommand.
echo.
echo %CGY%CraftCommand can automatically install it for you.%R%
echo.
echo %BOLD%[1]%R% Install via %CC%Winget%R% (System-wide, recommended)
echo %BOLD%[2]%R% Install via %CC%Bootstrapper%R% (Local folder, zero-config)
echo %BOLD%[0]%R% Exit
echo.
set /p n_choice=" %CC%%BOLD%TERM: %R%"
if "%n_choice%"=="1" (
echo.
echo %CC% Installing Node.js via winget... %R%
winget install OpenJS.NodeJS.LTS --accept-package-agreements --accept-source-agreements
if !errorlevel! equ 0 (
echo.
echo %CG%%BOLD%SUCCESS!%R% Node.js installed.
echo %CY%Please RESTART this script to refresh your environment.%R%
pause
exit
) else (
echo %CR% Winget installation failed. %R%
pause
goto :VALIDATE_ENVIRONMENT
)
)
if "%n_choice%"=="2" (
echo.
echo %CC% Bootstrapping portable runtime... %R%
powershell -NoProfile -ExecutionPolicy Bypass -File "scripts\core\bootstrap-runtime.ps1"
if !errorlevel! equ 0 (
set "PATH=%CD%\.runtimes\node;!PATH!"
echo.
echo %CG%%BOLD%SUCCESS!%R% Portable runtime is ready.
ping 127.0.0.1 -n 3 >nul
exit /b 0
) else (
echo %CR% Bootstrapping failed. %R%
pause
goto :VALIDATE_ENVIRONMENT
)
)
if "%n_choice%"=="0" exit /b 1
goto :VALIDATE_ENVIRONMENT
:VERSION_SYNC
:: --- VERSION SYNC ---
set "CC_VERSION=1.12.5"
if exist "version.json" (
for /f "tokens=2 delims=:," %%a in ('findstr /R /C:"^[ ]*.version.:" version.json') do (
set "VERSION_VAL=%%~a"
set "VERSION_VAL=!VERSION_VAL: =!"
set "VERSION_VAL=!VERSION_VAL:"=!"
set "CC_VERSION=!VERSION_VAL!"
)
)
title CraftCommand v%CC_VERSION%
:: --- FIRST RUN SETUP ---
if not exist "node_modules" (
echo.
echo %CY%%BOLD% INITIAL SETUP DETECTED %R%
echo %CGY%Setting up core dependencies for first launch...%R%
call npm install
if !errorlevel! neq 0 (
echo %CR%[ERROR] Root dependency installation failed.%R%
pause
goto :MENU
)
)
:: --- CONFIG AUTOMATION ---
if not exist ".env" (
echo.
echo %CY%%BOLD% CONFIG GENERATION %R%
echo %CGY%Generating secure environment configuration...%R%
if not exist ".env.example" (
echo %CR%%BOLD% ERROR %R% .env.example not found.
pause
exit /b 1
)
copy ".env.example" ".env" >nul
powershell -Command "$s=(-join ((65..90) + (97..122) + (48..57) | Get-Random -Count 64 | ForEach-Object {[char]$_})); (Get-Content .env) -replace 'JWT_SECRET=.*', ('JWT_SECRET=' + $s) | Set-Content .env"
)
:: --- UPDATE CHECK ---
if not exist "version.json" goto SKIP_UPDATE_CHECK
echo.
echo %CGY%Checking for updates... %R%
(
echo $wc = New-Object System.Net.WebClient
echo $wc.Headers.Add^('User-Agent', 'CraftCommand-Launcher'^)
echo try {
echo $r = $wc.DownloadString^('https://raw.githubusercontent.com/Extroos/Craft-Commands/main/version.json'^) ^| ConvertFrom-Json
echo $l = Get-Content 'version.json' -Raw ^| ConvertFrom-Json
echo if ^($r.version -ne $l.version^) { Write-Host 'UPDATE_AVAILABLE' } else { Write-Host 'UP_TO_DATE' }
echo ^} catch { Write-Host 'OFFLINE' }
) > "%TEMP%\cc_check.ps1"
set "UPDATE_STATUS=OFFLINE"
for /f "usebackq tokens=*" %%i in (`powershell -ExecutionPolicy Bypass -File "%TEMP%\cc_check.ps1"`) do set "UPDATE_STATUS=%%i"
del "%TEMP%\cc_check.ps1" >nul 2>nul
if not "!UPDATE_STATUS!"=="UPDATE_AVAILABLE" goto SKIP_UPDATE_CHECK
:: Check Auto-Update Setting
set "AUTO_UPDATE=false"
if not exist "backend\data\settings.json" goto SKIP_AUTO_CHECK
powershell -NoProfile -Command "$s = Get-Content 'backend\data\settings.json' -Raw | ConvertFrom-Json; if ($s.app.autoUpdate -eq $true) { Write-Host 'true' } else { Write-Host 'false' }" > "%TEMP%\cc_au.txt"
for /f "usebackq tokens=*" %%a in ("%TEMP%\cc_au.txt") do set "AUTO_UPDATE=%%a"
del "%TEMP%\cc_au.txt" >nul 2>nul
:SKIP_AUTO_CHECK
echo.
echo %CY%%BOLD% UPDATE AVAILABLE %R%
echo %CGY%New version detected on GitHub.%R%
echo.
if "!AUTO_UPDATE!"=="true" (
echo %CY%Do you want to install this update? %CGY%^(y/n^)%R%
set /p u_choice=" %CC%^> %R%"
if /i "!u_choice!"=="y" (
echo.
echo %CG%Starting automated patching...%R%
:: 1. Download & Prepare
node scripts/core/system-updater.cjs
if exist "update-plan.json" (
:: 2. Apply (Atomic Swap)
powershell -NoProfile -ExecutionPolicy Bypass -File "scripts\ops\apply_update.ps1"
if !errorlevel! equ 0 (
echo.
echo %CG%%BOLD%+%R% Update applied successfully.
echo %CY%[UPDATE] Syncing environment and dependencies...%R%
:: 3. Post-Update Sync
call node scripts/core/sync-env.cjs
cd backend && call npm install && cd ..
echo.
echo %CG%%BOLD%SUCCESS!%R% System is now stable on version !REMOTE_VER!.
echo %CGY%Configuration synchronized. Launching...%R%
ping 127.0.0.1 -n 4 >nul
goto MENU
) else (
echo.
echo %CR%%BOLD%X%R% Update application failed. System reverted.
pause
)
) else (
echo.
echo %CR%%BOLD%X%R% Update staging failed. See logs above.
pause
)
)
) else (
echo %CY% Auto-Update is DISABLED in System Settings.%R%
echo %CGY% Enable it in the dashboard to install updates.%R%
echo.
pause
)
:SKIP_UPDATE_CHECK
:: --- ASSET SYNC ---
if not exist "backend\data\settings.json" goto SKIP_ASSET_SYNC
<nul set /p "=%CGY% Syncing assets... %R%"
powershell -NoProfile -Command "$s = Get-Content 'backend\data\settings.json' -Raw | ConvertFrom-Json; if ($s.app.updateWeb -eq $true) { exit 1 } else { exit 0 }"
if !errorlevel! equ 1 (
node scripts/core/update-web-cli.cjs
) else (
echo %CGY%Skipped.%R%
)
:SKIP_ASSET_SYNC
:: ============================================================================
:: MAIN MENU
:: ============================================================================
:MENU
set "choice="
cls
:: --- UPDATE EXECUTION (ATOMIC SWAP) ---
if exist "update-plan.json" (
echo.
echo %CY%[UPDATE] Pending update found!%R%
echo %CC%Executing update applicator...%R%
powershell -NoProfile -ExecutionPolicy Bypass -File "scripts\ops\apply_update.ps1"
if !errorlevel! neq 0 (
echo %CR%[ERROR] Update failed! Check console.%R%
pause
) else (
echo %CG%[SUCCESS] Update applied.%R%
)
)
:: --- POST-UPDATE CLEANUP ---
if exist "update_applied.flag" (
echo.
echo %CY%[UPDATE] Finalizing update...%R%
call node scripts/core/sync-env.cjs
echo.
echo %CY%[UPDATE] Updating dependencies...%R%
cd backend
call npm install >nul 2>nul
cd ..
cd frontend
call npm install >nul 2>nul
cd ..
cd agent
call npm install >nul 2>nul
cd ..
echo.
echo %CY%[UPDATE] Rebuilding frontend assets...%R%
call node scripts/core/update-web-cli.cjs --force
del "update_applied.flag"
echo %CG%[SUCCESS] System synchronized, dependencies updated, and assets rebuilt.%R%
timeout /t 2 >nul
)
:: --- UI HEADER: HERO CARD ---
echo.
echo %CC%%BOLD% __ __ __ __ %R%
echo %CC% ██████╗██████╗ █████╗ ███████╗████████╗ ██████╗ ██████╗ ███╗ ███╗███╗ ███╗ █████╗ ███╗ ██╗██████╗ %R%
echo %CC% ██╔════╝██╔══██╗██╔══██╗██╔════╝╚══██╔══╝ ██╔════╝██╔═══██╗████╗ ████║████╗ ████║██╔══██╗████╗ ██║██╔══██╗%R%
echo %CC% ██║ ██████╔╝███████║█████╗ ██║ ██║ ██║ ██║██╔████╔██║██╔████╔██║███████║██╔██╗ ██║██║ ██║%R%
echo %CC% ██║ ██╔══██╗██╔══██║██╔══╝ ██║ ██║ ██║ ██║██║╚██╔╝██║██║╚██╔╝██║██╔══██║██║╚██╗██║██║ ██║%R%
echo %CC% ╚██████╗██║ ██║██║ ██║██║ ██║ ╚██████╗╚██████╔╝██║ ╚═╝ ██║██║ ╚═╝ ██║██║ ██║██║ ╚████║██████╔╝%R%
echo %CC% ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═════╝ %R%
:: --- STATUS BAR ---
set "LOCAL_IP=127.0.0.1"
for /f "tokens=4" %%a in ('route print ^| findstr 0.0.0.0 ^| findstr /V "0.0.0.0.0"') do set "LOCAL_IP=%%a"
echo %CGY%------------------------------------------------------------------------%R%
echo %BOLD%%CW%v!CC_VERSION!%R% %CGY%:%R% %CG%%BOLD%ONLINE%R% %CGY%:%R% %CGY%IPV4: %CB%!LOCAL_IP!%R% %CGY%:%R% %CGY%NODE: %CM%WDL-ADMIN-01%R%
echo %CGY%------------------------------------------------------------------------%R%
echo %CGY%[01]%R% %CG%%BOLD%START PLATFORM%R% %CGY%Launch Backend ^& Frontend%R%
echo %CGY%[02]%R% %CC%SECURITY: HTTPS%R% %CGY%Caddy Automation / SSL%R%
echo %CGY%[03]%R% %CC%NETWORK: REMOTE%R% %CGY%Tunnels ^& Mesh VPNs%R%
echo.
echo %CGY%[04]%R% %CY%SYSTEM CHECK%R% %CGY%Check health ^& files%R%
echo %CGY%[05]%R% %CY%SYSTEM MAINTENANCE%R% %CGY%Environment Reconstruction%R%
echo %CGY%[06]%R% %CB%SYSTEM RECOVERY%R% %CGY%Rollback from Snapshot%R%
echo.
echo %CGY%[07]%R% %CM%REMOTE NODE%R% %CGY%Start Node Agent%R%
echo %CGY%[08]%R% %CR%STOP ALL%R% %CGY%Emergency Shutdown%R%
echo %CGY%------------------------------------------------------------------------%R%
echo %BOLD%%CW%[00]%R% %CGY%POWER OFF%R%
echo %CGY%------------------------------------------------------------------------%R%
set /p choice=" %CC%%BOLD%TERM: %R%"
if "%choice%"=="1" goto START
if "%choice%"=="01" goto START
if "%choice%"=="2" goto HTTPS_MENU
if "%choice%"=="02" goto HTTPS_MENU
if "%choice%"=="3" goto REMOTE_SETUP
if "%choice%"=="03" goto REMOTE_SETUP
if "%choice%"=="4" goto STABILITY_CHECK
if "%choice%"=="04" goto STABILITY_CHECK
if "%choice%"=="5" goto REINSTALL
if "%choice%"=="05" goto REINSTALL
if "%choice%"=="6" goto ROLLBACK
if "%choice%"=="06" goto ROLLBACK
if "%choice%"=="7" goto AGENT_START
if "%choice%"=="07" goto AGENT_START
if "%choice%"=="8" goto REMOTE_DISABLE
if "%choice%"=="08" goto REMOTE_DISABLE
if "%choice%"=="0" exit
if "%choice%"=="00" exit
goto MENU
:: ============================================================================
:: LAUNCH LOGIC
:: ============================================================================
:START
cls
echo.
echo %CC%%BOLD% PLATFORM LAUNCH SEQUENCE%R%
echo %CGY%-----------------------------------------------------------------------%R%
echo.
:: Check Node.js
set "NODE_PATH=.runtimes\node\node.exe"
where node >nul 2>nul
if %errorlevel% neq 0 (
if exist "%NODE_PATH%" (
set "PATH=%CD%\.runtimes\node;%PATH%"
) else (
echo.
echo %CY%%BOLD%!%R% Node.js not found. Attempting auto-bootstrap...
if exist "scripts\core\bootstrap-runtime.ps1" (
powershell -NoProfile -ExecutionPolicy Bypass -File "scripts\core\bootstrap-runtime.ps1"
if !errorlevel! equ 0 (
set "PATH=%CD%\.runtimes\node;%PATH%"
) else (
echo %CR%Failed to bootstrap runtime. Please install Node.js manually.%R%
pause
exit /b 1
)
) else (
echo %CR%Bootstrap script missing. Please install Node.js manually.%R%
pause
exit /b 1
)
)
)
:: Ensure NPM is in path (Refresh)
where npm >nul 2>nul
if %errorlevel% neq 0 (
if exist ".runtimes\node\npm.cmd" set "PATH=%CD%\.runtimes\node;%PATH%"
)
for /f "delims=" %%v in ('node -v') do set "NODE_V=%%v"
echo %CG%%BOLD%+%R% Runtime %CGY%!NODE_V!%R%
echo %CG%%BOLD%+%R% Platform %CGY%CraftCommand v!CC_VERSION!%R%
:: Smart Install
set MISSING_DEPS=0
if not exist "node_modules" set MISSING_DEPS=1
if not exist "backend\node_modules" set MISSING_DEPS=1
if not exist "frontend\node_modules" set MISSING_DEPS=1
if not exist "agent\node_modules" set MISSING_DEPS=1
if "%MISSING_DEPS%"=="1" (
echo.
echo %CY%%BOLD%!%R% First-time setup: installing dependencies
echo.
:: --- RUN-ANYWHERE PERMISSION GUARD ---
:: This forcefully unlocks files if we are on Desktop/OneDrive
icacls "%~dp0." /grant %USERNAME%:F /q >nul 2>nul
if exist "frontend" (
echo %CGY%[1/4]%R% Frontend...
cd frontend && (call npm install --no-audit --no-fund || (echo %CR%[FAIL] Frontend deps failed.%R% && pause && cd .. && goto MENU)) && cd ..
) else (
echo %CY%[!]%R% Frontend directory missing, skipping.
)
if exist "backend" (
echo %CGY%[2/4]%R% Backend...
cd backend && (call npm install --no-audit --no-fund || (
echo.
echo %CR%[FAIL] Backend installation was blocked.%R%
echo This is usually caused by %BOLD%Desktop/OneDrive locks%R%.
echo.
echo %CY%[ACTION]%R% Move this folder to %CC%C:\CraftCommand%R% and try again.
echo %CGY% Running from C:\ resolves 99%% of permission errors.%R%
echo.
pause
cd ..
goto MENU
)) && cd ..
) else (
echo %CR%[ERROR]%R% Backend directory missing! Platform cannot start.
pause
goto MENU
)
echo %CGY%[3/4]%R% Shared/Root...
call npm install --no-audit --no-fund || (echo %CR%[FAIL] Root deps failed.%R% && pause && goto MENU)
if exist "agent" (
echo %CGY%[4/4]%R% Node Agent...
cd agent && (call npm install --no-audit --no-fund || (echo %CR%[FAIL] Agent deps failed.%R% && pause && cd .. && goto MENU)) && cd ..
) else (
echo %CY%[!]%R% Agent directory missing, skipping.
)
echo.
echo %CG%%BOLD%+%R% Dependencies resolved
)
:: Detect Protocol
set "B_TYPE=HTTP"
tasklist /fi "imagename eq caddy.exe" 2>nul | findstr /i "caddy.exe" >nul
if !errorlevel! equ 0 set "B_TYPE=HTTPS (Caddy)"
tasklist /fi "imagename eq playit.exe" 2>nul | findstr /i "playit.exe" >nul
if !errorlevel! equ 0 set "B_TYPE=TUNNEL (Playit)"
set "ACC_URL=http://localhost:3000"
if exist "backend\data\settings.json" (
set "DOM_VAL="
powershell -NoProfile -Command "$s = Get-Content 'backend\data\settings.json' -ErrorAction SilentlyContinue | ConvertFrom-Json; if ($s.app.https.enabled -eq $true) { if ($s.app.https.domain) { Write-Output $s.app.https.domain } else { Write-Output 'localhost' } } else { exit 1 }" > "%TEMP%\cc_domain.txt" 2>nul
if !errorlevel! equ 0 (
set /p DOM_VAL= < "%TEMP%\cc_domain.txt"
if not "!DOM_VAL!"=="" set "ACC_URL=https://!DOM_VAL!"
)
)
echo.
echo %CGY%-----------------------------------------------------------------------%R%
echo Protocol %BOLD%%CG%!B_TYPE!%R%
echo Access %BOLD%%CC%!ACC_URL!%R%
echo %CGY%-----------------------------------------------------------------------%R%
echo.
echo.
echo %CGY%Streaming logs...%R%
echo.
:: Try to use concurrently (dev script) as it's the standard for dev launch
call npm run dev
if !errorlevel! neq 0 (
:: Fallback to start:all if dev failed/missing
call npm run start:all
)
if %errorlevel% neq 0 (
echo.
echo %CR%%BOLD%X%R% Process terminated %CGY%Exit code: %errorlevel%%R%
pause
)
goto MENU
:: ============================================================================
:: REMOTE ACCESS
:: ============================================================================
:REMOTE_SETUP
cls
echo.
echo %CC%%BOLD% REMOTE ACCESS%R%
echo %CGY%-----------------------------------------------------------------------%R%
echo.
echo %CGY%%BOLD% CONNECTIVITY%R%
echo %BOLD%%CW%1%R% %CG%Mesh VPN%R% %CGY%Tailscale / ZeroTier%R%
echo %BOLD%%CW%2%R% %CG%Zero-Config Tunnel%R% %CGY%Playit.gg%R%
echo %BOLD%%CW%3%R% %CC%Web Share%R% %CGY%Cloudflare Tunnel%R%
echo %BOLD%%CW%4%R% %CC%Direct Bind%R% %CGY%Manual Port Forward%R%
echo.
echo %CGY%%BOLD% CONTROL%R%
echo %BOLD%%CW%5%R% %CR%Disable All%R% %CGY%Kill remote bridges%R%
echo %BOLD%%CW%0%R% %CGY%Back%R%
echo.
echo %CGY%-----------------------------------------------------------------------%R%
echo.
<nul set /p "= %CC%%BOLD%^> %R%"
set /p r_choice=""
if "%r_choice%"=="1" (
call node scripts/ops/cli-remote-setup.cjs vpn
goto START
)
if "%r_choice%"=="2" (
call node scripts/ops/cli-remote-setup.cjs proxy
call node scripts/ops/install-proxy.cjs
taskkill /f /im playit.exe >nul 2>nul
echo %CG%Configured for Zero-Config Tunnel.%R%
goto START
)
if "%r_choice%"=="3" (
start "CraftCommand - Web Share" node scripts/ops/share-website.cjs
timeout /t 3 >nul
goto START
)
if "%r_choice%"=="4" (
call node scripts/ops/cli-remote-setup.cjs direct
goto START
)
if "%r_choice%"=="5" goto REMOTE_DISABLE
if "%r_choice%"=="0" goto MENU
if "%r_choice%"=="6" goto MENU
goto REMOTE_SETUP
:: ============================================================================
:: HTTPS CONFIGURATION
:: ============================================================================
:HTTPS_MENU
cls
echo.
echo %CC%%BOLD% HTTPS CONFIGURATION%R%
echo %CGY%-----------------------------------------------------------------------%R%
echo.
echo %BOLD%%CW%1%R% %CG%Automated Caddy%R% %CGY%One-click HTTPS%R%
echo %BOLD%%CW%2%R% %CY%Manual Certificates%R% %CGY%Bind custom PEM/CRT%R%
echo %BOLD%%CW%3%R% %CR%Disable HTTPS%R% %CGY%Revert to HTTP%R%
echo %BOLD%%CW%0%R% %CGY%Back%R%
echo.
echo %CGY%-----------------------------------------------------------------------%R%
echo.
<nul set /p "= %CC%%BOLD%^> %R%"
set /p h_choice=""
if "%h_choice%"=="1" goto PROTOCOL_PROXY
if "%h_choice%"=="2" goto PROTOCOL_DIRECT
if "%h_choice%"=="3" (
call node scripts/ops/manage-caddy.cjs disable
taskkill /f /im caddy.exe >nul 2>nul
pause
goto MENU
)
if "%h_choice%"=="0" goto MENU
if "%h_choice%"=="4" goto MENU
goto HTTPS_MENU
:PROTOCOL_PROXY
cls
echo.
echo %CC%%BOLD% AUTOMATED HTTPS%R%
echo %CGY%-----------------------------------------------------------------------%R%
echo.
<nul set /p "= Domain %CGY%(e.g. panel.example.com)%CW%: %R%"
set /p DOMAIN=""
echo.
call node scripts/ops/install-caddy.cjs
call node scripts/ops/manage-caddy.cjs setup !DOMAIN!
taskkill /f /im caddy.exe >nul 2>&1
timeout /t 1 >nul
start "CraftCommand - HTTPS Bridge" proxy\caddy.exe run --config proxy\Caddyfile --adapter caddyfile
echo.
echo %CG%%BOLD%+%R% HTTPS active for %CC%!DOMAIN!%R%
pause
goto MENU
:PROTOCOL_DIRECT
cls
echo.
echo %CC%%BOLD% MANUAL SSL BINDING%R%
echo %CGY%-----------------------------------------------------------------------%R%
echo.
<nul set /p "= Certificate (.pem/.crt): "
set /p CERT_PATH=""
<nul set /p "= Private Key (.key): "
set /p KEY_PATH=""
<nul set /p "= Passphrase (optional): "
set /p PASSPHRASE=""
echo.
call node scripts/maintenance/setup-https.cjs "!CERT_PATH!" "!KEY_PATH!" "!PASSPHRASE!"
pause
goto MENU
:: ============================================================================
:: PANIC CONTROL
:: ============================================================================
:REMOTE_DISABLE
cls
echo.
echo %CR%%BOLD% NETWORK ISOLATION%R%
echo %CGY%-----------------------------------------------------------------------%R%
echo.
echo Terminating external bridges...
taskkill /f /im caddy.exe >nul 2>nul
taskkill /f /im playit.exe >nul 2>nul
taskkill /f /im cloudflared.exe >nul 2>nul
echo.
echo Updating security registry...
call node scripts/ops/emergency-disable-remote.cjs
if %errorlevel% neq 0 (
echo.
echo %CR%%BOLD%X%R% Isolation failed. Check settings.json.
) else (
echo.
echo %CG%%BOLD%+%R% Network isolated. Dashboard is local-only.
)
timeout /t 5 >nul
goto MENU
:: ============================================================================
:: STABILITY AUDIT
:: ============================================================================
:STABILITY_CHECK
cls
echo.
echo %CY%%BOLD% STABILITY AUDIT%R%
echo %CGY%-----------------------------------------------------------------------%R%
echo.
echo:: --- RUNTIME DISCOVERY ---
set "NODE_BIN=node"
where node >nul 2>nul
if !errorlevel! neq 0 (
if exist ".runtimes\node\node.exe" set "NODE_BIN=%CD%\.runtimes\node\node.exe"
)
!NODE_BIN! -r ts-node/register -r tsconfig-paths/register scripts/tests/user_verification_test.ts 2>nul
if !errorlevel! neq 0 (
echo.
echo %CR%%BOLD% ERROR %R% Stability Audit failed to launch.
echo %CGY% Missing dependencies or corrupted runtime.%R%
echo %CC% Fix: Run [05] SYSTEM MAINTENANCE first.%R%
)
echo.
pause
goto MENU
:: ============================================================================
:: SYSTEM RECOVERY
:: ============================================================================
:ROLLBACK
cls
echo.
echo %CB%%BOLD% SYSTEM RECOVERY%R%
echo %CGY%-----------------------------------------------------------------------%R%
echo.
echo Searching for pre-update snapshots...
echo.
powershell -NoProfile -ExecutionPolicy Bypass -File "scripts\ops\rollback.ps1"
echo.
pause
goto MENU
:: ============================================================================
:: MAINTENANCE
:: ============================================================================
:REINSTALL
cls
echo.
echo %CY%%BOLD% MAINTENANCE MODE: NUCLEAR CLEANUP %R%
echo %CGY%-----------------------------------------------------------------------%R%
echo.
echo This mode will:
echo 1. Wipe ALL node_modules and lock-files
echo 2. Force-clear the NPM cache (Resolves 404 errors)
echo 3. Re-install every dependency from scratch
echo.
if "%ONEDRIVE_DETECTED%"=="true" (
echo %CR%%BOLD%[DETECTION]%R% You are still in a OneDrive folder.
echo Permissions %BOLD%WILL%R% fail unless you move to %CC%C:\CraftCommand%R%
echo.
)
<nul set /p "= Confirm Nuclear Reset? %CGY%(y/n)%CW%: %R%"
set confirm=
set /p confirm=""
if /i not "!confirm!"=="y" goto MENU
echo.
echo %CGY%[1/4]%R% Cleaning Registry/Cache...
call npm cache clean --force
echo %CGY%[2/4]%R% Flushing node_modules (Force-unlocking)...
:: Strip attributes that cause EPERM
attrib -r -s /s /d "node_modules\*" >nul 2>nul
attrib -r -s /s /d "backend\node_modules\*" >nul 2>nul
attrib -r -s /s /d "frontend\node_modules\*" >nul 2>nul
echo %CY%[ACTION] Reseting Directory Permissions...%R%
icacls "%CD%" /grant %USERNAME%:F /T >nul 2>nul
for /d /r . %%d in (node_modules) do @if exist "%%d" (
echo Deleting: %%d
rmdir /s /q "%%d" 2>nul
)
echo %CGY%[3/4]%R% Removing poison lock-files & resetting registry...
call npm config set registry https://registry.npmjs.org/
del /s /q /f "package-lock.json" >nul 2>nul
del /s /q /f "yarn.lock" >nul 2>nul
for /r . %%f in (package-lock.json) do del /f /q "%%f" >nul 2>nul
for /r . %%f in (yarn.lock) do del /f /q "%%f" >nul 2>nul
echo %CGY%[4/4]%R% Reinstalling all components...
echo Root...
call npm install
echo Backend...
cd backend && call npm install && cd ..
echo Frontend...
cd frontend && call npm install && cd ..
echo.
echo %CG%%BOLD%+%R% Dependencies restored.
echo.
pause
goto MENU
:CHECK_OPTIONAL_DEPENDENCIES
:: Check for Docker
where docker >nul 2>nul
if !errorlevel! neq 0 (
echo.
echo %CY%%BOLD%[OPTIONAL]%R% %CGY%Docker%R% is not installed.
echo %CGY% Docker is recommended for isolated Minecraft server hosting.%R%
echo %CGY% You can install it later via %CC%https://www.docker.com%R%
)
:: Check for Git (needed for some npm dependencies)
where git >nul 2>nul
if !errorlevel! neq 0 (
echo.
echo %CY%%BOLD%[OPTIONAL]%R% %CGY%Git%R% is not installed.
echo %CGY% Some advanced features might require Git.%R%
)
ping 127.0.0.1 -n 2 >nul
exit /b 0
:: ============================================================================
:: NODE AGENT
:: ============================================================================
:AGENT_START
cls
echo.
echo %CM%%BOLD% REMOTE NODE AGENT%R%
echo %CGY%-----------------------------------------------------------------------%R%
echo.
<nul set /p "= Node ID: "
set /p N_ID=""
<nul set /p "= Node Secret: "
set /p N_SEC=""
if "%N_ID%"=="" goto MENU
echo.
echo Initializing agent...
cd agent
if not exist "dist\agent\src\index.js" (
echo [System] Preparing agent...
if not exist "node_modules" call npm install
call npm run build
if !errorlevel! neq 0 (
echo %CR%[ERROR]%R% Agent build failed.
pause
cd ..
goto MENU
)
)
title CraftCommand - Node Agent [%N_ID:~0,8%...]
node dist/agent/src/index.js --panel-url http://localhost:3001 --node-id %N_ID% --secret %N_SEC%
cd ..
goto MENU
:: ============================================================================
:: SECURITY: HTTPS & DOMAIN BRIDGE
:: ============================================================================
:HTTPS_MENU
cls
echo.
echo %CC%%BOLD% SECURITY: HTTPS ^& DOMAIN BRIDGE %R%
echo %CGY%-----------------------------------------------------------------------%R%
echo.
echo CraftCommand uses %CC%Caddy%R% for automated SSL/HTTPS.
echo.
echo %BOLD%Requirements:%R%
echo 1. A domain name (e.g. yourpanel.com)
echo 2. Ports %BOLD%80%R% and %BOLD%443%R% forwarded to this machine.
echo.
echo %CGY%[1]%R% Setup new Domain ^& Start HTTPS
echo %CGY%[2]%R% Start existing Configuration
echo %CGY%[3]%R% Stop HTTPS Proxy
echo.
echo %CGY%[0]%R% Back to Menu
echo.
set /p h_choice=" %CC%%BOLD%TERM: %R%"
if "%h_choice%"=="1" goto HTTPS_SETUP
if "%h_choice%"=="2" goto HTTPS_START
if "%h_choice%"=="3" goto HTTPS_STOP
if "%h_choice%"=="0" goto MENU
goto HTTPS_MENU
:HTTPS_SETUP
echo.
echo %CY%%BOLD%STEP 1:%R% Enter your public domain name.
echo %CGY%(e.g. craft.mydomain.com or mypanel.duckdns.org)%R%
echo.
set /p USER_DOMAIN=" Domain: "
if "%USER_DOMAIN%"=="" goto HTTPS_MENU
echo.
echo %CY%%BOLD%STEP 2:%R% Generating Caddyfile...
(
echo # Generated by CraftCommand Launcher
echo %USER_DOMAIN% {
echo handle /api/* {
echo reverse_proxy localhost:3001
echo }
echo handle /socket.io/* {
echo reverse_proxy localhost:3001
echo }
echo handle {
echo reverse_proxy localhost:3000
echo }
echo header {
echo Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
echo X-Frame-Options "SAMEORIGIN"
echo X-Content-Type-Options "nosniff"
echo }
echo }
) > "Caddyfile"
echo %CG% SUCCESS %R% Configuration generated.
goto HTTPS_START
:HTTPS_START
if not exist "Caddyfile" (
echo %CR% ERROR %R% No Caddyfile found. Run Setup first.
pause
goto HTTPS_MENU
)
if not exist "proxy\caddy.exe" (
echo %CR% ERROR %R% Caddy binary missing in proxy/ folder.
pause
goto HTTPS_MENU
)
echo.
echo %CC% Launching Caddy Bridge... %R%
start "CraftCommand - HTTPS Bridge" /d "%CD%" "proxy\caddy.exe" run --config Caddyfile
echo.
echo %CG% BRIDGE ACTIVE %R% Your panel is now being secured.
echo %CGY%Check the new window for SSL issuance logs.%R%
echo.
pause
goto HTTPS_MENU
:HTTPS_STOP
taskkill /f /fi "windowtitle eq CraftCommand - HTTPS Bridge*" >nul 2>nul
echo.
echo %CY% BRIDGE STOPPED %R% HTTPS proxy has been terminated.
echo.
pause
goto HTTPS_MENU