@echo off rem Tempsession.sh client for cmd.exe (experimental). Protocol v1, client 1.2.0. rem rem Load it INTO the cmd window you want to share: rem curl -fsSL -o "%TEMP%\tempsession.cmd" https://tempsession.sh/connect/v1/cmd && call "%TEMP%\tempsession.cmd" rem rem What it does: rem 1. Registers this window with TempSession and prints an approval link. rem 2. Once you approve it in the browser, the window is shared at once and rem shows who approved it. No remote command runs before that. rem 3. Runs approved commands one at a time with CALL in this cmd.exe, so rem environment variables and the current directory are shared. Commands rem run with batch file rules (for example %%i instead of %i in FOR). rem 4. When the session ends, its variables (TS_*), its temporary files and rem this script are removed. rem rem Limits: output is sent when a command has finished; a running command rem cannot be cancelled remotely. Commands run with input from nul, and git rem is told not to ask (GIT_TERMINAL_PROMPT=0) while they run. Ctrl+C stops a running program, then cmd rem asks "Terminate batch job (Y/N)?": N keeps sharing, Y stops sharing at rem once (the server then ends the session on its own after a few minutes). rem Needs curl.exe (Windows 10 1803 or later). set "TS_BASE=https://tempsession.sh" set "TS_API=%TS_BASE%/api/agent/v1" set "TS_VERSION=1.2.0" set "TS_DIR=%TEMP%\tempsession-%RANDOM%%RANDOM%" set "TS_REGISTERED=0" mkdir "%TS_DIR%" >nul 2>&1 if not exist "%TS_DIR%\" ( echo [Tempsession.sh] Cannot create a temporary folder. goto :ts_cleanup_local ) type nul > "%TS_DIR%\auth" set "TS_CP=" for /f "tokens=2 delims=:." %%c in ('chcp') do set /a "TS_CP=%%c" >nul 2>&1 set "TS_ENCODING=UTF-8" if defined TS_CP if not "%TS_CP%"=="65001" set "TS_ENCODING=CP%TS_CP%" set "TS_WINVER=" for /f "tokens=2 delims=[]" %%v in ('ver') do set "TS_WINVER=%%v" rem The registration fields go into a curl config file (-K): too many for CALL arguments. rem Backslashes are doubled there, since curl reads "\x" in a quoted value as an escape. set "TS_CWD=%CD%" set "TS_CWD=%TS_CWD:\=\\%" > "%TS_DIR%\register.cfg" echo data-urlencode = "shell=cmd" >> "%TS_DIR%\register.cfg" echo data-urlencode = "shell_version=%TS_WINVER%" >> "%TS_DIR%\register.cfg" echo data-urlencode = "os=Windows" >> "%TS_DIR%\register.cfg" echo data-urlencode = "hostname=%COMPUTERNAME%" >> "%TS_DIR%\register.cfg" echo data-urlencode = "local_user=%USERNAME%" >> "%TS_DIR%\register.cfg" echo data-urlencode = "cwd=%TS_CWD%" >> "%TS_DIR%\register.cfg" echo data-urlencode = "features=complete" >> "%TS_DIR%\register.cfg" echo data-urlencode = "client_version=%TS_VERSION%" >> "%TS_DIR%\register.cfg" echo data-urlencode = "output_encoding=%TS_ENCODING%" call :ts_http POST /sessions -K "%TS_DIR%\register.cfg" if not "%TS_CODE%"=="201" ( echo [Tempsession.sh] Could not register this window ^(HTTP %TS_CODE%^). Is %TS_BASE% reachable? goto :ts_cleanup_local ) set "TS_REGISTERED=1" call :ts_fields > "%TS_DIR%\auth" echo Authorization: Bearer %TS_F_agent_token% set "TS_F_agent_token=" echo. echo [Tempsession.sh] Approve this window in your browser: echo. echo %TS_F_pairing_url% echo. echo [Tempsession.sh] Code: %TS_F_pairing_code% (the browser must show the same code) echo [Tempsession.sh] Waiting for approval. Press Ctrl+C to cancel. :ts_wait ping -n 3 127.0.0.1 >nul call :ts_http GET /session if "%TS_CODE%"=="410" goto :ts_not_approved if "%TS_CODE%"=="401" goto :ts_not_approved if not "%TS_CODE%"=="200" goto :ts_wait call :ts_fields if "%TS_F_status%"=="pending_approval" goto :ts_wait if not "%TS_F_status%"=="active" goto :ts_not_approved echo. echo [Tempsession.sh] Approved by: findstr /b /c:"approved_by=" /c:"workspace=" "%TS_DIR%\body" echo [Tempsession.sh] This window is now shared. Remote commands run here. echo [Tempsession.sh] To stop sharing press Ctrl+C and answer Y. :ts_loop call :ts_http POST /commands/next if "%TS_CODE%"=="204" goto :ts_idle if "%TS_CODE%"=="200" goto :ts_run if "%TS_CODE%"=="410" goto :ts_ended_remotely if "%TS_CODE%"=="401" goto :ts_ended_remotely echo [Tempsession.sh] Connection problem (HTTP %TS_CODE%). Retrying... ping -n 3 127.0.0.1 >nul goto :ts_loop :ts_idle ping -n 2 127.0.0.1 >nul goto :ts_loop :ts_run set "TS_CMP_ID=" for /f "tokens=1,* delims=: " %%a in ('findstr /i /b /c:"X-TempSession-Completion-Id:" "%TS_DIR%\headers"') do set "TS_CMP_ID=%%b" if defined TS_CMP_ID goto :ts_complete set "TS_CMD_ID=" for /f "tokens=1,* delims=: " %%a in ('findstr /i /b /c:"X-TempSession-Command-Id:" "%TS_DIR%\headers"') do set "TS_CMD_ID=%%b" move /y "%TS_DIR%\body" "%TS_DIR%\command.cmd" >nul echo. echo [Tempsession.sh] Remote command: type "%TS_DIR%\command.cmd" echo. rem Nothing may ask at this window, where nobody remote can answer: git fails at once instead. set "TS_OLD_GIT_PROMPT=%GIT_TERMINAL_PROMPT%" set "TS_OLD_GCM=%GCM_INTERACTIVE%" set "GIT_TERMINAL_PROMPT=0" set "GCM_INTERACTIVE=never" call "%TS_DIR%\command.cmd" > "%TS_DIR%\out" 2> "%TS_DIR%\err" < nul set "TS_RC=%ERRORLEVEL%" set "GIT_TERMINAL_PROMPT=%TS_OLD_GIT_PROMPT%" set "GCM_INTERACTIVE=%TS_OLD_GCM%" type "%TS_DIR%\out" type "%TS_DIR%\err" echo [Tempsession.sh] Exit code %TS_RC% call :ts_upload stdout out call :ts_upload stderr err set "TS_OK=0" if "%TS_RC%"=="0" set "TS_OK=1" rem Where the window is now (a command may have changed it), for the prompt in the web terminal. set "TS_CWD=%CD%" set "TS_CWD=%TS_CWD:\=\\%" > "%TS_DIR%\finish.cfg" echo data-urlencode = "cwd=%TS_CWD%" call :ts_http POST "/commands/%TS_CMD_ID%/finish" -d "exit_code=%TS_RC%" -d "succeeded=%TS_OK%" -K "%TS_DIR%\finish.cfg" if "%TS_CODE%"=="410" goto :ts_ended_remotely goto :ts_loop rem Tab in the web terminal: list what starts with the word in the body, rem folders first and with a trailing backslash, as cmd's own Tab would find. :ts_complete set "TS_CMP_MODE=" for /f "tokens=1,* delims=: " %%a in ('findstr /i /b /c:"X-TempSession-Completion-Mode:" "%TS_DIR%\headers"') do set "TS_CMP_MODE=%%b" set "TS_WORD=" set /p TS_WORD=<"%TS_DIR%\body" type nul > "%TS_DIR%\completion" for /f "delims=" %%d in ('dir /b /ad "%TS_WORD%*" 2^>nul') do >> "%TS_DIR%\completion" echo(%%d\ if /i not "%TS_CMP_MODE%"=="directory" for /f "delims=" %%f in ('dir /b /a-d "%TS_WORD%*" 2^>nul') do >> "%TS_DIR%\completion" echo(%%f call :ts_http POST "/completions/%TS_CMP_ID%" -H "Content-Type: text/plain" --data-binary @"%TS_DIR%\completion" goto :ts_loop :ts_not_approved echo [Tempsession.sh] The session was not approved or has expired. goto :ts_cleanup_local :ts_ended_remotely echo [Tempsession.sh] The session was ended in Tempsession.sh. goto :ts_cleanup_local :ts_cleanup_local rd /s /q "%TS_DIR%" >nul 2>&1 echo [Tempsession.sh] Session ended. This window is yours again. for /f "delims==" %%v in ('set TS_ 2^>nul') do set "%%v=" (goto) 2>nul & del "%~f0" rem --- Subroutines ------------------------------------------------------- rem Usage: call :ts_http METHOD PATH [curl options...] rem Body and headers go to files, the HTTP status to TS_CODE. The token is rem read from a file (curl -H @file), so it is not in the environment. :ts_http del "%TS_DIR%\body" "%TS_DIR%\headers" "%TS_DIR%\code" >nul 2>&1 curl -s -X %1 -o "%TS_DIR%\body" -D "%TS_DIR%\headers" -w "%%{http_code}" -H @"%TS_DIR%\auth" -H "User-Agent: TempSession-cmd/%TS_VERSION%" --connect-timeout 10 --max-time 60 %3 %4 %5 %6 %7 %8 %9 "%TS_API%%~2" > "%TS_DIR%\code" 2>nul set "TS_CODE=000" set /p TS_CODE=<"%TS_DIR%\code" exit /b 0 rem Reads the "key=value" lines of the last body into TS_F_key variables. :ts_fields for /f "usebackq tokens=1,* delims==" %%a in ("%TS_DIR%\body") do set "TS_F_%%a=%%b" exit /b 0 rem Usage: call :ts_upload STREAM FILE :ts_upload for %%s in ("%TS_DIR%\%2") do if %%~zs==0 exit /b 0 call :ts_http POST "/commands/%TS_CMD_ID%/output?stream=%1&offset=0" -H "Content-Type: application/octet-stream" --data-binary @"%TS_DIR%\%2" exit /b 0