@echo off setlocal EnableExtensions title Tailscale Serve rem =========================================================================== rem Runs: tailscale serve --bg --https=8443 http://localhost:3001 rem (with the ports you choose) rem rem tailscale-serve.bat publish with the default ports below rem tailscale-serve.bat 8443 3001 HTTPS port 8443 -> local port 3001 rem tailscale-serve.bat off 8443 3001 remove that publication rem tailscale-serve.bat status show what is published now rem rem Other devices on your tailnet then open rem https://this-pc.your-tailnet.ts.net:8443/ rem (the exact address is printed at the end) rem rem To remove ALL publications: tailscale serve reset rem =========================================================================== rem ---------------- SETTINGS ---------------- set "DEFAULT_HTTPS_PORT=8443" set "DEFAULT_LOCAL_PORT=3002" rem ------------------------------------------ set "ACTION=on" if /i "%~1"=="off" set "ACTION=off" if /i "%~1"=="status" set "ACTION=status" if not "%ACTION%"=="on" shift set "PATH=%PATH%;%ProgramFiles%\Tailscale" where tailscale >nul 2>&1 if errorlevel 1 ( echo [error] tailscale.exe was not found. Install Tailscale first: https://tailscale.com/download goto :fail ) if "%ACTION%"=="status" ( tailscale serve status goto :done ) set "HTTPS_PORT=%~1" set "LOCAL_PORT=%~2" if not defined HTTPS_PORT set "HTTPS_PORT=%DEFAULT_HTTPS_PORT%" if not defined LOCAL_PORT set "LOCAL_PORT=%DEFAULT_LOCAL_PORT%" call :checkport "%HTTPS_PORT%" if errorlevel 1 goto :badport set "HTTPS_PORT=%CP_VALUE%" call :checkport "%LOCAL_PORT%" if errorlevel 1 goto :badport set "LOCAL_PORT=%CP_VALUE%" set "TARGET=http://localhost:%LOCAL_PORT%" rem ---- is Tailscale connected? ---- set "TSIP=" for /f "delims=" %%i in ('tailscale ip -4 2^>nul') do if not defined TSIP set "TSIP=%%i" if not defined TSIP ( echo [error] Tailscale is not connected. Open the Tailscale app and log in, or run: tailscale up goto :fail ) if "%ACTION%"=="off" ( echo Removing: HTTPS port %HTTPS_PORT% to %TARGET% tailscale serve --https=%HTTPS_PORT% %TARGET% off if errorlevel 1 goto :serve_failed echo. tailscale serve status goto :done ) netstat -ano | findstr /R /C:":%LOCAL_PORT% .*LISTENING" >nul if errorlevel 1 echo [note] Nothing is listening on local port %LOCAL_PORT% yet. Start the app too. echo Publishing: %TARGET% on HTTPS port %HTTPS_PORT% tailscale serve --bg --https=%HTTPS_PORT% %TARGET% if errorlevel 1 goto :serve_failed echo. tailscale serve status goto :done :checkport rem %1 = value. Sets CP_VALUE. Returns errorlevel 1 if it is not a port from 1 to 65535. set "CP_VALUE=%~1" echo %CP_VALUE%| findstr /R "^[0-9][0-9]*$" >nul if errorlevel 1 exit /b 1 for /f "tokens=* delims=0" %%a in ("%CP_VALUE%") do set "CP_VALUE=%%a" echo %CP_VALUE%| findstr /R "^[0-9][0-9][0-9][0-9][0-9][0-9]" >nul if not errorlevel 1 exit /b 1 if %CP_VALUE% lss 1 exit /b 1 if %CP_VALUE% gtr 65535 exit /b 1 exit /b 0 :badport echo [error] Ports must be numbers from 1 to 65535. goto :fail :serve_failed echo. echo [error] tailscale serve failed. Read the message above. echo If it says that HTTPS or Serve is not enabled, open the link it shows, echo or enable HTTPS on the DNS page of the Tailscale admin console, then run this again. goto :fail :done echo. pause exit /b 0 :fail echo. pause exit /b 1