make a client

This commit is contained in:
Dmitriy Tillyaev 2021-08-07 07:25:45 +05:00
parent 8ed01eb6a9
commit 1dd1169794
2 changed files with 112 additions and 8 deletions

51
tstream
View file

@ -1,7 +1,7 @@
#!/bin/sh
# Kimapr's streaming setup
# how to use:
# install ncat, lua (tested with 5.4.3), and script
# install tmux, ncat, lua (tested with 5.4.3), and script
# mkdir ~/stream
# make a text file ~/stream/welcome, it will be displayed on stream startup.
# make a text file ~/stream/the_end, it will be displayed on stream end.
@ -10,6 +10,25 @@
# end the stream by exitting from the shell in the stream window
# you can also replay past streams using `scriptreplay -O live -T live-timing` in ~/stream/hist/*/
echo "checking dependencies..."
if ! tmux -V; then
echo "tmux not installed!"
exit 1
fi
if ! ncat --version; then
echo "ncat not installed!"
exit 1
fi
if ! lua -v; then
echo "lua not installed!"
exit 1
fi
if ! script --version; then
echo "script not installed!"
exit 1
fi
echo "all tests pass!"
echo "usage: tstream <port> <width> <height> [max bandwidth in KiB/s (per-client)]"
port=$1
width=$2
@ -18,15 +37,20 @@ bandwidth=$4
if [ "x$bandwidth" = "x" ]; then
bandwidth=32
fi
randid=`head -c 16 /dev/urandom | xxd -p`
unset TMUX
randid=`head -c 4 /dev/urandom | xxd -p`
name=`date -u +%Y-%m-%d_%H-%M-%S_UTC`
tmux new-session -d -s wstream_$randid "clear;cat ~/stream/welcome;$SHELL"
tmux resize-window -t wstream_$randid -x $width -y $(($height - 1))
tmux new-session -d -s wstream_$randid "tmux set status-left-length 100;clear;cat ~/stream/welcome;$SHELL"
tmux resize-window -t wstream_$randid -x $(($width)) -y $(($height - 1))
tmux set-option -t wstream_$randid status-right "? viewers"
cat >/tmp/tstream-helper_$randid <<EOF
#!/bin/sh
tmux set status off
#tmux unbind -a
tmux set prefix None
clear
TMUX= tmux attach -t wstream_$randid
sleep 2
clear
cat ~/stream/the_end
for i in \$(seq 7); do
@ -149,13 +173,23 @@ chmod +x /tmp/tstream-smarttail_$randid
cat >/tmp/tstream-cat_$randid <<EOF
#!/bin/sh
vcrefresh='$vcrefresh'
echo "\$vcrefresh"
{ sleep 5;/tmp/tstream-refresh_$randid; } &
echo "Welcome to Kimapr's stream. Please wait..."
echo "SIZE $width $height"
echo "# Welcome to a text livestream."
echo "# Powered by tstream: https://gitlab.com/Kimapr/tstream"
echo "# Before you continue, make sure your terminal is exactly ${width}x$height sized, otherwise rendering errors would occur."
echo "# Here's one way to do it: disconnect, run the following command"
echo "# $ tmux new \"tmux resize-window -x $width -y $height; tmux set status off; \\\$SHELL\""
echo "# After executing the above command, a ${width}x$height sized terminal window would appear in your terminal. Then you can connect to the server the same way as you did before."
echo "# Alternatively, if your terminal supports dynamic resizing you can just resize your terminal to the right size, without disconnecting."
echo "# There's also a custom client that does it for you: https://gitlab.com/Kimapr/tstream/-/raw/master/tstream-client"
echo "# Press Return/Enter to continue..."
head -n 1;
{ sleep 0.5;/tmp/tstream-refresh_$randid; } &
echo \$((\$(cat /tmp/tstream-count_$randid) + 1)) > /tmp/tstream-count_$randid
sh -c "\$vcrefresh"
/tmp/tstream-smarttail_$randid
echo \$((\$(cat /tmp/tstream-count_$randid) - 1)) > /tmp/tstream-count_$randid
sleep 1
sh -c "\$vcrefresh"
EOF
chmod +x /tmp/tstream-cat_$randid
@ -163,6 +197,7 @@ chmod +x /tmp/tstream-refresh_$randid
touch /tmp/tstream-seektimes_$randid
{
while true; do
tmux set -t wstream_$randid status-left "`date +%H:%M` "
sleep 5
echo "$(lua -e "local file=io.open(\"$HOME/stream/hist/$name/live\")print(file:seek(\"end\")..\" \"..os.time())file:close()")" >>/tmp/tstream-seektimes_$randid
/tmp/tstream-refresh_$randid
@ -175,7 +210,7 @@ tmux kill-session -t wstream_$randid
tmux kill-session -t stream_$randid
touch /tmp/tstream-death_$randid
echo PLEASE WAIT...
sleep 2;
sleep 1;
rm /tmp/tstream-death_$randid
rm /tmp/tstream-helper_$randid
rm /tmp/tstream-refresh_$randid

69
tstream-client Executable file
View file

@ -0,0 +1,69 @@
#!/bin/sh
if ! tmux -V 2>/dev/null >/dev/null; then
echo "tmux not installed!"
exit 1
fi
if ! lua -v 2>/dev/null >/dev/null; then
echo "lua not installed!"
exit 1
fi
if ! nc -h 2>/dev/null >/dev/null; then
echo "nc not installed!"
exit 1
fi
RANDID=$(cat /dev/urandom | head -c 4 | xxd -p)
TMPDIR=/tmp/tstream_client_$RANDID
mkdir $TMPDIR
mkfifo $TMPDIR/datout
mkfifo $TMPDIR/nc_in
mkfifo $TMPDIR/nc_out
mkfifo $TMPDIR/arg_width
mkfifo $TMPDIR/arg_height
cat >$TMPDIR/ncread <<EOF
#!/usr/bin/env lua
local nc_out=io.open("$TMPDIR/nc_out")
local nc_in=io.open("$TMPDIR/nc_in","w")
local function wrfile(name,str)
local file=io.open("$TMPDIR/"..name,"w")
file:write(tostring(str))
file:close()
end
while true do
local line=nc_out:read()
if not line then
wrfile("arg_width","40")
wrfile("arg_height","10")
wrfile("datout","error occured\n")
error("what!")
end
if line:match("SIZE .+") then
local w,h=line:match("SIZE (.-) (.+)")
w,h=tonumber(w),tonumber(h)
wrfile("arg_width",w)
wrfile("arg_height",h)
break
end
end
nc_in:write("\n")
nc_in:flush()
local datout=io.open("$TMPDIR/datout","w")
while true do
datout:write(nc_out:read(1))
datout:flush()
end
EOF
chmod +x $TMPDIR/ncread
$TMPDIR/ncread &
{ cat $TMPDIR/nc_in | nc $@ | tee $TMPDIR/nc_out > /dev/null; } &
echo run
WIDTH=$(head -n 1 $TMPDIR/arg_width)
echo width $WIDTH
HEIGHT=$(head -n 1 $TMPDIR/arg_height)
echo height $HEIGHT
tmux new "tmux resize-window -x $WIDTH -y $HEIGHT;tmux set status off; cat $TMPDIR/datout"
jobs
kill %1
kill %2
rm -rf $TMPDIR
wait