Last active
March 23, 2024 10:49
-
-
Save SaeedDev94/b805a22e7c64723f0c3c4f2057e3bfb0 to your computer and use it in GitHub Desktop.
Windows tun2socks routing helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
SET "SCRIPT=%0" | |
SET "ARG=%1" | |
SET "GATEWAY=192.168.1.1" | |
SET "SERVER_ADDRESS=x.x.x.x" | |
SET "SOCKS_SCHEME=socks5" | |
SET "SOCKS_ADDRESS=127.0.0.1" | |
SET "SOCKS_PORT=10808" | |
SET "TUN_NAME=wintun" | |
SET "TUN_ADDRESS=10.10.10.10" | |
SET "TUN_SUBNET=255.255.255.0" | |
SET "TUN_PRIMARY_DNS=8.8.8.8" | |
SET "TUN_SECONDARY_DNS=8.8.4.4" | |
SET "TUN_POST_UP=.\%SCRIPT% t" | |
SET "OS_DEFAULT_ROUTE=0.0.0.0 MASK 0.0.0.0 %GATEWAY%" | |
SET "TUN_ROUTE=0.0.0.0 MASK 0.0.0.0 %TUN_ADDRESS% METRIC 1" | |
SET "SERVER_ROUTE=%SERVER_ADDRESS% MASK 255.255.255.255 %GATEWAY% METRIC 10" | |
SET "RESTORE_DEFAULT_ROUTE=.\%SCRIPT% r" | |
if "%ARG%" == "t" ( | |
netsh interface ip set address name=%TUN_NAME% source=static addr=%TUN_ADDRESS% mask=%TUN_SUBNET% gateway=none | |
netsh interface ip set dns name=%TUN_NAME% source=static address=%TUN_PRIMARY_DNS% register=primary | |
netsh interface ip add dns name=%TUN_NAME% address=%TUN_SECONDARY_DNS% index=2 | |
goto :eof | |
) | |
if "%ARG%" == "r" ( | |
echo: | |
echo "Performing cleanup..." | |
taskkill /F /IM tun2socks.exe | |
taskkill /F /IM xray.exe | |
route DELETE %SERVER_ROUTE% | |
route DELETE %TUN_ROUTE% | |
route -p ADD %OS_DEFAULT_ROUTE% METRIC 1 | |
echo: | |
echo "OS default route restored" | |
goto :eof | |
) | |
:: We need to delete OS default route first | |
route DELETE %OS_DEFAULT_ROUTE% | |
:: Route all trafic to the tun device | |
route -p ADD %TUN_ROUTE% | |
:: Exclude proxy server itself | |
route -p ADD %SERVER_ROUTE% | |
:: Run your proxy in background | |
start /B xray run -c config.json | |
:: To setup tun device we need wintun | |
:: You can get it from here: https://www.wintun.net | |
:: You must put wintun.dll file in tun2socks dir | |
start /B tun2socks -device %TUN_NAME% -proxy %SOCKS_SCHEME%://%SOCKS_ADDRESS%:%SOCKS_PORT% -tun-post-up "%TUN_POST_UP%" -tcp-auto-tuning | |
SET trap= | |
for /f "usebackq delims=" %%a in (`"prompt $h & for %%b in (1) do REM Press Ctrl+C twice to exit"`) do SET "bs=%%a" | |
SET /p dummy=%bs%||SET trap="catch" | |
if defined trap ( | |
%RESTORE_DEFAULT_ROUTE% | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment