File: //bigscoots/wpo/cloudflare/turnstiles.sh
#!/bin/bash
CLOUDFLARE_EMAIL=""
CLOUDFLARE_APIKEY=""
DOMAIN=""
ACTION=""
SITEKEY=""
WIDGET_NAME=""
SITE_KEY=""
SECRET_KEY=""
MODE=""
DOMAINS=""
print_usage() {
echo "Usage: $0 --action ACTION --domain DOMAIN [--email EMAIL] [--apikey APIKEY] [--sitekey SITEKEY] [--widget-name WIDGET_NAME] [--site-key SITE_KEY] [--secret-key SECRET_KEY] [--mode MODE] [--domains DOMAINS]"
echo "Actions: list, create, delete, get, update"
echo ""
echo "Examples:"
echo " List Widgets:"
echo " $0 --action list --email \"[email protected]\" --apikey \"your_api_key\" --domain \"example.com\""
echo ""
echo " Create Widget:"
echo " $0 --action create --email \"[email protected]\" --apikey \"your_api_key\" --domain \"example.com\" --widget-name \"MyWidget\" --mode \"non-interactive\" --domains \"example.com\""
echo ""
echo " Delete Widget:"
echo " $0 --action delete --email \"[email protected]\" --apikey \"your_api_key\" --domain \"example.com\" --sitekey \"0x4AAAAAAAdzKjSQ3ekG8QCd\""
echo ""
echo " Get Widget:"
echo " $0 --action get --email \"[email protected]\" --apikey \"your_api_key\" --domain \"example.com\" --sitekey \"0x4AAAAAAAdzKjSQ3ekG8QCd\""
echo ""
echo " Update Widget:"
echo " $0 --action update --email \"[email protected]\" --apikey \"your_api_key\" --domain \"example.com\" --sitekey \"0x4AAAAAAAdzKjSQ3ekG8QCd\" --widget-name \"UpdatedWidgetName\" --mode \"interactive\" --site-key \"site_key_here\" --secret-key \"secret_key_here\" --domains \"example.com,anotherdomain.com\""
}
while [[ "$#" -gt 0 ]]; do
case $1 in
--email) CLOUDFLARE_EMAIL="$2"; shift ;;
--apikey) CLOUDFLARE_APIKEY="$2"; shift ;;
--domain) DOMAIN="$2"; shift ;;
--action) ACTION="$2"; shift ;;
--sitekey) SITEKEY="$2"; shift ;;
--widget-name) WIDGET_NAME="$2"; shift ;;
--site-key) SITE_KEY="$2"; shift ;;
--secret-key) SECRET_KEY="$2"; shift ;;
--mode) MODE="$2"; shift ;;
--domains) DOMAINS="$2"; shift ;;
*) print_usage; exit 1 ;;
esac
shift
done
if [[ -z "$CLOUDFLARE_EMAIL" || -z "$CLOUDFLARE_APIKEY" || -z "$DOMAIN" || -z "$ACTION" ]]; then
print_usage
exit 1
fi
BASE_URL="https://api.cloudflare.com/client/v4/accounts"
# Function to get account ID
get_account_id() {
curl -s -X GET "https://api.cloudflare.com/client/v4/accounts" \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_APIKEY" | jq -r '.result[0].id'
}
ACCOUNT_ID=$(get_account_id)
if [[ -z "$ACCOUNT_ID" ]]; then
echo "Unable to find account ID for domain: $DOMAIN"
exit 1
fi
# Function to list turnstile widgets
list_widgets() {
curl -s -X GET "$BASE_URL/$ACCOUNT_ID/challenges/widgets" \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_APIKEY"
}
# Function to create a turnstile widget
create_widget() {
curl -s -X POST "$BASE_URL/$ACCOUNT_ID/challenges/widgets" \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_APIKEY" \
-H "Content-Type: application/json" \
--data '{
"name": "'"$WIDGET_NAME"'",
"mode": "'"$MODE"'",
"domains": ["'"$DOMAINS"'"]
}'
}
# Function to delete a turnstile widget
delete_widget() {
curl -s -X DELETE "$BASE_URL/$ACCOUNT_ID/challenges/widgets/$SITEKEY" \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_APIKEY"
}
# Function to get a turnstile widget
get_widget() {
curl -s -X GET "$BASE_URL/$ACCOUNT_ID/challenges/widgets/$SITEKEY" \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_APIKEY"
}
# Function to update a turnstile widget
update_widget() {
curl -s -X PUT "$BASE_URL/$ACCOUNT_ID/challenges/widgets/$SITEKEY" \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_APIKEY" \
-H "Content-Type: application/json" \
--data '{"name":"'"$WIDGET_NAME"'","mode":"'"$MODE"'","site_key":"'"$SITE_KEY"'","secret_key":"'"$SECRET_KEY"'","domains":"'"$DOMAINS"'"}'
}
case $ACTION in
list)
list_widgets
;;
create)
if [[ -z "$WIDGET_NAME" || -z "$MODE" || -z "$DOMAINS" ]]; then
echo "Widget name, mode, and domains are required for creation."
exit 1
fi
create_widget
;;
delete)
if [[ -z "$SITEKEY" ]]; then
echo "Site key is required for deletion."
exit 1
fi
delete_widget
;;
get)
if [[ -z "$SITEKEY" ]]; then
echo "Site key is required for getting details."
exit 1
fi
get_widget
;;
update)
if [[ -z "$SITEKEY" || -z "$WIDGET_NAME" || -z "$MODE" || -z "$DOMAINS" ]]; then
echo "Site key, Widget Name, mode, and domains are required for updating."
exit 1
fi
update_widget
;;
*)
print_usage
exit 1
;;
esac