From 6bfbdbf35365174903c305ec4b254c2523413ca6 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Tue, 29 Mar 2022 16:50:45 -0500 Subject: refactor: moved scripts into wsl package --- wsl/.local/bin/discord-relay.sh | 2 + wsl/.local/bin/gpg-test.sh | 102 +++++++++++++++++++ wsl/.local/bin/mkln.sh | 115 +++++++++++++++++++++ wsl/.local/bin/socket-relay.sh | 217 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 436 insertions(+) create mode 100755 wsl/.local/bin/discord-relay.sh create mode 100644 wsl/.local/bin/gpg-test.sh create mode 100755 wsl/.local/bin/mkln.sh create mode 100755 wsl/.local/bin/socket-relay.sh (limited to 'wsl/.local/bin') diff --git a/wsl/.local/bin/discord-relay.sh b/wsl/.local/bin/discord-relay.sh new file mode 100755 index 0000000..3932908 --- /dev/null +++ b/wsl/.local/bin/discord-relay.sh @@ -0,0 +1,2 @@ +#!/bin/sh +exec socat UNIX-LISTEN:/var/run/discord-ipc-0,fork,group=discord,umask=007 EXEC:"npiperelay.exe -ep -s //./pipe/discord-ipc-0",nofork >/dev/null 2>&1 diff --git a/wsl/.local/bin/gpg-test.sh b/wsl/.local/bin/gpg-test.sh new file mode 100644 index 0000000..f23b83d --- /dev/null +++ b/wsl/.local/bin/gpg-test.sh @@ -0,0 +1,102 @@ +#!/usr/bin/env bash + +TEMP=$(getopt -o hvdsea: --long help,verbose,debug,signature,encryption,authentication \ + -n 'javawrap' -- "$@") + +if [ $? != 0 ]; then + echo "Terminating..." >&2 + exit 1 +fi + +eval set -- "$TEMP" + +usage() { + cat <$temp/test.txt + + result="$(gpg --output $temp/test.gpg -e $temp/test.txt 2>&1 && gpg --output $temp/test.out -d $temp/test.txt.gpg 2>&1)" + grep '42' $temp/test.out 2>&1 + print_result result +fi + +if [[ $ALL -eq "true" || $AUTHENTICATION -eq "true" ]]; then + echo "Testing authentication key..." + result="$(ssh-add -l)" + print_result result +fi diff --git a/wsl/.local/bin/mkln.sh b/wsl/.local/bin/mkln.sh new file mode 100755 index 0000000..ef174c2 --- /dev/null +++ b/wsl/.local/bin/mkln.sh @@ -0,0 +1,115 @@ +#!/usr/bin/env bash + +TEMP=$(getopt -o hvqdf --long help,verbose,quiet,debug,force \ + -n 'javawrap' -- "$@") + +if [ $? != 0 ]; then + echo "Terminating..." >&2 + exit 1 +fi + +eval set -- "$TEMP" + +SCRIPT="$(basename $0)" +VERBOSE=false +QUIET=false +DEBUG=false +FORCE=false + +read -r -d '' USAGE <<-EOF +USAGE: $SCRIPT [OPTIONS] + +OPTIONS: + -h, --help Show this message + -v, --verbose Show more output + -q, --quiet Suppress all output + -d, --debug NOT IMPLEMENTED + -f, --force Overwrite items + +ARGS: + ... File to link. +EOF + +while true; do + case "$1" in + -h | --help) + echo "$USAGE" + exit 0 + ;; + -v | --verbose) + VERBOSE=true + shift + ;; + -q | --quiet) + QUIET=true + shift + ;; + -d | --debug) + DEBUG=true + shift + ;; + -f | --force) + FORCE=true + shift + ;; + --) + shift + break + ;; + *) + break + ;; + esac +done + +if ! command -v powershell.exe &>/dev/null; then + [ "$QUIET" != true ] && echo "Powershell not found in path." >&2 + exit 1 +fi + +if [ -z "$2" ]; then + 2="$(pwd)" +fi + +[ "$VERBOSE" == true ] && echo "linking $2 -> $1" + +if [ -d "$1" ]; then + [ "$VERBOSE" == true ] && echo "$1 is a directory. Creating symbolic link." + args='/D' +fi + +mkdir -p $(dirname $2) + +source=$(wslpath -w $1) +target=$(wslpath -w $(dirname $2))\\$(basename $2) + +if ls -la "$(dirname $2)/" 2>/dev/null | grep -q "$(basename $2)"; then + + current_path=$(powershell.exe -c "(Get-Item $target).Target" 2>/dev/null) + + if [[ "${current_path/*wsl$/}" == *"${source/*wsl$/}"* ]]; then + [ "$VERBOSE" == true ] && echo "$(basename $2) is set correctly. Skipping." + exit 0 + fi + + if [ "$FORCE" == true ]; then + [ "$VERBOSE" == true ] && echo "$(basename $2) exists. Overwriting." + rm -rf "$2" + else + [ "$QUIET" != true ] && echo "$(basename $target) already exists. Use -f to overwrite." >&2 + exit 1 + fi +fi + +mkdir -p "$(dirname $2)" +cmd="cd ~; cmd /c mklink ${args} ${target} ${source}" + +if [ "$DEBUG" == true ]; then + [ "$QUIET" != true ] && printf '\nCommand: \n%s\n\n' "powershell.exe -c ${cmd} &>/dev/null" +else + if $QUIET; then + powershell.exe -c "${cmd}" &>/dev/null + else + powershell.exe -c "${cmd}" + fi +fi diff --git a/wsl/.local/bin/socket-relay.sh b/wsl/.local/bin/socket-relay.sh new file mode 100755 index 0000000..436fa54 --- /dev/null +++ b/wsl/.local/bin/socket-relay.sh @@ -0,0 +1,217 @@ +#!/usr/bin/env bash +set -euo pipefail + +OPTS=$(getopt -o hds:x: --long help,debug,sock:,exec:,ssh,gpg,gpg-extra,discord -n 'javawrap' -- "$@") + +eval set -- "$OPTS" + +PAGEANT="$HOME/.ssh/wsl2-ssh-pageant.exe" +NPIPE="$(command -v npiperelay.exe)" +SCRIPT="$(basename "$0")" + +help() { + cat <<-EOF +$SCRIPT +Toby Vincent + +$SCRIPT description + +USAGE: + $SCRIPT [OPTIONS] + $SCRIPT [OPTIONS] + +OPTIONS: + -h, --help Display this message + -s, --sock Path to the linux socket + -x, --exec Command to run for socat's EXEC: arg + --ssh Relay the ssh agent's socket + --gpg Relay the gpg agent's socket + --gpg-extra Relay the gpg agent's extra socket + +ARGS: + start (default) + Start the socat process + + stop + Kill the socat process +EOF +} + +say() { + if ! $quiet; then + echo "$SCRIPT: $1" + fi +} + +say_err() { + say "$1" >&2 +} + +err() { + say_err "ERROR: $1" + exit 1 +} + +need() { + for cmd in $@; do + if ! command -v $cmd >/dev/null 2>&1; then + err "need $cmd (command not found)" + fi + done +} + +start() { + if ! ss -a | grep -q "$sock"; then + if test -x "$relay"; then + rm -rf "$sock" + (setsid nohup socat UNIX-LISTEN:"${sock},${listen_args}" EXEC:"$cmd" >/dev/null 2>&1 &) + + else + echo >&2 "WARNING: $relay is not executable." + fi + fi + + if $gpg; then + gpg-connect-agent.exe /bye &>/dev/null + fi +} + +stop() { + if $gpg; then + gpg-connect-agent.exe KILLAGENT /bye &>/dev/null + fi + + if pgrep -f "socat.*$sock.*$relay" &>/dev/null; then + pkill -f "socat.*$sock.*$relay" + fi +} + +check() { + pgrep -fa "socat.*$sock.*$relay" +} + +quiet=false +debug=false +sock="" +cmd="" +gpg=false +ssh=false +extra=false +discord=false +while test $# -gt 0; do + case $1 in + -h | --help) + help + return 0 + ;; + -q | --quiet) + quiet=true + shift + ;; + -d | --debug) + debug=true + shift + ;; + -s | --sock) + sock=$2 + shift + shift + ;; + -x | --exec) + cmd=$2 + shift + shift + ;; + --ssh) + ssh=true + shift + ;; + --gpg) + gpg=true + shift + ;; + --gpg-extra) + gpg=true + extra=true + shift + ;; + --discord) + discord=true + shift + ;; + --) + shift + break + ;; + *) + help + exit 1 + ;; + esac +done + +need ss +need socat + +listen_args="fork" + +case true in +$ssh) + need $PAGEANT + + sock="${SSH_AUTH_SOCK:-$HOME/.ssh/agent.sock}" + cmd="$PAGEANT" + ;; +$gpg) + need $PAGEANT + need "gpg-connect-agent.exe" + + sock="${GPG_AGENT_SOCK:-$HOME/.gnupg/S.gpg-agent}" + if $extra; then + sock+=".extra" + fi + + cmd="$PAGEANT --gpg $(basename $sock)" + ;; +$discord) + need $NPIPE + + sock="${DISCORD_IPC_SOCK:-/var/run/discord-ipc-0}" + cmd="$NPIPE -ep -s //./pipe/$(basename $sock)" + + listen_args="fork,group=discord,umask=007" + ;; +$(test -z "${cmd}")) + err "No EXEC provided. Must supply either --ssh, --gpg, --gpg-extra, --pipe, or --exec" + ;; +esac + +if test -z "${sock}"; then + err "No socket provided." +fi + +relay="$(echo $cmd | head -n1 | awk '{print $1;}')" + +case ${1:-'start'} in +check) + shift + check + ;; +start) + shift + start + ;; +stop) + shift + stop + ;; +restart) + shift + stop + start + ;; +*) + help + exit 1 + ;; +esac -- cgit v1.2.3-70-g09d2