mirror of
https://github.com/cmur2/dyndnsd.git
synced 2024-12-21 14:54:22 +01:00
daemon: break extracting myip down into smaller parts
This commit is contained in:
parent
6080e14356
commit
08eaacb6ab
@ -146,50 +146,46 @@ module Dyndnsd
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract_myips(env, params)
|
def is_ip_valid?(ip)
|
||||||
if params.has_key?("myip6")
|
begin
|
||||||
# require presence of myip parameter as valid IPAddr (v4) and valid myip6
|
IPAddr.new(ip)
|
||||||
return [] if not params["myip"]
|
return true
|
||||||
begin
|
rescue ArgumentError
|
||||||
IPAddr.new(params["myip"], Socket::AF_INET)
|
return false
|
||||||
IPAddr.new(params["myip6"], Socket::AF_INET6)
|
|
||||||
|
|
||||||
# myip will be an array
|
|
||||||
myip = [params["myip"], params["myip6"]]
|
|
||||||
rescue ArgumentError
|
|
||||||
return []
|
|
||||||
end
|
|
||||||
else
|
|
||||||
# fallback value, always present
|
|
||||||
myip = env["REMOTE_ADDR"]
|
|
||||||
|
|
||||||
# check whether X-Real-IP header has valid IPAddr
|
|
||||||
if env.has_key?("HTTP_X_REAL_IP")
|
|
||||||
begin
|
|
||||||
IPAddr.new(env["HTTP_X_REAL_IP"])
|
|
||||||
myip = env["HTTP_X_REAL_IP"]
|
|
||||||
rescue ArgumentError
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# check whether myip parameter has valid IPAddr
|
|
||||||
if params.has_key?("myip")
|
|
||||||
begin
|
|
||||||
IPAddr.new(params["myip"])
|
|
||||||
myip = params["myip"]
|
|
||||||
rescue ArgumentError
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
myip
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def process_changes(hostnames, myip)
|
def extract_v4_and_v6_address(env, params)
|
||||||
|
return [] if not params["myip"]
|
||||||
|
begin
|
||||||
|
IPAddr.new(params["myip"], Socket::AF_INET)
|
||||||
|
IPAddr.new(params["myip6"], Socket::AF_INET6)
|
||||||
|
[params["myip"], params["myip6"]]
|
||||||
|
rescue ArgumentError
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def extract_myips(env, params)
|
||||||
|
# require presence of myip parameter as valid IPAddr (v4) and valid myip6
|
||||||
|
return extract_v4_and_v6_address(env, params) if params.has_key?("myip6")
|
||||||
|
|
||||||
|
# check whether myip parameter has valid IPAddr
|
||||||
|
return [params["myip"]] if params.has_key?("myip") and is_ip_valid?(params["myip"])
|
||||||
|
|
||||||
|
# check whether X-Real-IP header has valid IPAddr
|
||||||
|
return [env["HTTP_X_REAL_IP"]] if env.has_key?("HTTP_X_REAL_IP") and is_ip_valid?(env["HTTP_X_REAL_IP"])
|
||||||
|
|
||||||
|
# fallback value, always present
|
||||||
|
[env["REMOTE_ADDR"]]
|
||||||
|
end
|
||||||
|
|
||||||
|
def process_changes(hostnames, myips)
|
||||||
changes = []
|
changes = []
|
||||||
hostnames.each do |hostname|
|
hostnames.each do |hostname|
|
||||||
if (not @db['hosts'].include? hostname) or (@db['hosts'][hostname] != myip)
|
# myips order is always deterministic
|
||||||
@db['hosts'][hostname] = myip
|
if (not @db['hosts'].include? hostname) or (@db['hosts'][hostname] != myips)
|
||||||
|
@db['hosts'][hostname] = myips
|
||||||
changes << :good
|
changes << :good
|
||||||
Metriks.meter('requests.good').mark
|
Metriks.meter('requests.good').mark
|
||||||
else
|
else
|
||||||
@ -211,33 +207,34 @@ module Dyndnsd
|
|||||||
def handle_dyndns_request(env)
|
def handle_dyndns_request(env)
|
||||||
params = Rack::Utils.parse_query(env["QUERY_STRING"])
|
params = Rack::Utils.parse_query(env["QUERY_STRING"])
|
||||||
|
|
||||||
|
# require hostname parameter
|
||||||
return [422, {'X-DynDNS-Response' => 'hostname_missing'}, []] if not params["hostname"]
|
return [422, {'X-DynDNS-Response' => 'hostname_missing'}, []] if not params["hostname"]
|
||||||
|
|
||||||
hostnames = params["hostname"].split(',')
|
hostnames = params["hostname"].split(',')
|
||||||
|
|
||||||
# Check if hostname match rules
|
# check for invalid hostnames
|
||||||
hostnames.each do |hostname|
|
invalid_hostnames = hostnames.select { |hostname| not is_fqdn_valid?(hostname) }
|
||||||
return [422, {'X-DynDNS-Response' => 'hostname_malformed'}, []] if not is_fqdn_valid?(hostname)
|
return [422, {'X-DynDNS-Response' => 'hostname_malformed'}, []] if invalid_hostnames.any?
|
||||||
end
|
|
||||||
|
|
||||||
user = env["REMOTE_USER"]
|
user = env["REMOTE_USER"]
|
||||||
|
|
||||||
hostnames.each do |hostname|
|
# check for hostnames that the user does not own
|
||||||
return [422, {'X-DynDNS-Response' => 'host_forbidden'}, []] if not @users[user]['hosts'].include? hostname
|
forbidden_hostnames = hostnames - @users[user]['hosts']
|
||||||
end
|
return [422, {'X-DynDNS-Response' => 'host_forbidden'}, []] if forbidden_hostnames.any?
|
||||||
|
|
||||||
myip = extract_myips(env, params)
|
myips = extract_myips(env, params)
|
||||||
|
|
||||||
return [422, {'X-DynDNS-Response' => 'host_forbidden'}, []] if myip.empty?
|
# require at least one IP to update
|
||||||
|
return [422, {'X-DynDNS-Response' => 'host_forbidden'}, []] if myips.empty?
|
||||||
|
|
||||||
Metriks.meter('requests.valid').mark
|
Metriks.meter('requests.valid').mark
|
||||||
Dyndnsd.logger.info "Request to update #{hostnames} to #{myip} for user #{user}"
|
Dyndnsd.logger.info "Request to update #{hostnames} to #{myips} for user #{user}"
|
||||||
|
|
||||||
changes = process_changes(hostnames, myip)
|
changes = process_changes(hostnames, myips)
|
||||||
|
|
||||||
update_db if @db.changed?
|
update_db if @db.changed?
|
||||||
|
|
||||||
[200, {'X-DynDNS-Response' => 'success'}, [changes, myip]]
|
[200, {'X-DynDNS-Response' => 'success'}, [changes, myips]]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -19,7 +19,7 @@ module Dyndnsd
|
|||||||
out << "@ IN NS #{@dns}"
|
out << "@ IN NS #{@dns}"
|
||||||
out << ""
|
out << ""
|
||||||
zone['hosts'].each do |hostname,ips|
|
zone['hosts'].each do |hostname,ips|
|
||||||
(ips.is_a?(Array) ? ips : [ips]).each do |ip|
|
ips.each do |ip|
|
||||||
ip = IPAddr.new(ip).native
|
ip = IPAddr.new(ip).native
|
||||||
type = ip.ipv6? ? "AAAA" : "A"
|
type = ip.ipv6? ? "AAAA" : "A"
|
||||||
name = hostname.chomp('.' + @domain)
|
name = hostname.chomp('.' + @domain)
|
||||||
|
@ -34,9 +34,8 @@ module Dyndnsd
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_success_body(states, ip)
|
def get_success_body(changes, myips)
|
||||||
ips = ip.is_a?(Array) ? ip.join(' ') : ip
|
changes.map { |change| "#{change} #{myips.join(' ')}" }.join("\n")
|
||||||
states.map { |state| "#{state} #{ips}" }.join("\n")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_error_response_map
|
def get_error_response_map
|
||||||
|
@ -34,10 +34,8 @@ module Dyndnsd
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_success_body(changes, myips)
|
||||||
def get_success_response(states, ip)
|
changes.map { |change| change == :good ? "Changed to #{myips.join(' ')}" : "No change needed for #{myips.join(' ')}" }.join("\n")
|
||||||
ips = ip.is_a?(Array) ? ip.join(' ') : ip
|
|
||||||
states.map { |state| state == :good ? "Changed to #{ips}" : "No change needed for #{ips}" }.join("\n")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_error_response_map
|
def get_error_response_map
|
||||||
|
Loading…
Reference in New Issue
Block a user