-
Notifications
You must be signed in to change notification settings - Fork 1
/
vultr_ddns.lua
executable file
·145 lines (121 loc) · 3.56 KB
/
vultr_ddns.lua
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/lua
-- For use with OpenWrt/LEDE since them support LUA.
-- DynDNS implementation for Vultr DNS service via API
-- The original idea was taken from https://github.com/nileshgr/utilities/blob/master/general/updateip.lua
-- You can put this script in interface hotplug or crontab.
-- Prerequisites:
-- luasec
-- luasocket
-- libubus-lua
-- json4lua
require("os")
require("socket")
require("ltn12")
require("ubus")
json = require("json")
https = require("ssl.https")
apiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
recordName = "homerouter"
domainName = "domain.tld"
domainFound = false
requestMethod = "POST"
requestURI = "https://api.vultr.com/v2/domains/" .. domainName .. "/records"
recordTemplate = {
name = recordName,
data = "127.0.0.1",
type = "A",
ttl = "300",
priority = 0
}
requestHeaders = {
["Authorization"] = "Bearer " .. apiKey,
["Content-Type"] = "application/json",
}
remoteIP = {}
domainList = {}
dnsRecords = {}
function log(msg)
os.execute("logger -t dyndns '" .. msg .. "'")
end
u = ubus.connect()
if not u then
log("Ubus connect failed")
os.exit(1)
end
status = u:call("network.interface.wan", "status", {})
wanIP = status["ipv4-address"][1]["address"]
remoteIPResponse = https.request({
url = "https://ifconfig.me/all.json",
sink = ltn12.sink.table(remoteIP),
method = "GET",
headers = requestHeaders,
})
if remoteIPResponse then
local detectedIP = json.decode(remoteIP[1]).ip_addr
if remoteIP and wanIP ~= detectedIP then
log("We are behind NAT: " .. wanIP .. " <-> " .. detectedIP )
wanIP = detectedIP
end
end
apiResponse = https.request({
url = "https://api.vultr.com/v2/domains",
method = "GET",
headers = requestHeaders,
sink = ltn12.sink.table(domainList)
})
if not apiResponse then
log("Failed to fetch domains list")
os.exit(1)
end
domainList = json.decode(domainList[1]).domains
for _, key in ipairs(domainList) do
if key.domain == domainName then
domainFound = true
break
end
end
if not domainFound then
log("Domain " .. domainName .. " has not been found")
os.exit(1)
end
apiResponse = https.request({
url = "https://api.vultr.com/v2/domains/" .. domainName .. "/records" ,
method = "GET",
headers = requestHeaders,
sink = ltn12.sink.table(dnsRecords)
})
if not apiResponse then
log("Failed to fetch DNS records for " .. domainName .. " domain")
os.exit(1)
end
dnsRecords = json.decode(dnsRecords[1]).records
for _, record in ipairs(dnsRecords) do
if record.name == recordName and record.data == wanIP then
log("Record update is not needed: " .. wanIP )
os.exit(0)
elseif record.name == recordName then
requestMethod = "PATCH"
requestURI = requestURI .. "/" .. record.id
recordTemplate["data"] = wanIP
break
else
recordTemplate["data"] = wanIP
end
end
recordTemplate = json.encode(recordTemplate)
requestHeaders["Content-Length"] = string.len(recordTemplate)
apiResponse = https.request({
url = requestURI,
method = requestMethod,
headers = requestHeaders,
source = ltn12.source.string(recordTemplate)
})
if not apiResponse then
log("Failed to update record " .. recordName .. "." .. domainName .. " [" .. wanIP .. "]")
else
if requestMethod == "POST" then
log("Record has been created: " .. recordName .. "." .. domainName .. " [" .. wanIP .. "]")
else
log("Record has been updated: " .. recordName .. "." .. domainName .. " [" .. wanIP .. "]")
end
end