1
0
mirror of https://github.com/cmur2/dyndnsd.git synced 2025-07-01 22:30:19 +02:00

gem: add solargraph support

This commit is contained in:
cn
2020-02-28 15:13:28 +01:00
parent 6d0457d70c
commit 73dbf2a5fa
14 changed files with 145 additions and 2 deletions

View File

@ -7,10 +7,12 @@ module Dyndnsd
def_delegators :@db, :[], :[]=, :each, :has_key?
# @param db_file [String]
def initialize(db_file)
@db_file = db_file
end
# @return [void]
def load
if File.file?(@db_file)
@db = JSON.parse(File.open(@db_file, 'r', &:read))
@ -20,6 +22,7 @@ module Dyndnsd
@db_hash = @db.hash
end
# @return [void]
def save
Helper.span('database_save') do |_span|
File.open(@db_file, 'w') { |f| JSON.dump(@db, f) }
@ -27,6 +30,7 @@ module Dyndnsd
end
end
# @return [Boolean]
def changed?
@db_hash != @db.hash
end

View File

@ -2,6 +2,8 @@
module Dyndnsd
module Generator
class Bind
# @param domain [String]
# @param config [Hash{String => Object}]
def initialize(domain, config)
@domain = domain
@ttl = config['ttl']
@ -10,6 +12,8 @@ module Dyndnsd
@additional_zone_content = config['additional_zone_content']
end
# @param db [Dyndnsd::Database]
# @return [String]
def generate(db)
out = []
out << "$TTL #{@ttl}"

View File

@ -3,6 +3,9 @@ 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)
@ -11,6 +14,8 @@ module Dyndnsd
true
end
# @param ip [String]
# @return [Boolean]
def self.ip_valid?(ip)
IPAddr.new(ip)
true
@ -18,15 +23,26 @@ module Dyndnsd
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

View File

@ -2,10 +2,13 @@
module Dyndnsd
module Responder
class DynDNSStyle
# @param app [#call]
def initialize(app)
@app = app
end
# @param env [Hash{String => String}]
# @return [Array{Integer,Hash{String => String},Array{String}}]
def call(env)
@app.call(env).tap do |status_code, headers, body|
if headers.key?('X-DynDNS-Response')
@ -18,6 +21,10 @@ module Dyndnsd
private
# @param status_code [Integer]
# @param headers [Hash{String => String}]
# @param body [Array{String}]
# @return [Array{Integer,Hash{String => String},Array{String}}]
def decorate_dyndnsd_response(status_code, headers, body)
if status_code == 200
[200, {'Content-Type' => 'text/plain'}, [get_success_body(body[0], body[1])]]
@ -26,6 +33,10 @@ module Dyndnsd
end
end
# @param status_code [Integer]
# @param headers [Hash{String => String}]
# @param _body [Array{String}]
# @return [Array{Integer,Hash{String => String},Array{String}}]
def decorate_other_response(status_code, headers, _body)
if status_code == 400
[status_code, headers, ['Bad Request']]
@ -34,10 +45,14 @@ module Dyndnsd
end
end
# @param changes [Array{Symbol}]
# @param myips [Array{String}]
# @return [String]
def get_success_body(changes, myips)
changes.map { |change| "#{change} #{myips.join(' ')}" }.join("\n")
end
# @return [Hash{String => Object}]
def error_response_map
{
# general http errors

View File

@ -2,10 +2,13 @@
module Dyndnsd
module Responder
class RestStyle
# @param app [#call]
def initialize(app)
@app = app
end
# @param env [Hash{String => String}]
# @return [Array{Integer,Hash{String => String},Array{String}}]
def call(env)
@app.call(env).tap do |status_code, headers, body|
if headers.key?('X-DynDNS-Response')
@ -18,6 +21,10 @@ module Dyndnsd
private
# @param status_code [Integer]
# @param headers [Hash{String => String}]
# @param body [Array{String}]
# @return [Array{Integer,Hash{String => String},Array{String}}]
def decorate_dyndnsd_response(status_code, headers, body)
if status_code == 200
[200, {'Content-Type' => 'text/plain'}, [get_success_body(body[0], body[1])]]
@ -26,6 +33,10 @@ module Dyndnsd
end
end
# @param status_code [Integer]
# @param headers [Hash{String => String}]
# @param _body [Array{String}]
# @return [Array{Integer,Hash{String => String},Array{String}}]
def decorate_other_response(status_code, headers, _body)
if status_code == 400
[status_code, headers, ['Bad Request']]
@ -34,10 +45,14 @@ module Dyndnsd
end
end
# @param changes [Array{Symbol}]
# @param myips [Array{String}]
# @return [String]
def get_success_body(changes, myips)
changes.map { |change| change == :good ? "Changed to #{myips.join(' ')}" : "No change needed for #{myips.join(' ')}" }.join("\n")
end
# @return [Hash{String => Object}]
def error_response_map
{
# general http errors

View File

@ -5,8 +5,11 @@ require 'metriks'
module Dyndnsd
class TextfileReporter
# @return [String]
attr_reader :file
# @param file [String]
# @param options [Hash{Symbol => Object}]
def initialize(file, options = {})
@file = file
@ -17,6 +20,7 @@ module Dyndnsd
@on_error = options[:on_error] || proc { |ex| }
end
# @return [void]
def start
@thread ||= Thread.new do
loop do
@ -33,16 +37,19 @@ module Dyndnsd
end
end
# @return [void]
def stop
@thread&.kill
@thread = nil
end
# @return [void]
def restart
stop
start
end
# @return [void]
def write
File.open(@file, 'w') do |f|
@registry.each do |name, metric|
@ -85,6 +92,12 @@ module Dyndnsd
end
end
# @param file [String]
# @param base_name [String]
# @param metric [Object]
# @param keys [Array{Symbol}]
# @param snapshot_keys [Array{Symbol}]
# @return [void]
def write_metric(file, base_name, metric, keys, snapshot_keys = [])
time = Time.now.to_i

View File

@ -2,12 +2,16 @@
module Dyndnsd
module Updater
class CommandWithBindZone
# @param domain [String]
# @param config [Hash{String => Object}]
def initialize(domain, config)
@zone_file = config['zone_file']
@command = config['command']
@generator = Generator::Bind.new(domain, config)
end
# @param db [Dyndnsd::Database]
# @return [void]
def update(db)
Helper.span('updater_update') do |span|
span.set_tag('dyndnsd.updater.name', self.class.name.split('::').last)