mirror of
https://github.com/cmur2/dyndnsd.git
synced 2025-06-26 02:30:22 +02:00
Support changing multiple hostname at once
This commit is contained in:
@ -55,21 +55,25 @@ module Dyndnsd
|
||||
end
|
||||
|
||||
def call(env)
|
||||
return @responder.response_for(:method_forbidden) if env["REQUEST_METHOD"] != "GET"
|
||||
return @responder.response_for(:not_found) if env["PATH_INFO"] != "/nic/update"
|
||||
return @responder.response_for_error(:method_forbidden) if env["REQUEST_METHOD"] != "GET"
|
||||
return @responder.response_for_error(:not_found) if env["PATH_INFO"] != "/nic/update"
|
||||
|
||||
params = Rack::Utils.parse_query(env["QUERY_STRING"])
|
||||
|
||||
return @responder.response_for(:hostname_missing) if not params["hostname"]
|
||||
return @responder.response_for_error(:hostname_missing) if not params["hostname"]
|
||||
|
||||
hostname = params["hostname"]
|
||||
hostnames = params["hostname"].split(',')
|
||||
|
||||
# Check if hostname match rules
|
||||
return @responder.response_for(:hostname_malformed) if not is_fqdn_valid?(hostname)
|
||||
hostnames.each do |hostname|
|
||||
return @responder.response_for_error(:hostname_malformed) if not is_fqdn_valid?(hostname)
|
||||
end
|
||||
|
||||
user = env["REMOTE_USER"]
|
||||
|
||||
return @responder.response_for(:host_forbidden) if not @users[user]['hosts'].include? hostname
|
||||
hostnames.each do |hostname|
|
||||
return @responder.response_for_error(:host_forbidden) if not @users[user]['hosts'].include? hostname
|
||||
end
|
||||
|
||||
# no myip?
|
||||
if not params["myip"]
|
||||
@ -85,16 +89,23 @@ module Dyndnsd
|
||||
|
||||
myip = params["myip"]
|
||||
|
||||
@db['hosts'][hostname] = myip
|
||||
changes = []
|
||||
hostnames.each do |hostname|
|
||||
if (not @db['hosts'].include? hostname) or (@db['hosts'][hostname] != myip)
|
||||
changes << :good
|
||||
@db['hosts'][hostname] = myip
|
||||
else
|
||||
changes << :nochg
|
||||
end
|
||||
end
|
||||
|
||||
if @db.changed?
|
||||
@db['serial'] += 1
|
||||
@db.save
|
||||
update
|
||||
return @responder.response_for(:good, myip)
|
||||
end
|
||||
|
||||
@responder.response_for(:nochg, myip)
|
||||
@responder.response_for_changes(changes, myip)
|
||||
end
|
||||
|
||||
def self.run!
|
||||
|
@ -2,7 +2,7 @@
|
||||
module Dyndnsd
|
||||
module Responder
|
||||
class DynDNSStyle
|
||||
def response_for(state, ip = nil)
|
||||
def response_for_error(state)
|
||||
# general http errors
|
||||
return [405, {"Content-Type" => "text/plain"}, ["Method Not Allowed"]] if state == :method_forbidden
|
||||
return [404, {"Content-Type" => "text/plain"}, ["Not Found"]] if state == :not_found
|
||||
@ -10,9 +10,11 @@ module Dyndnsd
|
||||
return [200, {"Content-Type" => "text/plain"}, ["notfqdn"]] if state == :hostname_missing
|
||||
return [200, {"Content-Type" => "text/plain"}, ["nohost"]] if state == :host_forbidden
|
||||
return [200, {"Content-Type" => "text/plain"}, ["notfqdn"]] if state == :hostname_malformed
|
||||
# OKs
|
||||
return [200, {"Content-Type" => "text/plain"}, ["good #{ip}"]] if state == :good
|
||||
return [200, {"Content-Type" => "text/plain"}, ["nochg #{ip}"]] if state == :nochg
|
||||
end
|
||||
|
||||
def response_for_changes(states, ip)
|
||||
body = states.map { |state| "#{state} #{ip}" }.join("\n")
|
||||
return [200, {"Content-Type" => "text/plain"}, [body]]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -2,7 +2,7 @@
|
||||
module Dyndnsd
|
||||
module Responder
|
||||
class RestStyle
|
||||
def response_for(state, ip = nil)
|
||||
def response_for_error(state)
|
||||
# general http errors
|
||||
return [405, {"Content-Type" => "text/plain"}, ["Method Not Allowed"]] if state == :method_forbidden
|
||||
return [404, {"Content-Type" => "text/plain"}, ["Not Found"]] if state == :not_found
|
||||
@ -10,9 +10,11 @@ module Dyndnsd
|
||||
return [422, {"Content-Type" => "text/plain"}, ["Hostname missing"]] if state == :hostname_missing
|
||||
return [403, {"Content-Type" => "text/plain"}, ["Forbidden"]] if state == :host_forbidden
|
||||
return [422, {"Content-Type" => "text/plain"}, ["Hostname malformed"]] if state == :hostname_malformed
|
||||
# OKs
|
||||
return [200, {"Content-Type" => "text/plain"}, ["Changed to #{ip}"]] if state == :good
|
||||
return [200, {"Content-Type" => "text/plain"}, ["No change needed for #{ip}"]] if state == :nochg
|
||||
end
|
||||
|
||||
def response_for_changes(states, ip)
|
||||
body = states.map { |state| state == :good ? "Changed to #{ip}" : "No change needed for #{ip}" }.join("\n")
|
||||
return [200, {"Content-Type" => "text/plain"}, [body]]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user