SHARE
TWEET

VULTR plugin for acme.sh

a guest Sep 18th, 2018 127 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env sh
  2.  
  3. # Ensure your environment variable contains your API key:
  4. # VULTR_API_KEY="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  5.  
  6. VULTR_API_BASEURL="https://api.vultr.com/v1"
  7.  
  8. ########  Public functions #####################
  9.  
  10. # add txt record to dns server
  11. dns_vultr_add() {
  12.   fulldomain=$1
  13.   txtvalue=$2
  14.   _info "Using VULTR"
  15.  
  16.   _debug "Checking VULTR_API_KEY environment variable is set"
  17.   if [ -z "$VULTR_API_KEY" ]; then
  18.     _err "Missing VULTR API key"
  19.     return 1
  20.   fi
  21.   _debug "VULTR API key found"
  22.  
  23.   _debug "First detect the root zone"
  24.   if ! _get_root "$fulldomain"; then
  25.     _err "Invalid domain"
  26.     return 1
  27.   fi
  28.  
  29.   # add our record to dns server
  30.   _info "Attempting to add TXT record to DNS server"
  31.   if ! _vultr_call_api POST "/dns/create_record" "domain=$_domain&name=$_sub_domain&type=TXT&ttl=300&data=\"$txtvalue\""; then
  32.     _err "API call failed: $response"
  33.     return 1
  34.   fi
  35.  
  36.   # make sure we don't hit api rate limits
  37.   sleep 1
  38.  
  39.   # check list of records for domain to see if we've added our txt record successfully
  40.   _debug "Verifying we've added our TXT record"
  41.   if ! _vultr_call_api GET "/dns/records?domain=$_domain"; then
  42.     _err "API call failed: $response"
  43.     return 1
  44.   fi
  45.   if [ $(echo $response | sed "s/},{/\n/g" | grep -iP "\"type\"\s*:\s*\"TXT\"" | grep -iP "\"name\"\s*:\s*\"$_sub_domain\"" | grep -iPc "\"data\"\s*:\s*\"\\\\\"$txtvalue\\\\\"\"") -ne 0 ]; then
  46.     _info "Successfully added TXT record: $fulldomain"
  47.     return 0
  48.   fi
  49.  
  50.   _err "Failed to create TXT record: $fulldomain"
  51.   return 1
  52. }
  53.  
  54. # remove txt record from dns server
  55. dns_vultr_rm() {
  56.   fulldomain=$1
  57.   txtvalue=$2
  58.   _info "Using VULTR"
  59.  
  60.   _debug "Checking VULTR_API_KEY environment variable is set"
  61.   if [ -z "$VULTR_API_KEY" ]; then
  62.     _err "Missing VULTR API key"
  63.     return 1
  64.   fi
  65.   _debug "VULTR API key found"
  66.  
  67.   _debug "First detect the root zone"
  68.   if ! _get_root "$fulldomain"; then
  69.     _err "Invalid domain"
  70.     return 1
  71.   fi
  72.   _debug "domain: $_domain"
  73.   _debug "subdomain: $_sub_domain"
  74.  
  75.   # get record id of TXT record matching $_sub_domain
  76.   _debug "Searching for existing TXT record"
  77.   if ! _vultr_call_api GET "/dns/records?domain=$_domain"; then
  78.     _err "API call failed: $response"
  79.     return 1
  80.   fi
  81.   _record_id=$(echo "$response" | sed "s/},{/\n/g" | grep -iP "\"type\"\s*:\s*\"TXT\"" | grep -iP "\"name\"\s*:\s*\"$_sub_domain\"" | grep -iP "\"data\"\s*:\s*\"\\\\\"$txtvalue\\\\\"\"" | grep -oP "\"RECORDID\"\s*:\s*\K\d+")
  82.   if [ -z "$_record_id" ]; then
  83.     _err "Unable to find existing TXT record"
  84.     return 1
  85.   fi
  86.  
  87.   # use record id to delete the existing TXT record
  88.   _info "Attempting to delete TXT record from DNS server"
  89.   if ! _vultr_call_api POST "/dns/delete_record" "domain=$_domain&RECORDID=$_record_id"; then
  90.     _err "API call failed: $response"
  91.     return 1
  92.   fi
  93.  
  94.   # make sure we don't hit api rate limits
  95.   sleep 1
  96.  
  97.   # check list of records for domain to see if we've successfully removed our temporary txt record
  98.   _debug "Verifying deletion of our TXT record"
  99.   if ! _vultr_call_api GET "/dns/records?domain=$_domain"; then
  100.     _err "API call failed: $response"
  101.     return 1
  102.   fi
  103.   if [ $(echo $response | sed "s/},{/\n/g" | grep -iP "\"type\"\s*:\s*\"TXT\"" | grep -iP "\"name\"\s*:\s*\"$_sub_domain\"" | grep -icP "\"data\"\s*:\s*\"\\\\\"$txtvalue\\\\\"\"") -ne 0 ]; then
  104.     _err "Failed to remove TXT record: $fulldomain"
  105.     return 1
  106.   fi
  107.  
  108.   _info "Successfully removed TXT record: $fulldomain"
  109.   return 0
  110. }
  111.  
  112. ####################  Private functions below ##################################
  113.  
  114. # get domain and subdomain parts from full domain
  115. #  
  116. # param
  117. #   domain=_acme-challenge.www.domain.com
  118. # returns
  119. #   _sub_domain=_acme-challenge.www
  120. #   _domain=domain.com
  121. _get_root() {
  122.   domain=$1
  123.  
  124.   # get list of domains hosted by the dns server
  125.   _debug "Getting list of existing domains"
  126.   if ! _vultr_call_api GET "/dns/list"; then
  127.     _err "API failure: Could not get list of domains"
  128.     return 1
  129.   fi;
  130.  
  131.   if [ -z "$response" ]; then
  132.     _err "No domains hosted on DNS server"
  133.     return 1
  134.   fi;
  135.  
  136.   # parse response to get an array of domain names
  137.   response=$(echo "$response" | grep -oP '"domain"\s*:\s*"\K[^"]+')
  138.   read -r -a hosted_domains <<< $response
  139.  
  140.   # compare different parts of fulldomain with the list of domains hosted by dns server
  141.   i=2
  142.   p=1
  143.   while true; do
  144.     h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  145.     # no more parts left
  146.     if [ -z "$h" ]; then
  147.       return 1
  148.     fi
  149.    
  150.     for dom in "${hosted_domains[@]}"; do
  151.       if [ $dom == $h ]; then
  152.         _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  153.         _domain=$h
  154.         return 0
  155.       fi
  156.     done
  157.    
  158.     p=$i
  159.     i=$(_math "$i" + 1)
  160.   done
  161.   return 1
  162. }
  163.  
  164. # main vultr api method
  165. #
  166. # param
  167. #   method=POST
  168. #   call=/dns/create_record
  169. #   data=var1="val1"&var2="va12"
  170. # method api_call data
  171. #   returns
  172. #     response
  173. _vultr_call_api() {
  174.   method=$1
  175.   api_call="$2"
  176.   data="$3"
  177.   _debug "API call: $api_call"
  178.  
  179.   _vultr_url="$VULTR_API_BASEURL$api_call"
  180.   _debug2 _vultr_url "$_vultr_url"
  181.   _secure_debug VULTR_API_KEY "$VULTR_API_KEY"
  182.  
  183.   # auth
  184.   export _H1="API-Key: $VULTR_API_KEY"
  185.   unset _H2 _H3 _H4 _H5
  186.   if [ "$data" ] || [ "$method" = "POST" ]; then
  187.     _debug data "$data"
  188.     response="$(_post "$data" "$_vultr_url" "" "$method")"
  189.   else
  190.     response="$(_get "$_vultr_url")"
  191.   fi
  192.  
  193.   if [ "$?" != "0" ] || _contains "$response" "Invalid API key"; then
  194.     _err "Error: $response"
  195.     return 1
  196.   fi
  197.   _debug2 response "$response"
  198.   return 0
  199. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top