From 75ba7b58bf65409eba023e179be97d042ab08432 Mon Sep 17 00:00:00 2001 From: zawz Date: Mon, 21 Mar 2022 16:02:42 +0100 Subject: [PATCH] feat(cache): implement agent cache --- default.conf | 10 ++++++++++ script.sh | 10 ++++++++++ src/agent.sh | 35 +++++++++++++++++++++++++++++++++++ src/cache.sh | 26 +++++++++++++++++++++----- src/config.sh | 8 ++++---- src/help.sh | 2 ++ src/main.sh | 1 + src/util.sh | 2 +- 8 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 default.conf create mode 100644 script.sh create mode 100644 src/agent.sh diff --git a/default.conf b/default.conf new file mode 100644 index 0000000..4e676c2 --- /dev/null +++ b/default.conf @@ -0,0 +1,10 @@ +ZPASS_KEY_CACHE_TIME=300 +ZPASS_CLIPBOARD_TIME=30 +ZPASS_PRIORITIZE_CLI=true +ZPASS_COPY_ON_EDIT=true + +ZPASS_PATH=remote.php/dav/files/zawz/zpass +ZPASS_REMOTE_METHOD=webdav +ZPASS_REMOTE_ADDR=nextcloud.zawz.net +ZPASS_REMOTE_USER=zawz +ZPASS_REMOTE_PASSWORD=8C9Hd-TMdkg-683cQ-HHfqB-okTj2 diff --git a/script.sh b/script.sh new file mode 100644 index 0000000..cf346b8 --- /dev/null +++ b/script.sh @@ -0,0 +1,10 @@ + +{ + curl -s --user zawz:8C9Hd-TMdkg-683cQ-HHfqB-okTj2 -X PROPFIND --upload-file - -H 'Depth: 1' https://nextcloud.zawz.net/remote.php/dav/files/zawz/zpass/ << EOF + + + + +EOF + +} | xmllint --xpath "$1" - diff --git a/src/agent.sh b/src/agent.sh new file mode 100644 index 0000000..403301a --- /dev/null +++ b/src/agent.sh @@ -0,0 +1,35 @@ + +sockpath() { + echo "${1-$sockpath}" +} + +start_agent() { + redis-server --save "" --unixsocket "$(sockpath)" --unixsocketperm 700 +} + +# $1 = socket +redis_cli() { + redis-cli --raw -s "$1" +} + +escape() { + printf "%s\n" "$1" | sed 's/\"/\\\"/g' +} + +agent_cli() { + socket=$(sockpath) + op=$1 + shift + case "$op" in + set) + echo "set $1 \"$(escape "$2")\"" + echo "expire $1 $3" + ;; + get) + echo "get $1" + ;; + clear) + echo "FLUSHDB" + ;; + esac | redis_cli "$(sockpath)" +} diff --git a/src/cache.sh b/src/cache.sh index fc4131e..3e27dfa 100644 --- a/src/cache.sh +++ b/src/cache.sh @@ -5,18 +5,34 @@ get_filecache() { echo "$cachepath/$(filehash)$ZPASS_EXTENSION" } - clear_cache() { - rm "$cachepath"/* + rm -f "$cachepath"/* + if [ -S "$sockpath" ] ; then + agent_cli clear + fi } write_cache() { - echo "$1" > "$cachepath/$(keyfile)" - delete_cache "$ZPASS_KEY_CACHE_TIME" + if [ -S "$sockpath" ] ; then + agent_cli set "$(keyfile)" "$1" "$ZPASS_KEY_CACHE_TIME" >/dev/null + else + echo "$1" > "$cachepath/$(keyfile)" + delete_cache "$ZPASS_KEY_CACHE_TIME" + fi } get_key_cached() { - cat "$cachepath/$(keyfile)" 2>/dev/null + if [ -S "$sockpath" ] ; then + out=$(agent_cli get "$(keyfile)") + if [ "$out" != "" ] ; then + echo "$out" + return 0 + else + return 1 + fi + else + cat "$cachepath/$(keyfile)" 2>/dev/null + fi } # $1 = delay in sec diff --git a/src/config.sh b/src/config.sh index b052ec3..182d953 100644 --- a/src/config.sh +++ b/src/config.sh @@ -2,10 +2,9 @@ # XDG config/cache datapath=".local/share/zpass" -cachepath="$HOME/.cache/zpass" -configpath="$HOME/.config/zpass" -[ -n "$XDG_CONFIG_HOME" ] && configpath="$XDG_CONFIG_HOME/zpass" -[ -n "$XDG_CACHE_HOME" ] && cachepath="$XDG_CACHE_HOME/zpass" + +cachepath=${XDG_CACHE_HOME-$HOME/.cache}/zpass +configpath=${XDG_CONFIG_HOME-$HOME/.config}/zpass CONFIGFILE=${CONFIGFILE-$configpath/default.conf} [ -z "$TMPDIR" ] && TMPDIR=/tmp @@ -25,6 +24,7 @@ rm -f "$tmpenv" 2>/dev/null # resolve zpass_path datapath=${ZPASS_PATH-$datapath} cachepath=${ZPASS_CACHE_PATH-$cachepath} +sockpath=${ZPASS_CACHE_SOCK-$XDG_RUNTIME_DIR/zpass.socket} # default ZPASS ZPASS_FILE=${ZPASS_FILE-default} diff --git a/src/help.sh b/src/help.sh index 06bc8e3..7549162 100644 --- a/src/help.sh +++ b/src/help.sh @@ -5,6 +5,7 @@ usage() { echo "$fname [options] [Global Operations]: + agent Start caching agent, based on redis server list-files List eligible files in data path. Shortcut 'lsf' cache-clear Delete all cached keys. Shortcut 'cc' help Display help @@ -31,6 +32,7 @@ usage() CONFIGFILE '\$XDG_CONFIG_HOME/zpass/defaut.conf' Path to the config file to load ZPASS_PATH '\$XDG_DATA_HOME/zpass' Folder containing password files ZPASS_CACHE_PATH '\$XDG_CACHE_HOME/zpass' Path used for caching keys + ZPASS_CACHE_SOCK '\$XDG_RUNTIME_DIR/zpass.socket' Socket file for redis caching server ZPASS_FILE 'default' File to use for operations ZPASS_KEY Key to use for encrypting/decrypting files ZPASS_KEY_CACHE_TIME '60' Time a key stays in cache for decrypting, in seconds diff --git a/src/main.sh b/src/main.sh index 6928ad0..501b950 100755 --- a/src/main.sh +++ b/src/main.sh @@ -22,6 +22,7 @@ shift 1 case $arg in -h|h|help) usage && exit 1;; + agent) start_agent "$@" ;; lsf|list-files) list_files ;; rmf|rm-file) remove_files "$@" ;; cc|cache-clear) clear_cache 2>/dev/null ;; diff --git a/src/util.sh b/src/util.sh index aeccbb5..4ddc70b 100644 --- a/src/util.sh +++ b/src/util.sh @@ -5,7 +5,7 @@ error(){ } randalnum() { - tr -cd 'a-zA-Z' < /dev/urandom | head -c $1 + tr -cd 'a-zA-Z0-9' < /dev/urandom | head -c $1 } # $* = input