dnsmanager/tools/token-handler.cr

57 lines
1.5 KiB
Crystal

require "authd"
require "ipc"
require "../src/client.cr"
require "http/server"
port = ARGV[0].to_i rescue 8080
addr = ARGV[1] rescue "127.0.0.1"
#login = ENV["LOGIN"]
#pass = ENV["PASS"]
dnsmanagerd = DNSManager::Client.new
#dnsmanagerd.login login, pass
server = HTTP::Server.new do |context|
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\/(?<token>[a-z0-9-]+)/
token = $~["token"]
if token.nil?
context.response.status_code = 404
context.response.print "invalid token"
else
context.response.content_type = "text/html"
context.response.print "coucou, token: #{token} from #{ip}"
begin
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!" # ⚠
end
end
else
context.response.status_code = 404
context.response.print "Not found"
end
end
address = server.bind_tcp addr, port
puts "Receiving tokens on http://#{address}/token-update/<token>"
server.listen