From f63eff59a82c6f81e8850dded2f5fab39dfd8f22 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sun, 31 Mar 2024 17:45:43 +0200 Subject: [PATCH] Token handler: print a relevant error message. --- src/client/lib/dnsmanager-client.cr | 4 +--- tools/token-handler.cr | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/client/lib/dnsmanager-client.cr b/src/client/lib/dnsmanager-client.cr index 99d4d59..a98222c 100644 --- a/src/client/lib/dnsmanager-client.cr +++ b/src/client/lib/dnsmanager-client.cr @@ -147,9 +147,7 @@ class DNSManager::Client < IPC def use_token(token_uuid : String, address : String) request = Request::UseToken.new token_uuid, address send_now request - response = parse_message [ Response::Success ], read - raise "token update failed" unless response.class == Response::Success - response + parse_message [ Response::Success ], read end # diff --git a/tools/token-handler.cr b/tools/token-handler.cr index 119edca..ad1ccd5 100644 --- a/tools/token-handler.cr +++ b/tools/token-handler.cr @@ -13,8 +13,14 @@ dnsmanagerd = DNSManager::Client.new #dnsmanagerd.login login, pass server = HTTP::Server.new do |context| - pp! context.request.headers["Host"] - ip = context.request.headers["Host"] rescue next + ip = case addr + when "127.0.0.1" + pp! context.request.remote_address + context.request.remote_address.not_nil!.to_s + else + pp! context.request.headers["Host"] + context.request.headers["Host"] rescue next + end case context.request.path when /^\/token-update\/(?[a-z0-9-]+)/ @@ -26,8 +32,14 @@ server = HTTP::Server.new do |context| context.response.content_type = "text/html" context.response.print "coucou, token: #{token} from #{ip}" begin - dnsmanagerd.use_token token, ip - context.response.print "; Success!" # 🔻 + response = dnsmanagerd.use_token token, ip + case response + when DNSManager::Response::Success + context.response.print "; Success!" # 🔻 + when DNSManager::Response::Error + puts "error while updating token: #{response.reason}" + context.response.print "; command failed: #{response.reason}" # âš  + end rescue e puts "error while updating token: #{e}" context.response.print "; The command failed!" # âš