diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/.sample.env b/.sample.env new file mode 100644 index 0000000..ef18e10 --- /dev/null +++ b/.sample.env @@ -0,0 +1,4 @@ +EMAIL="my@mail.com" +KEY="my_api_key" # found in cloudflare account settings +ZONE="example.org" +DNS_RECORD="some.example.org" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..553e871 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,7 @@ +version: '3' + +services: + updater: + build: . + restart: always + env_file: .env \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..235d80e --- /dev/null +++ b/dockerfile @@ -0,0 +1,12 @@ +FROM alpine:3.8 + +ENV file /usr/local/bin/run.sh + +RUN apk add --no-cache --update curl grep bash + +RUN echo '* * * * * ${file}' > /etc/crontabs/root + +COPY ./run.sh ${file} +RUN chmod +x ${file} + +CMD ["crond", "-l2", "-f"] \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..004cd86 --- /dev/null +++ b/run.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# CHANGE THESE +auth_email=${EMAIL} +auth_key=${KEY} +zone_name=${ZONE} +record_name=${DNS_RECORD} + +# MAYBE CHANGE THESE +ip=$(curl -s http://ipv4.icanhazip.com) +ip_file="ip.txt" +id_file="cloudflare.ids" +log_file="cloudflare.log" + +# LOGGER +log() { + if [ "$1" ]; then + echo -e "[$(date)] - $1" >> $log_file + fi +} + +# SCRIPT START +log "Check Initiated" + +if [ -f $ip_file ]; then + old_ip=$(cat $ip_file) + if [ $ip == $old_ip ]; then + echo "IP has not changed." + exit 0 + fi +fi + +if [ -f $id_file ] && [[ $(wc -l $id_file | cut -d " " -f 1) == 2 ]]; then + zone_identifier=$(head -1 $id_file) + record_identifier=$(tail -1 $id_file) +else + zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 ) + record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*') + echo "$zone_identifier" > $id_file + echo "$record_identifier" >> $id_file +fi + +echo "Updating $zone_identifier, $record_identifier" + +update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}") + +if [[ $update == *"\"success\":false"* ]]; then + message="API UPDATE FAILED. DUMPING RESULTS:\n$update" + log "$message" + echo -e "$message" + exit 1 +else + message="IP changed to: $ip" + echo "$ip" > $ip_file + log "$message" + echo "$message" +fi