Sha256: 4eabc288c75183e83e21270c30ec8dfc6d7fb3f5327cb2ec7f4002449363251a

Contents?: true

Size: 1.74 KB

Versions: 10

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

require 'ipaddr'

module Dyndnsd
  class Helper
    # @param hostname [String]
    # @param domain [String]
    # @return [Boolean]
    def self.fqdn_valid?(hostname, domain)
      return false if hostname.length < domain.length + 2
      return false if !hostname.end_with?(domain)
      name = hostname.chomp(domain)
      return false if !name.match(/^[a-zA-Z0-9_-]+\.$/)
      true
    end

    # @param ip [String]
    # @return [Boolean]
    def self.ip_valid?(ip)
      IPAddr.new(ip)
      true
    rescue ArgumentError
      false
    end

    # @param username [String]
    # @param password [String]
    # @param users [Hash]
    # @return [Boolean]
    def self.user_allowed?(username, password, users)
      (users.key? username) && (users[username]['password'] == password)
    end

    # @param hostname [String]
    # @param myips [Array]
    # @param hosts [Hash]
    # @return [Boolean]
    def self.changed?(hostname, myips, hosts)
      # myips order is always deterministic
      ((!hosts.include? hostname) || (hosts[hostname] != myips)) && !myips.empty?
    end

    # @param operation [String]
    # @param block [Proc]
    # @return [void]
    def self.span(operation, &block)
      scope = OpenTracing.start_active_span(operation)
      span = scope.span
      span.set_tag('component', 'dyndnsd')
      span.set_tag('span.kind', 'server')
      begin
        block.call(span)
      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,
          stack: e.backtrace&.join("\n") || ''
        )
        raise e
      ensure
        scope.close
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
dyndnsd-3.4.1 lib/dyndnsd/helper.rb
dyndnsd-3.4.0 lib/dyndnsd/helper.rb
dyndnsd-3.3.3 lib/dyndnsd/helper.rb
dyndnsd-3.3.2 lib/dyndnsd/helper.rb
dyndnsd-3.3.2.rc1 lib/dyndnsd/helper.rb
dyndnsd-3.3.1 lib/dyndnsd/helper.rb
dyndnsd-3.3.0 lib/dyndnsd/helper.rb
dyndnsd-3.2.0 lib/dyndnsd/helper.rb
dyndnsd-3.1.3 lib/dyndnsd/helper.rb
dyndnsd-3.1.2 lib/dyndnsd/helper.rb