@echo off
chcp 65001 >nul
title JV SSH reverse tunnel (do not close)
call "%~dp0_load-env.bat"
if errorlevel 1 exit /b 1

set "KEY=%USERPROFILE%\.ssh\keiba_tools"
set "SSH_OPTS=-o StrictHostKeyChecking=no -o ServerAliveInterval=30"
if exist "%KEY%" set "SSH_OPTS=%SSH_OPTS% -i %KEY%"
set "RPORT=%SSH_REMOTE_PORT%"
if not defined RPORT set "RPORT=18765"

echo ============================================================
echo   SSH -R: Linux 127.0.0.1:%RPORT% -^> this PC 127.0.0.1:8765
echo   Requires JVLinkServer on port 8765 (other window).
echo   Nothing new appearing here = OK. DO NOT CLOSE.
echo ============================================================
echo.
echo Connecting: %SERVER_SSH%
ssh %SSH_OPTS% -N -R %RPORT%:127.0.0.1:8765 %SERVER_SSH%
echo.
echo [INFO] SSH tunnel ended
pause
exit /b %ERRORLEVEL%
