forked from oznu/docker-cloudflare-ddns
/
cloudflare-setup
executable file
·94 lines (79 loc) · 3.27 KB
/
cloudflare-setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
. ./config
. ./cloudflare.sh
# Verify the $EMAIL and $API_KEY env variables are valid
echo $API_KEY
statusCode=$(verifyToken)
if [ "$statusCode" != "200" ]; then
echo "----------------------------------------------------------------"
echo "ERROR: Invalid CloudFlare Credentials - $statusCode"
echo "----------------------------------------------------------------"
if [ -z "$EMAIL" ]; then
echo "Make sure the API_KEY is correct. You can"
echo "get your scoped CloudFlare API Token here:"
echo "https://dash.cloudflare.com/profile/api-tokens"
else
echo "Make sure the EMAIL and API_KEY variables are correct. You can"
echo "get your CloudFlare API Key here:"
echo "https://www.cloudflare.com/a/profile"
echo
echo "If you are using a zone token please remove the EMAIL variable"
echo "from this container."
fi
echo
echo "----------------------------------------------------------------"
exit 1
fi
# Get the ID of the zone from the CloudFlare API
CFZoneId=$(getZoneId)
echo $CFZoneId
if [ "$CFZoneId" == "null" ]; then
echo "----------------------------------------------------------------"
echo "ERROR: Zone for $ZONE was not found in your CloudFlare Account"
echo "----------------------------------------------------------------"
echo "Make sure the ZONE variable is correct and the domain exists"
echo "in your CloudFlare account. You can add a new domain here:"
echo
echo "https://www.cloudflare.com/a/add-site"
echo "----------------------------------------------------------------"
exit 1
fi
echo "DNS Zone: $ZONE ($CFZoneId)"
# Check we can get the current public ip address
PublicIpAddress=$(getPublicIpAddress)
if [ ! -z ${INTERFACE+x} ]; then
PublicIpAddress=$(getLocalIpAddress)
fi
if [ "$PublicIpAddress" == "" ]; then
echo "----------------------------------------------------------------"
if [ "$RRTYPE" == "AAAA" ]; then
echo "ERROR: Failed To Get Public IPv6 Address"
echo "----------------------------------------------------------------"
echo "You must be running Docker with IPv6 support enabled or this"
echo "container with host networking enabled."
echo
echo "Add '--net=host' to your docker run command or if using"
echo "docker-compose add 'network_mode: host' to this service."
else
echo "ERROR: Failed To Get Public IPv4 Address"
fi
echo "----------------------------------------------------------------"
exit 1
fi
# Get the ID of the dns record from the CloudFlare API
CFDnsRecordName=$(getDnsRecordName)
CFDnsRecordId=$(getDnsRecordId $CFZoneId $CFDnsRecordName)
# If the dns record does not exist, create it now.
if [ "$CFDnsRecordId" == "null" ]; then
echo "DNS record for '$CFDnsRecordName' was not found in $ZONE zone. Creating now..."
CFDnsRecordId=$(createDnsRecord $CFZoneId $CFDnsRecordName $PublicIpAddress)
if [ "$CFDnsRecordId" == "null" ]; then
echo "----------------------------------------------------------------"
echo "ERROR: Failed to create DNS record '$CFDnsRecordName'"
echo "----------------------------------------------------------------"
exit 1
fi
fi
echo "DNS Record: $CFDnsRecordName ($CFDnsRecordId)"
echo "CF_ZONE_ID=\"$CFZoneId\" CF_RECORD_ID=\"$CFDnsRecordId\" CF_RECORD_NAME=\"$CFDnsRecordName\"" >cloudflare.conf
. ./ddns