1
0
mirror of https://github.com/cmur2/dyndnsd.git synced 2024-12-22 00:54:22 +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: Gemspec/OrderedDependencies:
Enabled: false 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: Metrics/LineLength:
Max: 200 Max: 200
Metrics/MethodLength:
Enabled: false
Style/ConditionalAssignment:
Enabled: false
Style/Documentation: Style/Documentation:
Enabled: false 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 # This configuration was generated by
# `rubocop --auto-gen-config` # `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 # The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base. # one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new # Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again. # 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) RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new RuboCop::RakeTask.new
task :default => [:rubocop, :spec] task default: [:rubocop, :spec]

View File

@ -1,5 +1,5 @@
$:.push File.expand_path("../lib", __FILE__) $LOAD_PATH.push File.expand_path('../lib', __FILE__)
require 'dyndnsd/version' require 'dyndnsd/version'
@ -13,7 +13,7 @@ Gem::Specification.new do |s|
s.homepage = 'https://github.com/cmur2/dyndnsd' s.homepage = 'https://github.com/cmur2/dyndnsd'
s.license = 'Apache-2.0' 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.test_files = s.files.grep(%r{^(test|spec|features)/})
s.require_paths = ['lib'] s.require_paths = ['lib']
s.executables = ['dyndnsd'] s.executables = ['dyndnsd']

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

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

View File

@ -13,7 +13,7 @@ module Dyndnsd
def load def load
if File.file?(@db_file) 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 else
@db = {} @db = {}
end end

View File

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

View File

@ -3,21 +3,19 @@ require 'ipaddr'
module Dyndnsd module Dyndnsd
class Helper 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 hostname.length < domain.length + 2
return false if not hostname.end_with?(domain) return false if !hostname.end_with?(domain)
name = hostname.chomp(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 true
end end
def self.is_ip_valid?(ip) def self.ip_valid?(ip)
begin
IPAddr.new(ip) IPAddr.new(ip)
return true return true
rescue ArgumentError rescue ArgumentError
return false return false
end end
end end
end
end end

View File

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

View File

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

View File

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

View File

@ -20,9 +20,9 @@ describe Dyndnsd::Daemon do
updater = Dyndnsd::Updater::Dummy.new updater = Dyndnsd::Updater::Dummy.new
daemon = Dyndnsd::Daemon.new(config, db, updater) 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 end
it 'requires authentication' do 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' 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).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' get '/nic/update?hostname=foo.example.org&myip=BROKENIP&myip6=2001:db8::1'
expect(last_response).to be_ok expect(last_response).to be_ok

View File

@ -25,5 +25,3 @@ module Dyndnsd
end end
end end
end end