Skip to content

Instantly share code, notes, and snippets.

@Roman-Sky
Last active March 11, 2024 04:10
Show Gist options
  • Save Roman-Sky/87f9a9927d65b5d8097a0bac5a018124 to your computer and use it in GitHub Desktop.
Save Roman-Sky/87f9a9927d65b5d8097a0bac5a018124 to your computer and use it in GitHub Desktop.
Get Let's Encrypt Wildcard-certificate by performing DNS Challenge using Reg.ru API
# 1. Allow access from server's IP to reg.ru API: https://www.reg.ru/user/prefs/security
# 2. Set your username and password to reg.ru account in reg_ru_add.sh and reg_ru_delete.sh
apt-get install -y jq
certbot certonly --server "https://acme-v02.api.letsencrypt.org/directory" \
-n --agree-tos --manual-public-ip-logging-ok --preferred-challenges dns --manual \
-d "example.com" -d "*.example.com" -d "*.abc.example.com" \
--manual-auth-hook /root/reg_ru/reg_ru_add.sh \
--manual-cleanup-hook /root/reg_ru/reg_ru_delete.sh \
--deploy-hook "service nginx reload"
#! /bin/bash
USERNAME=username@mail.com
PASSWORD=password
DNAME=$CERTBOT_DOMAIN
SUBDOMAIN=_acme-challenge
RESULT=
ERROR_CODE=
function remove_quotes {
echo $(sed -e 's/^"//' -e 's/"$//' <<<"$1")
}
function add_txt {
RESPONSE=$(curl -s -X POST \
-d "username=$USERNAME&password=$PASSWORD&dname=$DNAME&subdomain=$SUBDOMAIN&text=$CERTBOT_VALIDATION&output_content_type=json" \
https://api.reg.ru/api/regru2/zone/add_txt)
echo $RESPONSE
RESULT=$(remove_quotes $(echo $RESPONSE | jq '.answer.domains[0].result'))
ERROR_CODE=$(remove_quotes $(echo $RESPONSE | jq ".answer.domains[0].error_code"))
DNAME=$(remove_quotes $(echo $RESPONSE | jq '.answer.domains[0].dname'))
SUBDOMAIN=$SUBDOMAIN.$(cut -d'.' -f1 <<< "$DNAME")
DNAME=$(echo ${DNAME#*.})
}
add_txt
while [[ $RESULT = 'error' && $ERROR_CODE = 'DOMAIN_NOT_FOUND' ]]; do
add_txt
done
# Sleep to make sure the change has time to propagate over to DNS
sleep 30
#! /bin/bash
USERNAME=username@mail.com
PASSWORD=password
DNAME=$CERTBOT_DOMAIN
SUBDOMAIN=_acme-challenge
RESULT=
ERROR_CODE=
function remove_quotes {
echo $(sed -e 's/^"//' -e 's/"$//' <<<"$1")
}
function delete_txt {
RESPONSE=$(curl -s -X POST \
-d "username=$USERNAME&password=$PASSWORD&dname=$DNAME&subdomain=$SUBDOMAIN&record_type=TXT&content=$CERTBOT_VALIDATION&output_content_type=json" \
https://api.reg.ru/api/regru2/zone/remove_record)
echo $RESPONSE
RESULT=$(remove_quotes $(echo $RESPONSE | jq '.answer.domains[0].result'))
ERROR_CODE=$(remove_quotes $(echo $RESPONSE | jq ".answer.domains[0].error_code"))
DNAME=$(remove_quotes $(echo $RESPONSE | jq '.answer.domains[0].dname'))
SUBDOMAIN=$SUBDOMAIN.$(cut -d'.' -f1 <<< "$DNAME")
DNAME=$(echo ${DNAME#*.})
}
delete_txt
while [[ $RESULT = 'error' && $ERROR_CODE = 'DOMAIN_NOT_FOUND' ]]; do
delete_txt
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment