-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dns-sync.sh
executable file
·103 lines (90 loc) · 2.54 KB
/
dns-sync.sh
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
95
96
97
98
99
100
101
102
103
#!/bin/bash
# Vercel Dynamic DNS
# https://github.com/iam-medvedev/vercel-ddns
source ./dns.config
# Check if jq is installed
if ! command -v jq >/dev/null; then
echo "Error: 'jq' is not installed. Please install 'jq' to run this script."
exit 1
fi
# Returns current IP
get_current_ip() {
local ip
ip=$(curl -s http://whatismyip.akamai.com/)
echo $ip
}
# Function to check if subdomain exists
check_subdomain_exists() {
local subdomain="$1"
local response
response=$(curl -sX GET "https://api.vercel.com/v4/domains/$DOMAIN_NAME/records" \
-H "Authorization: Bearer $VERCEL_TOKEN" \
-H "Content-Type: application/json")
local record_id
record_id=$(echo "$response" | jq -r ".records[] | select(.name == \"$subdomain\") | .id")
if [[ -n "$record_id" ]]; then
# Return record ID if exists
echo "$record_id"
else
# Subdomain does not exist
return 1
fi
}
# Updates dns record
update_dns_record() {
local ip="$1"
local record_id="$2"
local response
response=$(curl -sX PATCH "https://api.vercel.com/v1/domains/records/$record_id" \
-H "Authorization: Bearer $VERCEL_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"comment": "vercel-ddns",
"name": "'$SUBDOMAIN'",
"type": "A",
"value": "'$ip'",
"ttl": 60
}')
# Check for errors in the response
if [[ "$response" == *"error"* ]]; then
local error_message
error_message=$(echo "$response" | jq -r '.error.message')
echo "⚠️ Error updating DNS record: $error_message"
exit 1
fi
}
# Creates dns record
create_dns_record() {
local ip="$1"
local response
response=$(curl -sX POST "https://api.vercel.com/v4/domains/$DOMAIN_NAME/records" \
-H "Authorization: Bearer $VERCEL_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"comment": "vercel-ddns",
"name": "'$SUBDOMAIN'",
"type": "A",
"value": "'$ip'",
"ttl": 60
}')
# Check for errors in the response
if [[ "$response" == *"error"* ]]; then
local error_message
error_message=$(echo "$response" | jq -r '.error.message')
echo "⚠️ Error creating DNS record: $error_message"
exit 1
fi
}
# Get current IP
ip=$(get_current_ip)
echo "Updating IP: $ip"
# Check if subdomain exists
record_id=$(check_subdomain_exists "$SUBDOMAIN")
if [[ -n "$record_id" ]]; then
echo "Record for $SUBDOMAIN already exists (id: $record_id). Updating..."
update_dns_record "$ip" "$record_id"
else
echo "Record for $SUBDOMAIN does not exist. Creating..."
create_dns_record "$ip"
fi
echo "🎉 Done!"