Skip to content

Commit

Permalink
Merge pull request #14 from mhum/fix-domain-removal
Browse files Browse the repository at this point in the history
Use replaceRR API and don't swallow API errors
  • Loading branch information
mhum authored Dec 17, 2023
2 parents 15ac204 + 7128031 commit 973e93d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 40 deletions.
14 changes: 4 additions & 10 deletions dns.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,19 @@ proc updateIPs {current_ip domain_ip} {
puts "[clock format $now -format {%y-%m-%d %H:%M:%S}]: The domain IP doesn't appear to be set yet."
} else {
puts "[clock format $now -format {%y-%m-%d %H:%M:%S}]: Current IP: $current_ip doesn't match Domain IP: $domain_ip"

# The case where the server returns 0.0.0.0 is probably
# vestigial, but I'm leaving it in just in case.
if {$domain_ip ne {0.0.0.0}} {
::nfs::Http::removeDomain $domain_ip
}
}

::nfs::Http::addDomain $current_ip
# Set or update the domain IP
::nfs::Http::replaceDomain $current_ip

# Check to see if the update was successful
set now [clock seconds]
if {[::nfs::Utility::doIPsMatch]} {
set domain_ip [::nfs::Http::fetchDomainIP]
set now [clock seconds]

puts "[clock format $now -format {%y-%m-%d %H:%M:%S}]: IPs match now! Current IP: $current_ip Domain IP: $domain_ip"
exit
} else {
puts "[clock format $now -format {%y-%m-%d %H:%M:%S}]: They still don't match. Current IP: $current_ip Domain IP: $domain_ip"
exit
}
}

Expand Down
41 changes: 14 additions & 27 deletions packages/http.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package require nfsUtility

namespace eval ::nfs::Http:: {

namespace export getRequest fetchCurrentIP fetchDomainIP removeDomain addDomain
namespace export getRequest fetchCurrentIP fetchDomainIP replaceDomain
}

proc ::nfs::Http::getRequest {uri {body { }}} {
Expand Down Expand Up @@ -46,13 +46,14 @@ proc ::nfs::Http::fetchDomainIP {} {

set uri "/dns/$::nfs::CFG(domain)/listRRs"
set body "name=$::nfs::CFG(subdomain)"
set now [clock seconds]

set data [getRequest $uri $body]

# Response will be empty if domain is not set
if {$data eq {[]}} {
puts "No IP address is currently set."
return "0.0.0.0"
puts "[clock format $now -format {%y-%m-%d %H:%M:%S}]: No IP address is currently set."
return "UNSET"
}

set resp_data [json::json2dict $data]
Expand All @@ -64,41 +65,27 @@ proc ::nfs::Http::fetchDomainIP {} {
return $ip
}

proc ::nfs::Http::removeDomain {domain_ip} {
proc ::nfs::Http::replaceDomain {current_ip} {
variable ::nfs::CFG

set now [clock seconds]

if {$::nfs::CFG(subdomain) eq ""} {
puts "Removing $::nfs::CFG(domain)..."
puts "[clock format $now -format {%y-%m-%d %H:%M:%S}]: Setting $::nfs::CFG(domain) to $current_ip..."
} else {
puts "Removing $::nfs::CFG(subdomain).$::nfs::CFG(domain)..."
puts "[clock format $now -format {%y-%m-%d %H:%M:%S}]: Setting $::nfs::CFG(subdomain).$::nfs::CFG(domain) to $current_ip..."
}

set uri "/dns/$::nfs::CFG(domain)/removeRR"
set body "name=$::nfs::CFG(subdomain)&type=A&data=$domain_ip"
set uri "/dns/$::nfs::CFG(domain)/replaceRR"
set body "name=$::nfs::CFG(subdomain)&type=A&data=$current_ip"

set data [getRequest $uri $body]

if {[catch {set resp_data [json::json2dict $data]}]} {
puts "Response to removeRR request isn't valid JSON. That's probably fine."
return
}

::nfs::Utility::validateResponse $resp_data
}

proc ::nfs::Http::addDomain {current_ip} {
variable ::nfs::CFG
if {$data ne {}} {
set resp_data [json::json2dict $data]

if {$::nfs::CFG(subdomain) eq ""} {
puts "$::nfs::CFG(domain) to $current_ip..."
} else {
puts "Setting $::nfs::CFG(subdomain).$::nfs::CFG(domain) to $current_ip..."
::nfs::Utility::validateResponse $resp_data
}

set uri "/dns/$::nfs::CFG(domain)/addRR"
set body "name=$::nfs::CFG(subdomain)&type=A&data=$current_ip"

set data [getRequest $uri $body]
}

proc createHeader {uri body} {
Expand Down
8 changes: 5 additions & 3 deletions packages/utility.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ proc ::nfs::Utility::doIPsMatch {} {

proc ::nfs::Utility::validateResponse {resp} {
if {[dict exists $resp error]} {
puts "ERROR: [dict get $resp error]"
puts "ERROR: [dict get $resp debug]"
set now [clock seconds]

puts "[clock format $now -format {%y-%m-%d %H:%M:%S}]: ERROR: [dict get $resp error]"
puts "[clock format $now -format {%y-%m-%d %H:%M:%S}]: ERROR: [dict get $resp debug]"

exit
}
}
}

0 comments on commit 973e93d

Please sign in to comment.