1
0
mirror of https://github.com/cmur2/dyndnsd.git synced 2024-11-18 20:56:16 +01:00

gem: fix applicable rubocop todos

This commit is contained in:
cn 2018-02-23 12:54:43 +01:00
parent 9a7c20babb
commit 21857959b5
14 changed files with 142 additions and 401 deletions

View File

@ -6,8 +6,51 @@ AllCops:
Gemspec/OrderedDependencies:
Enabled: false
# allows nicer usage of private_class_method
Layout/EmptyLinesAroundArguments:
Enabled: false
Layout/SpaceInsideHashLiteralBraces:
Enabled: false
Metrics/AbcSize:
Enabled: false
Metrics/BlockLength:
Enabled: false
Metrics/ClassLength:
Enabled: false
Metrics/LineLength:
Max: 200
Metrics/MethodLength:
Enabled: false
Style/ConditionalAssignment:
Enabled: false
Style/Documentation:
Enabled: false
Style/FormatStringToken:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: false
Style/GuardClause:
Enabled: false
Style/IdenticalConditionalBranches:
Enabled: false
Style/InverseMethods:
Enabled: false
Style/NegatedIf:
Enabled: false
Style/SymbolArray:
Enabled: false

View File

@ -1,308 +1,7 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-02-23 11:13:02 +0100 using RuboCop version 0.52.1.
# on 2018-02-23 12:54:10 +0100 using RuboCop version 0.52.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
Layout/ExtraSpacing:
Exclude:
- 'dyndnsd.gemspec'
# Offense count: 1
# Cop supports --auto-correct.
Layout/SpaceAfterComma:
Exclude:
- 'lib/dyndnsd/generator/bind.rb'
# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment.
Layout/SpaceAroundOperators:
Exclude:
- 'dyndnsd.gemspec'
# Offense count: 38
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
# SupportedStyles: space, no_space, compact
# SupportedStylesForEmptyBraces: space, no_space
Layout/SpaceInsideHashLiteralBraces:
Exclude:
- 'lib/dyndnsd.rb'
- 'lib/dyndnsd/responder/dyndns_style.rb'
- 'lib/dyndnsd/responder/rest_style.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: final_newline, final_blank_line
Layout/TrailingBlankLines:
Exclude:
- 'spec/support/dummy_database.rb'
# Offense count: 8
# Cop supports --auto-correct.
Layout/TrailingWhitespace:
Exclude:
- 'lib/dyndnsd/database.rb'
- 'lib/dyndnsd/updater/command_with_bind_zone.rb'
- 'spec/support/dummy_database.rb'
# Offense count: 4
Lint/IneffectiveAccessModifier:
Exclude:
- 'lib/dyndnsd.rb'
# Offense count: 1
# Cop supports --auto-correct.
Lint/ScriptPermission:
Exclude:
- 'lib/dyndnsd.rb'
# Offense count: 5
# Cop supports --auto-correct.
# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods.
Lint/UnusedMethodArgument:
Exclude:
- 'lib/dyndnsd.rb'
- 'lib/dyndnsd/responder/dyndns_style.rb'
- 'lib/dyndnsd/responder/rest_style.rb'
# Offense count: 1
Lint/UselessAssignment:
Exclude:
- 'spec/daemon_spec.rb'
# Offense count: 5
Metrics/AbcSize:
Max: 31
# Offense count: 1
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 159
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 149
# Offense count: 6
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 19
# Offense count: 2
Naming/AccessorMethodName:
Exclude:
- 'lib/dyndnsd/responder/dyndns_style.rb'
- 'lib/dyndnsd/responder/rest_style.rb'
# Offense count: 3
# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist, MethodDefinitionMacros.
# NamePrefix: is_, has_, have_
# NamePrefixBlacklist: is_, has_, have_
# NameWhitelist: is_a?
# MethodDefinitionMacros: define_method, define_singleton_method
Naming/PredicateName:
Exclude:
- 'spec/**/*'
- 'lib/dyndnsd.rb'
- 'lib/dyndnsd/helper.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect.
Security/JSONLoad:
Exclude:
- 'lib/dyndnsd/database.rb'
# Offense count: 1
# Cop supports --auto-correct.
Security/YAMLLoad:
Exclude:
- 'lib/dyndnsd.rb'
# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, conditionals
Style/AndOr:
Exclude:
- 'lib/dyndnsd.rb'
# Offense count: 1
# Cop supports --auto-correct.
Style/ColonMethodCall:
Exclude:
- 'lib/dyndnsd.rb'
# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SingleLineConditionsOnly, IncludeTernaryExpressions.
# SupportedStyles: assign_to_condition, assign_inside_condition
Style/ConditionalAssignment:
Exclude:
- 'lib/dyndnsd.rb'
- 'lib/dyndnsd/database.rb'
# Offense count: 2
# Cop supports --auto-correct.
Style/DefWithParentheses:
Exclude:
- 'lib/dyndnsd.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: format, sprintf, percent
Style/FormatString:
Exclude:
- 'lib/dyndnsd.rb'
# Offense count: 3
# Configuration parameters: .
# SupportedStyles: annotated, template, unannotated
Style/FormatStringToken:
EnforcedStyle: unannotated
# Offense count: 15
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: when_needed, always, never
Style/FrozenStringLiteralComment:
Exclude:
- 'Gemfile'
- 'Rakefile'
- 'dyndnsd.gemspec'
- 'lib/dyndnsd.rb'
- 'lib/dyndnsd/database.rb'
- 'lib/dyndnsd/generator/bind.rb'
- 'lib/dyndnsd/helper.rb'
- 'lib/dyndnsd/responder/dyndns_style.rb'
- 'lib/dyndnsd/responder/rest_style.rb'
- 'lib/dyndnsd/updater/command_with_bind_zone.rb'
- 'lib/dyndnsd/version.rb'
- 'spec/daemon_spec.rb'
- 'spec/spec_helper.rb'
- 'spec/support/dummy_database.rb'
- 'spec/support/dummy_updater.rb'
# Offense count: 2
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'lib/dyndnsd/responder/dyndns_style.rb'
- 'lib/dyndnsd/responder/rest_style.rb'
# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
Style/HashSyntax:
EnforcedStyle: hash_rockets
# Offense count: 2
Style/IdenticalConditionalBranches:
Exclude:
- 'lib/dyndnsd.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: InverseMethods, InverseBlocks.
Style/InverseMethods:
Exclude:
- 'lib/dyndnsd.rb'
# Offense count: 1
# Cop supports --auto-correct.
Style/MethodCallWithoutArgsParentheses:
Exclude:
- 'lib/dyndnsd.rb'
# Offense count: 1
# Cop supports --auto-correct.
Style/MutableConstant:
Exclude:
- 'lib/dyndnsd/version.rb'
# Offense count: 6
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: both, prefix, postfix
Style/NegatedIf:
Exclude:
- 'lib/dyndnsd.rb'
- 'lib/dyndnsd/helper.rb'
# Offense count: 8
# Cop supports --auto-correct.
Style/Not:
Exclude:
- 'lib/dyndnsd.rb'
- 'lib/dyndnsd/helper.rb'
# Offense count: 6
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: short, verbose
Style/PreferredHashMethods:
Exclude:
- 'lib/dyndnsd.rb'
- 'lib/dyndnsd/responder/dyndns_style.rb'
- 'lib/dyndnsd/responder/rest_style.rb'
# Offense count: 1
# Cop supports --auto-correct.
Style/RedundantBegin:
Exclude:
- 'lib/dyndnsd/helper.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AllowAsExpressionSeparator.
Style/Semicolon:
Exclude:
- 'lib/dyndnsd.rb'
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: .
# SupportedStyles: use_perl_names, use_english_names
Style/SpecialGlobalVars:
EnforcedStyle: use_perl_names
# Offense count: 80
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
Style/StringLiterals:
Exclude:
- 'dyndnsd.gemspec'
- 'lib/dyndnsd.rb'
- 'lib/dyndnsd/generator/bind.rb'
- 'lib/dyndnsd/responder/dyndns_style.rb'
- 'lib/dyndnsd/responder/rest_style.rb'
- 'lib/dyndnsd/version.rb'
- 'spec/daemon_spec.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: MinSize.
# SupportedStyles: percent, brackets
Style/SymbolArray:
EnforcedStyle: brackets
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: IgnoredMethods.
# IgnoredMethods: respond_to, define_method
Style/SymbolProc:
Exclude:
- 'lib/dyndnsd.rb'
- 'lib/dyndnsd/database.rb'

View File

@ -5,4 +5,4 @@ require 'rubocop/rake_task'
RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new
task :default => [:rubocop, :spec]
task default: [:rubocop, :spec]

View File

@ -1,19 +1,19 @@
$:.push File.expand_path("../lib", __FILE__)
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
require 'dyndnsd/version'
Gem::Specification.new do |s|
s.name = 'dyndnsd'
s.name = 'dyndnsd'
s.version = Dyndnsd::VERSION
s.summary = 'dyndnsd.rb'
s.description = 'A small, lightweight and extensible DynDNS server written with Ruby and Rack.'
s.author = 'Christian Nicolai'
s.author = 'Christian Nicolai'
s.email = 'chrnicolai@gmail.com'
s.homepage = 'https://github.com/cmur2/dyndnsd'
s.homepage = 'https://github.com/cmur2/dyndnsd'
s.license = 'Apache-2.0'
s.files = `git ls-files`.split($/)
s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
s.test_files = s.files.grep(%r{^(test|spec|features)/})
s.require_paths = ['lib']
s.executables = ['dyndnsd']

81
lib/dyndnsd.rb Normal file → Executable file
View File

@ -27,8 +27,8 @@ module Dyndnsd
end
class LogFormatter
def call(lvl, time, progname, msg)
"[%s] %-5s %s\n" % [Time.now.strftime('%Y-%m-%d %H:%M:%S'), lvl, msg.to_s]
def call(lvl, _time, _progname, msg)
format("[%s] %-5s %s\n", Time.now.strftime('%Y-%m-%d %H:%M:%S'), lvl, msg.to_s)
end
end
@ -42,12 +42,15 @@ module Dyndnsd
@db.load
@db['serial'] ||= 1
@db['hosts'] ||= {}
(@db.save; @updater.update(@db)) if @db.changed?
if @db.changed?
@db.save
@updater.update(@db)
end
end
def is_authorized?(username, password)
allow = ((@users.has_key? username) and (@users[username]['password'] == password))
if not allow
def authorized?(username, password)
allow = ((@users.key? username) && (@users[username]['password'] == password))
if !allow
Dyndnsd.logger.warn "Login failed for #{username}"
Metriks.meter('requests.auth_failed').mark
end
@ -55,40 +58,40 @@ module Dyndnsd
end
def call(env)
return [422, {'X-DynDNS-Response' => 'method_forbidden'}, []] if env["REQUEST_METHOD"] != "GET"
return [422, {'X-DynDNS-Response' => 'not_found'}, []] if env["PATH_INFO"] != "/nic/update"
return [422, {'X-DynDNS-Response' => 'method_forbidden'}, []] if env['REQUEST_METHOD'] != 'GET'
return [422, {'X-DynDNS-Response' => 'not_found'}, []] if env['PATH_INFO'] != '/nic/update'
handle_dyndns_request(env)
end
def self.run!
if ARGV.length != 1
puts "Usage: dyndnsd config_file"
puts 'Usage: dyndnsd config_file'
exit 1
end
config_file = ARGV[0]
if not File.file?(config_file)
puts "Config file not found!"
if !File.file?(config_file)
puts 'Config file not found!'
exit 1
end
puts "DynDNSd version #{Dyndnsd::VERSION}"
puts "Using config file #{config_file}"
config = YAML::load(File.open(config_file, 'r') { |f| f.read })
config = YAML.safe_load(File.open(config_file, 'r', &:read))
setup_logger(config)
Dyndnsd.logger.info "Starting..."
Dyndnsd.logger.info 'Starting...'
# drop priviliges as soon as possible
# NOTE: first change group than user
Process::Sys.setgid(Etc.getgrnam(config['group']).gid) if config['group']
Process::Sys.setuid(Etc.getpwnam(config['user']).uid) if config['user']
setup_traps()
setup_traps
setup_monitoring(config)
@ -97,12 +100,12 @@ module Dyndnsd
private
def extract_v4_and_v6_address(env, params)
return [] if not params["myip"]
def extract_v4_and_v6_address(params)
return [] if !(params['myip'])
begin
IPAddr.new(params["myip"], Socket::AF_INET)
IPAddr.new(params["myip6"], Socket::AF_INET6)
[params["myip"], params["myip6"]]
IPAddr.new(params['myip'], Socket::AF_INET)
IPAddr.new(params['myip6'], Socket::AF_INET6)
[params['myip'], params['myip6']]
rescue ArgumentError
[]
end
@ -110,23 +113,23 @@ module Dyndnsd
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")
return extract_v4_and_v6_address(params) if params.key?('myip6')
# check whether myip parameter has valid IPAddr
return [params["myip"]] if params.has_key?("myip") and Helper.is_ip_valid?(params["myip"])
return [params['myip']] if params.key?('myip') && Helper.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 Helper.is_ip_valid?(env["HTTP_X_REAL_IP"])
return [env['HTTP_X_REAL_IP']] if env.key?('HTTP_X_REAL_IP') && Helper.ip_valid?(env['HTTP_X_REAL_IP'])
# fallback value, always present
[env["REMOTE_ADDR"]]
[env['REMOTE_ADDR']]
end
def process_changes(hostnames, myips)
changes = []
hostnames.each do |hostname|
# myips order is always deterministic
if (not @db['hosts'].include? hostname) or (@db['hosts'][hostname] != myips)
if (!@db['hosts'].include? hostname) || (@db['hosts'][hostname] != myips)
@db['hosts'][hostname] = myips
changes << :good
Metriks.meter('requests.good').mark
@ -138,7 +141,7 @@ module Dyndnsd
changes
end
def update_db()
def update_db
@db['serial'] += 1
Dyndnsd.logger.info "Committing update ##{@db['serial']}"
@db.save
@ -147,18 +150,18 @@ module Dyndnsd
end
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 !(params['hostname'])
hostnames = params["hostname"].split(',')
hostnames = params['hostname'].split(',')
# check for invalid hostnames
invalid_hostnames = hostnames.select { |hostname| not Helper.is_fqdn_valid?(hostname, @domain) }
invalid_hostnames = hostnames.select { |hostname| !Helper.fqdn_valid?(hostname, @domain) }
return [422, {'X-DynDNS-Response' => 'hostname_malformed'}, []] if invalid_hostnames.any?
user = env["REMOTE_USER"]
user = env['REMOTE_USER']
# check for hostnames that the user does not own
forbidden_hostnames = hostnames - @users[user]['hosts']
@ -181,29 +184,29 @@ module Dyndnsd
# SETUP
def self.setup_logger(config)
private_class_method def self.setup_logger(config)
if config['logfile']
Dyndnsd.logger = Logger.new(config['logfile'])
else
Dyndnsd.logger = Logger.new(STDOUT)
end
Dyndnsd.logger.progname = "dyndnsd"
Dyndnsd.logger.progname = 'dyndnsd'
Dyndnsd.logger.formatter = LogFormatter.new
end
def self.setup_traps()
private_class_method def self.setup_traps
Signal.trap('INT') do
Dyndnsd.logger.info "Quitting..."
Dyndnsd.logger.info 'Quitting...'
Rack::Handler::WEBrick.shutdown
end
Signal.trap('TERM') do
Dyndnsd.logger.info "Quitting..."
Dyndnsd.logger.info 'Quitting...'
Rack::Handler::WEBrick.shutdown
end
end
def self.setup_monitoring(config)
private_class_method def self.setup_monitoring(config)
# configure metriks
if config['graphite']
host = config['graphite']['host'] || 'localhost'
@ -224,14 +227,14 @@ module Dyndnsd
end
end
def self.setup_rack(config)
private_class_method def self.setup_rack(config)
# configure daemon
db = Database.new(config['db'])
updater = Updater::CommandWithBindZone.new(config['domain'], config['updater']['params']) if config['updater']['name'] == 'command_with_bind_zone'
daemon = Daemon.new(config, db, updater)
# configure rack
app = Rack::Auth::Basic.new(daemon, "DynDNS", &daemon.method(:is_authorized?))
app = Rack::Auth::Basic.new(daemon, 'DynDNS', &daemon.method(:authorized?))
if config['responder'] == 'RestStyle'
app = Responder::RestStyle.new(app)
@ -239,7 +242,7 @@ module Dyndnsd
app = Responder::DynDNSStyle.new(app)
end
Rack::Handler::WEBrick.run app, :Host => config['host'], :Port => config['port']
Rack::Handler::WEBrick.run app, Host: config['host'], Port: config['port']
end
end
end

View File

@ -4,27 +4,27 @@ require 'forwardable'
module Dyndnsd
class Database
extend Forwardable
def_delegators :@db, :[], :[]=, :each, :has_key?
def initialize(db_file)
@db_file = db_file
end
def load
if File.file?(@db_file)
@db = JSON.load(File.open(@db_file, 'r') { |f| f.read })
@db = JSON.parse(File.open(@db_file, 'r', &:read))
else
@db = {}
end
@db_hash = @db.hash
end
def save
File.open(@db_file, 'w') { |f| JSON.dump(@db, f) }
@db_hash = @db.hash
end
def changed?
@db_hash != @db.hash
end

View File

@ -14,21 +14,21 @@ module Dyndnsd
out = []
out << "$TTL #{@ttl}"
out << "$ORIGIN #{@domain}."
out << ""
out << ''
out << "@ IN SOA #{@dns} #{@email_addr} ( #{zone['serial']} 3h 5m 1w 1h )"
out << "@ IN NS #{@dns}"
out << ""
zone['hosts'].each do |hostname,ips|
out << ''
zone['hosts'].each do |hostname, ips|
ips.each do |ip|
ip = IPAddr.new(ip).native
type = ip.ipv6? ? "AAAA" : "A"
type = ip.ipv6? ? 'AAAA' : 'A'
name = hostname.chomp('.' + @domain)
out << "#{name} IN #{type} #{ip}"
end
end
out << ""
out << ''
out << @additional_zone_content
out << ""
out << ''
out.join("\n")
end
end

View File

@ -3,21 +3,19 @@ require 'ipaddr'
module Dyndnsd
class Helper
def self.is_fqdn_valid?(hostname, domain)
def self.fqdn_valid?(hostname, domain)
return false if hostname.length < domain.length + 2
return false if not hostname.end_with?(domain)
return false if !hostname.end_with?(domain)
name = hostname.chomp(domain)
return false if not name.match(/^[a-zA-Z0-9_-]+\.$/)
return false if !name.match(/^[a-zA-Z0-9_-]+\.$/)
true
end
def self.is_ip_valid?(ip)
begin
IPAddr.new(ip)
return true
rescue ArgumentError
return false
end
def self.ip_valid?(ip)
IPAddr.new(ip)
return true
rescue ArgumentError
return false
end
end
end

View File

@ -8,7 +8,7 @@ module Dyndnsd
def call(env)
@app.call(env).tap do |status_code, headers, body|
if headers.has_key?("X-DynDNS-Response")
if headers.key?('X-DynDNS-Response')
return decorate_dyndnsd_response(status_code, headers, body)
else
return decorate_other_response(status_code, headers, body)
@ -20,17 +20,17 @@ module Dyndnsd
def decorate_dyndnsd_response(status_code, headers, body)
if status_code == 200
[200, {"Content-Type" => "text/plain"}, [get_success_body(body[0], body[1])]]
[200, {'Content-Type' => 'text/plain'}, [get_success_body(body[0], body[1])]]
elsif status_code == 422
get_error_response_map[headers["X-DynDNS-Response"]]
error_response_map[headers['X-DynDNS-Response']]
end
end
def decorate_other_response(status_code, headers, body)
def decorate_other_response(status_code, headers, _body)
if status_code == 400
[status_code, headers, ["Bad Request"]]
[status_code, headers, ['Bad Request']]
elsif status_code == 401
[status_code, headers, ["badauth"]]
[status_code, headers, ['badauth']]
end
end
@ -38,15 +38,15 @@ module Dyndnsd
changes.map { |change| "#{change} #{myips.join(' ')}" }.join("\n")
end
def get_error_response_map
def error_response_map
{
# general http errors
'method_forbidden' => [405, {"Content-Type" => "text/plain"}, ["Method Not Allowed"]],
'not_found' => [404, {"Content-Type" => "text/plain"}, ["Not Found"]],
'method_forbidden' => [405, {'Content-Type' => 'text/plain'}, ['Method Not Allowed']],
'not_found' => [404, {'Content-Type' => 'text/plain'}, ['Not Found']],
# specific errors
'hostname_missing' => [200, {"Content-Type" => "text/plain"}, ["notfqdn"]],
'hostname_malformed' => [200, {"Content-Type" => "text/plain"}, ["notfqdn"]],
'host_forbidden' => [200, {"Content-Type" => "text/plain"}, ["nohost"]]
'hostname_missing' => [200, {'Content-Type' => 'text/plain'}, ['notfqdn']],
'hostname_malformed' => [200, {'Content-Type' => 'text/plain'}, ['notfqdn']],
'host_forbidden' => [200, {'Content-Type' => 'text/plain'}, ['nohost']]
}
end
end

View File

@ -8,7 +8,7 @@ module Dyndnsd
def call(env)
@app.call(env).tap do |status_code, headers, body|
if headers.has_key?("X-DynDNS-Response")
if headers.key?('X-DynDNS-Response')
return decorate_dyndnsd_response(status_code, headers, body)
else
return decorate_other_response(status_code, headers, body)
@ -20,17 +20,17 @@ module Dyndnsd
def decorate_dyndnsd_response(status_code, headers, body)
if status_code == 200
[200, {"Content-Type" => "text/plain"}, [get_success_body(body[0], body[1])]]
[200, {'Content-Type' => 'text/plain'}, [get_success_body(body[0], body[1])]]
elsif status_code == 422
get_error_response_map[headers["X-DynDNS-Response"]]
error_response_map[headers['X-DynDNS-Response']]
end
end
def decorate_other_response(status_code, headers, body)
def decorate_other_response(status_code, headers, _body)
if status_code == 400
[status_code, headers, ["Bad Request"]]
[status_code, headers, ['Bad Request']]
elsif status_code == 401
[status_code, headers, ["Unauthorized"]]
[status_code, headers, ['Unauthorized']]
end
end
@ -38,15 +38,15 @@ module Dyndnsd
changes.map { |change| change == :good ? "Changed to #{myips.join(' ')}" : "No change needed for #{myips.join(' ')}" }.join("\n")
end
def get_error_response_map
def error_response_map
{
# general http errors
'method_forbidden' => [405, {"Content-Type" => "text/plain"}, ["Method Not Allowed"]],
'not_found' => [404, {"Content-Type" => "text/plain"}, ["Not Found"]],
'method_forbidden' => [405, {'Content-Type' => 'text/plain'}, ['Method Not Allowed']],
'not_found' => [404, {'Content-Type' => 'text/plain'}, ['Not Found']],
# specific errors
'hostname_missing' => [422, {"Content-Type" => "text/plain"}, ["Hostname missing"]],
'hostname_malformed' => [422, {"Content-Type" => "text/plain"}, ["Hostname malformed"]],
'host_forbidden' => [403, {"Content-Type" => "text/plain"}, ["Forbidden"]]
'hostname_missing' => [422, {'Content-Type' => 'text/plain'}, ['Hostname missing']],
'hostname_malformed' => [422, {'Content-Type' => 'text/plain'}, ['Hostname malformed']],
'host_forbidden' => [403, {'Content-Type' => 'text/plain'}, ['Forbidden']]
}
end
end

View File

@ -7,7 +7,7 @@ module Dyndnsd
@command = config['command']
@generator = Generator::Bind.new(domain, config)
end
def update(zone)
# write zone file in bind syntax
File.open(@zone_file, 'w') { |f| f.write(@generator.generate(zone)) }

View File

@ -1,4 +1,4 @@
module Dyndnsd
VERSION = "1.6.1"
VERSION = '1.6.1'.freeze
end

View File

@ -20,9 +20,9 @@ describe Dyndnsd::Daemon do
updater = Dyndnsd::Updater::Dummy.new
daemon = Dyndnsd::Daemon.new(config, db, updater)
app = Rack::Auth::Basic.new(daemon, "DynDNS", &daemon.method(:is_authorized?))
app = Rack::Auth::Basic.new(daemon, 'DynDNS', &daemon.method(:authorized?))
app = Dyndnsd::Responder::DynDNSStyle.new(app)
Dyndnsd::Responder::DynDNSStyle.new(app)
end
it 'requires authentication' do
@ -189,7 +189,7 @@ describe Dyndnsd::Daemon do
get '/nic/update?hostname=foo.example.org&myip=1.2.3.4&myip6=2001:db8::1'
expect(last_response).to be_ok
expect(last_response.body).to eq("good 1.2.3.4 2001:db8::1")
expect(last_response.body).to eq('good 1.2.3.4 2001:db8::1')
get '/nic/update?hostname=foo.example.org&myip=BROKENIP&myip6=2001:db8::1'
expect(last_response).to be_ok

View File

@ -4,7 +4,7 @@ require 'forwardable'
module Dyndnsd
class DummyDatabase
extend Forwardable
def_delegators :@db, :[], :[]=, :each, :has_key?
def initialize(db_init)
@ -25,5 +25,3 @@ module Dyndnsd
end
end
end