@echo off
chcp 936 >nul
setlocal EnableExtensions EnableDelayedExpansion
title Network Auto Repair Tool
color 0A

:: Auto elevate to admin
net session >nul 2>&1
if !errorlevel! neq 0 (
    echo Requesting administrator privileges...
    powershell -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    exit /b
)

cls
echo ===========================================================
echo       Network Auto Repair Tool v2.0 (No Interaction)
echo       Network Repair - SSTAP Support - Fully Automatic
echo ===========================================================
echo.
echo [INFO] Starting fully automatic network repair...
echo       Please do not close this window.
echo.

echo ---------- Step 1/7: Cleaning Proxy Settings ----------
echo [INFO] Cleaning IE proxy settings...
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f >nul
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /d "" /f >nul
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyOverride /d "" /f >nul
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v AutoConfigURL /d "" /f >nul
echo [OK] IE proxy cleaned
echo [INFO] Resetting WinHTTP proxy...
netsh winhttp reset proxy >nul
echo [OK] WinHTTP proxy reset
echo [INFO] Clearing port proxy...
netsh interface portproxy reset >nul
echo [OK] Port proxy cleared
echo.

echo ---------- Step 2/7: Flushing DNS and ARP Cache ----------
echo [INFO] Flushing DNS cache...
ipconfig /flushdns
echo [INFO] Registering DNS...
ipconfig /registerdns
echo [INFO] Clearing ARP cache...
netsh interface ip delete arpcache
echo [OK] DNS and ARP cache cleared
echo.

echo ---------- Step 3/7: Resetting Winsock and TCP/IP ----------
echo [INFO] Resetting Winsock catalog...
netsh winsock reset >nul
echo [OK] Winsock reset done
echo [INFO] Resetting TCP/IP stack...
netsh int ip reset >nul
echo [OK] TCP/IP reset done
echo [INFO] Resetting IPv6...
netsh int ipv6 reset >nul
echo [OK] IPv6 reset done
echo.

echo ---------- Step 4/7: Resetting Firewall ----------
echo [INFO] Resetting firewall policy...
netsh advfirewall reset >nul
echo [OK] Firewall reset to default
echo [INFO] Enabling firewall...
netsh advfirewall set allprofiles state on >nul
echo [OK] Firewall enabled
echo.

echo ---------- Step 5/7: Repairing Hosts File ----------
set hostspath=%SystemRoot%\System32\drivers\etc\hosts
echo [INFO] Checking hosts file: %hostspath%
powershell -Command "$c = Get-Content '%hostspath%'; $custom = $c | Where-Object { $_ -notmatch '^#|^\s*$' }; if ($custom) { Write-Host '[WARN] Custom hosts entries found, backing up and resetting...' -ForegroundColor Yellow; Copy-Item '%hostspath%' '%hostspath%.backup' -Force; $default = @('# Copyright (c) 1993-2009 Microsoft Corp.', '#', '# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.', '#', '# localhost name resolution is handled within DNS itself.', '#	127.0.0.1       localhost', '#	::1             localhost'); [System.IO.File]::WriteAllLines('%hostspath%', $default); Write-Host '[OK] Hosts file reset (backup: hosts.backup)' -ForegroundColor Green } else { Write-Host '[OK] Hosts file is clean' -ForegroundColor Green }"
echo.

echo ---------- Step 6/7: Reinstalling SSTAP TAP Driver ----------
set sstappath=%LOCALAPPDATA%\SSTap-beta
if not exist "%sstappath%" set sstappath=%ProgramFiles(x86)%\SSTap-beta
if exist "%sstappath%" (
    set tapdriver=!sstappath!\tap-driver\x64
    if not exist "!tapdriver!" set tapdriver=!sstappath!\tap-driver\x86
    if exist "!tapdriver!" (
        echo [INFO] SSTAP found: !sstappath!
        echo [INFO] TAP driver dir: !tapdriver!
        echo [INFO] Uninstalling old TAP driver...
        pushd "!tapdriver!"
        tapinstall.exe remove tap0901 >nul 2>&1
        timeout /t 2 /nobreak >nul
        echo [INFO] Installing new TAP driver...
        tapinstall.exe install OemVista.inf tap0901
        popd
        echo [OK] TAP driver reinstall complete
    ) else (
        echo [WARN] TAP driver dir not found, skipping
    )
) else (
    echo [INFO] SSTAP not installed, skipping TAP repair
)
echo.

echo ---------- Step 7/7: Restarting Network Adapters ----------
echo [INFO] Detecting physical network adapters...
powershell -Command "$a = Get-NetAdapter | Where-Object { $_.Status -eq 'Up' -and $_.HardwareInterface }; if ($a) { $a | ForEach-Object { Write-Host '  -' $_.Name '(' $_.InterfaceDescription ')' }; Write-Host '[INFO] Restarting adapters...' ; $a | Restart-NetAdapter -Confirm:$false; Write-Host '[OK] Network adapters restarted' -ForegroundColor Green } else { Write-Host '[WARN] No active physical adapters found' -ForegroundColor Yellow }"
echo [INFO] Waiting for network recovery...
timeout /t 5 /nobreak >nul
echo.

echo ===========================================================
echo       All Repair Steps Completed!
echo ===========================================================
echo.
echo [INFO] Verifying network connectivity...
echo.

ping -n 2 114.114.114.114 >nul 2>&1
if !errorlevel! equ 0 (echo [OK] Baidu DNS 114.114.114.114 : OK) else (echo [FAIL] Baidu DNS 114.114.114.114 : FAIL)

ping -n 2 www.baidu.com >nul 2>&1
if !errorlevel! equ 0 (echo [OK] Baidu Website : OK) else (echo [FAIL] Baidu Website : FAIL)

echo.
echo ===========================================================
echo       Repair Complete - Reboot Recommended
echo ===========================================================
echo.
echo [IMPORTANT] Please REBOOT your computer to apply all changes!
echo             Especially for Winsock/TCP-IP reset to take effect.
echo.
echo [INFO] Current TAP adapter status:
powershell -Command "Get-NetAdapter -IncludeHidden | Where-Object { $_.InterfaceDescription -match 'TAP' } | Select-Object Name, InterfaceDescription, Status | Format-Table -AutoSize"
echo.
echo -----------------------------------------------------------
echo Press any key to close this window...
pause >nul
exit /b 0
