0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 18:22:50 +01:00

ircd:Ⓜ️:fed: Add well-known request for server name resolution w/ console cmd.

This commit is contained in:
Jason Volk 2020-03-06 18:01:14 -08:00
parent fa19b9b182
commit 6ab71b8fdc
3 changed files with 123 additions and 0 deletions

View file

@ -14,6 +14,7 @@
namespace ircd::m::fed namespace ircd::m::fed
{ {
net::hostport matrix_service(net::hostport); net::hostport matrix_service(net::hostport);
net::hostport well_known(const mutable_buffer &out, const net::hostport &remote);
id::event::buf fetch_head(const id::room &room_id, const net::hostport &remote, const id::user &); id::event::buf fetch_head(const id::room &room_id, const net::hostport &remote, const id::user &);
id::event::buf fetch_head(const id::room &room_id, const net::hostport &remote); id::event::buf fetch_head(const id::room &room_id, const net::hostport &remote);
} }

View file

@ -1736,3 +1736,94 @@ ircd::m::fed::fetch_head(const id::room &room_id,
return prev_event_id; return prev_event_id;
} }
ircd::net::hostport
ircd::m::fed::well_known(const mutable_buffer &buf,
const net::hostport &remote)
try
{
const unique_buffer<mutable_buffer> head_buf
{
16_KiB
};
window_buffer wb{head_buf};
http::request
{
wb, host(remote), "GET", "/.well-known/matrix/server",
};
// Hard target https service; do not inherit matrix service from remote.
const net::hostport target
{
host(remote), "https", port(remote)
};
const const_buffer out_head
{
wb.completed()
};
// Remaining space in buffer is used for received head
const mutable_buffer in_head
{
buf + size(out_head)
};
server::request::opts opts;
server::request request
{
target,
{ out_head },
{ in_head, buf },
&opts
};
const auto code
{
request.get(seconds(8)) //TODO: XXX conf
};
const json::object &response
{
request.in.content
};
const json::string &m_server
{
response["m.server"]
};
const net::hostport ret
{
m_server
};
thread_local char rembuf[rfc3986::DOMAIN_BUFSIZE * 2];
log::debug
{
log, "Well-known matrix server query to %s %u %s :%s",
string(rembuf, target),
uint(code),
http::status(code),
string_view{response},
};
return matrix_service(ret);
}
catch(const ctx::interrupted &)
{
throw;
}
catch(const std::exception &e)
{
thread_local char rembuf[rfc3986::DOMAIN_BUFSIZE * 2];
log::derror
{
log, "Matrix server well-known query for %s :%s",
string(rembuf, remote),
e.what(),
};
return remote;
}

View file

@ -14680,3 +14680,34 @@ console_cmd__redact(opt &out, const string_view &line)
<< std::endl; << std::endl;
return true; return true;
} }
//
// well-known
//
bool
console_cmd__well_known__matrix__server(opt &out, const string_view &line)
{
const params param{line, " ",
{
"remote"
}};
const net::hostport &remote
{
param.at("remote")
};
const unique_buffer<mutable_buffer> buf
{
1_KiB
};
const net::hostport result
{
m::fed::well_known(buf, remote)
};
out << result << std::endl;
return true;
}