shithub: riscv

ref: 7d7e8ae31ac01434c7017afce4eb0a033281a757
dir: /rc/bin/rconnect/

View raw version
#!/bin/rc
rfork e

argv0=$0
aanto=86400 #one day
fn usage {
	echo 'usage:' $argv0 '[-u user] [-k keypattern] [-p] [-t timeout] host remotescript localcommand arg ...' >[1=2]
	exit 'usage'
}

fn pvar {
	while(! ~ $#* 0){
		~ $#$1 0 && echo $1'=()' ||
			path=/dev/null builtin whatis $1
		shift
	}
}

filter=()
keyspec=()
while(~ $1 -*){
	switch($1){
	case -p; filter=aan
	case *
		~ $#* 1 && usage
		switch($1){
		case -k; keyspec=($keyspec $2)
		case -u; keyspec=($keyspec `{user=$2 pvar user})
		case -t; aanto=$2
		case *; usage
		}
		shift
	}
	shift
}

~ $#* 0 1 2 && usage

host=$1
shift

fn sendscript {
	echo -n $host >/proc/$pid/args
	cat $1 >/env/v; wc -c </env/v; cat /env/v; rm /env/v
	shift
	$*
}

if(~ $filter aan){
	fn aanserver {
		~ $#netdir 1 || netdir=/net/tcp/clone
		netdir=`{basename -d $netdir} || exit
		<>$netdir/clone {
			netdir=$netdir/`{read} || exit
			>[3] $netdir/ctl {
				echo -n 'announce *!0' >[1=3]
				echo `{cat $netdir/local} || exit
				bind '#|' /mnt/aan || exit
				exec aan -m $aanto $netdir <>/mnt/aan/data1 >[1=0] >[2]/dev/null &
			}
		}
		<>/mnt/aan/data >[1=0] >[2]/dev/null {
			rfork n
			fn server {
				echo -n aanserver $netdir >/proc/$pid/args
				rm -f /env/^('fn#server' 'fn#aanserver' 'aanto')
				. <{n=`{read} && ! ~ $#n 0 && read -c $n} >[2=1]
			}
			exec tlssrv -A /bin/rc -c server
			exit
		}
	}
	fn aanclient {
		host=`{echo $host | sed 's/(.*)!([^!]+)$/\1/'}^!^`{read | sed 's/(.*)!([^!]+)$/\2/'} || exit
		tlsclient -a -o <>{rfork s; aan -m $aanto -c $host >[1=0]} /bin/rc -c 'sendscript $*' $*
	}
	exec $argv0 -k $"keyspec $host <{builtin whatis aanserver; pvar aanto; echo aanserver} aanclient $*
	exit
}

switch($host){
case *!*!*;
case *!*; host=$host!rcpu
case *; host=net!$host!rcpu
}

exec tlsclient -ak $"keyspec $host /bin/rc -c 'sendscript $*' $*