#!/bin/sh PORT=9090 PROCFILE=/tmp/.xrsh usage(){ echo "Usage: xrsh [--keyboard] [--kill]"; exit; } assert_cmds(){ for cmd in $1; do command -v $cmd &>/dev/null || { echo "error: please install $cmd (not found)"; exit 1; } done } showip(){ { # naive way of getting network ip command -v ip &>/dev/null && ip addr show command -v ifconfig &>/dev/null && ifconfig } | grep 'inet ' | grep -v ' 127' | awk '! /addr:/ { gsub(/\/.*/,"",$2); if( !SEEN[$2] ) print $2; SEEN[$2]=1 }' } setupcerts(){ test -f ~/.websocat/cert.pem && return 0 mkdir ~/.websocat 2>/dev/null || true echo "--- generating temporary SSL certs (press enter to all questions) ---" #openssl req -x509 -newkey rsa:4096 -keyout ~/.websocat/key.pem -out ~/.websocat/cert.pem -days 365 openssl req -x509 -newkey rsa:2048 -new -nodes -keyout ~/.websocat/key.pem -out ~/.websocat/cert.pem -days 3650 openssl pkcs12 -export -out ~/.websocat/q.pkcs12 -inkey ~/.websocat/key.pem -in ~/.websocat/cert.pem wget "https://curl.haxx.se/ca/cacert.pem" -O ~/.websocat/cacert.pem } keyboard(){ assert_cmds websocat pkill trap "stty sane; trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT # cleanup processes when exit echo $$ > $PROCFILE echo "" echo " 1. type 'k' (remote keyboard) in xrsh" echo " 2. enter ipaddress: $(showip)" echo "" echo "warn: CTRL-C is forwarded too" echo "warn: to exit this app run: $0 --kill" echo "warn: this terminal is now in raw sendmode, expect weird characters!" stty raw if test -n "$SSL"; then setupcerts websocat -E -b -s --pkcs12-der ~/.websocat/q.pkcs12 0.0.0.0:$PORT 2>&1 else websocat -E -b -s 0.0.0.0:$PORT 2>&1 # http-only fi } # launch! test -n "$1" || { echo "usage: xrsh [--ssl] [--keyboard] [--kill]"; exit; } for arg in $*; do case $arg in --kill) pkill -P $(cat $PROCFILE); exit ;; --ssl) export SSL=1; ;; --keyboard) keyboard "$@"; ;; esac done