feat(cache): implement agent cache

This commit is contained in:
zawz 2022-03-21 16:02:42 +01:00
parent fa9be31e5c
commit 75ba7b58bf
8 changed files with 84 additions and 10 deletions

10
default.conf Normal file
View file

@ -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

10
script.sh Normal file
View file

@ -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
<?xml version="1.0"?>
<a:propfind xmlns:a="DAV:">
<a:prop><a:resourcetype/></a:prop>
</a:propfind>
EOF
} | xmllint --xpath "$1" -

35
src/agent.sh Normal file
View file

@ -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)"
}

View file

@ -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

View file

@ -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}

View file

@ -5,6 +5,7 @@ usage()
{
echo "$fname [options] <operation>
[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

View file

@ -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 ;;

View file

@ -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