2024-03-14 04:36:03 +01:00
|
|
|
require "authd"
|
2024-03-12 21:51:59 +01:00
|
|
|
require "ipc"
|
2024-07-01 11:56:50 +02:00
|
|
|
|
2024-03-14 04:36:03 +01:00
|
|
|
require "../src/client.cr"
|
2024-03-12 02:36:01 +01:00
|
|
|
require "http/server"
|
|
|
|
|
|
|
|
port = ARGV[0].to_i rescue 8080
|
|
|
|
addr = ARGV[1] rescue "127.0.0.1"
|
|
|
|
|
2024-03-14 04:36:03 +01:00
|
|
|
#login = ENV["LOGIN"]
|
|
|
|
#pass = ENV["PASS"]
|
|
|
|
|
|
|
|
dnsmanagerd = DNSManager::Client.new
|
|
|
|
#dnsmanagerd.login login, pass
|
2024-03-12 02:36:01 +01:00
|
|
|
|
2024-03-12 21:51:59 +01:00
|
|
|
server = HTTP::Server.new do |context|
|
2024-03-31 17:45:43 +02:00
|
|
|
ip = case addr
|
|
|
|
when "127.0.0.1"
|
2024-03-31 17:57:11 +02:00
|
|
|
a = context.request.remote_address.not_nil!
|
|
|
|
case a
|
|
|
|
when Socket::IPAddress
|
|
|
|
a.address.to_s
|
|
|
|
when Socket::UNIXAddress
|
|
|
|
next
|
|
|
|
end
|
2024-03-31 17:45:43 +02:00
|
|
|
else
|
|
|
|
context.request.headers["Host"] rescue next
|
|
|
|
end
|
2024-03-12 02:36:01 +01:00
|
|
|
|
2024-03-31 17:57:11 +02:00
|
|
|
next if ip.nil?
|
|
|
|
pp! ip
|
|
|
|
|
2024-03-12 21:51:59 +01:00
|
|
|
case context.request.path
|
2024-03-14 04:36:03 +01:00
|
|
|
when /^\/token-update\/(?<token>[a-z0-9-]+)/
|
2024-03-12 02:36:01 +01:00
|
|
|
token = $~["token"]
|
|
|
|
if token.nil?
|
|
|
|
context.response.status_code = 404
|
|
|
|
context.response.print "invalid token"
|
|
|
|
else
|
|
|
|
context.response.content_type = "text/html"
|
2024-03-31 17:57:11 +02:00
|
|
|
context.response.print "token: #{token} from #{ip}"
|
2024-03-14 04:36:03 +01:00
|
|
|
begin
|
2024-03-31 17:45:43 +02:00
|
|
|
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
|
2024-03-14 04:36:03 +01:00
|
|
|
rescue e
|
|
|
|
puts "error while updating token: #{e}"
|
|
|
|
context.response.print "; The command failed!" # ⚠
|
|
|
|
end
|
2024-03-12 02:36:01 +01:00
|
|
|
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
|