Adding server log options

This commit is contained in:
WolverinDEV 2020-09-15 01:18:35 +02:00
parent 4f5a4dc993
commit 2a1f0187ac
8 changed files with 3879 additions and 55 deletions

@ -1 +1 @@
Subproject commit 8fbf120f756d603da38029843a8844a743440bac
Subproject commit 61e4c6bc67e72843bfeef09108db58fd79babf1e

View File

@ -26,6 +26,7 @@
#include "VirtualServer.h"
#include "src/manager/ConversationManager.h"
#include <misc/sassert.h>
#include <src/manager/ActionLogger.h>
using namespace std;
using namespace std::chrono;
@ -40,7 +41,6 @@ using namespace ts::buffer;
#define BUILD_VERSION "Unknown build"
#endif
extern ts::server::InstanceHandler* serverInstance;
VirtualServer::VirtualServer(uint16_t serverId, sql::SqlManager* database) : serverId(serverId), sql(database) {
memtrack::allocated<VirtualServer>(this);
}
@ -80,6 +80,60 @@ bool VirtualServer::initialize(bool test_properties) {
this->properties()[property::VIRTUALSERVER_ID] = serverId;
this->_disable_ip_saving = this->properties()[property::VIRTUALSERVER_DISABLE_IP_SAVING];
/* initialize logging */
{
auto server_id = this->serverId;
auto sync_property = [server_id](Property& prop) {
log::LoggerGroup action_type;
switch (prop.type().property_index) {
case property::VIRTUALSERVER_LOG_SERVER:
action_type = log::LoggerGroup::SERVER;
break;
case property::VIRTUALSERVER_LOG_CHANNEL:
action_type = log::LoggerGroup::CHANNEL;
break;
case property::VIRTUALSERVER_LOG_CLIENT:
action_type = log::LoggerGroup::CLIENT;
break;
case property::VIRTUALSERVER_LOG_FILETRANSFER:
action_type = log::LoggerGroup::FILES;
break;
case property::VIRTUALSERVER_LOG_PERMISSIONS:
action_type = log::LoggerGroup::PERMISSION;
break;
case property::VIRTUALSERVER_LOG_QUERY:
action_type = log::LoggerGroup::QUERY;
break;
default:
return;
}
serverInstance->action_logger()->toggle_logging_group(server_id, action_type, prop.as_save<bool>([]{ return true; }));
};
for(const property::VirtualServerProperties& property : {
property::VIRTUALSERVER_LOG_SERVER,
property::VIRTUALSERVER_LOG_CHANNEL,
property::VIRTUALSERVER_LOG_CLIENT,
property::VIRTUALSERVER_LOG_FILETRANSFER,
property::VIRTUALSERVER_LOG_QUERY,
property::VIRTUALSERVER_LOG_PERMISSIONS
}) {
auto prop = this->_properties->find(property::PROP_TYPE_SERVER, property);
sync_property(prop);
}
this->_properties->registerNotifyHandler([sync_property](Property& prop){
sync_property(prop);
});
}
if(!properties()[property::VIRTUALSERVER_KEYPAIR].as<std::string>().empty()){
debugMessage(this->serverId, "Importing server keypair");
this->_serverKey = new ecc_key;

File diff suppressed because it is too large Load Diff

280
server/src/absl/btree/map.h Normal file
View File

@ -0,0 +1,280 @@
/*
* Copyright (c) 2019 German Mendez Bravo (Kronuz)
* Copyright (c) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* A btree::map<> implements the STL unique sorted associative container
* interface and the pair associative container interface (a.k.a map<>) using a
* btree. See btree.h for details of the btree implementation and caveats.
*/
#ifndef BTREE_MAP_H__
#define BTREE_MAP_H__
#include "btree.h"
#include <stdexcept>
namespace btree {
// A common base class for map and safe_map.
template <typename Tree>
class btree_map_container : public btree_unique_container<Tree> {
typedef btree_map_container<Tree> self_type;
typedef btree_unique_container<Tree> super_type;
public:
typedef typename Tree::key_type key_type;
typedef typename Tree::data_type data_type;
typedef typename Tree::value_type value_type;
typedef typename Tree::mapped_type mapped_type;
typedef typename Tree::key_compare key_compare;
typedef typename Tree::allocator_type allocator_type;
typedef typename Tree::iterator iterator;
typedef typename Tree::const_iterator const_iterator;
public:
// Default constructor.
btree_map_container(const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type())
: super_type(comp, alloc) {
}
// Copy constructor.
btree_map_container(const self_type& x)
: super_type(x) {
}
// Range constructor.
template <class InputIterator>
btree_map_container(InputIterator b, InputIterator e,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type())
: super_type(b, e, comp, alloc) {
}
template <typename... Args>
std::pair<iterator, bool> try_emplace(const key_type& key, Args&&... args) {
return this->__tree.emplace_unique_key_args(key,
std::piecewise_construct,
std::forward_as_tuple(key),
std::forward_as_tuple(std::forward<Args>(args)...));
}
template <typename... Args>
std::pair<iterator, bool> try_emplace(key_type&& key, Args&&... args) {
return this->__tree.emplace_unique_key_args(key,
std::piecewise_construct,
std::forward_as_tuple(std::move(key)),
std::forward_as_tuple(std::forward<Args>(args)...));
}
template <typename... Args>
iterator try_emplace(const_iterator hint, const key_type& key, Args&&... args) {
return this->__tree.emplace_hint_unique_key_args(hint, key,
std::piecewise_construct,
std::forward_as_tuple(key),
std::forward_as_tuple(std::forward<Args>(args)...));
}
template <typename... Args>
iterator try_emplace(const_iterator hint, key_type&& key, Args&&... args) {
return this->__tree.emplace_hint_unique_key_args(hint, key,
std::piecewise_construct,
std::forward_as_tuple(std::move(key)),
std::forward_as_tuple(std::forward<Args>(args)...));
}
// Access specified element with bounds checking.
mapped_type& at(const key_type& key) {
auto it = this->find(key);
if (it == this->end()) {
throw std::out_of_range("map::at: key not found");
}
return it->second;
}
const mapped_type& at(const key_type& key) const {
auto it = this->find(key);
if (it == this->end()) {
throw std::out_of_range("map::at: key not found");
}
return it->second;
}
// Insertion routines.
data_type& operator[](const key_type& key) {
return this->try_emplace(key).first->second;
}
data_type& operator[](key_type&& key) {
return this->try_emplace(std::move(key)).first->second;
}
};
// The map class is needed mainly for its constructors.
template <typename Key, typename Value,
typename Compare = std::less<Key>,
typename Alloc = std::allocator<std::pair<const Key, Value>>,
int TargetNodeSize = 256>
class map : public btree_map_container<
btree<btree_map_params<Key, Value, Compare, Alloc, TargetNodeSize>>> {
typedef map<Key, Value, Compare, Alloc, TargetNodeSize> self_type;
typedef btree_map_params<Key, Value, Compare, Alloc, TargetNodeSize> params_type;
typedef btree<params_type> btree_type;
typedef btree_map_container<btree_type> super_type;
public:
typedef typename btree_type::key_compare key_compare;
typedef typename btree_type::allocator_type allocator_type;
public:
// Default constructor.
map(const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type())
: super_type(comp, alloc) {
}
// Copy constructor.
map(const self_type& x)
: super_type(x) {
}
// Range constructor.
template <class InputIterator>
map(InputIterator b, InputIterator e,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type())
: super_type(b, e, comp, alloc) {
}
};
} // namespace btree
template <typename K, typename V, typename C, typename A, int N>
bool operator==(const btree::map<K, V, C, A, N>& lhs, const btree::map<K, V, C, A, N>& rhs) {
return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
template <typename K, typename V, typename C, typename A, int N>
bool operator<(const btree::map<K, V, C, A, N>& lhs, const btree::map<K, V, C, A, N>& rhs) {
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template <typename K, typename V, typename C, typename A, int N>
bool operator!=(const btree::map<K, V, C, A, N>& lhs, const btree::map<K, V, C, A, N>& rhs) {
return !(lhs == rhs);
}
template <typename K, typename V, typename C, typename A, int N>
bool operator>(const btree::map<K, V, C, A, N>& lhs, const btree::map<K, V, C, A, N>& rhs) {
return rhs < lhs;
}
template <typename K, typename V, typename C, typename A, int N>
bool operator>=(const btree::map<K, V, C, A, N>& lhs, const btree::map<K, V, C, A, N>& rhs) {
return !(lhs < rhs);
}
template <typename K, typename V, typename C, typename A, int N>
bool operator<=(const btree::map<K, V, C, A, N>& lhs, const btree::map<K, V, C, A, N>& rhs) {
return !(rhs < lhs);
}
template <typename K, typename V, typename C, typename A, int N>
inline void swap(btree::map<K, V, C, A, N>& x, btree::map<K, V, C, A, N>& y) {
x.swap(y);
}
namespace btree {
// The multimap class is needed mainly for its constructors.
template <typename Key, typename Value,
typename Compare = std::less<Key>,
typename Alloc = std::allocator<std::pair<const Key, Value> >,
int TargetNodeSize = 256>
class multimap : public btree_multi_container<
btree<btree_map_params<Key, Value, Compare, Alloc, TargetNodeSize> > > {
typedef multimap<Key, Value, Compare, Alloc, TargetNodeSize> self_type;
typedef btree_map_params< Key, Value, Compare, Alloc, TargetNodeSize> params_type;
typedef btree<params_type> btree_type;
typedef btree_multi_container<btree_type> super_type;
public:
typedef typename btree_type::key_compare key_compare;
typedef typename btree_type::allocator_type allocator_type;
typedef typename btree_type::data_type data_type;
typedef typename btree_type::mapped_type mapped_type;
public:
// Default constructor.
multimap(const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type())
: super_type(comp, alloc) {
}
// Copy constructor.
multimap(const self_type& x)
: super_type(x) {
}
// Range constructor.
template <class InputIterator>
multimap(InputIterator b, InputIterator e,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type())
: super_type(b, e, comp, alloc) {
}
};
} // namespace btree
template <typename K, typename V, typename C, typename A, int N>
bool operator==(const btree::multimap<K, V, C, A, N>& lhs, const btree::multimap<K, V, C, A, N>& rhs) {
return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
template <typename K, typename V, typename C, typename A, int N>
bool operator<(const btree::multimap<K, V, C, A, N>& lhs, const btree::multimap<K, V, C, A, N>& rhs) {
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template <typename K, typename V, typename C, typename A, int N>
bool operator!=(const btree::multimap<K, V, C, A, N>& lhs, const btree::multimap<K, V, C, A, N>& rhs) {
return !(lhs == rhs);
}
template <typename K, typename V, typename C, typename A, int N>
bool operator>(const btree::multimap<K, V, C, A, N>& lhs, const btree::multimap<K, V, C, A, N>& rhs) {
return rhs < lhs;
}
template <typename K, typename V, typename C, typename A, int N>
bool operator>=(const btree::multimap<K, V, C, A, N>& lhs, const btree::multimap<K, V, C, A, N>& rhs) {
return !(lhs < rhs);
}
template <typename K, typename V, typename C, typename A, int N>
bool operator<=(const btree::multimap<K, V, C, A, N>& lhs, const btree::multimap<K, V, C, A, N>& rhs) {
return !(rhs < lhs);
}
template <typename K, typename V, typename C, typename A, int N>
inline void swap(btree::multimap<K, V, C, A, N>& x, btree::multimap<K, V, C, A, N>& y) {
x.swap(y);
}
#endif // BTREE_MAP_H__

183
server/src/absl/btree/set.h Normal file
View File

@ -0,0 +1,183 @@
/*
* Copyright (c) 2019 German Mendez Bravo (Kronuz)
* Copyright (c) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* A btree::set<> implements the STL unique sorted associative container
* interface (a.k.a set<>) using a btree. See btree.h for details of the btree
* implementation and caveats.
*/
#ifndef BTREE_SET_H__
#define BTREE_SET_H__
#include "btree.h"
namespace btree {
// The set class is needed mainly for its constructors.
template <typename Key,
typename Compare = std::less<Key>,
typename Alloc = std::allocator<Key>,
int TargetNodeSize = 256>
class set : public btree_unique_container<
btree<btree_set_params<Key, Compare, Alloc, TargetNodeSize> > > {
typedef set<Key, Compare, Alloc, TargetNodeSize> self_type;
typedef btree_set_params<Key, Compare, Alloc, TargetNodeSize> params_type;
typedef btree<params_type> btree_type;
typedef btree_unique_container<btree_type> super_type;
public:
typedef typename btree_type::key_compare key_compare;
typedef typename btree_type::allocator_type allocator_type;
public:
// Default constructor.
set(const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type())
: super_type(comp, alloc) {
}
// Copy constructor.
set(const self_type& x)
: super_type(x) {
}
// Range constructor.
template <class InputIterator>
set(InputIterator b, InputIterator e,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type())
: super_type(b, e, comp, alloc) {
}
};
} // namespace btree
template <typename K, typename C, typename A, int N>
bool operator==(const btree::set<K, C, A, N>& lhs, const btree::set<K, C, A, N>& rhs) {
return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
template <typename K, typename C, typename A, int N>
bool operator<(const btree::set<K, C, A, N>& lhs, const btree::set<K, C, A, N>& rhs) {
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template <typename K, typename C, typename A, int N>
bool operator!=(const btree::set<K, C, A, N>& lhs, const btree::set<K, C, A, N>& rhs) {
return !(lhs == rhs);
}
template <typename K, typename C, typename A, int N>
bool operator>(const btree::set<K, C, A, N>& lhs, const btree::set<K, C, A, N>& rhs) {
return rhs < lhs;
}
template <typename K, typename C, typename A, int N>
bool operator>=(const btree::set<K, C, A, N>& lhs, const btree::set<K, C, A, N>& rhs) {
return !(lhs < rhs);
}
template <typename K, typename C, typename A, int N>
bool operator<=(const btree::set<K, C, A, N>& lhs, const btree::set<K, C, A, N>& rhs) {
return !(rhs < lhs);
}
template <typename K, typename C, typename A, int N>
inline void swap(btree::set<K, C, A, N>& x, btree::set<K, C, A, N>& y) {
x.swap(y);
}
namespace btree {
// The multiset class is needed mainly for its constructors.
template <typename Key,
typename Compare = std::less<Key>,
typename Alloc = std::allocator<Key>,
int TargetNodeSize = 256>
class multiset : public btree_multi_container<
btree<btree_set_params<Key, Compare, Alloc, TargetNodeSize> > > {
typedef multiset<Key, Compare, Alloc, TargetNodeSize> self_type;
typedef btree_set_params<Key, Compare, Alloc, TargetNodeSize> params_type;
typedef btree<params_type> btree_type;
typedef btree_multi_container<btree_type> super_type;
public:
typedef typename btree_type::key_compare key_compare;
typedef typename btree_type::allocator_type allocator_type;
public:
// Default constructor.
multiset(const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type())
: super_type(comp, alloc) {
}
// Copy constructor.
multiset(const self_type& x)
: super_type(x) {
}
// Range constructor.
template <class InputIterator>
multiset(InputIterator b, InputIterator e,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type())
: super_type(b, e, comp, alloc) {
}
};
} // namespace btree
template <typename K, typename C, typename A, int N>
bool operator==(const btree::multiset<K, C, A, N>& lhs, const btree::multiset<K, C, A, N>& rhs) {
return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
template <typename K, typename C, typename A, int N>
bool operator<(const btree::multiset<K, C, A, N>& lhs, const btree::multiset<K, C, A, N>& rhs) {
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template <typename K, typename C, typename A, int N>
bool operator!=(const btree::multiset<K, C, A, N>& lhs, const btree::multiset<K, C, A, N>& rhs) {
return !(lhs == rhs);
}
template <typename K, typename C, typename A, int N>
bool operator>(const btree::multiset<K, C, A, N>& lhs, const btree::multiset<K, C, A, N>& rhs) {
return rhs < lhs;
}
template <typename K, typename C, typename A, int N>
bool operator>=(const btree::multiset<K, C, A, N>& lhs, const btree::multiset<K, C, A, N>& rhs) {
return !(lhs < rhs);
}
template <typename K, typename C, typename A, int N>
bool operator<=(const btree::multiset<K, C, A, N>& lhs, const btree::multiset<K, C, A, N>& rhs) {
return !(rhs < lhs);
}
template <typename K, typename C, typename A, int N>
inline void swap(btree::multiset<K, C, A, N>& x, btree::multiset<K, C, A, N>& y) {
x.swap(y);
}
#endif // BTREE_SET_H__

View File

@ -12,6 +12,9 @@ using namespace ts::server::log;
Invoker log::kInvokerSystem{"System", "system", 0};
ConstLogGroupSettings<true> log::kLogGroupSettingTrue{};
ConstLogGroupSettings<false> log::kLogGroupSettingFalse{};
void AbstractActionLogger::do_log(ServerId sid, Action action, sql::command &command) const {
if(!this->enabled_) return;
@ -445,4 +448,37 @@ std::vector<LogEntryInfo> ActionLogger::query(
}
return result;
}
void ActionLogger::toggle_logging_group(ServerId server_id, LoggerGroup group, bool flag) {
switch (group) {
case LoggerGroup::SERVER:
this->log_group_server_.toggle_activated(server_id, flag);
break;
case LoggerGroup::PERMISSION:
this->log_group_permissions_.toggle_activated(server_id, flag);
break;
case LoggerGroup::QUERY:
this->log_group_query_.toggle_activated(server_id, flag);
break;
case LoggerGroup::FILES:
this->log_group_file_transfer_.toggle_activated(server_id, flag);
break;
case LoggerGroup::CLIENT:
this->log_group_client_.toggle_activated(server_id, flag);
break;
case LoggerGroup::CHANNEL:
this->log_group_channel_.toggle_activated(server_id, flag);
break;
case LoggerGroup::CUSTOM:
case LoggerGroup::MAX:
default:
break;
}
}

View File

@ -9,6 +9,7 @@
#include <Properties.h>
#include <PermissionManager.h>
#include <query/command3.h>
#include "../absl/btree/set.h"
namespace ts::server {
class ConnectedClient;
@ -152,9 +153,46 @@ namespace ts::server::log {
class ActionLogger;
struct LogGroupSettings {
[[nodiscard]] virtual bool is_activated(ServerId server_id) const = 0;
};
template <bool kDefaultValue>
struct BiasedLogGroupSettings : public LogGroupSettings {
public:
BiasedLogGroupSettings() = default;
[[nodiscard]] bool is_activated(ServerId server_id) const override {
auto it = this->status.find(server_id);
return it == this->status.end() ? kDefaultValue : !kDefaultValue;
}
inline void toggle_activated(ServerId server_id, bool enabled) {
if(enabled == kDefaultValue) {
this->status.erase(enabled);
} else {
this->status.emplace(server_id);
}
}
private:
btree::set<ServerId> status{};
};
template <bool kValue>
struct ConstLogGroupSettings : public LogGroupSettings {
public:
ConstLogGroupSettings() = default;
[[nodiscard]] bool is_activated(ServerId server_id) const override {
return kValue;
}
};
extern ConstLogGroupSettings<true> kLogGroupSettingTrue;
extern ConstLogGroupSettings<false> kLogGroupSettingFalse;
class AbstractActionLogger {
public:
explicit AbstractActionLogger(ActionLogger* impl) : logger_{impl} {}
explicit AbstractActionLogger(ActionLogger* impl, LogGroupSettings* group_settings) : logger_{impl}, group_settings_{group_settings} {}
[[nodiscard]] virtual bool setup(int /* database version */, std::string& /* error */) = 0;
virtual void finalize() { this->enabled_ = false; }
@ -172,6 +210,7 @@ namespace ts::server::log {
) = 0;
protected:
ActionLogger* logger_;
LogGroupSettings* group_settings_;
bool enabled_{false};
void do_log(ServerId /* server id */, Action /* action */, sql::command& /* command */) const;
@ -238,8 +277,8 @@ namespace ts::server::log {
static void bind_fixed_header(sql::command& /* command */, const FixedHeaderValues& /* values */);
static bool parse_fixed_header(FixedHeaderValues& /* result */, std::string* /* values */, size_t /* values length */);
explicit TypedActionLogger(ActionLogger* impl, std::string table_name, FixedHeaderKeys fh_keys, const T2DatabaseColumn<LoggingArguments>&... keys)
: AbstractActionLogger{impl}, fh_keys{std::move(fh_keys)}, table_name{std::move(table_name)} {
explicit TypedActionLogger(ActionLogger* impl, LogGroupSettings* group_settings, std::string table_name, FixedHeaderKeys fh_keys, const T2DatabaseColumn<LoggingArguments>&... keys)
: AbstractActionLogger{impl, group_settings}, fh_keys{std::move(fh_keys)}, table_name{std::move(table_name)} {
this->bind_key(0, keys...);
}
@ -330,7 +369,7 @@ namespace ts::server::log {
//Table: `id`, `timestamp`, `server_id`, `invoker_database_id`, `invoker_name`, `action`, `property`, `value_old`, `value_new`
class ServerEditActionLogger : public TypedActionLogger<std::string_view, std::string, std::string> {
public:
explicit ServerEditActionLogger(ActionLogger* impl);
explicit ServerEditActionLogger(ActionLogger* impl, LogGroupSettings* group_settings);
bool setup(int, std::string &) override;
void log_server_edit(
@ -356,7 +395,7 @@ namespace ts::server::log {
//Table: `id`, `timestamp`, `server_id`, `invoker_database_id`, `invoker_name`, `action`, `reason`
class ServerActionLogger : public TypedActionLogger<std::string_view> {
public:
explicit ServerActionLogger(ActionLogger* impl);
explicit ServerActionLogger(ActionLogger* impl, LogGroupSettings* group_settings);
bool setup(int, std::string &) override;
@ -407,7 +446,7 @@ namespace ts::server::log {
//Table: `id`, `timestamp`, `server_id`, `invoker_database_id`, `invoker_name`, `action`, `property` (may be the delete reason), `value_old`, `value_new`
class ChannelActionLogger : public TypedActionLogger<ChannelId, std::string_view, std::string, std::string> {
public:
explicit ChannelActionLogger(ActionLogger* impl);
explicit ChannelActionLogger(ActionLogger* impl, LogGroupSettings* group_settings);
bool setup(int, std::string &) override;
@ -465,7 +504,7 @@ namespace ts::server::log {
//Table: `id`, `timestamp`, `server_id`, `invoker_database_id`, `invoker_name`, `action`, `target`, `id1`, `id1_name`, `id2`, `id2_name`, `permission`, `old_value`, `old_negated`, `old_skipped`, `new_value`, `new_negated`, `new_skipped`
class PermissionActionLogger : public TypedActionLogger<std::string_view, uint64_t, std::string, uint64_t, std::string, std::string, int32_t, bool, bool, int32_t, bool, bool> {
public:
explicit PermissionActionLogger(ActionLogger* impl);
explicit PermissionActionLogger(ActionLogger* impl, LogGroupSettings* group_settings);
bool setup(int, std::string &) override;
@ -559,7 +598,7 @@ namespace ts::server::log {
//Table: `id`, `timestamp`, `server_id`, `invoker_database_id`, `invoker_name`, `action`, `type`, `target`, `group`, `group_name`, `source`, `source_name`
class GroupActionLogger : public TypedActionLogger<std::string_view, std::string_view, uint64_t, std::string, uint64_t, std::string> {
public:
explicit GroupActionLogger(ActionLogger* impl);
explicit GroupActionLogger(ActionLogger* impl, LogGroupSettings* group_settings);
bool setup(int, std::string &) override;
@ -609,7 +648,7 @@ namespace ts::server::log {
//Table: `id`, `timestamp`, `server_id`, `invoker_database_id`, `invoker_name`, `action`, `target`, `group`, `group_name`, `client`, `client_name`
class GroupAssignmentActionLogger : public TypedActionLogger<std::string_view, uint64_t, std::string, uint64_t, std::string> {
public:
explicit GroupAssignmentActionLogger(ActionLogger *impl);
explicit GroupAssignmentActionLogger(ActionLogger *impl, LogGroupSettings* group_settings);
bool setup(int, std::string &) override;
@ -637,7 +676,7 @@ namespace ts::server::log {
//Table: `id`, `timestamp`, `server_id`, `invoker_database_id`, `invoker_name`, `action`, `target_client`, `target_client_name`, `source_channel`, `source_channel_name`, `target_channel`, `target_channel_name`
class ClientChannelActionLogger : public TypedActionLogger<uint64_t, std::string, uint64_t, std::string, uint64_t, std::string> {
public:
explicit ClientChannelActionLogger(ActionLogger *impl);
explicit ClientChannelActionLogger(ActionLogger *impl, LogGroupSettings* group_settings);
bool setup(int, std::string &) override;
@ -680,7 +719,7 @@ namespace ts::server::log {
//Table: `id`, `timestamp`, `server_id`, `invoker_database_id`, `invoker_name`, `action`, `target_client`, `target_client_name`, `property`, `old_value`, `new_value`
class ClientEditActionLogger : public TypedActionLogger<uint64_t, std::string, std::string_view, std::string, std::string> {
public:
explicit ClientEditActionLogger(ActionLogger *impl);
explicit ClientEditActionLogger(ActionLogger *impl, LogGroupSettings* group_settings);
bool setup(int, std::string &) override;
@ -697,7 +736,7 @@ namespace ts::server::log {
//Table: `id`, `timestamp`, `server_id`, `invoker_database_id`, `invoker_name`, `action`, `source_channel_id`, `source_path`, `target_channel_id`, `target_path`
class FilesActionLogger : public TypedActionLogger<uint64_t, std::string, uint64_t, std::string> {
public:
explicit FilesActionLogger(ActionLogger *impl);
explicit FilesActionLogger(ActionLogger *impl, LogGroupSettings* group_settings);
bool setup(int, std::string &) override;
@ -742,7 +781,7 @@ namespace ts::server::log {
//Table: `id`, `timestamp`, `server_id`, `invoker_database_id`, `invoker_name`, `action`, `message`
class CustomLogger : public TypedActionLogger<std::string> {
public:
explicit CustomLogger(ActionLogger *impl);
explicit CustomLogger(ActionLogger *impl, LogGroupSettings* group_settings);
bool setup(int, std::string &) override;
@ -770,7 +809,7 @@ namespace ts::server::log {
//Table: `id`, `timestamp`, `server_id`, `invoker_database_id`, `invoker_name`, `action`, `ip`, `username`, `result`
class QueryAuthenticateLogger : public TypedActionLogger<std::string, std::string, std::string_view> {
public:
explicit QueryAuthenticateLogger(ActionLogger *impl);
explicit QueryAuthenticateLogger(ActionLogger *impl, LogGroupSettings* group_settings);
bool setup(int, std::string &) override;
@ -785,7 +824,7 @@ namespace ts::server::log {
//Table: `id`, `timestamp`, `server_id`, `invoker_database_id`, `invoker_name`, `action`, `ip`, `username`, `source_server`
class QueryServerLogger : public TypedActionLogger<std::string, std::string, bool> {
public:
explicit QueryServerLogger(ActionLogger *impl);
explicit QueryServerLogger(ActionLogger *impl, LogGroupSettings* group_settings);
bool setup(int, std::string &) override;
@ -825,19 +864,29 @@ namespace ts::server::log {
size_t /* limit */
);
ServerActionLogger server_logger{this};
ServerEditActionLogger server_edit_logger{this};
ChannelActionLogger channel_logger{this};
PermissionActionLogger permission_logger{this};
GroupActionLogger group_logger{this};
GroupAssignmentActionLogger group_assignment_logger{this};
ClientChannelActionLogger client_channel_logger{this};
ClientEditActionLogger client_edit_logger{this};
FilesActionLogger file_logger{this};
CustomLogger custom_logger{this};
QueryServerLogger query_logger{this};
QueryAuthenticateLogger query_authenticate_logger{this};
[[nodiscard]] bool logging_group_enabled(ServerId /* server id */, LoggerGroup /* group */) const;
void toggle_logging_group(ServerId /* server id */, LoggerGroup /* group */, bool /* flag */);
ServerActionLogger server_logger{this, &log_group_server_};
ServerEditActionLogger server_edit_logger{this, &log_group_server_};
ChannelActionLogger channel_logger{this, &log_group_channel_};
PermissionActionLogger permission_logger{this, &log_group_permissions_};
GroupActionLogger group_logger{this, &log_group_server_};
GroupAssignmentActionLogger group_assignment_logger{this, &log_group_server_};
ClientChannelActionLogger client_channel_logger{this, &log_group_client_};
ClientEditActionLogger client_edit_logger{this, &log_group_client_};
FilesActionLogger file_logger{this, &log_group_file_transfer_};
CustomLogger custom_logger{this, &kLogGroupSettingTrue};
QueryServerLogger query_logger{this, &log_group_query_};
QueryAuthenticateLogger query_authenticate_logger{this, &log_group_query_};
private:
sql::SqlManager* sql_handle{nullptr};
BiasedLogGroupSettings<true> log_group_client_{};
BiasedLogGroupSettings<true> log_group_query_{};
BiasedLogGroupSettings<true> log_group_channel_{};
BiasedLogGroupSettings<true> log_group_permissions_{};
BiasedLogGroupSettings<true> log_group_server_{};
BiasedLogGroupSettings<true> log_group_file_transfer_{};
};
}

View File

@ -42,8 +42,9 @@ bool ServerActionLogger::setup(int version, std::string &error) {
}
}
ServerActionLogger::ServerActionLogger(ActionLogger* impl) : TypedActionLogger{
ServerActionLogger::ServerActionLogger(ActionLogger* impl, LogGroupSettings* group_settings) : TypedActionLogger{
impl,
group_settings,
"logs_server",
kDefaultHeaderFields,
{"reason", "VARCHAR(64)"}
@ -102,8 +103,9 @@ bool ServerEditActionLogger::setup(int version, std::string &error) {
}
}
ServerEditActionLogger::ServerEditActionLogger(ActionLogger* impl) : TypedActionLogger{
ServerEditActionLogger::ServerEditActionLogger(ActionLogger* impl, LogGroupSettings* group_settings) : TypedActionLogger{
impl,
group_settings,
"logs_server_edit",
kDefaultHeaderFields,
{"property", "VARCHAR(128)"},
@ -116,6 +118,9 @@ void ServerEditActionLogger::log_server_edit(ServerId sid,
const property::PropertyDescription &property,
const std::string &old_value,
const std::string &new_value) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -142,8 +147,9 @@ bool ChannelActionLogger::setup(int version, std::string &error) {
}
}
ChannelActionLogger::ChannelActionLogger(ActionLogger* impl) : TypedActionLogger{
ChannelActionLogger::ChannelActionLogger(ActionLogger* impl, LogGroupSettings* group_settings) : TypedActionLogger{
impl,
group_settings,
"logs_channel",
kDefaultHeaderFields,
{"channel_id", "BIGINT"},
@ -153,6 +159,10 @@ ChannelActionLogger::ChannelActionLogger(ActionLogger* impl) : TypedActionLogger
} { }
void ChannelActionLogger::log_channel_create(ServerId sid, const std::shared_ptr<ConnectedClient> &client, ChannelId cid, ChannelType type) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = sid,
@ -167,6 +177,10 @@ void ChannelActionLogger::log_channel_edit(ServerId sid,
const property::PropertyDescription &property,
const std::string &old_value,
const std::string &new_value) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = sid,
@ -176,6 +190,10 @@ void ChannelActionLogger::log_channel_edit(ServerId sid,
}
void ChannelActionLogger::log_channel_move(ServerId sid, const std::shared_ptr<ConnectedClient> &client, ChannelId cid, ChannelId opcid, ChannelId npcid, ChannelId oco, ChannelId nco) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
auto timestamp = std::chrono::system_clock::now();
if(opcid != npcid) {
this->do_log({
@ -197,6 +215,10 @@ void ChannelActionLogger::log_channel_move(ServerId sid, const std::shared_ptr<C
}
void ChannelActionLogger::log_channel_delete(ServerId sid, const std::shared_ptr<ConnectedClient> &client, ChannelId cid, ChannelDeleteReason reason) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = sid,
@ -222,8 +244,9 @@ bool PermissionActionLogger::setup(int version, std::string &error) {
}
}
PermissionActionLogger::PermissionActionLogger(ActionLogger* impl) : TypedActionLogger{
PermissionActionLogger::PermissionActionLogger(ActionLogger* impl, LogGroupSettings* group_settings) : TypedActionLogger{
impl,
group_settings,
"logs_permission",
kDefaultHeaderFields,
{"target", "VARCHAR(64)"},
@ -250,6 +273,10 @@ void PermissionActionLogger::log_permission_add_value(
int32_t new_value,
bool new_negated,
bool new_skipped) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = sid,
@ -266,6 +293,10 @@ void PermissionActionLogger::log_permission_add_grant(
uint64_t id2, const std::string& id2_name,
const permission::PermissionTypeEntry& permission,
int32_t new_value) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -288,6 +319,10 @@ void PermissionActionLogger::log_permission_edit_value(
int32_t new_value,
bool new_negated,
bool new_skipped) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = sid,
@ -305,6 +340,10 @@ void PermissionActionLogger::log_permission_edit_grant(
const permission::PermissionTypeEntry& permission,
int32_t old_value,
int32_t new_value) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -324,6 +363,10 @@ void PermissionActionLogger::log_permission_remove_value(
int32_t old_value,
bool old_negated,
bool old_skipped) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = sid,
@ -340,6 +383,9 @@ void PermissionActionLogger::log_permission_remove_grant(
uint64_t id2, const std::string& id2_name,
const permission::PermissionTypeEntry& permission,
int32_t old_value) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -383,8 +429,9 @@ bool GroupActionLogger::setup(int version, std::string &error) {
}
}
GroupActionLogger::GroupActionLogger(ActionLogger* impl) : TypedActionLogger{
GroupActionLogger::GroupActionLogger(ActionLogger* impl, LogGroupSettings* group_settings) : TypedActionLogger{
impl,
group_settings,
"logs_groups",
kDefaultHeaderFields,
{"type", "VARCHAR(64)"},
@ -404,6 +451,10 @@ void GroupActionLogger::log_group_create(
const std::string &name,
uint64_t sgid,
const std::string &sname) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = sid,
@ -421,6 +472,10 @@ void GroupActionLogger::log_group_permission_copy(
const std::string &name,
uint64_t sgid,
const std::string &sname) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = sid,
@ -437,6 +492,10 @@ void GroupActionLogger::log_group_rename(
uint64_t gid,
const std::string &name,
const std::string &sname) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = sid,
@ -452,6 +511,10 @@ void GroupActionLogger::log_group_delete(
GroupType type,
uint64_t gid,
const std::string &name) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = sid,
@ -477,8 +540,9 @@ bool GroupAssignmentActionLogger::setup(int version, std::string &error) {
}
}
GroupAssignmentActionLogger::GroupAssignmentActionLogger(ActionLogger* impl) : TypedActionLogger{
GroupAssignmentActionLogger::GroupAssignmentActionLogger(ActionLogger* impl, LogGroupSettings* group_settings) : TypedActionLogger{
impl,
group_settings,
"logs_group_assignments",
kDefaultHeaderFields,
{"target", "VARCHAR(64)"},
@ -496,6 +560,9 @@ void GroupAssignmentActionLogger::log_group_assignment_add(
const std::string &gname,
uint64_t tclient,
const std::string &client_name) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -513,6 +580,9 @@ void GroupAssignmentActionLogger::log_group_assignment_remove(
const std::string &gname,
uint64_t tclient,
const std::string &client_name) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -539,8 +609,9 @@ bool ClientChannelActionLogger::setup(int version, std::string &error) {
}
}
ClientChannelActionLogger::ClientChannelActionLogger(ActionLogger* impl) : TypedActionLogger{
ClientChannelActionLogger::ClientChannelActionLogger(ActionLogger* impl, LogGroupSettings* group_settings) : TypedActionLogger{
impl,
group_settings,
"logs_client_channel",
kDefaultHeaderFields,
{"target_client", "BIGINT"},
@ -556,6 +627,9 @@ void ClientChannelActionLogger::log_client_join(
const std::shared_ptr<ConnectedClient>& subject,
uint64_t target_channel,
const std::string& target_channel_name) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -570,6 +644,9 @@ void ClientChannelActionLogger::log_client_leave(
const std::shared_ptr<ConnectedClient>& subject,
uint64_t source_channel,
const std::string& source_channel_name) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -584,7 +661,9 @@ void ClientChannelActionLogger::log_client_move(
const std::shared_ptr<ConnectedClient> &issuer,
const std::shared_ptr<ConnectedClient> &subject,
uint64_t target_channel, const std::string &target_channel_name, uint64_t source_channel, const std::string &source_channel_name) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -599,7 +678,9 @@ void ClientChannelActionLogger::log_client_kick(
const std::shared_ptr<ConnectedClient> &issuer,
const std::shared_ptr<ConnectedClient> &subject,
uint64_t target_channel, const std::string &target_channel_name, uint64_t source_channel, const std::string &source_channel_name) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -626,8 +707,9 @@ bool ClientEditActionLogger::setup(int version, std::string &error) {
}
}
ClientEditActionLogger::ClientEditActionLogger(ActionLogger* impl) : TypedActionLogger{
ClientEditActionLogger::ClientEditActionLogger(ActionLogger* impl, LogGroupSettings* group_settings) : TypedActionLogger{
impl,
group_settings,
"logs_client_edit",
kDefaultHeaderFields,
{"target_client", "BIGINT"},
@ -645,6 +727,10 @@ void ClientEditActionLogger::log_client_edit(
const std::string& old_value,
const std::string& new_value
) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = sid,
@ -670,8 +756,9 @@ bool FilesActionLogger::setup(int version, std::string &error) {
}
}
FilesActionLogger::FilesActionLogger(ActionLogger* impl) : TypedActionLogger{
FilesActionLogger::FilesActionLogger(ActionLogger* impl, LogGroupSettings* group_settings) : TypedActionLogger{
impl,
group_settings,
"logs_files",
kDefaultHeaderFields,
{"source_channel_id", "BIGINT"},
@ -685,6 +772,9 @@ void FilesActionLogger::log_file_upload(
const std::shared_ptr<ConnectedClient> &issuer,
uint64_t channel_id,
const std::string &path) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -699,6 +789,9 @@ void FilesActionLogger::log_file_download(
const std::shared_ptr<ConnectedClient> &issuer,
uint64_t channel_id,
const std::string &path) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -715,6 +808,9 @@ void FilesActionLogger::log_file_rename(
const std::string &old_name,
uint64_t mew_channel_id,
const std::string &new_name) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -729,6 +825,9 @@ void FilesActionLogger::log_file_directory_create(
const std::shared_ptr<ConnectedClient> &issuer,
uint64_t channel_id,
const std::string &path) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -743,6 +842,9 @@ void FilesActionLogger::log_file_delete(
const std::shared_ptr<ConnectedClient> &issuer,
uint64_t channel_id,
const std::string &path) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -769,8 +871,9 @@ bool CustomLogger::setup(int version, std::string &error) {
}
}
CustomLogger::CustomLogger(ActionLogger* impl) : TypedActionLogger{
CustomLogger::CustomLogger(ActionLogger* impl, LogGroupSettings* group_settings) : TypedActionLogger{
impl,
group_settings,
"logs_custom",
kDefaultHeaderFields,
{"message", "TEXT"},
@ -780,6 +883,9 @@ void CustomLogger::add_log_message(
ServerId sid,
const std::shared_ptr<ConnectedClient> &issuer,
const std::string &message) {
if(!this->group_settings_->is_activated(sid)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -832,8 +938,9 @@ bool QueryAuthenticateLogger::setup(int version, std::string &error) {
}
}
QueryAuthenticateLogger::QueryAuthenticateLogger(ActionLogger* impl) : TypedActionLogger{
QueryAuthenticateLogger::QueryAuthenticateLogger(ActionLogger* impl, LogGroupSettings* group_settings) : TypedActionLogger{
impl,
group_settings,
"logs_query_authenticate",
kDefaultHeaderFields,
{"ip", "VARCHAR(64)"},
@ -846,6 +953,9 @@ void QueryAuthenticateLogger::log_query_authenticate(
const std::shared_ptr<QueryClient> &query,
const std::string &username,
QueryAuthenticateResult result) {
if(!this->group_settings_->is_activated(server)) {
return;
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
@ -872,8 +982,9 @@ bool QueryServerLogger::setup(int version, std::string &error) {
}
}
QueryServerLogger::QueryServerLogger(ActionLogger* impl) : TypedActionLogger{
QueryServerLogger::QueryServerLogger(ActionLogger* impl, LogGroupSettings* group_settings) : TypedActionLogger{
impl,
group_settings,
"logs_query_server",
kDefaultHeaderFields,
{"ip", "VARCHAR(64)"},
@ -882,17 +993,21 @@ QueryServerLogger::QueryServerLogger(ActionLogger* impl) : TypedActionLogger{
} { }
void QueryServerLogger::log_query_switch(const std::shared_ptr<QueryClient> &query, const std::string &username, ServerId source, ServerId target) {
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = source,
.invoker = client_to_invoker(query),
.action = Action::QUERY_LEAVE,
}, query->getLoggingPeerIp(), username, target);
if(this->group_settings_->is_activated(source)) {
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = source,
.invoker = client_to_invoker(query),
.action = Action::QUERY_LEAVE,
}, query->getLoggingPeerIp(), username, target);
}
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = target,
.invoker = client_to_invoker(query),
.action = Action::QUERY_JOIN,
}, query->getLoggingPeerIp(), username, source);
if(this->group_settings_->is_activated(source)) {
this->do_log({
.timestamp = std::chrono::system_clock::now(),
.server_id = target,
.invoker = client_to_invoker(query),
.action = Action::QUERY_JOIN,
}, query->getLoggingPeerIp(), username, target);
}
}