This commit is contained in:
nicco 2019-01-22 12:37:36 +01:00
parent b54e4bd144
commit 209e111712
5 changed files with 81 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.env

4
.sample.env Normal file
View File

@ -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"

7
docker-compose.yml Normal file
View File

@ -0,0 +1,7 @@
version: '3'
services:
updater:
build: .
restart: always
env_file: .env

12
dockerfile Normal file
View File

@ -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"]

57
run.sh Normal file
View File

@ -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