1
0
mirror of https://github.com/cmur2/dyndnsd.git synced 2024-12-11 05:04:19 +01:00
dyndnsd/lib/dyndnsd/helper.rb

42 lines
1001 B
Ruby
Raw Normal View History

2018-02-03 21:40:44 +01:00
require 'ipaddr'
module Dyndnsd
class Helper
2018-02-23 12:54:43 +01:00
def self.fqdn_valid?(hostname, domain)
2018-02-03 21:40:44 +01:00
return false if hostname.length < domain.length + 2
2018-02-23 12:54:43 +01:00
return false if !hostname.end_with?(domain)
2018-02-03 21:40:44 +01:00
name = hostname.chomp(domain)
2018-02-23 12:54:43 +01:00
return false if !name.match(/^[a-zA-Z0-9_-]+\.$/)
2018-02-03 21:40:44 +01:00
true
end
2018-02-23 12:54:43 +01:00
def self.ip_valid?(ip)
IPAddr.new(ip)
2018-03-26 20:56:53 +02:00
true
2018-02-23 12:54:43 +01:00
rescue ArgumentError
2018-03-26 20:56:53 +02:00
false
2018-02-03 21:40:44 +01:00
end
def self.user_allowed?(username, password, users)
(users.key? username) && (users[username]['password'] == password)
end
def self.changed?(hostname, myips, hosts)
# myips order is always deterministic
((!hosts.include? hostname) || (hosts[hostname] != myips)) && !myips.empty?
end
def self.span(operation, &block)
span = OpenTracing.start_span(operation)
span.set_tag('component', 'dyndnsd')
span.set_tag('span.kind', 'server')
begin
block.call(span)
ensure
span.finish
end
end
2018-02-03 21:40:44 +01:00
end
end