@echo off
chcp 65001 >nul
TITLE Windows Remote Port Configuration Tool

:START
CLS
ECHO ==================================================
ECHO                 Welcome to the Remote Port Configuration Tool
ECHO ==================================================
ECHO .
ECHO This tool is used to view and modify the Remote Desktop Protocol (RDP) port.
ECHO After modifying the port, firewall rules will be automatically updated.
ECHO .

:: Query the current port number
for /f "tokens=3" %%a in ('reg query "HKLM\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /v PortNumber') do (
    set /a CURRENT_PORT=%%a
)

:: Check if the port number is correctly parsed
IF "%CURRENT_PORT%"=="" (
    ECHO Error: Failed to parse the current remote port number. Please check the registry.
    PAUSE
    EXIT
)

:: Check if Remote Desktop is enabled
for /f "tokens=3" %%a in ('reg query "HKLM\System\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections') do (
    set /a RDP_STATUS=%%a
)
IF "%RDP_STATUS%"=="0" (
    set RDP_ENABLED=Enabled
) ELSE (
    set RDP_ENABLED=Disabled
)

:: Display the current port number and RDP status
ECHO The current remote port is: %CURRENT_PORT%
ECHO Remote Desktop is currently: %RDP_ENABLED%
ECHO .

:: Provide operation options
ECHO Please select an option:
ECHO 1. Query the current remote desktop port
ECHO 2. Modify the remote port number
ECHO 3. Disable remote connection
ECHO 4. Enable remote connection
ECHO 5. Exit the tool
SET /P OPTION=Enter the option number (1/2/3/4/5): 
IF "%OPTION%"=="1" GOTO QUERY_PORT
IF "%OPTION%"=="2" GOTO MODIFY_PORT
IF "%OPTION%"=="3" GOTO DISABLE_RDP
IF "%OPTION%"=="4" GOTO ENABLE_RDP
IF "%OPTION%"=="5" GOTO EXIT
ECHO Invalid option, please try again.
PAUSE
GOTO START

:QUERY_PORT
CLS
ECHO ==================================================
ECHO Query Current Remote Desktop Port
ECHO ==================================================
ECHO .
:: Re-query the port number directly from the registry
for /f "tokens=3" %%a in ('reg query "HKLM\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /v PortNumber') do (
    set /a CURRENT_PORT=%%a
)
for /f "tokens=3" %%a in ('reg query "HKLM\System\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections') do (
    set /a RDP_STATUS=%%a
)
IF "%RDP_STATUS%"=="0" (
    set RDP_ENABLED=Enabled
) ELSE (
    set RDP_ENABLED=Disabled
)
ECHO The current remote desktop port is: %CURRENT_PORT%
ECHO Remote Desktop is currently: %RDP_ENABLED%
PAUSE
GOTO START

:MODIFY_PORT
CLS
ECHO ==================================================
ECHO Modify the Remote Port Number
ECHO ==================================================
ECHO .
ECHO Supported port range: 19000-20000.
ECHO If no port number is entered, it will default to 19000.
ECHO .
SET /P NEW_PORT=Enter the new port number (or press Enter for default 19000):
IF "%NEW_PORT%"=="" SET NEW_PORT=19000
IF %NEW_PORT% LSS 19000 (
    ECHO The port number is too low. Please enter a number between 19000 and 20000.
    PAUSE
    GOTO MODIFY_PORT
)
IF %NEW_PORT% GTR 20000 (
    ECHO The port number is too high. Please enter a number between 19000 and 20000.
    PAUSE
    GOTO MODIFY_PORT
)

:: Modify the remote port in the registry
reg add "HKLM\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /v PortNumber /t REG_DWORD /d %NEW_PORT% /f
IF %ERRORLEVEL% NEQ 0 (
    ECHO Failed to update the port in the registry.
    PAUSE
    GOTO START
)

:: Restart the remote service
ECHO Restarting the Remote Desktop Service...
net stop TermService >nul 2>&1
net start TermService >nul 2>&1

:: Add firewall rule
ECHO Updating firewall rules...
netsh advfirewall firewall add rule name="RDP Port %NEW_PORT%" dir=in action=allow protocol=TCP localport=%NEW_PORT% >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
    ECHO Failed to update firewall rules.
    PAUSE
    GOTO START
)

ECHO .
ECHO The port has been successfully changed to %NEW_PORT%.
PAUSE
GOTO START

:DISABLE_RDP
CLS
ECHO ==================================================
ECHO Disable Remote Connection
ECHO ==================================================
ECHO .
ECHO Disabling the Remote Desktop Service...
reg add "HKLM\System\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 1 /f
net stop TermService >nul 2>&1
ECHO .
ECHO Remote connection has been disabled.
PAUSE
GOTO START

:ENABLE_RDP
CLS
ECHO ==================================================
ECHO Enable Remote Connection
ECHO ==================================================
ECHO .
ECHO Enabling the Remote Desktop Service...
reg add "HKLM\System\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f
net start TermService >nul 2>&1

:: Set a default port if none exists
IF "%CURRENT_PORT%"=="0" (
    reg add "HKLM\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /v PortNumber /t REG_DWORD /d 3389 /f
    ECHO Default port set to 3389.
)

:: Add firewall rule for default port
netsh advfirewall firewall add rule name="RDP Default Port 3389" dir=in action=allow protocol=TCP localport=3389 >nul 2>&1

ECHO .
ECHO Remote Desktop has been enabled.
PAUSE
GOTO START

:EXIT
ECHO Thank you for using this tool. Goodbye!
EXIT
