From efb7fae61a1b02e973a68f12a2178fb15a31f51a Mon Sep 17 00:00:00 2001 From: Eng Acs Date: Sat, 16 Nov 2024 19:00:36 +0300 Subject: [PATCH 1/4] added 20i.com DNS --- dnsapi/dns_20i.sh | 125 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 dnsapi/dns_20i.sh diff --git a/dnsapi/dns_20i.sh b/dnsapi/dns_20i.sh new file mode 100644 index 00000000..e745463f --- /dev/null +++ b/dnsapi/dns_20i.sh @@ -0,0 +1,125 @@ +#!/bin/bash + +# Helper function: Perform a GET request +_get() { + local url=$1 + curl -s -H "Authorization: Bearer $U20I_Bearer" "$url" +} + +# Helper function: Perform a POST request +_post() { + local data=$1 + local url=$2 + curl -s -X POST -H "Authorization: Bearer $U20I_Bearer" -H "Content-Type: application/json" -d "$data" "$url" +} + +# Add a TXT record to 20i DNS +dns_20i_add() { + local fulldomain=$1 + local txtvalue=$2 + + if [ -z "$U20I_Bearer" ]; then + echo "Error: U20I_Bearer must be set in the environment." + return 1 + fi + + echo "Adding TXT record to 20i for domain: $fulldomain" + + # Extract root domain and subdomain + local domain=$(echo "$fulldomain" | sed -r 's/^[^\.]+\.(.+)$/\1/') + local subdomain="_acme-challenge" + + # Check if the TXT record already exists + local dns_response=$(_get "https://api.20i.com/domain/$domain/dns") + if echo "$dns_response" | jq -e '.error' > /dev/null 2>&1; then + local error_message=$(echo "$dns_response" | jq -r '.error.message') + echo "Error retrieving DNS records: $error_message" + return 1 + fi + + if echo "$dns_response" | jq -e ".records[] | select(.type == \"TXT\" and .host == \"$fulldomain\" and .txt == \"$txtvalue\")" > /dev/null 2>&1; then + echo "TXT record already exists. Skipping addition." + return 0 + fi + + # Construct payload for adding the TXT record + local payload=$(cat < /dev/null 2>&1; then + local error_message=$(echo "$add_response" | jq -r '.error.message') + echo "Error adding TXT record: $error_message" + return 1 + fi + + echo "TXT record added successfully." + return 0 +} + +# Remove a TXT record from 20i DNS +dns_20i_rm() { + local fulldomain=$1 + local txtvalue=$2 + + if [ -z "$U20I_Bearer" ]; then + echo "Error: U20I_Bearer must be set in the environment." + return 1 + fi + + echo "Removing TXT record from 20i for domain: $fulldomain" + + # Extract root domain and subdomain + local domain=$(echo "$fulldomain" | sed -r 's/^[^\.]+\.(.+)$/\1/') + + # Get existing DNS records + local dns_response=$(_get "https://api.20i.com/domain/$domain/dns") + if echo "$dns_response" | jq -e '.error' > /dev/null 2>&1; then + local error_message=$(echo "$dns_response" | jq -r '.error.message') + echo "Error retrieving DNS records: $error_message" + return 1 + fi + + # Find the record to delete by matching TXT record + local record_ref=$(echo "$dns_response" | jq -r \ + ".records[] | select(.type == \"TXT\" and .host == \"$fulldomain\" and .txt == \"$txtvalue\") | .ref") + + if [ -z "$record_ref" ] || [ "$record_ref" == "null" ]; then + echo "No matching TXT record found for removal." + return 1 + fi + + # Construct payload for deleting the TXT record + local payload=$(cat < /dev/null 2>&1; then + local error_message=$(echo "$remove_response" | jq -r '.error.message') + echo "Error removing TXT record: $error_message" + return 1 + fi + + echo "TXT record removed successfully." + return 0 +} From 63078470c2b7e0352c30e5665868228b72f74556 Mon Sep 17 00:00:00 2001 From: Eng Acs Date: Sat, 16 Nov 2024 20:45:50 +0300 Subject: [PATCH 2/4] Enhance 20i DNS helper for acme.sh - Added CLI argument parsing (`--bearer`, `--debug`). - Improved error handling and debugging support. - Fixed incorrect success messages on API errors. - Streamlined 20i API integration for managing TXT records. --- dnsapi/dns_20i.sh | 49 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/dnsapi/dns_20i.sh b/dnsapi/dns_20i.sh index e745463f..0505e43a 100644 --- a/dnsapi/dns_20i.sh +++ b/dnsapi/dns_20i.sh @@ -3,14 +3,31 @@ # Helper function: Perform a GET request _get() { local url=$1 - curl -s -H "Authorization: Bearer $U20I_Bearer" "$url" + local Bearer=$2 + curl -s -H "Authorization: Bearer $Bearer" "$url" } # Helper function: Perform a POST request _post() { local data=$1 local url=$2 - curl -s -X POST -H "Authorization: Bearer $U20I_Bearer" -H "Content-Type: application/json" -d "$data" "$url" + local Bearer=$3 + curl -s -X POST -H "Authorization: Bearer $Bearer" -H "Content-Type: application/json" -d "$data" "$url" +} + + +# Helper function to parse arguments +_parse_arguments() { + for arg in "$@"; do + case "$arg" in + --bearer=*) + _bearer="${arg#*=}" + ;; + --debug) + debug=true + ;; + esac + done } # Add a TXT record to 20i DNS @@ -18,8 +35,14 @@ dns_20i_add() { local fulldomain=$1 local txtvalue=$2 - if [ -z "$U20I_Bearer" ]; then - echo "Error: U20I_Bearer must be set in the environment." + # Parse arguments for bearer token or other credentials + _parse_arguments "$@" + + # Use the passed bearer token or fallback to environment variable + local bearer="${_bearer:-$U20I_Bearer}" + + if [ -z "$bearer" ]; then + echo "Error: Bearer token must be provided using --bearer or U20I_Bearer environment variable." return 1 fi @@ -30,7 +53,7 @@ dns_20i_add() { local subdomain="_acme-challenge" # Check if the TXT record already exists - local dns_response=$(_get "https://api.20i.com/domain/$domain/dns") + local dns_response=$(_get "https://api.20i.com/domain/$domain/dns" $bearer) if echo "$dns_response" | jq -e '.error' > /dev/null 2>&1; then local error_message=$(echo "$dns_response" | jq -r '.error.message') echo "Error retrieving DNS records: $error_message" @@ -58,7 +81,7 @@ EOF ) # Make API request to add the TXT record - local add_response=$(_post "$payload" "https://api.20i.com/domain/$domain/dns") + local add_response=$(_post "$payload" "https://api.20i.com/domain/$domain/dns" $bearer) if echo "$add_response" | jq -e '.error' > /dev/null 2>&1; then local error_message=$(echo "$add_response" | jq -r '.error.message') echo "Error adding TXT record: $error_message" @@ -74,8 +97,14 @@ dns_20i_rm() { local fulldomain=$1 local txtvalue=$2 - if [ -z "$U20I_Bearer" ]; then - echo "Error: U20I_Bearer must be set in the environment." + # Parse arguments for bearer token or other credentials + _parse_arguments "$@" + + # Use the passed bearer token or fallback to environment variable + local bearer="${_bearer:-$U20I_Bearer}" + + if [ -z "$bearer" ]; then + echo "Error: Bearer token must be provided using --bearer or U20I_Bearer environment variable." return 1 fi @@ -85,7 +114,7 @@ dns_20i_rm() { local domain=$(echo "$fulldomain" | sed -r 's/^[^\.]+\.(.+)$/\1/') # Get existing DNS records - local dns_response=$(_get "https://api.20i.com/domain/$domain/dns") + local dns_response=$(_get "https://api.20i.com/domain/$domain/dns" $bearer) if echo "$dns_response" | jq -e '.error' > /dev/null 2>&1; then local error_message=$(echo "$dns_response" | jq -r '.error.message') echo "Error retrieving DNS records: $error_message" @@ -113,7 +142,7 @@ EOF ) # Make API request to remove the TXT record - local remove_response=$(_post "$payload" "https://api.20i.com/domain/$domain/dns") + local remove_response=$(_post "$payload" "https://api.20i.com/domain/$domain/dns" $bearer) if echo "$remove_response" | jq -e '.error' > /dev/null 2>&1; then local error_message=$(echo "$remove_response" | jq -r '.error.message') echo "Error removing TXT record: $error_message" From ca63b8a502993e3fcfd62284dfd65c88626cd0f3 Mon Sep 17 00:00:00 2001 From: Eng Acs Date: Sat, 16 Nov 2024 21:12:04 +0300 Subject: [PATCH 3/4] feat: Add support for --bearer argument in acme.sh for DNS authentication - Added `--bearer` argument parsing in the CLI. - Integrated `_bearer` token support in DNS functions (e.g., dns_20i_add). - Ensures bearer tokens can be passed as arguments or environment variables. - Updated usage and debug outputs for clarity. --- acme.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/acme.sh b/acme.sh index 9842e3f1..8b7faa4e 100755 --- a/acme.sh +++ b/acme.sh @@ -7577,6 +7577,14 @@ _process() { _webroot="$_webroot,$wvalue" fi ;; + --bearer) + bearer_value="" + if [ "$2" ] && ! _startswith "$2" "-"; then + bearer_value="$2" + shift + fi + _bearer="$bearer_value" + ;; --dnssleep) _dnssleep="$2" Le_DNSSleep="$_dnssleep" From ea1f45746e35179990e3366f64dc3cc2d4542ef8 Mon Sep 17 00:00:00 2001 From: Eng Acs Date: Sat, 16 Nov 2024 21:24:30 +0300 Subject: [PATCH 4/4] fixed --- dnsapi/dns_20i.sh | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/dnsapi/dns_20i.sh b/dnsapi/dns_20i.sh index 0505e43a..aff661a8 100644 --- a/dnsapi/dns_20i.sh +++ b/dnsapi/dns_20i.sh @@ -16,20 +16,6 @@ _post() { } -# Helper function to parse arguments -_parse_arguments() { - for arg in "$@"; do - case "$arg" in - --bearer=*) - _bearer="${arg#*=}" - ;; - --debug) - debug=true - ;; - esac - done -} - # Add a TXT record to 20i DNS dns_20i_add() { local fulldomain=$1