Teaspeak-Server/license/shared/LicenseRequestHandler.cpp

105 lines
4.6 KiB
C++

#include <misc/endianness.h>
#include <LicenseRequest.pb.h>
#define DEFINE_HELPER
#include "LicenseRequest.h"
using namespace license;
using namespace std;
using namespace std::chrono;
void LicenceRequest::handlePacketDisconnect(const std::string& message) {
if(this->state != protocol::DISCONNECTING)
LICENSE_FERR(this, UnexcpectedDisconnectException, "Remote side closed the connection (" + message + ")");
}
void LicenceRequest::handlePacketHandshake(const std::string& data) {
if(this->state != protocol::HANDSCAKE) LICENSE_FERR(this, InvalidResponseException, "Protocol state mismatch");
if(data.length() < 3) LICENSE_FERR(this, InvalidResponseException, "Invalid packet size");
if((uint8_t) data[0] != 0xAF || (uint8_t) data[1] != 0xFE) LICENSE_FERR(this, InvalidResponseException, "Invalid handshake");
if((uint8_t) data[2] != LICENSE_PROT_VERSION) LICENSE_FERR(this, InvalidResponseException, "Invalid license protocol version. Please update TeaSpeak!");
auto key_length = be2le16(data.data(), 3);
if(data.length() < key_length + 3) LICENSE_FERR(this, InvalidResponseException, "Invalid packet size");
this->cryptKey = data.substr(5, key_length);
ts::proto::license::ServerValidation request;
if(this->data->license) {
request.set_licensed(true);
request.set_license_info(true);
request.set_license(exportLocalLicense(this->data->license));
} else {
request.set_licensed(false);
request.set_license_info(false);
}
request.mutable_info()->set_uname(this->data->info->uname);
request.mutable_info()->set_version(this->data->info->version);
request.mutable_info()->set_timestamp(this->data->info->timestamp);
request.mutable_info()->set_unique_id(this->data->info->unique_identifier);
this->sendPacket(protocol::packet{protocol::PACKET_CLIENT_SERVER_VALIDATION, request});
this->state = protocol::LICENSE_INFO;
}
void LicenceRequest::handlePacketLicenseInfo(const std::string& message) {
ts::proto::license::LicenseResponse response;
if(!response.ParseFromString(message)) LICENSE_FERR(this, InvalidResponseException, "Could not parse response");
auto result = make_shared<LicenseRequestResponse>();
auto licenseInfo = make_shared<LicenseInfo>();
if(!response.has_license_info() && this->data->license) LICENSE_FERR(this, InvalidResponseException, "Missing license info");
if(this->data->license) {
licenseInfo->type = (LicenseType) response.license_info().type();
licenseInfo->email = response.license_info().email();
licenseInfo->username = response.license_info().username();
licenseInfo->first_name = response.license_info().first_name();
licenseInfo->last_name = response.license_info().last_name();
licenseInfo->creation = system_clock::time_point() + milliseconds(response.license_info().created());
licenseInfo->start = system_clock::time_point() + milliseconds(response.license_info().begin());
licenseInfo->end = system_clock::time_point() + milliseconds(response.license_info().end());
} else {
licenseInfo->type = LicenseType::DEMO;
licenseInfo->email = "license@teaspeak.de";
licenseInfo->username = "WolverinDEV";
licenseInfo->first_name = "Max";
licenseInfo->last_name = "Musterman";
licenseInfo->creation = system_clock::now();
licenseInfo->start = system_clock::now();
licenseInfo->end = system_clock::time_point();
}
result->license_valid = response.blacklist().state() == ts::proto::license::VALID; //TODO more detailed
result->age = system_clock::now();
result->license = licenseInfo;
this->response = result;
ts::proto::license::PropertyUpdateRequest infos;
infos.set_speach_total(this->data->speach_total);
infos.set_speach_dead(this->data->speach_dead);
infos.set_speach_online(this->data->speach_online);
infos.set_speach_varianz(this->data->speach_varianz);
infos.set_clients_online(this->data->client_online);
infos.set_bots_online(this->data->bots_online);
infos.set_queries_online(this->data->queries_online);
infos.set_servers_online(this->data->servers_online);
infos.set_web_clients_online(this->data->web_clients_online);
this->sendPacket({protocol::PACKET_CLIENT_PROPERTY_ADJUSTMENT, infos});
this->state = protocol::PROPERTY_ADJUSTMENT;
}
void LicenceRequest::handlePacketInfoAdjustment(const std::string& message) {
ts::proto::license::PropertyUpdateResponse response;
if(!response.ParseFromString(message)) LICENSE_FERR(this, InvalidResponseException, "Could not parse response");
this->response->properties_valid = response.accepted();
this->response->speach_varianz_adjustment = response.speach_varianz_corrector();
this->response->speach_reset = response.reset_speach();
this->currentFuture->executionSucceed(this->response);
this->response = nullptr;
this->disconnect("query succeeded!");
}