-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstart_server.bat
More file actions
203 lines (182 loc) · 7.45 KB
/
start_server.bat
File metadata and controls
203 lines (182 loc) · 7.45 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
@echo off
setlocal ENABLEDELAYEDEXPANSION
REM Resolve repo root to this script's directory
set "ROOT=%~dp0"
pushd "%ROOT%"
echo Starting QuMail Backend Server...
echo.
REM Prepare logs directory
set "LOG_DIR=%ROOT%logs"
if not exist "%LOG_DIR%" (
mkdir "%LOG_DIR%" >nul 2>&1
)
REM Check if .env file exists
if not exist ".env" (
echo ERROR: .env file not found!
echo Please copy .env.example to .env and configure your credentials.
pause
exit /b 1
)
REM Load environment variables from .env file
for /f "usebackq tokens=1,2 delims==" %%a in (".env") do (
if not "%%a"=="" if not "%%a:~0,1%"=="#" (
set "%%a=%%b"
)
)
REM Attempt to locate encoder.exe for OTP service
set "ENCODER_EXE="
if exist "%ROOT%level1\encoder.exe" set "ENCODER_EXE=%ROOT%level1\encoder.exe"
if exist "%ROOT%encoder.exe" set "ENCODER_EXE=%ROOT%encoder.exe"
REM Export ENCODER_EXE for child processes
if not "%ENCODER_EXE%"=="" (
echo Found encoder at: %ENCODER_EXE%
) else (
echo WARNING: encoder.exe not found. OTP encrypt/decrypt will fail until it's available.
)
echo Environment variables loaded from .env:
echo - Database: %DB_NAME%
echo - JWT Secret: Ready
echo.
REM Start Key Manager service (http://127.0.0.1:2020)
echo Starting Key Manager service...
set "KM_SCRIPT=%ROOT%Key_Manager\km\server.py"
if not exist "%KM_SCRIPT%" (
echo ERROR: %KM_SCRIPT% not found.
pause
exit /b 1
)
REM If port 2020 already in use, assume KM is running and skip starting
powershell -NoProfile -Command "(Test-NetConnection -ComputerName 127.0.0.1 -Port 2020).TcpTestSucceeded" >nul 2>&1
if errorlevel 1 (
start "Key Manager" powershell -NoExit -Command "Set-Location -LiteralPath '%ROOT%Key_Manager\km'; $env:PYTHONUNBUFFERED='1'; $log=Join-Path '%LOG_DIR%' ('key_manager_'+(Get-Date -Format yyyyMMdd_HHmmss_fff)+'_'+$PID+'.log'); Write-Host ('Log: '+$log); python -u 'server.py' 2>&1 | Tee-Object -FilePath $log -Append"
) else (
echo Detected Key Manager already listening on 2020; skipping start.
)
REM Wait for Key Manager to be ready (max ~20s)
set /a __tries_km=0
echo Waiting for Key Manager (127.0.0.1:2020)...
:wait_km
powershell -NoProfile -Command "(Test-NetConnection -ComputerName 127.0.0.1 -Port 2020).TcpTestSucceeded" >nul 2>&1
if errorlevel 1 (
set /a __tries_km+=1
if !__tries_km! geq 20 (
echo WARNING: Key Manager did not open port 2020 yet. Continuing...
) else (
timeout /t 1 >nul
goto wait_km
)
)
REM Start OTP Flask API (runs on http://127.0.0.1:2021)
echo Starting OTP API service...
set "OTP_SCRIPT=%ROOT%level1\otp_api_test.py"
if not exist "%OTP_SCRIPT%" (
echo ERROR: %OTP_SCRIPT% not found.
pause
exit /b 1
)
REM If port 2021 already in use, assume OTP API is running and skip starting
powershell -NoProfile -Command "(Test-NetConnection -ComputerName 127.0.0.1 -Port 2021).TcpTestSucceeded" >nul 2>&1
if errorlevel 1 (
start "OTP API" powershell -NoExit -Command "Set-Location -LiteralPath '%ROOT%level1'; $env:ENCODER_EXE='%ENCODER_EXE%'; $env:PYTHONUNBUFFERED='1'; $log=Join-Path '%LOG_DIR%' ('otp_api_'+(Get-Date -Format yyyyMMdd_HHmmss_fff)+'_'+$PID+'.log'); Write-Host ('Log: '+$log); python -u 'otp_api_test.py' 2>&1 | Tee-Object -FilePath $log -Append"
) else (
echo Detected OTP API already listening on 2021; skipping start.
)
REM Start AES Server (runs on http://127.0.0.1:2022)
echo Starting AES Server service...
set "AES_SCRIPT=%ROOT%level2new\server2.py"
if not exist "%AES_SCRIPT%" (
echo ERROR: %AES_SCRIPT% not found.
pause
exit /b 1
)
REM If port 2022 already in use, assume AES Server is running and skip starting
powershell -NoProfile -Command "(Test-NetConnection -ComputerName 127.0.0.1 -Port 2022).TcpTestSucceeded" >nul 2>&1
if errorlevel 1 (
start "AES Server" powershell -NoExit -Command "Set-Location -LiteralPath '%ROOT%level2new'; $env:PYTHONUNBUFFERED='1'; $log=Join-Path '%LOG_DIR%' ('aes_server_'+(Get-Date -Format yyyyMMdd_HHmmss_fff)+'_'+$PID+'.log'); Write-Host ('Log: '+$log); python -u 'server2.py' 2>&1 | Tee-Object -FilePath $log -Append"
) else (
echo Detected AES Server already listening on 2022; skipping start.
)
REM Wait for OTP API to become available (max ~20s)
set /a __tries=0
echo Waiting for OTP API (127.0.0.1:2021)...
:wait_otp
powershell -NoProfile -Command "(Test-NetConnection -ComputerName 127.0.0.1 -Port 2021).TcpTestSucceeded" >nul 2>&1
if errorlevel 1 (
set /a __tries+=1
if !__tries! geq 20 (
echo WARNING: OTP API did not open port 2021 yet. Continuing...
) else (
timeout /t 1 >nul
goto wait_otp
)
)
REM Wait for AES Server to become available (max ~20s)
set /a __tries=0
echo Waiting for AES Server (127.0.0.1:2022)...
:wait_aes
powershell -NoProfile -Command "(Test-NetConnection -ComputerName 127.0.0.1 -Port 2022).TcpTestSucceeded" >nul 2>&1
if errorlevel 1 (
set /a __tries+=1
if !__tries! geq 20 (
echo WARNING: AES Server did not open port 2022 yet. Continuing...
) else (
timeout /t 1 >nul
goto wait_aes
)
)
REM Start PQC Server (runs on http://127.0.0.1:2023)
echo Starting PQC Server service...
set "PQC_SCRIPT=%ROOT%level3\pqc_server.py"
if not exist "%PQC_SCRIPT%" (
echo ERROR: %PQC_SCRIPT% not found.
pause
exit /b 1
)
REM If port 2023 already in use, assume PQC Server is running and skip starting
powershell -NoProfile -Command "(Test-NetConnection -ComputerName 127.0.0.1 -Port 2023).TcpTestSucceeded" >nul 2>&1
if errorlevel 1 (
start "PQC Server" powershell -NoExit -Command "Set-Location -LiteralPath '%ROOT%level3'; $env:PYTHONUNBUFFERED='1'; $log=Join-Path '%LOG_DIR%' ('pqc_server_'+(Get-Date -Format yyyyMMdd_HHmmss_fff)+'_'+$PID+'.log'); Write-Host ('Log: '+$log); python -u 'pqc_server.py' 2>&1 | Tee-Object -FilePath $log -Append"
) else (
echo Detected PQC Server already listening on 2023; skipping start.
)
REM Wait for PQC Server to become available (max ~20s)
set /a __tries=0
echo Waiting for PQC Server (127.0.0.1:2023)...
:wait_pqc
powershell -NoProfile -Command "(Test-NetConnection -ComputerName 127.0.0.1 -Port 2023).TcpTestSucceeded" >nul 2>&1
if errorlevel 1 (
set /a __tries+=1
if !__tries! geq 20 (
echo WARNING: PQC Server did not open port 2023 yet. Continuing...
) else (
timeout /t 1 >nul
goto wait_pqc
)
)
REM Quick sanity check of OTP encrypt endpoint; write result to a separate health file
powershell -NoProfile -Command "$p=@{text='health-check'}|ConvertTo-Json; try { $r=Invoke-RestMethod -Uri 'http://127.0.0.1:2021/api/otp/encrypt' -Method Post -ContentType 'application/json' -Body $p; 'OTP encrypt OK' } catch { 'OTP encrypt FAILED: ' + $_.Exception.Message }" >> "%LOG_DIR%\otp_health.txt" 2>&1
REM Resolve API port (try 5000-5010) by parsing TcpTestSucceeded output
set "API_URL="
set "API_PORT="
for /l %%P in (5000,1,5010) do (
for /f %%R in ('powershell -NoProfile -Command "(Test-NetConnection -ComputerName 127.0.0.1 -Port %%P).TcpTestSucceeded"') do (
set "TCP=%%R"
)
if /I "!TCP!"=="False" (
set "API_PORT=%%P"
set "API_URL=http://0.0.0.0:%%P"
goto api_port_found
)
)
echo ERROR: No free port found in range 5000-5010. Please free a port and re-run.
pause
goto :eof
:api_port_found
echo Starting QuMail API on %API_URL% ...
echo Press Ctrl+C to stop the QuMail API
echo.
REM Start the server with ASPNETCORE_URLS override
set "ASPNETCORE_URLS=%API_URL%"
dotnet run --project "%ROOT%Email_client\QuMail.EmailProtocol\QuMail.EmailProtocol.csproj"
pause
popd