0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 10:12:39 +01:00

ircd::net: Simplify listener config object.

This commit is contained in:
Jason Volk 2018-03-23 13:10:00 -07:00
parent 439e212b90
commit 980d24b38e
4 changed files with 60 additions and 62 deletions

View file

@ -52,6 +52,6 @@ struct ircd::net::listener::acceptor
bool interrupt() noexcept; bool interrupt() noexcept;
void join() noexcept; void join() noexcept;
acceptor(const json::object &opts); acceptor(const string_view &name, const json::object &opts);
~acceptor() noexcept; ~acceptor() noexcept;
}; };

View file

@ -26,7 +26,7 @@ struct ircd::net::listener
std::shared_ptr<struct acceptor> acceptor; std::shared_ptr<struct acceptor> acceptor;
public: public:
explicit listener(const std::string &options); explicit listener(const string_view &name, const std::string &options);
listener(const json::object &options); listener(const string_view &name, const json::object &options);
~listener() noexcept; ~listener() noexcept;
}; };

View file

@ -167,8 +167,7 @@ ircd::m::init::modules()
namespace ircd::m namespace ircd::m
{ {
static void init_listener(const json::object &config, const json::object &opts, const string_view &bindaddr); static void init_listener(const json::object &config, const string_view &name, const json::object &conf);
static void init_listener(const json::object &config, const json::object &opts);
} }
void void
@ -186,58 +185,33 @@ ircd::m::init::listeners()
const json::array listeners const json::array listeners
{ {
config["listeners"] config[{"ircd", "listen"}]
}; };
if(m::listeners.empty()) for(const auto &name : listeners) try
init_listener(config, {}); {
else const json::object &opts
for(const json::object opts : listeners) {
init_listener(config, opts); config.at({"listen", unquote(name)})
};
init_listener(config, name, opts);
}
catch(const json::not_found &e)
{
throw ircd::user_error
{
"Failed to find configuration block for listener %s", name
};
}
} }
static void static void
ircd::m::init_listener(const json::object &config, ircd::m::init_listener(const json::object &config,
const string_view &name,
const json::object &opts) const json::object &opts)
{ {
const json::array binds m::listeners.emplace_back(name, opts);
{
opts["bind_addresses"]
};
if(binds.empty())
init_listener(config, opts, "0.0.0.0");
else
for(const auto &bindaddr : binds)
init_listener(config, opts, unquote(bindaddr));
}
static void
ircd::m::init_listener(const json::object &config,
const json::object &opts,
const string_view &host)
{
const json::array resources
{
opts["resources"]
};
// resources has multiple names with different configs which are being
// ignored :-/
const std::string name{"Matrix"s};
// Translate synapse options to our options (which reflect asio::ssl)
const json::strung options{json::members
{
{ "name", name },
{ "host", host },
{ "port", opts.get("port", 8448L) },
{ "ssl_certificate_file_pem", config["tls_certificate_path"] },
{ "ssl_private_key_file_pem", config["tls_private_key_path"] },
{ "ssl_tmp_dh_file", config["tls_dh_params_path"] },
}};
m::listeners.emplace_back(options);
} }
void void
@ -470,14 +444,24 @@ noexcept
void void
ircd::m::keys::init::certificate() ircd::m::keys::init::certificate()
{ {
const string_view origin
{
unquote(this->config.at({"ircd", "origin"}))
};
const json::object config
{
this->config.at({"origin", unquote(origin)})
};
const std::string private_key_file const std::string private_key_file
{ {
unquote(config.at("tls_private_key_path")) unquote(config.at("ssl_private_key_file_pem"))
}; };
const std::string public_key_file const std::string public_key_file
{ {
unquote(config.get("tls_public_key_path", private_key_file + ".pub")) unquote(config.get("ssl_public_key_file_pem", private_key_file + ".pub"))
}; };
if(!fs::exists(private_key_file)) if(!fs::exists(private_key_file))
@ -488,7 +472,7 @@ ircd::m::keys::init::certificate()
const std::string cert_file const std::string cert_file
{ {
unquote(config.at("tls_certificate_path")) unquote(config.at("ssl_certificate_file_pem"))
}; };
if(!fs::exists(cert_file)) if(!fs::exists(cert_file))

View file

@ -783,13 +783,21 @@ ircd::net::blocking(const socket &socket)
// net/listener.h // net/listener.h
// //
ircd::net::listener::listener(const std::string &opts) ircd::net::listener::listener(const string_view &name,
:listener{json::object{opts}} const std::string &opts)
:listener
{
name, json::object{opts}
}
{ {
} }
ircd::net::listener::listener(const json::object &opts) ircd::net::listener::listener(const string_view &name,
:acceptor{std::make_shared<struct acceptor>(opts)} const json::object &opts)
:acceptor
{
std::make_shared<struct acceptor>(name, opts)
}
{ {
// Starts the first asynchronous accept. This has to be done out here after // Starts the first asynchronous accept. This has to be done out here after
// the acceptor's shared object is constructed. // the acceptor's shared object is constructed.
@ -824,16 +832,18 @@ ircd::net::listener::acceptor::timeout
{ "default", 5000L }, { "default", 5000L },
}; };
ircd::net::listener::acceptor::acceptor(const json::object &opts) ircd::net::listener::acceptor::acceptor(const string_view &name,
const json::object &opts)
try try
:name :name
{ {
unquote(opts.get("name", "IRCd (ssl)"s)) name
} }
,backlog ,backlog
{ {
//TODO: XXX
//boost::asio::ip::tcp::socket::max_connections <-- linkage failed? //boost::asio::ip::tcp::socket::max_connections <-- linkage failed?
opts.get<size_t>("backlog", SOMAXCONN) //TODO: XXX std::min(opts.get<uint>("backlog", SOMAXCONN), uint(SOMAXCONN))
} }
,ssl ,ssl
{ {
@ -842,7 +852,7 @@ try
,ep ,ep
{ {
ip::address::from_string(unquote(opts.get("host", "127.0.0.1"s))), ip::address::from_string(unquote(opts.get("host", "127.0.0.1"s))),
opts.at<uint16_t>("port") opts.get<uint16_t>("port", 8448L)
} }
,a ,a
{ {
@ -851,8 +861,9 @@ try
{ {
static const auto &max_connections static const auto &max_connections
{ {
//TODO: XXX
//boost::asio::ip::tcp::socket::max_connections <-- linkage failed? //boost::asio::ip::tcp::socket::max_connections <-- linkage failed?
SOMAXCONN //TODO: XXX std::min(opts.get<uint>("max_connections", SOMAXCONN), uint(SOMAXCONN))
}; };
static const ip::tcp::acceptor::reuse_address reuse_address static const ip::tcp::acceptor::reuse_address reuse_address
@ -882,7 +893,10 @@ try
} }
catch(const boost::system::system_error &e) catch(const boost::system::system_error &e)
{ {
throw error("listener: %s", e.what()); throw error
{
"listener: %s", e.what()
};
} }
ircd::net::listener::acceptor::~acceptor() ircd::net::listener::acceptor::~acceptor()