shithub: riscv

ref: bd178b6dc6a6d6a81be05c0a1f7c03b352f833c1
dir: /rc/bin/rconnect/

View raw version
#!/bin/rc
rfork e

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

fn pvar {
	while(! ~ $#* 0){
		~ $#$1 0 || 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 *; 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 $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')
				. <{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 -c $host >[1=0]} /bin/rc -c 'sendscript $*' $*
	}
	exec $argv0 -k $"keyspec $host <{builtin whatis aanserver; 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 $*' $*