﻿@echo off
chcp 65001 >nul
title TITAN VPN - Windows 종합 수리 / 一键修复
:: ===== 관리자 권한 자가 승격 / Auto-elevate to Administrator =====
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo  관리자 권한 요청 창에서 "예"를 눌러주세요. / 请在弹窗点击"是"。
    powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    exit /b
)
echo  ============================================================
echo    TITAN VPN  Windows 종합 수리 / 一键修复
echo  ============================================================
echo.

echo  [1/3] IKEv2 인증서 설치 / 安装证书 ...
echo        (IKEv2 가 연결 후 바로 끊길 때의 핵심 원인)
powershell -NoProfile -Command "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls; Invoke-WebRequest -UseBasicParsing -Uri 'https://titan.jobjapan.com/upload/download/USERTrust_RSA_Root.crt' -OutFile \"$env:TEMP\titan_ut.crt\""
powershell -NoProfile -Command "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls; Invoke-WebRequest -UseBasicParsing -Uri 'https://titan.jobjapan.com/upload/download/Sectigo_R46_Root.crt' -OutFile \"$env:TEMP\titan_r46.crt\""
certutil -addstore -f Root "%TEMP%\titan_ut.crt"
certutil -addstore -f Root "%TEMP%\titan_r46.crt"
del "%TEMP%\titan_ut.crt" >nul 2>&1
del "%TEMP%\titan_r46.crt" >nul 2>&1
echo.

echo  [2/3] WAN Miniport (IKEv2) 재설정 / 重置适配器 ...
rasdial /disconnect >nul 2>&1
netsh interface set interface "WAN Miniport (IKEv2)" admin=disable >nul 2>&1
timeout /t 3 /nobreak >nul
netsh interface set interface "WAN Miniport (IKEv2)" admin=enable >nul 2>&1
ipconfig /flushdns >nul 2>&1
echo        완료. / 完成。
echo.

echo  ============================================================
echo    IKEv2 수리 완료! 이제 앱에서 다시 연결해 보세요.
echo    IKEv2 修复完成! 请在应用中重新连接。
echo  ============================================================
echo.

echo  [3/3] 앱이 아예 안 켜지거나 실행 직후 꺼지나요?
echo        그런 경우만 .NET / VC++ 런타임을 설치하면 됩니다 (시간 걸림, ~80MB).
echo        应用打不开/秒退? 仅这种情况才需安装 .NET / VC++ (较大)。
set /p ans="   설치할까요? Y(예) / N(건너뛰기) 후 Enter: "
if /i "%ans%"=="Y" (
    echo.
    echo    다운로드+설치 중... 잠시 기다려주세요 / 正在下载安装, 请稍候 ...
    powershell -NoProfile -Command "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -UseBasicParsing -Uri 'https://aka.ms/vs/17/release/vc_redist.x86.exe' -OutFile \"$env:TEMP\vcx86.exe\""
    powershell -NoProfile -Command "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -UseBasicParsing -Uri 'https://aka.ms/vs/17/release/vc_redist.x64.exe' -OutFile \"$env:TEMP\vcx64.exe\""
    powershell -NoProfile -Command "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -UseBasicParsing -Uri 'https://aka.ms/dotnet/8.0/windowsdesktop-runtime-win-x64.exe' -OutFile \"$env:TEMP\dotnet.exe\""
    "%TEMP%\vcx86.exe" /install /quiet /norestart
    "%TEMP%\vcx64.exe" /install /quiet /norestart
    "%TEMP%\dotnet.exe" /install /quiet /norestart
    del "%TEMP%\vcx86.exe" "%TEMP%\vcx64.exe" "%TEMP%\dotnet.exe" >nul 2>&1
    echo    런타임 설치 완료. / 运行库安装完成。
)
echo.
echo  ============================================================
echo    모든 수리 완료! PC 재부팅 후 사용을 권장합니다.
echo    全部完成! 建议重启电脑后使用。
echo  ============================================================
echo.
pause
