Skip to content

Commit

Permalink
Merge pull request #13 from jmrplens/BUG/Fix_minor_bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrplens committed Jun 18, 2023
2 parents 758c89d + d8a4c48 commit 2e9fe57
Showing 1 changed file with 54 additions and 81 deletions.
135 changes: 54 additions & 81 deletions update-cloudflare-records.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ dns_record_ip6=""
dns_record_id_6=""
is_proxied4=""
is_proxied6=""
is_proxiable4=""
is_proxiable6=""

# Colors
end_color=$(tput sgr0)
Expand Down Expand Up @@ -68,14 +70,14 @@ get_domain_settings() {
domain_ip_type=${def_ip_type}
fi
if [ $def_ipv4_enabled == false ]; then
domain_ipv4=${domains__ipv4[$i]}
enable_ipv4=${domains__ipv4[$i]}
else
domain_ipv4=${def_ipv4}
enable_ipv4=${def_ipv4}
fi
if [ $def_ipv6_enabled == false ]; then
domain_ipv6=${domains__ipv6[$i]}
enable_ipv6=${domains__ipv6[$i]}
else
domain_ipv6=${def_ipv6}
enable_ipv6=${def_ipv6}
fi
if [ $def_proxied_enabled == false ]; then
domain_proxied=${domains__proxied[$i]}
Expand All @@ -87,7 +89,7 @@ get_domain_settings() {
else
domain_ttl=${def_ttl}
fi
echo "$domain_name $domain_ip_type $domain_ipv4 $domain_ipv6 $domain_proxied $domain_ttl"
echo "$domain_name $domain_ip_type $enable_ipv4 $enable_ipv6 $domain_proxied $domain_ttl"
}
# shellcheck disable=SC2154
get_api_settings() {
Expand All @@ -98,7 +100,8 @@ get_api_settings() {
##################################################################
# CLOUDFLARE API FUNCTIONS
api_validation() {
if [[ "$1" == *"\"success\":false"* ]]; then
success_api=$(echo "${1}" | grep -o '"success":[^,]*' | grep -o '[^:]*$')
if [ "$success_api" == false ]; then
error_msg "Error! Can't get ${end_color}${load_c}$2${end_color}${done_c} $3 record information from Cloudflare API"
error_dom=true
else
Expand All @@ -107,7 +110,8 @@ api_validation() {
fi
}
push_validation() {
if [[ $1 == *"\"success\":false"* ]]; then
success_api=$(echo "${1}" | grep -o '"success":[^,]*' | grep -o '[^:]*$')
if [ "$success_api" == false ]; then
error_msg "Error! Update $2 Failed"
error_dom=true
else
Expand Down Expand Up @@ -198,81 +202,50 @@ get_ip_internal() {
}

get_ip() {
local ip_type=$1
local enable_ipv4=$2
local enable_ipv6=$3

# Get External ip from '$get_ip_from'
if [ "$1" == "external" ]; then
read -r ip4 ip6 < <(get_ip_external "$2" "$3")
external_validation "$ip4" "IPv4"
external_validation "$ip6" "IPv6"
if [ "$ip_type" == "external" ]; then
read -r ip4 ip6 <<< "$(get_ip_external "$enable_ipv4" "$enable_ipv6")"
[[ "$enable_ipv4" == true ]] && (external_validation "$ip4" "IPv4")
[[ "$enable_ipv6" == true ]] && (external_validation "$ip6" "IPv6")
printf "\n"
fi

### Get Internal IP from primary interface
if [ "$1" == "internal" ]; then
read -r ip4 ip6 interface < <(get_ip_internal "$2" "$3")
internal_validation "$ip4" "IPv4" "$interface"
internal_validation "$ip6" "IPv6" "$interface"
if [ "$ip_type" == "internal" ]; then
read -r ip4 ip6 interface <<< "$(get_ip_internal "$2" "$3")"
[[ "$enable_ipv4" == true ]] && (internal_validation "$ip4" "IPv4" "$interface")
[[ "$enable_ipv6" == true ]] && (internal_validation "$ip6" "IPv6" "$interface")
printf "\n"
fi
}

get_dns_record_ip() {
local domain_name=$1
local proxied=$2
local zone_id=$3
local zone_token=$4

### Get IP address of DNS record from 1.1.1.1 DNS server when proxied is "false"
if [ "${proxied}" == "false" ]; then
### Check if "nslookup" command is present
if which nslookup >/dev/null; then
if [ "${domain_ipv4}" == true ]; then
dns_record_ip4=$(nslookup -query=A "${domain_name}" 1.1.1.1 | awk '/Address/ { print $2 }' | sed -n '2p')
fi
if [ "${domain_ipv6}" == true ]; then
dns_record_ip6=$(nslookup -query=AAAA "${domain_name}" 1.1.1.1 | awk '/Address/ { print $2 }' | sed -n '2p')
fi
else
### if no "nslookup" command use "host" command
if [ "${domain_ipv4}" == true ]; then
dns_record_ip4=$(host -t A "${domain_name}" 1.1.1.1 | awk '/has address/ { print $4 }' | sed -n '1p')
fi
if [ "${domain_ipv6}" == true ]; then
dns_record_ip6=$(host -t AAAA "${domain_name}" 1.1.1.1 | awk '/has address/ { print $4 }' | sed -n '1p')
fi
fi

if [ "${domain_ipv4}" == true ]; then
if [ -z "${dns_record_ip4}" ]; then
echo "Error! Can't resolve the ${domain_name} IPv4 via 1.1.1.1 DNS server"
error_dom=true
fi
fi
if [ "${domain_ipv6}" == true ]; then
if [ -z "${dns_record_ip6}" ]; then
echo "Error! Can't resolve the ${domain_name} IPv6 via 1.1.1.1 DNS server"
error_dom=true
fi
fi
is_proxied4="${proxied}"
is_proxied6="${proxied}"
local zone_id=$2
local zone_token=$3

### Get the dns record id and current proxy status from cloudflare's api
if [ "${enable_ipv4}" == true ]; then
dns_record_info4=$(read_record "$zone_id" "$zone_token" "A" "$domain_name")
api_validation "$dns_record_info4" "$domain_name" "IPv4"
is_proxied4=$(echo "${dns_record_info4}" | grep -o '"proxied":[^,]*' | grep -o '[^:]*$')
is_proxiable4=$(echo "${dns_record_info4}" | grep -o '"proxiable":[^,]*' | grep -o '[^:]*$')
dns_record_ip4=$(echo "${dns_record_info4}" | grep -o '"content":"[^"]*' | cut -d'"' -f 4)
dns_record_id_4=$(echo "${dns_record_info4}" | grep -o '"id":"[^"]*' | cut -d'"' -f4)
[[ "$is_proxiable4" == false ]] && domain_proxied=false
fi

### Get the dns record id and current proxy status from cloudflare's api when proxied is "true"
if [ "${proxied}" == "true" ]; then
if [ "${domain_ipv4}" == true ]; then
dns_record_info4=$(read_record "$zone_id" "$zone_token" "A" "$domain_name")
api_validation "$dns_record_info4" "$domain_name" "IPv4"
is_proxied4=$(echo "${dns_record_info4}" | grep -o '"proxied":[^,]*' | grep -o '[^:]*$')
dns_record_ip4=$(echo "${dns_record_info4}" | grep -o '"content":"[^"]*' | cut -d'"' -f 4)
dns_record_id_4=$(echo "${dns_record_info4}" | grep -o '"id":"[^"]*' | cut -d'"' -f4)
fi
if [ "${domain_ipv6}" == true ]; then
dns_record_info6=$(read_record "$zone_id" "$zone_token" "AAAA" "$domain_name")
is_proxied6=$(echo "${dns_record_info6}" | grep -o '"proxied":[^,]*' | grep -o '[^:]*$')
dns_record_ip6=$(echo "${dns_record_info6}" | grep -o '"content":"[^"]*' | cut -d'"' -f 4)
dns_record_id_6=$(echo "${dns_record_info6}" | grep -o '"id":"[^"]*' | cut -d'"' -f4)
api_validation "$dns_record_info6" "$domain_name" "IPv6"
fi
if [ "${enable_ipv6}" == true ]; then
dns_record_info6=$(read_record "$zone_id" "$zone_token" "AAAA" "$domain_name")
api_validation "$dns_record_info6" "$domain_name" "IPv6"
is_proxied6=$(echo "${dns_record_info6}" | grep -o '"proxied":[^,]*' | grep -o '[^:]*$')
is_proxiable6=$(echo "${dns_record_info6}" | grep -o '"proxiable":[^,]*' | grep -o '[^:]*$')
dns_record_ip6=$(echo "${dns_record_info6}" | grep -o '"content":"[^"]*' | cut -d'"' -f 4)
dns_record_id_6=$(echo "${dns_record_info6}" | grep -o '"id":"[^"]*' | cut -d'"' -f4)
[[ "$is_proxiable6" == false ]] && domain_proxied=false
fi
}

Expand Down Expand Up @@ -440,7 +413,7 @@ printf "\n"

#################################
# Locate .yaml config file or set custom config file
read -r config_file < <(settings_file_validation "$1")
read -r config_file <<< "$(settings_file_validation "$1")"

#################################
# Create variables from .yaml config file
Expand All @@ -453,26 +426,26 @@ settings_domains_validation

#################################
# Cloudflare API
read -r api_zone_id api_zone_token < <(get_api_settings)
read -r api_zone_id api_zone_token <<< "$(get_api_settings)"

#################################
# Iterate over domains array
n_doms=${#domains__name[@]}
for ((i = 0; i < n_doms; i++)); do

### Get ith-domain settings
read -r domain_name domain_ip_type domain_ipv4 domain_ipv6 domain_proxied domain_ttl < <(get_domain_settings "$i")
read -r domain_name domain_ip_type enable_ipv4 enable_ipv6 domain_proxied domain_ttl <<< "$(get_domain_settings "$i")"
[[ "${domain_ttl}" == "auto" ]] && ttl=1 || ttl=${domain_ttl}

### Get Host IP (external or internal)
get_ip "$domain_ip_type" "$domain_ipv4" "$domain_ipv6"
### Get Host IP (external or internal) [VARS: ip4 and ip6]
get_ip "$domain_ip_type" "$enable_ipv4" "$enable_ipv6"

### Get IP Domain from DNS (proxied=false) or from Cloudflare API (proxied=true)
get_dns_record_ip "$domain_name" "$domain_proxied" "$api_zone_id" "$api_zone_token"
### Get IP Domain from Cloudflare API
get_dns_record_ip "$domain_name" "$api_zone_id" "$api_zone_token"
[ $error_dom == true ] && continue

### Check if IPv4 or proxy have changed and update if needed
if [ "$domain_ipv4" == true ] && [ "${dns_record_ip4}" == "${ip4}" ] && [ "${is_proxied4}" == "${domain_proxied}" ]; then
if [ "$enable_ipv4" == true ] && [ "${dns_record_ip4}" == "${ip4}" ] && [ "${is_proxied4}" == "${domain_proxied}" ]; then
up_to_date "IPv4" "$domain_name" "$dns_record_ip4" "$is_proxied4"
else
### Push new dns record information to cloudflare's api
Expand All @@ -482,7 +455,7 @@ for ((i = 0; i < n_doms; i++)); do
fi

### Check if IPv6 or proxy have changed and update if needed
if [ "$domain_ipv6" == true ] && [ "${dns_record_ip6}" == "${ip6}" ] && [ "${is_proxied6}" == "${domain_proxied}" ]; then
if [ "$enable_ipv6" == true ] && [ "${dns_record_ip6}" == "${ip6}" ] && [ "${is_proxied6}" == "${domain_proxied}" ]; then
up_to_date "IPv6" "$domain_name" "$dns_record_ip6" "$is_proxied6"
else
### Push new dns record information to cloudflare's api
Expand All @@ -502,8 +475,8 @@ for ((i = 0; i < n_doms; i++)); do
green_msg "----------------------------------------"
done_fb_msg "DOMAIN: $domain_name"
green_msg "----------------------------------------"
green_msg "Current IPv4 Address: $ip4"
green_msg "Current IPv6 Address: $ip6"
[[ "$enable_ipv4" == true ]] && green_msg "Current IPv4 Address: $ip4"
[[ "$enable_ipv6" == true ]] && green_msg "Current IPv6 Address: $ip6"
green_msg "Cloudflare proxy : $domain_proxied"
green_msg "TTL : $ttl_info"
green_msg "----------------------------------------"
Expand Down

0 comments on commit 2e9fe57

Please sign in to comment.