-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfddns.rb
More file actions
93 lines (79 loc) · 2.89 KB
/
cfddns.rb
File metadata and controls
93 lines (79 loc) · 2.89 KB
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
require "yaml"
require "uri"
require "json"
require "unirest"
config = YAML.load_file(ARGV[0])
auth_data = config['auth']
cf_data = config['cf']
cfddns_data = config['cfddns']
full_name = cf_data["host"] + "." + cf_data["domain"]
headers = {"X-Auth-Email" => auth_data["email"],
"X-Auth-Key" => auth_data["api_key"],
"Content-Type" => "application/json",
}
urls = {:public_ip => "https://icanhazip.com",
:cf_dns_zone => "https://api.cloudflare.com/client/v4/zones",
:cf_dns_records => "https://api.cloudflare.com/client/v4/zones/%s/dns_records/%s",
}
response = Unirest.get urls[:public_ip]
public_ip = response.body.chop
if (cfddns_data["debug"])
puts "My IP: #{public_ip}"
puts "User: #{auth_data['email']}"
puts "Target: #{full_name}"
end
response = Unirest.get(urls[:cf_dns_zone],
headers: headers,
:parameters => {:name => cf_data["domain"]})
zones = response.body
zone_id = zones["result"][0]["id"]
url = urls[:cf_dns_records] % [zone_id, nil]
response = Unirest.get(url,
headers: headers,
parameters: {:name => full_name})
records = response.body
if (records["result_info"]["total_count"] == 0)
if (not cf_data["create_record"])
abort "I didn't find the record you requested and you have our config set to NOT create records. Aborting."
else
parameters = {:type => "A", :name => full_name, :content => public_ip}
response = Unirest.post(urls[:cf_dns_records] % [zone_id, nil],
parameters: parameters.to_json,
headers: headers)
result = response.body
if (result["success"] == true)
puts "Created #{result['result']['name']} with an IP of #{result['result']['content']} Exiting."
else
abort "Failed to update with errors: #{result['errors'][0]['code']}: #{result['errors'][0]['message']}. Aborting."
end
end
else
records["result"].each do |record|
if (record["name"] != full_name)
puts "Not a match"
elsif (record["type"] != "A")
abort "Record found was a #{record['type']} but I only work with A records. Aborting."
elsif (public_ip == record["content"])
puts "Already up to date"
exit
else
puts "Outdated record found. Updating #{record['name']} from #{record['content']} to #{public_ip}"
url = "https://api.cloudflare.com/client/v4/zones/" + zone_id + "/dns_records/" + record["id"]
new_record = record
new_record["content"] = public_ip
response = Unirest.put(url,
parameters: new_record.to_json,
headers: {"X-Auth-Email" => auth_data["email"],
"X-Auth-Key" => auth_data["api_key"],
"Content-Type" => "application/json",
})
result = response.body
if (result["success"] == true)
puts "Completed update. Exiting."
exit
else
abort "Failed to update with errors: #{result['errors'][0]['code']}: #{result['errors'][0]['message']}. Aborting."
end
end
end
end