From bb0e2267609347e7bb901d33f22d50780c7f052e Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 2 Mar 2023 19:59:48 -0800 Subject: [PATCH] modules/admin: Implement GET /server_version. --- matrix/matrix.cc | 1 + modules/Makefile.am | 2 + modules/admin/server_version.cc | 158 ++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 modules/admin/server_version.cc diff --git a/matrix/matrix.cc b/matrix/matrix.cc index 8ad8336be..d891b9ac7 100644 --- a/matrix/matrix.cc +++ b/matrix/matrix.cc @@ -181,6 +181,7 @@ ircd::m::module_names "admin_users", "admin_deactivate", + "admin_server_version", }; /// This is a list of modules that are considered "optional" and any loading diff --git a/modules/Makefile.am b/modules/Makefile.am index cfa67572b..485f5f814 100644 --- a/modules/Makefile.am +++ b/modules/Makefile.am @@ -560,10 +560,12 @@ admin_moduledir = @moduledir@ admin_admin_users_la_SOURCES = admin/users.cc admin_admin_deactivate_la_SOURCES = admin/deactivate.cc +admin_admin_server_version_la_SOURCES = admin/server_version.cc admin_module_LTLIBRARIES = \ admin/admin_users.la \ admin/admin_deactivate.la \ + admin/admin_server_version.la \ ### ############################################################################### diff --git a/modules/admin/server_version.cc b/modules/admin/server_version.cc new file mode 100644 index 000000000..cf716fdc2 --- /dev/null +++ b/modules/admin/server_version.cc @@ -0,0 +1,158 @@ +// The Construct +// +// Copyright (C) The Construct Developers, Authors & Contributors +// Copyright (C) 2016-2023 Jason Volk +// +// 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::admin +{ + static void make_deps(json::stack &deps); + static resource::response handle_get(client &, const resource::request &); + + extern resource::method get_method; + extern resource server_version_resource; +}; + +ircd::mapi::header +IRCD_MODULE +{ + "Admin (undocumented) :Server Version" +}; + +decltype(ircd::m::admin::server_version_resource) +ircd::m::admin::server_version_resource +{ + "/_synapse/admin/v1/server_version", + { + "(Synapse) Admin Server Version" + } +}; + +decltype(ircd::m::admin::get_method) +ircd::m::admin::get_method +{ + server_version_resource, "GET", handle_get, + { + get_method.REQUIRES_OPER + } +}; + +ircd::m::resource::response +ircd::m::admin::handle_get(client &client, + const resource::request &request) +{ + const unique_mutable_buffer buf + { + 8_KiB + }; + + json::stack deps + { + buf + }; + + return m::resource::response + { + client, json::members + { + { "server_name", BRANDING_NAME }, + { "server_version", BRANDING_VERSION }, + { "package", + { + { "name", PACKAGE_NAME }, + { "version", PACKAGE_VERSION }, + { "string", PACKAGE_STRING }, + { "tarname", PACKAGE_TARNAME }, + }}, + { "build", + { + { "version", RB_VERSION }, + { "branch", RB_VERSION_BRANCH }, + { "tag", RB_VERSION_TAG }, + { "commit", RB_VERSION_COMMIT }, + { "date", RB_DATE_CONFIGURED }, + { "time", RB_TIME_CONFIGURED }, + }}, + { "info", + { + { "name", info::name }, + { "version", info::version }, + { "tag", info::tag }, + { "branch", info::branch }, + { "commit", info::commit }, + { "configured", info::configured }, + { "compiled", info::compiled }, + { "compiler", info::compiler }, + { "startup", info::startup }, + { "kernel", info::kernel_name }, + { "user_agent", info::user_agent }, + { "server_agent", info::server_agent }, + }}, + { "deps", deps.completed() }, + } + }; +} + +void +ircd::m::admin::make_deps(json::stack &deps) +{ + json::stack::array array + { + deps + }; + + for(const auto &version : info::versions::list) + { + json::stack::object dep + { + array + }; + + json::stack::member + { + dep, "name", version->name + }; + + json::stack::member + { + dep, "type", json::value + { + version->type == version->API? "API": + version->type == version->ABI? "ABI": + "???" + } + }; + + json::stack::member + { + dep, "monotonic", json::value + { + version->monotonic + } + }; + + char buf[32]; + json::stack::member + { + dep, "semantic", string_view + { + fmt::sprintf + { + buf, "%ld.%ld.%ld", + version->semantic[0], + version->semantic[1], + version->semantic[2], + } + } + }; + + json::stack::member + { + dep, "string", version->string + }; + } +}