2020-03-06 21:33:29 +01:00
|
|
|
# frozen_string_literal: true
|
2018-02-03 21:40:44 +01:00
|
|
|
|
|
|
|
require 'ipaddr'
|
|
|
|
|
|
|
|
module Dyndnsd
|
|
|
|
class Helper
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param hostname [String]
|
|
|
|
# @param domain [String]
|
|
|
|
# @return [Boolean]
|
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
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param ip [String]
|
|
|
|
# @return [Boolean]
|
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
|
2018-01-26 16:25:15 +01:00
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param username [String]
|
|
|
|
# @param password [String]
|
|
|
|
# @param users [Hash]
|
|
|
|
# @return [Boolean]
|
2018-01-26 16:25:15 +01:00
|
|
|
def self.user_allowed?(username, password, users)
|
|
|
|
(users.key? username) && (users[username]['password'] == password)
|
|
|
|
end
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param hostname [String]
|
|
|
|
# @param myips [Array]
|
|
|
|
# @param hosts [Hash]
|
|
|
|
# @return [Boolean]
|
2018-01-26 16:25:15 +01:00
|
|
|
def self.changed?(hostname, myips, hosts)
|
|
|
|
# myips order is always deterministic
|
2018-02-23 16:37:10 +01:00
|
|
|
((!hosts.include? hostname) || (hosts[hostname] != myips)) && !myips.empty?
|
2018-01-26 16:25:15 +01:00
|
|
|
end
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param operation [String]
|
|
|
|
# @param block [Proc]
|
|
|
|
# @return [void]
|
2018-01-26 16:25:15 +01:00
|
|
|
def self.span(operation, &block)
|
2018-07-12 22:36:01 +02:00
|
|
|
scope = OpenTracing.start_active_span(operation)
|
|
|
|
span = scope.span
|
2018-01-26 16:25:15 +01:00
|
|
|
span.set_tag('component', 'dyndnsd')
|
|
|
|
span.set_tag('span.kind', 'server')
|
|
|
|
begin
|
|
|
|
block.call(span)
|
2018-07-13 14:29:22 +02:00
|
|
|
rescue StandardError => e
|
|
|
|
span.set_tag('error', true)
|
|
|
|
span.log_kv(
|
|
|
|
event: 'error',
|
|
|
|
'error.kind': e.class.to_s,
|
|
|
|
'error.object': e,
|
|
|
|
message: e.message,
|
2020-02-28 16:44:27 +01:00
|
|
|
stack: e.backtrace&.join("\n") || ''
|
2018-07-13 14:29:22 +02:00
|
|
|
)
|
|
|
|
raise
|
2018-01-26 16:25:15 +01:00
|
|
|
ensure
|
2018-07-12 22:36:01 +02:00
|
|
|
scope.close
|
2018-01-26 16:25:15 +01:00
|
|
|
end
|
|
|
|
end
|
2018-02-03 21:40:44 +01:00
|
|
|
end
|
|
|
|
end
|