Bump CMake minimum to 4.0 for VoidLibrary toolchain

This commit is contained in:
faumaray committed 2026-06-22 15:53:27 +03:00
1 parent e646080683
commit bcf4f4770a
12 files changed
+1784 -1208

No files matched your search

+216
View File
@@ -0,0 +1,216 @@
#include "storage/SecureStorage.hpp"
#include <QCoreApplication>
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QStandardPaths>
#include <QtEndian>
namespace Echoion::Desktop
{
SecureStorage::SecureStorage() = default;
QString SecureStorage::getStorageDir()
{
QString appDir = QCoreApplication::applicationDirPath();
QString dataDir = appDir + "/data";
QDir dir(dataDir);
if (!dir.exists()) {
dir.mkpath(".");
}
return dataDir;
}
QString SecureStorage::getSettingsPath()
{
return getStorageDir() + "/settings.dat";
}
QString SecureStorage::getTokensPath()
{
return getStorageDir() + "/tokens.dat";
}
QByteArray SecureStorage::generateKey() const
{
if (!cachedKey_.isEmpty()) {
return cachedKey_;
}
// Generate a deterministic key from machine-specific data
// This makes it harder to copy tokens to another machine
QString seed =
QCoreApplication::applicationDirPath() +
QCoreApplication::applicationName() +
QString::number(QDateTime::currentDateTime().date().year());
QByteArray hash =
QCryptographicHash::hash(seed.toUtf8(), QCryptographicHash::Sha256);
// Add some randomness but keep it deterministic for this machine
QRandomGenerator rng(qHash(seed));
for (int i = 0; i < hash.size(); ++i) {
hash[i] = hash[i] ^ static_cast<char>(rng.generate() & 0xFF);
}
cachedKey_ = hash;
return hash;
}
QByteArray SecureStorage::xorEncrypt(const QByteArray& data,
const QByteArray& key) const
{
QByteArray result = data;
int keyLen = key.length();
for (int i = 0; i < result.length(); ++i) {
result[i] = result[i] ^ key[i % keyLen];
}
return result;
}
QByteArray SecureStorage::encrypt(const QByteArray& data) const
{
QByteArray key = generateKey();
// Add simple header for validation
QByteArray header;
QDataStream headerStream(&header, QIODevice::WriteOnly);
headerStream << quint32(0x4543484F); // "ECHO" magic
headerStream << quint32(data.length());
QByteArray encrypted = xorEncrypt(data, key);
return header + encrypted;
}
QByteArray SecureStorage::decrypt(const QByteArray& data) const
{
if (data.length() < 8) {
return QByteArray(); // Too short
}
// Verify header
QDataStream headerStream(data);
quint32 magic;
quint32 dataLen;
headerStream >> magic >> dataLen;
if (magic != 0x4543484F) {
return QByteArray(); // Invalid magic
}
QByteArray key = generateKey();
QByteArray encrypted = data.mid(8); // Skip header
return xorEncrypt(encrypted, key);
}
bool SecureStorage::saveToFile(const QString& filepath,
const QByteArray& data) const
{
// Create backup of existing file
if (QFile::exists(filepath)) {
createBackup(filepath);
}
// Write to temp file first
QString tempPath = filepath + ".tmp";
QFile tempFile(tempPath);
if (!tempFile.open(QIODevice::WriteOnly)) {
return false;
}
if (tempFile.write(data) != data.length()) {
tempFile.close();
QFile::remove(tempPath);
return false;
}
tempFile.close();
// Atomic rename
if (!QFile::rename(tempPath, filepath)) {
QFile::remove(tempPath);
return false;
}
return true;
}
QByteArray SecureStorage::loadFromFile(const QString& filepath) const
{
QFile file(filepath);
if (!file.open(QIODevice::ReadOnly)) {
// Try to restore from backup
if (restoreFromBackup(filepath)) {
return loadFromFile(filepath); // Retry
}
return QByteArray();
}
QByteArray data = file.readAll();
file.close();
return data;
}
void SecureStorage::createBackup(const QString& filepath) const
{
QString backupPath = filepath + ".backup";
QFile::remove(backupPath); // Remove old backup
QFile::copy(filepath, backupPath);
// Cleanup old backups (keep only last 3)
cleanupOldBackups(filepath, 3);
}
bool SecureStorage::restoreFromBackup(const QString& filepath) const
{
QString backupPath = filepath + ".backup";
if (!QFile::exists(backupPath)) {
return false;
}
QFile::remove(filepath);
return QFile::copy(backupPath, filepath);
}
void SecureStorage::cleanupOldBackups(const QString& filepath,
int maxBackups) const
{
QString basePath = filepath + ".backup";
QFileInfo info(basePath);
QDir dir = info.dir();
QString baseName = info.fileName();
// List all backup files
QStringList filters;
filters << baseName + "*";
QFileInfoList backups = dir.entryInfoList(filters, QDir::Files);
if (backups.size() <= maxBackups) {
return;
}
// Sort by modification time (oldest first)
std::sort(backups.begin(), backups.end(),
[](const QFileInfo& a, const QFileInfo& b) {
return a.lastModified() < b.lastModified();
});
// Remove oldest backups
for (int i = 0; i < backups.size() - maxBackups; ++i) {
QFile::remove(backups[i].absoluteFilePath());
}
}
} // namespace Echoion::Desktop
+53
View File
@@ -0,0 +1,53 @@
#pragma once
#include <QCryptographicHash>
#include <QDataStream>
#include <QFile>
#include <QRandomGenerator>
#include <QString>
namespace Echoion::Desktop
{
class SecureStorage
{
public:
SecureStorage();
// Encrypt/decrypt data using Qt crypto
[[nodiscard]] QByteArray encrypt(const QByteArray& data) const;
[[nodiscard]] QByteArray decrypt(const QByteArray& data) const;
// File operations with backup
[[nodiscard]] bool saveToFile(const QString& filepath,
const QByteArray& data) const;
[[nodiscard]] QByteArray
loadFromFile(const QString& filepath) const;
// Backup management
void createBackup(const QString& filepath) const;
bool restoreFromBackup(const QString& filepath) const;
void cleanupOldBackups(const QString& filepath,
int maxBackups = 3) const;
// Get storage directory (relative to executable)
[[nodiscard]] static QString getStorageDir();
[[nodiscard]] static QString getSettingsPath();
[[nodiscard]] static QString getTokensPath();
[[nodiscard]] static QString getCacheDir();
private:
// Generate encryption key from machine-specific data
[[nodiscard]] QByteArray generateKey() const;
// Simple XOR encryption with SHA-256 derived key
[[nodiscard]] QByteArray xorEncrypt(const QByteArray& data,
const QByteArray& key) const;
mutable QByteArray cachedKey_;
};
} // namespace Echoion::Desktop
+294
View File
@@ -0,0 +1,294 @@
#include "storage/SecureStorage.hpp"
#include "storage/SettingsManager.hpp"
#include <QDataStream>
namespace Echoion::Desktop
{
SettingsManager::SettingsManager(QObject* parent) : QObject(parent) {}
bool SettingsManager::load()
{
bool settingsOk = loadSettings();
bool tokensOk = loadTokens();
if (settingsOk || tokensOk) {
emit settingsLoaded();
return true;
}
return false;
}
bool SettingsManager::save()
{
bool settingsOk = saveSettings();
bool tokensOk = saveTokens();
if (settingsOk && tokensOk) {
emit settingsSaved();
return true;
}
return false;
}
bool SettingsManager::loadSettings()
{
SecureStorage storage;
QString path = SecureStorage::getSettingsPath();
if (!QFile::exists(path)) {
// No settings file yet, use defaults
return true;
}
QByteArray encrypted = storage.loadFromFile(path);
if (encrypted.isEmpty()) {
return false;
}
QByteArray decrypted = storage.decrypt(encrypted);
if (decrypted.isEmpty()) {
return false;
}
QDataStream stream(decrypted);
stream >> settings_;
return stream.status() == QDataStream::Ok;
}
bool SettingsManager::saveSettings()
{
SecureStorage storage;
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << settings_;
if (stream.status() != QDataStream::Ok) {
return false;
}
QByteArray encrypted = storage.encrypt(data);
return storage.saveToFile(SecureStorage::getSettingsPath(), encrypted);
}
bool SettingsManager::loadTokens()
{
SecureStorage storage;
QString path = SecureStorage::getTokensPath();
if (!QFile::exists(path)) {
// No tokens file yet
return true;
}
QByteArray encrypted = storage.loadFromFile(path);
if (encrypted.isEmpty()) {
return false;
}
QByteArray decrypted = storage.decrypt(encrypted);
if (decrypted.isEmpty()) {
return false;
}
QDataStream stream(decrypted);
stream >> tokens_;
return stream.status() == QDataStream::Ok;
}
bool SettingsManager::saveTokens()
{
SecureStorage storage;
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << tokens_;
if (stream.status() != QDataStream::Ok) {
return false;
}
QByteArray encrypted = storage.encrypt(data);
return storage.saveToFile(SecureStorage::getTokensPath(), encrypted);
}
void SettingsManager::addProfile(const ConnectionProfile& profile)
{
// Remove existing profile with same name
removeProfile(profile.name);
settings_.profiles.append(profile);
// If this is the first profile or marked as default, update
// lastProfileName
if (settings_.profiles.size() == 1 || profile.isDefault) {
if (profile.isDefault) {
// Unset default on other profiles
for (auto& p : settings_.profiles) {
if (p.name != profile.name) {
p.isDefault = false;
}
}
}
settings_.lastProfileName = profile.name;
}
save();
emit profileAdded(profile.name);
}
void SettingsManager::removeProfile(const QString& name)
{
auto it = std::remove_if(
settings_.profiles.begin(),
settings_.profiles.end(),
[&name](const ConnectionProfile& p) { return p.name == name; });
if (it != settings_.profiles.end()) {
settings_.profiles.erase(it, settings_.profiles.end());
// Update lastProfileName if needed
if (settings_.lastProfileName == name) {
if (!settings_.profiles.isEmpty()) {
settings_.lastProfileName = settings_.profiles.first().name;
} else {
settings_.lastProfileName.clear();
}
}
save();
emit profileRemoved(name);
}
}
void SettingsManager::updateProfile(const QString& name,
const ConnectionProfile& profile)
{
removeProfile(name);
addProfile(profile);
emit profileChanged(profile.name);
}
QVector<ConnectionProfile> SettingsManager::profiles() const
{
return settings_.profiles;
}
std::optional<ConnectionProfile>
SettingsManager::getProfile(const QString& name) const
{
for (const auto& profile : settings_.profiles) {
if (profile.name == name) {
return profile;
}
}
return std::nullopt;
}
std::optional<ConnectionProfile> SettingsManager::getDefaultProfile() const
{
// First try to find profile marked as default
for (const auto& profile : settings_.profiles) {
if (profile.isDefault) {
return profile;
}
}
// Then try last used profile
if (!settings_.lastProfileName.isEmpty()) {
auto profile = getProfile(settings_.lastProfileName);
if (profile) {
return profile;
}
}
// Finally return first profile
if (!settings_.profiles.isEmpty()) {
return settings_.profiles.first();
}
return std::nullopt;
}
void SettingsManager::setDefaultProfile(const QString& name)
{
for (auto& profile : settings_.profiles) {
profile.isDefault = (profile.name == name);
}
settings_.lastProfileName = name;
save();
}
void SettingsManager::saveToken(const QString& host, int port,
const QString& nickname,
const QString& token)
{
// Remove existing token for this host:port
deleteToken(host, port);
CachedToken ct;
ct.host = host;
ct.port = port;
ct.nickname = nickname;
ct.token = token;
ct.savedAt = QDateTime::currentDateTime();
tokens_.append(ct);
saveTokens();
emit tokenSaved(host, port);
}
std::optional<QString> SettingsManager::loadToken(
const QString& host, int port, QString& outNickname) const
{
for (const auto& ct : tokens_) {
if (ct.host == host && ct.port == port) {
outNickname = ct.nickname;
return ct.token;
}
}
return std::nullopt;
}
void SettingsManager::deleteToken(const QString& host, int port)
{
auto it = std::remove_if(tokens_.begin(), tokens_.end(),
[&host, port](const CachedToken& ct) {
return ct.host == host && ct.port == port;
});
if (it != tokens_.end()) {
tokens_.erase(it, tokens_.end());
saveTokens();
emit tokenDeleted(host, port);
}
}
void SettingsManager::clearAllTokens()
{
tokens_.clear();
saveTokens();
}
bool SettingsManager::hasToken(const QString& host, int port) const
{
for (const auto& ct : tokens_) {
if (ct.host == host && ct.port == port) {
return true;
}
}
return false;
}
AppSettings& SettingsManager::settings() { return settings_; }
const AppSettings& SettingsManager::settings() const { return settings_; }
} // namespace Echoion::Desktop
+153
View File
@@ -0,0 +1,153 @@
#pragma once
#include <QDataStream>
#include <QDateTime>
#include <QObject>
#include <QString>
#include <QVector>
namespace Echoion::Desktop
{
struct ConnectionProfile {
QString name;
QString host;
int port = 8765;
QString nickname;
bool autoConnect = false;
bool autoAuth = false;
bool isDefault = false;
// Serialization
friend QDataStream& operator<<(QDataStream& out,
const ConnectionProfile& profile)
{
out << profile.name << profile.host << profile.port
<< profile.nickname << profile.autoConnect
<< profile.autoAuth << profile.isDefault;
return out;
}
friend QDataStream& operator>>(QDataStream& in,
ConnectionProfile& profile)
{
in >> profile.name >> profile.host >> profile.port >>
profile.nickname >> profile.autoConnect >>
profile.autoAuth >> profile.isDefault;
return in;
}
};
struct CachedToken {
QString host;
int port;
QString nickname;
QString token;
QDateTime savedAt;
// Serialization
friend QDataStream& operator<<(QDataStream& out,
const CachedToken& token)
{
out << token.host << token.port << token.nickname << token.token
<< token.savedAt;
return out;
}
friend QDataStream& operator>>(QDataStream& in, CachedToken& token)
{
in >> token.host >> token.port >> token.nickname >>
token.token >> token.savedAt;
return in;
}
};
struct AppSettings {
QVector<ConnectionProfile> profiles;
QString lastProfileName;
bool autoConnect = true;
bool autoAuth = true;
bool autoReconnect = true;
int maxReconnectAttempts = 5;
int requestTimeoutMs = 10000;
// Serialization
friend QDataStream& operator<<(QDataStream& out,
const AppSettings& settings)
{
out << settings.profiles << settings.lastProfileName
<< settings.autoConnect << settings.autoAuth
<< settings.autoReconnect << settings.maxReconnectAttempts
<< settings.requestTimeoutMs;
return out;
}
friend QDataStream& operator>>(QDataStream& in,
AppSettings& settings)
{
in >> settings.profiles >> settings.lastProfileName >>
settings.autoConnect >> settings.autoAuth >>
settings.autoReconnect >> settings.maxReconnectAttempts >>
settings.requestTimeoutMs;
return in;
}
};
class SettingsManager : public QObject
{
Q_OBJECT
public:
explicit SettingsManager(QObject* parent = nullptr);
// Load/save all settings
bool load();
bool save();
// Connection profiles
void addProfile(const ConnectionProfile& profile);
void removeProfile(const QString& name);
void updateProfile(const QString& name,
const ConnectionProfile& profile);
QVector<ConnectionProfile> profiles() const;
std::optional<ConnectionProfile>
getProfile(const QString& name) const;
std::optional<ConnectionProfile> getDefaultProfile() const;
void setDefaultProfile(const QString& name);
// Token management
void saveToken(const QString& host, int port,
const QString& nickname, const QString& token);
std::optional<QString> loadToken(const QString& host, int port,
QString& outNickname) const;
void deleteToken(const QString& host, int port);
void clearAllTokens();
bool hasToken(const QString& host, int port) const;
// Settings accessors
AppSettings& settings();
const AppSettings& settings() const;
signals:
void settingsLoaded();
void settingsSaved();
void profileAdded(const QString& name);
void profileRemoved(const QString& name);
void profileChanged(const QString& name);
void tokenSaved(const QString& host, int port);
void tokenDeleted(const QString& host, int port);
private:
bool loadSettings();
bool saveSettings();
bool loadTokens();
bool saveTokens();
AppSettings settings_;
QVector<CachedToken> tokens_;
};
} // namespace Echoion::Desktop