lolautoaccept/src/clientaccess.cpp

48 lines
1.1 KiB
C++
Raw Normal View History

2022-08-24 16:12:03 +02:00
#include "clientaccess.h"
#include <fstream>
#include <iomanip>
2023-05-31 22:22:23 +02:00
#include <QDebug>
#include <QStringList>
2022-08-24 16:12:03 +02:00
ClientAccess::ClientAccess() {}
2023-05-31 22:22:23 +02:00
ClientAccess::ClientAccess(const QString& token, uint16_t port) : authcode(token), port(port) {}
2022-08-24 16:12:03 +02:00
std::shared_ptr<ClientAccess> createFromLockfile(std::istream& lockfile) {
2023-05-31 22:22:23 +02:00
2022-08-24 16:12:03 +02:00
std::string content;
2023-05-31 22:22:23 +02:00
std::getline(lockfile, content);
QStringList parts = QString::fromStdString(content).split(':');
2022-08-24 16:12:03 +02:00
if(parts.size() != 5) {
2023-05-31 22:22:23 +02:00
qCritical() << "lockfile contained " << parts.size() << " parts, expected 5";
2022-08-24 16:12:03 +02:00
return {};
}
2023-05-31 22:22:23 +02:00
const QString portstr = parts.at(2);
const QString token = parts.at(3);
2022-08-24 16:12:03 +02:00
// try to parse port
2023-05-31 22:22:23 +02:00
bool success = false;
uint16_t port = portstr.toUInt(&success);
if(!success) {
qCritical() << "could not parse port: " << portstr;
return nullptr;
2022-08-24 16:12:03 +02:00
}
2023-05-31 22:22:23 +02:00
return std::shared_ptr<ClientAccess>(new ClientAccess(token, port));
2022-08-24 16:12:03 +02:00
}
2023-05-31 22:22:23 +02:00
QString ClientAccess::getBasicAuth() const {
2022-08-24 16:12:03 +02:00
return "riot:" + authcode;
}
uint16_t ClientAccess::getPort() const {
return port;
}
2023-05-31 22:22:23 +02:00
QString ClientAccess::getURL() const {
return "https://127.0.0.1:" + QString::number(port) + "/";
2022-08-24 16:12:03 +02:00
}