From 295697a1bf168fd9f29b3da3f83faa3d9eeab9a0 Mon Sep 17 00:00:00 2001 From: Andrei Jiroh Eugenio Halili Date: Sun, 25 Apr 2021 16:09:34 +0800 Subject: [PATCH] bin: Update import-pgp-keys + create cloudflarectl Just in case flarectl is browoken in Termux on some Android devices KEK. Signed-off-by: Andrei Jiroh Eugenio Halili --- bin/cloudflarectl | 59 +++++++++++++++++++++++++++++++++++++++++++++ bin/import-pgp-keys | 8 +++++- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100755 bin/cloudflarectl diff --git a/bin/cloudflarectl b/bin/cloudflarectl new file mode 100755 index 0000000..7922221 --- /dev/null +++ b/bin/cloudflarectl @@ -0,0 +1,59 @@ +#!/bin/bash + +HTTP_CLIENT_PATH=$(command -v curl) + +if [[ $HTTP_CLIENT_PATH == "" ]]; then + echo "Please install cURL first as this script may not work in wGET." + exit 1 +fi + +if command -v jq >> /dev/null; then + echo "OK" >> /dev/null +else + echo "Please install jq to prettify JSON responses from API." + exit 1 +fi + +# API Handler +apiHandler() { + CF_HOST="https://api.cloudflare.com/client/v4" + RESPONSE_FORMAT="application/json" + API_METHOD=${2:GET} + curl -H "Content-Type:$RESPONSE_FORMAT" \ + -H "Authorization: Bearer $CF_API_KEY" \ + -X "$API_METHOD" "$CF_HOST/$1" +} + +if [[ $# == "0" ]] || [[ $1 == "help" ]]; then + echo "Usage: $0 COMMAND ARGS" + echo "Available commands:" + echo "validate-token Check if an token is still working" + echo "zones Manage your Cloudflare zones" +else + if [[ $@ == "validate-token" ]]; then + if [[ $CF_API_EMAIL != "" ]]; then + echo "Unsupported variable found: CF_API_EMAIL" + echo "Please use API keys instead of global API token." + exit 1 + fi + if [[ $CF_API_KEY != "" ]]; then + apiHandler user/tokens/verify GET | jq + else + echo "No token found! Set your API token with:" + echo " export CF_API_KEY=abcdef123456" + fi + elif [[ $@ == "zones" ]]; then + echo "Usage: $0 zones [ARGS]" + echo + echo "Manage and list your zones managed by Cloudflare." + echo "Available arguments:" + echo "list List available zones to manage" + echo "add Add new zone" + echo "remove Remove an zone" + exit + elif [[ $@ == "zones list" ]]; then + apiHandler zones GET | jq .result + else + echo "Unsupported command! See the help command for details" + fi +fi diff --git a/bin/import-pgp-keys b/bin/import-pgp-keys index 27eeabd..783abe1 100755 --- a/bin/import-pgp-keys +++ b/bin/import-pgp-keys @@ -4,5 +4,11 @@ if [ "$#" == "0" ]; then echo "Usage: $0 your-pgp-key-name-here" echo "If the script says the file isn't found, make sure" echo "you placed it on your DOTFILES_HOME/secrets/pgp directory" - echo "( + exit fi + +PGP_KEY_EXPORT_FILE="$HOME/.dotfiles/secrets/pgp/$1.pgp.sec" +echo "info: Export file: ~/.dotfiles/secrets/pgp/$1.pgp.sec" +PASSPHRASE_BASE64=$(cat ~/.dotfiles/secrets/pgp/passphrase-$PGP_KEY_EXPORT_FILE) +echo "info: Base64'd file contents: $PASSPHRASE_BASE64" +echo $PASSPHARSE_BASE64 | base64 -d