mirror of
https://github.com/cmur2/dyndnsd.git
synced 2025-07-02 08:30:18 +02:00
Fiat lux
This commit is contained in:
32
lib/dyndnsd/database.rb
Normal file
32
lib/dyndnsd/database.rb
Normal file
@ -0,0 +1,32 @@
|
||||
|
||||
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 })
|
||||
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
|
||||
end
|
||||
end
|
29
lib/dyndnsd/generator/bind.rb
Normal file
29
lib/dyndnsd/generator/bind.rb
Normal file
@ -0,0 +1,29 @@
|
||||
|
||||
module Dyndnsd
|
||||
module Generator
|
||||
class Bind
|
||||
def initialize(config)
|
||||
@ttl = config['ttl']
|
||||
@origin = config['origin']
|
||||
@dns = config['dns']
|
||||
@email_addr = config['email_addr']
|
||||
end
|
||||
|
||||
def generate(zone)
|
||||
out = []
|
||||
out << "$TTL #{@ttl}"
|
||||
out << "$ORIGIN #{@origin}"
|
||||
out << ""
|
||||
out << "@ IN SOA #{@dns} #{@email_addr} ( #{zone['serial']} 3h 5m 1w 1h )"
|
||||
out << "@ IN NS #{@dns}"
|
||||
out << ""
|
||||
zone['hosts'].each do |hostname,ip|
|
||||
name = hostname.chomp('.' + @origin[0..-2])
|
||||
out << "#{name} IN A #{ip}"
|
||||
end
|
||||
out << ""
|
||||
out.join("\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
19
lib/dyndnsd/responder/rest_style.rb
Normal file
19
lib/dyndnsd/responder/rest_style.rb
Normal file
@ -0,0 +1,19 @@
|
||||
|
||||
module Dyndnsd
|
||||
module Responder
|
||||
class RestStyle
|
||||
def response_for(state)
|
||||
# general http errors
|
||||
return [405, {"Content-Type" => "text/plain"}, ["Method Not Allowed"]] if state == :method_forbidden
|
||||
return [404, {"Content-Type" => "text/plain"}, ["Not Found"]] if state == :not_found
|
||||
# specific errors
|
||||
return [422, {"Content-Type" => "text/plain"}, ["Hostname missing"]] if state == :hostname_missing
|
||||
return [403, {"Content-Type" => "text/plain"}, ["Forbidden"]] if state == :host_forbidden
|
||||
return [422, {"Content-Type" => "text/plain"}, ["Hostname malformed"]] if state == :hostname_malformed
|
||||
# OKs
|
||||
return [200, {"Content-Type" => "text/plain"}, ["Good"]] if state == :good
|
||||
return [200, {"Content-Type" => "text/plain"}, ["No change"]] if state == :nochg
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
21
lib/dyndnsd/updater/command_with_bind_zone.rb
Normal file
21
lib/dyndnsd/updater/command_with_bind_zone.rb
Normal file
@ -0,0 +1,21 @@
|
||||
|
||||
module Dyndnsd
|
||||
module Updater
|
||||
class CommandWithBindZone
|
||||
def initialize(config)
|
||||
@zone_file = config['zone_file']
|
||||
@command = config['command']
|
||||
@generator = Generator::Bind.new(config)
|
||||
end
|
||||
|
||||
def update(zone)
|
||||
# write zone file in bind syntax
|
||||
File.open(@zone_file, 'w') { |f| f.write(@generator.generate(zone)) }
|
||||
# call user-defined command
|
||||
pid = fork do
|
||||
exec @command
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
4
lib/dyndnsd/version.rb
Normal file
4
lib/dyndnsd/version.rb
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
module Dyndnsd
|
||||
VERSION = "0.0.1"
|
||||
end
|
Reference in New Issue
Block a user