Created
September 1, 2020 20:24
-
-
Save lord-alfred/8ae23c6bcb4b2d8422c4f9dc428770a7 to your computer and use it in GitHub Desktop.
Установка массового редиректа на другой домен с помощью Page Rules на CloudFlare
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Домены нужно положить рядом в файл domains.txt, без http(s) и www, просто хостнейм с доменной зоной (yandex.com) | |
# Запускать сие чудо: bash cf_mass_redirect.sh | |
# Мыло аккаунта на CloudFlare | |
export CF_API_EMAIL='[email protected]' | |
# Global API KEY для аккаунта, отсюда: https://dash.cloudflare.com/profile/api-tokens | |
export CF_API_KEY='abc12345672349234923942394cba' | |
# URL с http(s) куда будет редирект (не домен!) | |
export REDIRECT_URL='https://google.com/' | |
if [ "$(python3 -c 'print(True)')" != 'True' ]; then | |
echo 'python v3 not installed?' | |
exit 1 | |
fi | |
while IFS= read -r DOMAIN; do | |
ZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=${DOMAIN}" \ | |
-H "X-Auth-Email: ${CF_API_EMAIL}" \ | |
-H "X-Auth-Key: ${CF_API_KEY}" \ | |
-H "Content-Type: application/json" | python3 -c "import json; import sys; resp = json.load(sys.stdin); print(resp['result'][0]['id'])") | |
if [ ${#ZONE_ID} != 32 ] ; then | |
echo "Wrong zone_id for domain ${DOMAIN}: ${ZONE_ID}" | |
exit 1 | |
fi | |
RULE='{"targets":[{"target":"url","constraint":{"operator":"matches","value":"' | |
RULE+="${DOMAIN}/*" | |
RULE+='"}}],"actions":[{"id":"forwarding_url","value":{"url":"' | |
RULE+="${REDIRECT_URL}" | |
RULE+='","status_code":301}}],"priority":1,"status":"active"}' | |
RULE_RESP=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/pagerules" \ | |
-H "X-Auth-Email: ${CF_API_EMAIL}" \ | |
-H "X-Auth-Key: ${CF_API_KEY}" \ | |
-H "Content-Type: application/json" \ | |
--data "${RULE}") | |
RULE_RESP_STATUS=$(echo "${RULE_RESP}" | python3 -c "import json; import sys; resp = json.load(sys.stdin); print(resp['success'])") | |
if [ "${RULE_RESP_STATUS}" != 'True' ]; then | |
echo "Wrong create rule response for domain ${DOMAIN}: ${RULE_RESP}" | |
exit 1 | |
fi | |
CACHE_RESP=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/purge_cache" \ | |
-H "X-Auth-Email: ${CF_API_EMAIL}" \ | |
-H "X-Auth-Key: ${CF_API_KEY}" \ | |
-H "Content-Type: application/json" \ | |
--data '{"purge_everything":true}') | |
CACHE_RESP_STATUS=$(echo "${CACHE_RESP}" | python3 -c "import json; import sys; resp = json.load(sys.stdin); print(resp['success'])") | |
if [ "${CACHE_RESP_STATUS}" != 'True' ]; then | |
echo "Wrong purge cache response for domain ${DOMAIN}: ${CACHE_RESP}" | |
exit 1 | |
fi | |
echo "Done: ${DOMAIN}" | |
sleep 2 | |
done < <(grep "" domains.txt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment