@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/5] TLS 1.2 활성화  (Win7 로그인/API 연결 안될때 핵심수정)
echo         启用 TLS 1.2 (Win7 无法登录/连接时必修)
reg add "HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" /v Enabled /t REG_DWORD /d 1 /f >nul 2>&1
reg add "HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" /v DisabledByDefault /t REG_DWORD /d 0 /f >nul 2>&1
reg add "HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.1\Client" /v Enabled /t REG_DWORD /d 1 /f >nul 2>&1
reg add "HKLM\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.1\Client" /v DisabledByDefault /t REG_DWORD /d 0 /f >nul 2>&1
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp" /v DefaultSecureProtocols /t REG_DWORD /d 0xA00 /f >nul 2>&1
reg add "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp" /v DefaultSecureProtocols /t REG_DWORD /d 0xA00 /f >nul 2>&1
reg add "HKLM\SOFTWARE\Microsoft\.NETFramework\v4.0.30319" /v SchUseStrongCrypto /t REG_DWORD /d 1 /f >nul 2>&1
reg add "HKLM\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319" /v SchUseStrongCrypto /t REG_DWORD /d 1 /f >nul 2>&1
reg add "HKLM\SOFTWARE\Microsoft\.NETFramework\v4.0.30319" /v SystemDefaultTlsVersions /t REG_DWORD /d 1 /f >nul 2>&1
reg add "HKLM\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319" /v SystemDefaultTlsVersions /t REG_DWORD /d 1 /f >nul 2>&1
echo        완료. / 完成。
echo.

echo  [2/5] IKEv2 인증서 설치 / 安装 IKEv2 证书 ...
echo        (IKEv2 연결 후 바로 끊길 때 핵심 원인 / 连接后即断的核心原因)
powershell -NoProfile -Command "[IO.File]::WriteAllBytes($env:TEMP+'\t_c1.cer',[Convert]::FromBase64String('MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAwTzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2VhcmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJuZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBYMTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygch77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6UA5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sWT8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyHB5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UCB5iPNgiV5+I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SgEEzwxA57demyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc='))"
certutil -addstore -f Root "%TEMP%\t_c1.cer" >nul 2>&1 && echo    OK - ISRG Root X1 (Lets Encrypt)
del "%TEMP%\t_c1.cer" >nul 2>&1
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\"" >nul 2>&1
certutil -addstore -f Root "%TEMP%\titan_ut.crt" >nul 2>&1 && echo    OK - USERTrust RSA
del "%TEMP%\titan_ut.crt" >nul 2>&1
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\"" >nul 2>&1
certutil -addstore -f Root "%TEMP%\titan_r46.crt" >nul 2>&1 && echo    OK - Sectigo R46
del "%TEMP%\titan_r46.crt" >nul 2>&1
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/Titan_Networks_Root_CA.crt' -OutFile \"$env:TEMP\titan_root.crt\"" >nul 2>&1
certutil -addstore -f Root "%TEMP%\titan_root.crt" >nul 2>&1 && echo    OK - Titan Networks Root CA
del "%TEMP%\titan_root.crt" >nul 2>&1
echo.

echo  [3/5] 어댑터 / 프록시 / DNS / 방화벽 / 연결방해 요소 제거 ...
echo         重置网络 / 清除干扰项 ...
:: --- IKEv2 어댑터 리셋 ---
rasdial /disconnect >nul 2>&1
netsh interface set interface "WAN Miniport (IKEv2)" admin=disable >nul 2>&1
timeout /t 2 /nobreak >nul
netsh interface set interface "WAN Miniport (IKEv2)" admin=enable >nul 2>&1
:: --- 다른 VPN이 강제 설정한 프록시 해제 (Panda, 360VPN 등) ---
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f >nul 2>&1
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /f >nul 2>&1
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v AutoConfigURL /f >nul 2>&1
netsh winhttp reset proxy >nul 2>&1
:: --- 수동 고정 DNS → 자동으로 복원 ---
powershell -NoProfile -Command "Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IPEnabled=true' | ForEach-Object { $_.SetDNSServerSearchOrder() | Out-Null }" >nul 2>&1
ipconfig /flushdns >nul 2>&1
:: --- IKEv2 방화벽 규칙 추가 (UDP 500/4500) ---
netsh advfirewall firewall delete rule name="TITAN VPN IKEv2" >nul 2>&1
netsh advfirewall firewall delete rule name="TITAN VPN IKEv2 In" >nul 2>&1
netsh advfirewall firewall add rule name="TITAN VPN IKEv2" dir=out action=allow protocol=UDP remoteport=500,4500 >nul 2>&1
netsh advfirewall firewall add rule name="TITAN VPN IKEv2 In" dir=in action=allow protocol=UDP localport=500,4500 >nul 2>&1
:: --- TCP 최적화 (고지연 경로 China-Korea) ---
netsh int tcp set global autotuninglevel=normal >nul 2>&1
netsh int tcp set global ecncapability=disabled >nul 2>&1
:: --- IPSec IKE 서비스 재시작 ---
sc stop IKEEXT >nul 2>&1
timeout /t 2 /nobreak >nul
sc start IKEEXT >nul 2>&1
echo        완료 (어댑터+프록시+DNS+방화벽+TCP). / 完成。
echo.

echo  [4/5] 스테일 Wintun 어댑터 제거 + Wintun 드라이버 기동 ...
echo         清除残留 Wintun 虚拟网卡 + 启动 Wintun 驱动 ...
powershell -NoProfile -Command "Get-PnpDevice | Where-Object { $_.FriendlyName -like '*Wintun*' } | Remove-PnpDevice -Confirm:$false -ErrorAction SilentlyContinue" >nul 2>&1
sc start wintun >nul 2>&1
echo        완료. / 完成。
echo.

echo  ============================================================
echo    IKEv2 / V2RAY 수리 완료!
echo    修复完成! 请在应用中重新连接。
echo  ============================================================
echo.

echo  [5/5] 앱이 아예 안 켜지거나 실행 직후 꺼지나요?
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
