mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-05-08 14:33:23 +00:00
* refactor: move business logic from servers model * refactor: move containersModel initialization * refactor: added protocol ui controller and removed settings class from protocols model * refactor: moved cli management to separate controller * refactor: moved app split to separate controller * refactor: moved site split to separate controller * refactor: moved allowed dns to separate controller * refactor: moved language logic to separate ui controller * refactor: removed Settings from devices model * refactor: moved configs and services api logit to separate core controller * refactor: added a layer with a repository between the storage and controllers * refactor: use child parent system instead of smart pointers for controllers and models initialization * refactor: moved install functions from server controller to install controller * refactor: install controller refactoring * chore: renamed exportController to exportUiController * refactor: separate export controller * refactor: removed VpnConfigurationsController * chore: renamed ServerController to SshSession * refactor: replaced ServerController to SshSession * chore: moved qml controllers to separate folder * chore: include fixes * chore: moved utils from core root to core/utils * chore: include fixes * chore: rename core/utils files to camelCase foramt * chore: include fixes * chore: moved some utils to api and selfhosted folders * chore: include fixes * chore: remove unused file * chore: moved serialization folder to core/utils * chore: include fixes * chore: moved some files from client root to core/utils * chore: include fixes * chore: moved ui utils to ui/utils folder * chore: include fixes * chore: move utils from root to ui/utils * chore: include fixes * chore: moved configurators to core/configurators * chore: include fixes * refactor: moved iap logic from ui controller to core * refactor: moved remaining core logic from ApiConfigsController to SubscriptionController * chore: rename apiNewsController to apiNewsUiController * refactor: moved core logic from news ui controller to core * chore: renamed apiConfigsController to subscriptionUiController * chore: include fixes * refactor: merge ApiSettingsController with SubscriptionUiController * chore: moved ui selfhosted controllers to separate folder * chore: include fixes * chore: rename connectionController to connectiomUiController * refactor: moved core logic from connectionUiController * chore: rename settingsController to settingsUiController * refactor: move core logic from settingsUiController * refactor: moved core controller signal/slot connections to separate class * fix: newsController fixes after refactoring * chore: rename model to camelCase * chore: include fixes * chore: remove unused code * chore: move selfhosted core to separate folder * chore: include fixes * chore: rename importController to importUiController * refactor: move core logic from importUiController * chore: minor fixes * chore: remove prem v1 migration * refactor: remove openvpn over cloak and openvpn over shadowsocks * refactor: removed protocolsForContainer function * refactor: add core models * refactor: replace json with c++ structs for server config * refactor: move getDnsPair to ServerConfigUtils * feat: add admin selfhosted config export test * feat: add multi import test * refactor: use coreController for tests * feat: add few simple tests * chore: qrepos in all core controllers * feat: add test for settings * refactor: remove repo dependency from configurators * chore: moved protocols to core folder * chore: include fixes * refactor: moved containersDefs, defs, apiDefs, protocolsDefs to different places * chore: include fixes * chore: build fixes * chore: build fixes * refactor: remove q repo and interface repo * feat: add test for ui servers model and controller * chore: renamed to camelCase * chore: include fixes * refactor: moved core logic from sites ui controller * fix: fixed api config processing * fix: fixed processed server index processing * refactor: protocol models now use c++ structs instead of json configs * refactor: servers model now use c++ struct instead of json config * fix: fixed default server index processing * fix: fix logs init * fix: fix secure settings load keys * chore: build fixes * fix: fixed clear settings * fix: fixed restore backup * fix: sshSession usage * fix: fixed export functions signatures * fix: return missing part from buildContainerWorker * fix: fixed server description on page home * refactor: add container config helpers functions * refactor: c++ structs instead of json * chore: add dns protocol config struct * refactor: move config utils functions to config structs * feat: add test for selfhosted server setup * refactor: separate resources.qrc * fix: fixed server rename * chore: return nameOverriddenByUser * fix: build fixes * fix: fixed models init * refactor: cleanup models usage * fix: fixed models init * chore: cleanup connections and functions signatures * chore: cleanup updateModel calls * feat: added cache to servers repo * chore: cleanup unused functions * chore: ssxray processing * chore: remove transportProtoWithDefault and portWithDefault functions * chore: removed proto types any and l2tp * refactor: moved some constants * fix: fixed native configs export * refactor: remove json from processConfigWith functions * fix: fixed processed server index usage * fix: qml warning fixes * chore: merge fixes * chore: update tests * fix: fixed xray config processing * fix: fixed split tunneling processing * chore: rename sites controllers and model * chore: rename fixes * chore: minor fixes * chore: remove ability to load backup from "file with connection settings" button * fix: fixed api device revoke * fix: remove full model update when renaming a user * fix: fixed premium/free server rename * fix: fixed selfhosted new server install * fix: fixed updateContainer function * fix: fixed revoke for external premium configs * feat: add native configs qr processing * chore: codestyle fixes * fix: fixed admin config create * chore: again remove ability to load backup from "file with connection settings" button * chore: minor fixes * fix: fixed variables initialization * fix: fixed qml imports * fix: minor fixes * fix: fix vpnConnection function calls * feat: add buckup error handling * fix: fixed admin config revok * fix: fixed selfhosted awg installation * fix: ad visability * feat: add empty check for primary dns * chore: minor fixes
345 lines
8.8 KiB
C++
345 lines
8.8 KiB
C++
#include "logger.h"
|
|
|
|
#include <QDateTime>
|
|
#include <QDebug>
|
|
#include <QDesktopServices>
|
|
#include <QDir>
|
|
#include <QJsonDocument>
|
|
#include <QMetaEnum>
|
|
#include <QStandardPaths>
|
|
#include <QUrl>
|
|
|
|
#include "core/utils/utilities.h"
|
|
#include "version.h"
|
|
|
|
#ifdef AMNEZIA_DESKTOP
|
|
#include <core/utils/ipcClient.h>
|
|
#endif
|
|
|
|
#ifdef Q_OS_IOS
|
|
#include <AmneziaVPN-Swift.h>
|
|
#endif
|
|
|
|
QFile Logger::m_file;
|
|
QTextStream Logger::m_textStream;
|
|
QString Logger::m_logFileName = QString("%1.log").arg(APPLICATION_NAME);
|
|
QString Logger::m_serviceLogFileName = QString("%1.log").arg(SERVICE_NAME);
|
|
|
|
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
|
{
|
|
if (msg.simplified().isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
// Skip annoying messages from Qt
|
|
if (msg.contains("OpenType support missing for")) {
|
|
return;
|
|
}
|
|
|
|
if (msg.startsWith("Unknown property") || msg.startsWith("Could not create pixmap")
|
|
|| msg.startsWith("Populating font") || msg.startsWith("stale focus object")) {
|
|
return;
|
|
}
|
|
|
|
switch (type) {
|
|
case QtDebugMsg: Logger::Instance().debug() << msg; break;
|
|
case QtInfoMsg: Logger::Instance().info() << msg; break;
|
|
case QtWarningMsg: Logger::Instance().warning() << msg; break;
|
|
case QtCriticalMsg: Logger::Instance().error() << msg; break;
|
|
case QtFatalMsg: {
|
|
Logger::Instance().error() << msg;
|
|
} // Brackets are needed to ensure the destructor of LogStreamer is called before abort()
|
|
abort();
|
|
}
|
|
}
|
|
|
|
Logger &Logger::Instance()
|
|
{
|
|
static Logger s;
|
|
return s;
|
|
}
|
|
|
|
bool Logger::init(bool isServiceLogger)
|
|
{
|
|
QString path = isServiceLogger ? systemLogDir() : userLogsDir();
|
|
QString logFileName = isServiceLogger ? m_serviceLogFileName : m_logFileName;
|
|
QDir appDir(path);
|
|
if (!appDir.mkpath(path)) {
|
|
return false;
|
|
}
|
|
|
|
m_file.setFileName(appDir.filePath(logFileName));
|
|
if (!m_file.open(QIODevice::Append)) {
|
|
qWarning() << "Cannot open log file:" << logFileName;
|
|
return false;
|
|
}
|
|
|
|
m_file.setTextModeEnabled(true);
|
|
m_textStream.setDevice(&m_file);
|
|
|
|
qInstallMessageHandler(messageHandler);
|
|
|
|
return true;
|
|
}
|
|
|
|
void Logger::deInit()
|
|
{
|
|
m_textStream.setDevice(nullptr);
|
|
m_file.close();
|
|
}
|
|
|
|
bool Logger::setServiceLogsEnabled(bool enabled)
|
|
{
|
|
#ifdef AMNEZIA_DESKTOP
|
|
return IpcClient::withInterface([enabled](QSharedPointer<IpcInterfaceReplica> iface) {
|
|
iface->setLogsEnabled(enabled);
|
|
qDebug() << "Logger::setServiceLogsEnabled(): Logs transitioned to be " << (enabled ? "enabled" : "disabled");
|
|
return true;
|
|
},[](){
|
|
qWarning() << "Logger::setServiceLogsEnabled(): Service is not running";
|
|
return false;
|
|
});
|
|
#endif
|
|
|
|
return true;
|
|
}
|
|
|
|
QString Logger::userLogsDir()
|
|
{
|
|
return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/log";
|
|
}
|
|
|
|
QString Logger::systemLogDir()
|
|
{
|
|
#ifdef Q_OS_WIN
|
|
QStringList locationList = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
|
|
QString primaryLocation = "ProgramData";
|
|
foreach (const QString &location, locationList) {
|
|
if (location.contains(primaryLocation)) {
|
|
return QString("%1/%2/log").arg(location).arg(APPLICATION_NAME);
|
|
}
|
|
}
|
|
return QString();
|
|
#else
|
|
return QString("/var/log/%1").arg(APPLICATION_NAME);
|
|
#endif
|
|
}
|
|
|
|
QString Logger::userLogsFilePath()
|
|
{
|
|
return userLogsDir() + QDir::separator() + m_logFileName;
|
|
}
|
|
|
|
QString Logger::serviceLogsFilePath()
|
|
{
|
|
return systemLogDir() + QDir::separator() + m_serviceLogFileName;
|
|
}
|
|
|
|
QString Logger::getLogFile()
|
|
{
|
|
if (m_file.isOpen()) {
|
|
m_file.flush();
|
|
}
|
|
QFile file(userLogsFilePath());
|
|
|
|
file.open(QIODevice::ReadOnly);
|
|
QString qtLog = file.readAll();
|
|
|
|
#ifdef Q_OS_IOS
|
|
return QString().fromStdString(AmneziaVPN::swiftUpdateLogData(qtLog.toStdString()));
|
|
#else
|
|
return qtLog;
|
|
#endif
|
|
}
|
|
|
|
QString Logger::getServiceLogFile()
|
|
{
|
|
if (m_file.isOpen()) {
|
|
m_file.flush();
|
|
}
|
|
QFile file(serviceLogsFilePath());
|
|
|
|
file.open(QIODevice::ReadOnly);
|
|
QString qtLog = file.readAll();
|
|
|
|
#ifdef Q_OS_IOS
|
|
return QString().fromStdString(AmneziaVPN::swiftUpdateLogData(qtLog.toStdString()));
|
|
#else
|
|
return qtLog;
|
|
#endif
|
|
}
|
|
|
|
bool Logger::openLogsFolder(bool isServiceLogger)
|
|
{
|
|
QString path = isServiceLogger ? systemLogDir() : userLogsDir();
|
|
#ifdef Q_OS_WIN
|
|
path = "file:///" + path;
|
|
#endif
|
|
if (!QDesktopServices::openUrl(QUrl::fromLocalFile(path))) {
|
|
qWarning() << "Can't open url:" << path;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void Logger::clearLogs(bool isServiceLogger)
|
|
{
|
|
bool isLogActive = m_file.isOpen();
|
|
m_file.close();
|
|
|
|
QFile file(isServiceLogger ? serviceLogsFilePath() : userLogsFilePath());
|
|
|
|
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
|
|
file.resize(0);
|
|
file.close();
|
|
|
|
#ifdef Q_OS_IOS
|
|
AmneziaVPN::swiftDeleteLog();
|
|
#endif
|
|
|
|
if (isLogActive) {
|
|
init(isServiceLogger);
|
|
}
|
|
}
|
|
|
|
void Logger::clearServiceLogs()
|
|
{
|
|
#ifdef AMNEZIA_DESKTOP
|
|
IpcClient::withInterface([](QSharedPointer<IpcInterfaceReplica> iface) {
|
|
iface->clearLogs();
|
|
qDebug() << "Logger::clearServiceLogs(): Logs cleared";
|
|
}, []() {
|
|
qWarning() << "Logger::clearServiceLogs(): Service is not running";
|
|
});
|
|
#endif
|
|
}
|
|
|
|
void Logger::cleanUp()
|
|
{
|
|
clearLogs(false);
|
|
QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
|
|
dir.removeRecursively();
|
|
|
|
clearLogs(true);
|
|
}
|
|
|
|
Logger::LogStreamer::LogStreamer(Logger *logger, LogLevel logLevel)
|
|
: m_logger(logger), m_logLevel(logLevel), m_data(new Data())
|
|
{
|
|
}
|
|
|
|
Logger::LogStreamer::~LogStreamer()
|
|
{
|
|
QString logLevelString;
|
|
switch (m_logLevel) {
|
|
case LogLevel::Trace: logLevelString = "[TRACE]"; break;
|
|
case LogLevel::Debug: logLevelString = "[DEBUG]"; break;
|
|
case LogLevel::Info: logLevelString = "[INFO]"; break;
|
|
case LogLevel::Warning: logLevelString = "[WARNING]"; break;
|
|
case LogLevel::Error: logLevelString = "[ERROR]"; break;
|
|
}
|
|
|
|
const QString message = QString("%1 %2 Amnezia %3 : %4")
|
|
.arg(QDateTime::currentDateTimeUtc().toString("[yyyy-MM-dd hh:mm:ss.zzzZ]"),
|
|
logLevelString, m_logger->className(), m_data->m_buffer.trimmed());
|
|
|
|
if (m_file.isOpen()) {
|
|
QTextStream logToFile(&m_file);
|
|
logToFile << message << Qt::endl << Qt::flush;
|
|
}
|
|
|
|
QTextStream logToOutput((m_logLevel == LogLevel::Error) ? stderr : stdout);
|
|
logToOutput << message << Qt::endl << Qt::flush;
|
|
|
|
delete m_data;
|
|
}
|
|
|
|
Logger::LogStreamer Logger::error()
|
|
{
|
|
return { this, LogLevel::Error };
|
|
}
|
|
|
|
Logger::LogStreamer Logger::warning()
|
|
{
|
|
return { this, LogLevel::Warning };
|
|
}
|
|
|
|
Logger::LogStreamer Logger::info()
|
|
{
|
|
return { this, LogLevel::Info };
|
|
}
|
|
|
|
Logger::LogStreamer Logger::debug()
|
|
{
|
|
return { this, LogLevel::Debug };
|
|
}
|
|
|
|
QString Logger::sensitive(const QString &input)
|
|
{
|
|
#ifdef Q_DEBUG
|
|
return input;
|
|
#else
|
|
Q_UNUSED(input);
|
|
return { 8, 'X' };
|
|
#endif
|
|
}
|
|
|
|
#define CREATE_LOGSTREAMER_OP_REF(x) \
|
|
Logger::LogStreamer &Logger::LogStreamer::operator<<(x t) \
|
|
{ \
|
|
m_data->m_ts << t << ' '; \
|
|
return *this; \
|
|
}
|
|
|
|
CREATE_LOGSTREAMER_OP_REF(uint64_t);
|
|
CREATE_LOGSTREAMER_OP_REF(const char *);
|
|
CREATE_LOGSTREAMER_OP_REF(const QString &);
|
|
CREATE_LOGSTREAMER_OP_REF(const QByteArray &);
|
|
CREATE_LOGSTREAMER_OP_REF(const void *);
|
|
|
|
#undef CREATE_LOGSTREAMER_OP_REF
|
|
|
|
Logger::LogStreamer &Logger::LogStreamer::operator<<(const QStringList &t)
|
|
{
|
|
m_data->m_ts << '[' << t.join(",") << ']' << ' ';
|
|
return *this;
|
|
}
|
|
|
|
Logger::LogStreamer &Logger::LogStreamer::operator<<(const QJsonObject &t)
|
|
{
|
|
m_data->m_ts << QJsonDocument(t).toJson(QJsonDocument::Indented) << ' ';
|
|
return *this;
|
|
}
|
|
|
|
Logger::LogStreamer &Logger::LogStreamer::operator<<(QTextStreamFunction t)
|
|
{
|
|
m_data->m_ts << t;
|
|
return *this;
|
|
}
|
|
|
|
void Logger::LogStreamer::addMetaEnum(quint64 value, const QMetaObject *meta, const char *name)
|
|
{
|
|
QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
|
|
|
|
QString out;
|
|
QTextStream ts(&out);
|
|
|
|
if (const char *scope = me.scope()) {
|
|
ts << scope << "::";
|
|
}
|
|
|
|
const char *key = me.valueToKey(static_cast<int>(value));
|
|
const bool scoped = me.isScoped();
|
|
if (scoped || !key) {
|
|
ts << me.enumName() << (!key ? "(" : "::");
|
|
}
|
|
|
|
if (key) {
|
|
ts << key;
|
|
} else {
|
|
ts << value << ")";
|
|
}
|
|
|
|
m_data->m_ts << out;
|
|
}
|