2020-06-08 20:19:29 +02:00
|
|
|
// The Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) The Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2020 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
namespace ircd::m::fed::well_known
|
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
static net::hostport make_remote(const string_view &);
|
2020-10-10 15:57:34 +02:00
|
|
|
static void submit(request &);
|
|
|
|
static void receive(request &);
|
2020-10-15 13:03:53 +02:00
|
|
|
static void finish(request &);
|
2022-08-17 20:27:07 +02:00
|
|
|
static bool redirect(request &);
|
2020-10-15 13:03:53 +02:00
|
|
|
static bool handle(request &);
|
|
|
|
static void worker();
|
|
|
|
|
|
|
|
static server::request request_skip;
|
|
|
|
extern ctx::dock worker_dock;
|
|
|
|
extern ctx::context worker_context;
|
|
|
|
extern run::changed handle_quit;
|
2020-06-08 20:19:29 +02:00
|
|
|
extern log::log log;
|
|
|
|
}
|
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::m::fed::well_known::request>::allocator)
|
|
|
|
ircd::util::instance_list<ircd::m::fed::well_known::request>::allocator
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::m::fed::well_known::request>::list)
|
|
|
|
ircd::util::instance_list<ircd::m::fed::well_known::request>::list
|
2020-10-10 15:57:34 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
allocator
|
2020-10-10 15:57:34 +02:00
|
|
|
};
|
|
|
|
|
2020-06-08 20:19:29 +02:00
|
|
|
decltype(ircd::m::fed::well_known::log)
|
|
|
|
ircd::m::fed::well_known::log
|
|
|
|
{
|
|
|
|
"m.well-known"
|
|
|
|
};
|
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
decltype(ircd::m::fed::well_known::cache_default)
|
|
|
|
ircd::m::fed::well_known::cache_default
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.fed.well-known.cache.default" },
|
|
|
|
{ "default", 24 * 60 * 60L },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::fed::well_known::cache_error)
|
|
|
|
ircd::m::fed::well_known::cache_error
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.fed.well-known.cache.error" },
|
|
|
|
{ "default", 36 * 60 * 60L },
|
|
|
|
};
|
|
|
|
|
|
|
|
///NOTE: not yet used until HTTP cache headers in response are respected.
|
|
|
|
decltype(ircd::m::fed::well_known::cache_max)
|
|
|
|
ircd::m::fed::well_known::cache_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.fed.well-known.cache.max" },
|
|
|
|
{ "default", 48 * 60 * 60L },
|
|
|
|
};
|
|
|
|
|
2020-10-10 15:57:34 +02:00
|
|
|
decltype(ircd::m::fed::well_known::request::path)
|
|
|
|
ircd::m::fed::well_known::request::path
|
|
|
|
{
|
|
|
|
"/.well-known/matrix/server"
|
|
|
|
};
|
|
|
|
|
2020-10-15 10:33:14 +02:00
|
|
|
decltype(ircd::m::fed::well_known::request::type)
|
|
|
|
ircd::m::fed::well_known::request::type
|
|
|
|
{
|
|
|
|
"well-known.matrix.server"
|
|
|
|
};
|
|
|
|
|
2020-10-10 15:57:34 +02:00
|
|
|
decltype(ircd::m::fed::well_known::request::sopts)
|
|
|
|
ircd::m::fed::well_known::request::sopts
|
|
|
|
{
|
|
|
|
false // http_exceptions
|
|
|
|
};
|
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
decltype(ircd::m::fed::well_known::request::timeout)
|
|
|
|
ircd::m::fed::well_known::request::timeout
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
{ "name", "ircd.m.fed.well-known.request.timeout" },
|
|
|
|
{ "default", 15L },
|
2020-06-08 20:19:29 +02:00
|
|
|
};
|
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
decltype(ircd::m::fed::well_known::request::redirects_max)
|
|
|
|
ircd::m::fed::well_known::request::redirects_max
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
{ "name", "ircd.m.fed.well-known.request.redirects.max" },
|
|
|
|
{ "default", 2L },
|
2020-06-08 20:19:29 +02:00
|
|
|
};
|
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
decltype(ircd::m::fed::well_known::request::id_ctr)
|
|
|
|
ircd::m::fed::well_known::request::id_ctr;
|
|
|
|
|
|
|
|
decltype(ircd::m::fed::well_known::request::mutex)
|
|
|
|
ircd::m::fed::well_known::request::mutex;
|
|
|
|
|
|
|
|
decltype(ircd::m::fed::well_known::worker_dock)
|
|
|
|
ircd::m::fed::well_known::worker_dock;
|
2020-06-08 20:19:29 +02:00
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
decltype(ircd::m::fed::well_known::worker_context)
|
|
|
|
ircd::m::fed::well_known::worker_context
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
"m.fed.well_known",
|
|
|
|
512_KiB,
|
|
|
|
&worker,
|
|
|
|
context::POST
|
2020-06-08 20:19:29 +02:00
|
|
|
};
|
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
decltype(ircd::m::fed::well_known::handle_quit)
|
|
|
|
ircd::m::fed::well_known::handle_quit
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2022-06-24 04:18:05 +02:00
|
|
|
run::level::QUIT, []() noexcept
|
2020-10-15 13:03:53 +02:00
|
|
|
{
|
|
|
|
worker_dock.notify_all();
|
|
|
|
}
|
2020-06-08 20:19:29 +02:00
|
|
|
};
|
|
|
|
|
2020-10-15 10:33:14 +02:00
|
|
|
ircd::ctx::future<ircd::string_view>
|
2020-06-08 20:19:29 +02:00
|
|
|
ircd::m::fed::well_known::get(const mutable_buffer &buf,
|
2020-10-15 13:03:53 +02:00
|
|
|
const string_view &target,
|
2020-10-14 15:59:30 +02:00
|
|
|
const opts &opts)
|
2020-06-08 20:19:29 +02:00
|
|
|
try
|
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
const m::room::id::buf cache_room_id
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
|
|
|
"dns", m::my_host()
|
|
|
|
};
|
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
const m::room cache_room
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
cache_room_id
|
2020-06-08 20:19:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::idx event_idx
|
|
|
|
{
|
2020-10-14 15:59:30 +02:00
|
|
|
likely(opts.cache_check)?
|
2020-10-15 13:03:53 +02:00
|
|
|
cache_room.get(std::nothrow, request::type, target):
|
2020-10-14 15:59:30 +02:00
|
|
|
0UL
|
2020-06-08 20:19:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const milliseconds origin_server_ts
|
|
|
|
{
|
|
|
|
m::get<time_t>(std::nothrow, event_idx, "origin_server_ts", time_t(0))
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object content
|
|
|
|
{
|
2020-12-10 06:09:33 +01:00
|
|
|
m::get(std::nothrow, event_idx, "content", buf)
|
2020-06-08 20:19:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const seconds ttl
|
|
|
|
{
|
|
|
|
content.get<time_t>("ttl", time_t(86400))
|
|
|
|
};
|
|
|
|
|
|
|
|
const system_point expires
|
|
|
|
{
|
|
|
|
origin_server_ts + ttl
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool expired
|
|
|
|
{
|
|
|
|
ircd::now<system_point>() > expires
|
|
|
|
};
|
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
const json::string cached
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
content["m.server"]
|
2020-06-08 20:19:29 +02:00
|
|
|
};
|
|
|
|
|
2020-10-06 06:52:22 +02:00
|
|
|
const bool valid
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
// entry must not be blank
|
2020-10-06 06:52:22 +02:00
|
|
|
!empty(cached)
|
2020-06-08 20:19:29 +02:00
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
// entry must not be expired unless options allow expired hits
|
|
|
|
&& (!expired || opts.expired)
|
|
|
|
};
|
2020-06-08 20:19:29 +02:00
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
// Branch to return cache hit
|
|
|
|
if(likely(valid))
|
2020-10-15 10:33:14 +02:00
|
|
|
return ctx::future<string_view>
|
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
ctx::already, string_view
|
|
|
|
{
|
|
|
|
data(buf), move(buf, cached)
|
|
|
|
}
|
2020-10-15 10:33:14 +02:00
|
|
|
};
|
2020-06-08 20:19:29 +02:00
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
const net::hostport remote
|
2020-10-14 15:59:30 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
make_remote(target)
|
2020-10-14 15:59:30 +02:00
|
|
|
};
|
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
const bool fetch
|
2020-10-06 06:52:22 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
// options must allow network request
|
|
|
|
opts.request
|
2020-10-06 06:52:22 +02:00
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
// check if the peer already has a cached error in server::
|
|
|
|
&& !server::errant(remote)
|
2020-06-08 20:19:29 +02:00
|
|
|
};
|
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
// Branch if won't fetch; return target itself as result
|
|
|
|
if(!fetch)
|
|
|
|
return ctx::future<string_view>
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
ctx::already, string_view
|
|
|
|
{
|
|
|
|
data(buf), move(buf, target)
|
|
|
|
}
|
2020-06-08 20:19:29 +02:00
|
|
|
};
|
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
if(opts.cache_check)
|
|
|
|
{
|
|
|
|
char tmbuf[48];
|
|
|
|
log::dwarning
|
2020-10-15 10:33:14 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
log, "%s cache invalid %s event_idx:%u expires %s",
|
|
|
|
target,
|
|
|
|
cached?: json::string{"<not found>"},
|
|
|
|
event_idx,
|
|
|
|
cached? timef(tmbuf, expires, localtime): "<never>"_sv,
|
2020-10-15 10:33:14 +02:00
|
|
|
};
|
2020-06-08 20:19:29 +02:00
|
|
|
}
|
|
|
|
|
2023-02-25 05:10:17 +01:00
|
|
|
// Check for duplicate requests
|
2023-02-24 02:07:27 +01:00
|
|
|
for(const auto &req : request::list)
|
|
|
|
if(req->target == target)
|
2023-02-25 05:10:17 +01:00
|
|
|
{
|
|
|
|
//XXX: Support multiple result buffers.
|
|
|
|
if(data(req->out) != data(buf))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Ride any duplicate request already pending
|
2023-02-24 02:07:27 +01:00
|
|
|
return ctx::future<string_view>
|
|
|
|
{
|
|
|
|
req->promise
|
|
|
|
};
|
2023-02-25 05:10:17 +01:00
|
|
|
}
|
2023-02-24 02:07:27 +01:00
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
// Synchronize modification of the request::list
|
|
|
|
const std::lock_guard request_lock
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
request::mutex
|
|
|
|
};
|
2020-06-08 20:19:29 +02:00
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
// Start request
|
|
|
|
auto req
|
|
|
|
{
|
|
|
|
std::make_unique<request>()
|
|
|
|
};
|
2020-06-08 20:19:29 +02:00
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
// req->target is a copy in the request struct so the caller can disappear.
|
|
|
|
req->target = string_view
|
|
|
|
{
|
2020-10-21 05:08:33 +02:00
|
|
|
req->tgtbuf[0], copy(req->tgtbuf[0], target)
|
|
|
|
};
|
|
|
|
|
|
|
|
// req->m_server is a copy in the request struct so the caller can disappear.
|
|
|
|
req->m_server = string_view
|
|
|
|
{
|
|
|
|
req->tgtbuf[1], copy(req->tgtbuf[1], cached)
|
2020-10-15 13:03:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// but req->out is not safe once the caller destroys their future, which
|
|
|
|
// is indicated by req->promise's invalidation. Do not write to this
|
|
|
|
// unless the promise is valid.
|
|
|
|
req->out = buf;
|
|
|
|
|
|
|
|
// all other properties are independent of the caller
|
|
|
|
req->opts = opts;
|
|
|
|
req->expires = expires;
|
|
|
|
req->uri.path = request::path;
|
|
|
|
req->uri.remote = req->target;
|
|
|
|
ctx::future<string_view> ret{req->promise}; try
|
|
|
|
{
|
|
|
|
submit(*req);
|
|
|
|
req.release();
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
2020-10-14 15:59:30 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
log, "request submit for %s :%s",
|
|
|
|
target,
|
|
|
|
e.what(),
|
2020-10-14 15:59:30 +02:00
|
|
|
};
|
2020-10-15 13:03:53 +02:00
|
|
|
|
|
|
|
const ctx::exception_handler eh;
|
|
|
|
finish(*req);
|
2020-10-14 15:59:30 +02:00
|
|
|
}
|
2020-06-08 20:19:29 +02:00
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
return ret;
|
2020-06-08 20:19:29 +02:00
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
log, "get %s :%s",
|
|
|
|
target,
|
2020-06-08 20:19:29 +02:00
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
2020-10-15 10:33:14 +02:00
|
|
|
return ctx::future<string_view>
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2020-10-15 10:33:14 +02:00
|
|
|
ctx::already, string_view
|
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
data(buf), move(buf, target)
|
2020-10-15 10:33:14 +02:00
|
|
|
}
|
2020-06-08 20:19:29 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
void
|
|
|
|
ircd::m::fed::well_known::worker()
|
2020-06-08 20:19:29 +02:00
|
|
|
try
|
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
// Wait for runlevel RUN before proceeding...
|
|
|
|
run::barrier<ctx::interrupted>{};
|
|
|
|
while(!request::list.empty() || run::level == run::level::RUN)
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2022-06-24 04:18:05 +02:00
|
|
|
worker_dock.wait([]() noexcept
|
2020-10-15 13:03:53 +02:00
|
|
|
{
|
|
|
|
return !request::list.empty() || run::level != run::level::RUN;
|
|
|
|
});
|
2020-06-08 20:19:29 +02:00
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
if(request::list.empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
auto next
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
ctx::when_any(std::begin(request::list), std::end(request::list), []
|
|
|
|
(auto &it) -> server::request &
|
|
|
|
{
|
|
|
|
return !(*it)->req? request_skip: (*it)->req;
|
|
|
|
})
|
|
|
|
};
|
2020-06-08 20:19:29 +02:00
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
const ctx::uninterruptible::nothrow ui;
|
|
|
|
if(!next.wait(milliseconds(250), std::nothrow))
|
|
|
|
continue;
|
2020-06-08 20:19:29 +02:00
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
next.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(it != std::end(request::list));
|
|
|
|
if(unlikely(it == std::end(request::list)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const std::lock_guard request_lock
|
|
|
|
{
|
|
|
|
request::mutex
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<request> req
|
|
|
|
{
|
|
|
|
*it
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!handle(*req)) // redirect
|
|
|
|
req.release();
|
|
|
|
else
|
|
|
|
finish(*req);
|
2020-06-08 20:19:29 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
assert(request::list.empty());
|
2020-06-08 20:19:29 +02:00
|
|
|
}
|
2020-12-01 05:34:51 +01:00
|
|
|
catch(const ctx::interrupted &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
2020-06-08 20:19:29 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
log::critical
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
log, "Worker unhandled :%s",
|
2020-06-08 20:19:29 +02:00
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-17 20:27:07 +02:00
|
|
|
/// Returns true if the request is done with either success or error; false
|
|
|
|
/// if the request resubmitted itself.
|
2020-10-15 13:03:53 +02:00
|
|
|
bool
|
2020-10-10 15:57:34 +02:00
|
|
|
ircd::m::fed::well_known::handle(request &req)
|
2020-10-15 13:03:53 +02:00
|
|
|
try
|
2020-10-10 15:57:34 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
receive(req);
|
|
|
|
|
2022-08-17 20:27:07 +02:00
|
|
|
// Handle redirection.
|
|
|
|
if(http::category(req.code) == http::category::REDIRECT)
|
|
|
|
return redirect(req);
|
2020-10-10 15:57:34 +02:00
|
|
|
|
2022-08-17 20:27:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "%s handling :%s",
|
|
|
|
req.target,
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-10 15:57:34 +02:00
|
|
|
|
2022-08-17 20:27:07 +02:00
|
|
|
bool
|
|
|
|
ircd::m::fed::well_known::redirect(request &req)
|
|
|
|
{
|
2020-10-10 15:57:34 +02:00
|
|
|
// Indirection code, but no location response header
|
|
|
|
if(!req.location)
|
2020-10-15 13:03:53 +02:00
|
|
|
return true;
|
2020-10-10 15:57:34 +02:00
|
|
|
|
|
|
|
// Redirection; carry over the new target by copying it because it's
|
|
|
|
// in the buffer which we'll be overwriting for the new request.
|
|
|
|
req.carry = unique_mutable_buffer{req.location};
|
|
|
|
req.uri = string_view{req.carry};
|
|
|
|
|
|
|
|
// Indirection code, bad location header.
|
|
|
|
if(!req.uri.path || !req.uri.remote)
|
2020-10-15 13:03:53 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if(req.redirects++ >= request::redirects_max)
|
|
|
|
return true;
|
2020-10-10 15:57:34 +02:00
|
|
|
|
|
|
|
// Redirect
|
2020-10-15 13:03:53 +02:00
|
|
|
submit(req);
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-10 15:57:34 +02:00
|
|
|
|
|
|
|
void
|
2020-10-15 13:03:53 +02:00
|
|
|
ircd::m::fed::well_known::finish(request &req)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
json::string result;
|
|
|
|
const unwind resolve{[&req, &result]
|
|
|
|
{
|
|
|
|
if(req.promise.valid())
|
|
|
|
req.promise.set_value(string_view
|
|
|
|
{
|
|
|
|
data(req.out), move(req.out, result?: req.target)
|
|
|
|
});
|
|
|
|
}};
|
|
|
|
|
2020-10-21 05:08:33 +02:00
|
|
|
if(req.code == 200 && json::valid(req.response, std::nothrow))
|
2020-10-15 13:03:53 +02:00
|
|
|
result = req.response["m.server"];
|
|
|
|
|
|
|
|
if(!result)
|
|
|
|
result = req.m_server;
|
|
|
|
|
|
|
|
if(!result)
|
|
|
|
result = req.target;
|
|
|
|
|
2022-08-09 22:14:24 +02:00
|
|
|
if(!rfc3986::valid_remote(std::nothrow, result))
|
|
|
|
result = req.target;
|
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
if(result != req.target)
|
|
|
|
log::debug
|
|
|
|
{
|
2020-10-21 05:08:33 +02:00
|
|
|
log, "query to %s for %s resolved delegation to %s",
|
2020-10-15 13:03:53 +02:00
|
|
|
req.uri.remote,
|
|
|
|
req.target,
|
|
|
|
result,
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool cache_expired
|
|
|
|
{
|
2020-10-16 22:34:12 +02:00
|
|
|
req.expires < now<system_point>()
|
2020-10-15 13:03:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const bool cache_result
|
|
|
|
{
|
|
|
|
result
|
|
|
|
&& req.opts.cache_result
|
|
|
|
&& req.opts.request
|
|
|
|
&& (cache_expired || result != req.m_server)
|
|
|
|
};
|
|
|
|
|
|
|
|
req.m_server = result;
|
|
|
|
if(!cache_result)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Any time the well-known result is the same as the req.target (that
|
|
|
|
// includes legitimate errors where fetch_well_known() returns the
|
|
|
|
// req.target to default) we consider that an error and use the error
|
|
|
|
// TTL value. Sorry, no exponential backoff implemented yet.
|
|
|
|
const auto cache_ttl
|
|
|
|
{
|
|
|
|
req.target == req.m_server?
|
|
|
|
seconds(cache_error).count():
|
|
|
|
seconds(cache_default).count()
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::id::buf cache_room_id
|
|
|
|
{
|
|
|
|
"dns", m::my_host()
|
|
|
|
};
|
|
|
|
|
|
|
|
// Write our record to the cache room; note that this doesn't really
|
|
|
|
// match the format of other DNS records in this room since it's a bit
|
|
|
|
// simpler, but we don't share the ircd.dns.rr type prefix anyway.
|
|
|
|
const auto cache_id
|
|
|
|
{
|
|
|
|
m::send(cache_room_id, m::me(), request::type, req.target, json::members
|
|
|
|
{
|
|
|
|
{ "ttl", cache_ttl },
|
|
|
|
{ "m.server", req.m_server },
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%s cached delegation to %s with %s ttl:%ld",
|
|
|
|
req.target,
|
|
|
|
req.m_server,
|
|
|
|
string_view{cache_id},
|
|
|
|
cache_ttl,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
2020-10-04 04:00:23 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
log::error
|
2020-10-04 04:00:23 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
log, "%s completion :%s",
|
|
|
|
req.target,
|
|
|
|
e.what(),
|
2020-10-04 04:00:23 +02:00
|
|
|
};
|
2020-10-15 13:03:53 +02:00
|
|
|
}
|
2020-10-04 04:00:23 +02:00
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
void
|
|
|
|
ircd::m::fed::well_known::receive(request &req)
|
|
|
|
{
|
2020-10-29 12:14:50 +01:00
|
|
|
assert(req.sopts.http_exceptions == false);
|
2020-10-15 13:03:53 +02:00
|
|
|
req.code = req.req.get(seconds(request::timeout));
|
2020-10-10 15:57:34 +02:00
|
|
|
req.head = req.req.in.gethead(req.req);
|
|
|
|
req.location = req.head.location;
|
|
|
|
req.response = json::object
|
2020-10-04 04:00:23 +02:00
|
|
|
{
|
2020-10-10 15:57:34 +02:00
|
|
|
req.req.in.content
|
2020-10-04 04:00:23 +02:00
|
|
|
};
|
|
|
|
|
2020-10-10 15:57:34 +02:00
|
|
|
log::debug
|
2020-10-04 04:00:23 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
log, "fetch to %s %s :%u %s",
|
2020-10-10 15:57:34 +02:00
|
|
|
req.uri.remote,
|
|
|
|
req.uri.path,
|
|
|
|
uint(req.code),
|
|
|
|
http::status(req.code),
|
2020-10-04 04:00:23 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-10 15:57:34 +02:00
|
|
|
void
|
|
|
|
ircd::m::fed::well_known::submit(request &req)
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2020-10-10 15:57:34 +02:00
|
|
|
const net::hostport target
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2020-10-15 13:03:53 +02:00
|
|
|
make_remote(req.uri.remote)
|
2020-06-08 20:19:29 +02:00
|
|
|
};
|
|
|
|
|
2020-06-08 22:20:03 +02:00
|
|
|
const http::header headers[]
|
|
|
|
{
|
|
|
|
{ "User-Agent", info::user_agent },
|
|
|
|
};
|
|
|
|
|
2022-05-21 02:39:22 +02:00
|
|
|
window_buffer window
|
2020-10-10 15:57:34 +02:00
|
|
|
{
|
|
|
|
req.buf
|
|
|
|
};
|
|
|
|
|
2020-06-08 20:19:29 +02:00
|
|
|
http::request
|
|
|
|
{
|
2022-05-21 02:39:22 +02:00
|
|
|
window, host(target), "GET", req.uri.path, 0, {}, headers
|
2020-06-08 20:19:29 +02:00
|
|
|
};
|
|
|
|
|
2020-08-02 10:57:56 +02:00
|
|
|
server::out out;
|
2022-05-21 02:39:22 +02:00
|
|
|
out.head = window.completed();
|
2020-06-08 20:19:29 +02:00
|
|
|
|
|
|
|
// Remaining space in buffer is used for received head; note that below
|
|
|
|
// we specify this same buffer for in.content, but that's a trick
|
|
|
|
// recognized by ircd::server to place received content directly after
|
|
|
|
// head in this buffer without any additional dynamic allocation.
|
2020-08-02 10:57:56 +02:00
|
|
|
server::in in;
|
2020-10-15 13:03:53 +02:00
|
|
|
in.head = mutable_buffer{req.buf} + size(out.head);
|
2020-08-02 10:57:56 +02:00
|
|
|
in.content = in.head;
|
2020-06-08 20:19:29 +02:00
|
|
|
|
2020-10-15 13:03:53 +02:00
|
|
|
req.code = http::code(0);
|
|
|
|
req.head = {};
|
|
|
|
req.response = {};
|
|
|
|
req.location = {};
|
2020-10-10 15:57:34 +02:00
|
|
|
req.req = server::request
|
2020-06-08 20:19:29 +02:00
|
|
|
{
|
2020-10-10 15:57:34 +02:00
|
|
|
target, std::move(out), std::move(in), &req.sopts
|
2020-06-08 20:19:29 +02:00
|
|
|
};
|
2020-10-15 13:03:53 +02:00
|
|
|
|
|
|
|
worker_dock.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::net::hostport
|
|
|
|
ircd::m::fed::well_known::make_remote(const string_view &target)
|
|
|
|
{
|
|
|
|
const net::hostport remote
|
|
|
|
{
|
|
|
|
target
|
|
|
|
};
|
|
|
|
|
|
|
|
// Hard target https service; do not inherit any matrix service from remote.
|
|
|
|
const net::hostport ret
|
|
|
|
{
|
|
|
|
net::host(remote), "https", net::port(remote)
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
2020-06-08 20:19:29 +02:00
|
|
|
}
|