2020-12-12 05:38:16 +01:00
|
|
|
require "../../requests/*"
|
|
|
|
|
2023-02-15 19:21:49 +01:00
|
|
|
class DNSManager::Client < IPC
|
|
|
|
property server_fd : Int32 = -1
|
|
|
|
|
2020-12-12 05:38:16 +01:00
|
|
|
def initialize
|
2023-02-15 19:21:49 +01:00
|
|
|
super()
|
|
|
|
fd = self.connect "dnsmanager"
|
|
|
|
if fd.nil?
|
|
|
|
raise "couldn't connect to 'auth' IPC service"
|
|
|
|
end
|
|
|
|
@server_fd = fd
|
2020-12-12 05:38:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: parse_message should raise exception if response not anticipated
|
|
|
|
def parse_message(expected_messages, message)
|
|
|
|
em = Array(IPC::JSON.class).new
|
|
|
|
expected_messages.each do |e|
|
|
|
|
em << e
|
|
|
|
end
|
|
|
|
em << DNSManager::Response::Error
|
|
|
|
em.parse_ipc_json message
|
|
|
|
end
|
|
|
|
|
2023-02-15 19:21:49 +01:00
|
|
|
#
|
|
|
|
# Simple users.
|
|
|
|
#
|
2020-12-12 05:38:16 +01:00
|
|
|
|
|
|
|
def login(token : String)
|
|
|
|
request = DNSManager::Request::Login.new token
|
2023-02-15 19:21:49 +01:00
|
|
|
send_now request
|
2023-05-07 20:23:34 +02:00
|
|
|
parse_message [ Response::Success ], read
|
2020-12-12 05:38:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Adding a full zone.
|
|
|
|
def user_zone_add(zone : DNSManager::Storage::Zone)
|
2020-12-13 03:56:32 +01:00
|
|
|
request = DNSManager::Request::AddOrUpdateZone.new zone
|
2023-02-15 19:21:49 +01:00
|
|
|
send_now request
|
2023-05-07 20:23:34 +02:00
|
|
|
parse_message [ Response::Success ], read
|
|
|
|
end
|
|
|
|
|
|
|
|
# Removing a zone.
|
|
|
|
def user_zone_del(domain : String)
|
|
|
|
request = DNSManager::Request::DeleteZone.new domain
|
|
|
|
send_now request
|
|
|
|
parse_message [ Response::Success ], read
|
2020-12-12 05:38:16 +01:00
|
|
|
end
|
|
|
|
|
2023-05-07 21:05:53 +02:00
|
|
|
# Get a zone.
|
|
|
|
def user_zone_get(domain : String)
|
|
|
|
request = DNSManager::Request::GetZone.new domain
|
|
|
|
send_now request
|
|
|
|
parse_message [ Response::Zone ], read
|
|
|
|
end
|
|
|
|
|
2023-05-07 20:50:56 +02:00
|
|
|
# Get user domain list.
|
|
|
|
def user_domain_list()
|
|
|
|
request = DNSManager::Request::UserDomains.new
|
|
|
|
send_now request
|
|
|
|
parse_message [ Response::DomainList ], read
|
|
|
|
end
|
|
|
|
|
2023-02-15 19:21:49 +01:00
|
|
|
#
|
|
|
|
# Admin stuff.
|
|
|
|
#
|
|
|
|
|
2023-05-06 23:18:59 +02:00
|
|
|
def admin_maintenance(key : String, subject : DNSManager::Request::Maintenance::Subject, value : Int32? = nil)
|
|
|
|
request = DNSManager::Request::Maintenance.new(key,subject)
|
2020-12-12 05:38:16 +01:00
|
|
|
if value
|
|
|
|
request.value = value
|
|
|
|
end
|
2023-02-15 19:21:49 +01:00
|
|
|
send_now request
|
2020-12-12 05:38:16 +01:00
|
|
|
parse_message [ DNSManager::Response::Success ], read
|
|
|
|
end
|
2023-02-15 19:21:49 +01:00
|
|
|
|
|
|
|
def send_now(msg : IPC::JSON)
|
|
|
|
m = IPCMessage::TypedMessage.new msg.type.to_u8, msg.to_json
|
|
|
|
write @server_fd, m
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_now(type : Request::Type, payload)
|
|
|
|
m = IPCMessage::TypedMessage.new type.value.to_u8, payload
|
|
|
|
write @server_fd, m
|
|
|
|
end
|
|
|
|
|
|
|
|
def read
|
|
|
|
slice = self.read @server_fd
|
|
|
|
m = IPCMessage::TypedMessage.deserialize slice
|
|
|
|
m.not_nil!
|
|
|
|
end
|
2020-12-12 05:38:16 +01:00
|
|
|
end
|