Teaspeak-Server/server/src/client/voice/VoiceClientCommandHandler.cpp

84 lines
3.3 KiB
C++
Raw Normal View History

#include <log/LogUtils.h>
#include <misc/endianness.h>
#include <misc/base64.h>
#include <ThreadPool/Timer.h>
#include <openssl/sha.h>
#include <src/client/SpeakingClient.h>
#include "../../InstanceHandler.h"
#include "../../geo/GeoLocation.h"
#include "VoiceClient.h"
using namespace std;
using namespace std::chrono;
using namespace ts::server;
using namespace ts::protocol;
using namespace ts;
2020-01-25 23:42:37 +01:00
command_result VoiceClient::handleCommand(ts::Command &command) {
2020-01-24 02:57:58 +01:00
threads::MutexLock l2(this->command_lock);
2020-01-25 23:42:37 +01:00
if(this->state == ConnectionState::DISCONNECTED) return command_result{error::client_not_logged_in};
if(!this->voice_server) return command_result{error::server_unbound};
if(this->state == ConnectionState::INIT_HIGH && this->handshake.state == HandshakeState::SUCCEEDED) {
if(command.command() == "clientinit")
2020-01-24 02:57:58 +01:00
return this->handleCommandClientInit(command);
} else if(command.command() == "clientdisconnect")
2020-01-24 02:57:58 +01:00
return this->handleCommandClientDisconnect(command);
return SpeakingClient::handleCommand(command);
}
2019-11-23 21:16:55 +01:00
inline bool calculate_security_level(int& result, ecc_key* pubKey, size_t offset) {
size_t pubLength = 256;
char pubBuffer[256];
2019-11-23 21:16:55 +01:00
if((result = ecc_export(reinterpret_cast<unsigned char *>(pubBuffer), &pubLength, PK_PUBLIC, pubKey)) != CRYPT_OK)
2020-01-24 02:57:58 +01:00
return false;
std::string hashStr = base64_encode(pubBuffer, pubLength) + to_string(offset);
char shaBuffer[SHA_DIGEST_LENGTH];
SHA1((const unsigned char *) hashStr.data(), hashStr.length(), (unsigned char *) shaBuffer);
//Leading zero bits
int zeroBits = 0;
int i;
for(i = 0; i < SHA_DIGEST_LENGTH; i++)
if(shaBuffer[i] == 0) zeroBits += 8;
else break;
if(i < SHA_DIGEST_LENGTH) {
2020-01-24 02:57:58 +01:00
for(int bit = 0; bit < 8; bit++) {
if((shaBuffer[i] & (1 << bit)) == 0) zeroBits++;
else break;
}
}
2020-01-24 02:57:58 +01:00
result = zeroBits;
return true;
}
2020-01-25 23:42:37 +01:00
command_result VoiceClient::handleCommandClientInit(Command &cmd) {
2020-01-24 02:57:58 +01:00
this->crypto.client_init = true;
this->connection->acknowledge_handler.reset();
2020-01-24 02:57:58 +01:00
if(this->getType() == ClientType::CLIENT_TEAMSPEAK) {
int securityLevel;
if(!calculate_security_level(securityLevel, this->crypto.remote_key.get(), cmd["client_key_offset"])) {
logError(this->getServerId(), "[{}] Failed to calculate security level. Error code: {}", CLIENT_STR_LOG_PREFIX, securityLevel);
2020-01-25 23:42:37 +01:00
return command_result{error::vs_critical};
2020-01-24 02:57:58 +01:00
}
if(securityLevel < 8)
2020-01-25 23:42:37 +01:00
return command_result{error::client_could_not_validate_identity};
2020-01-24 02:57:58 +01:00
auto requiredLevel = this->getServer()->properties()[property::VIRTUALSERVER_NEEDED_IDENTITY_SECURITY_LEVEL].as<uint8_t>();
2020-01-25 23:42:37 +01:00
if(securityLevel < requiredLevel) return command_result{error::client_could_not_validate_identity, to_string(requiredLevel)};
2020-01-24 02:57:58 +01:00
}
2020-01-24 02:57:58 +01:00
this->lastPingResponse = std::chrono::system_clock::now();
return SpeakingClient::handleCommandClientInit(cmd);
}
2020-01-25 23:42:37 +01:00
command_result VoiceClient::handleCommandClientDisconnect(Command& cmd) {
2020-01-24 02:57:58 +01:00
auto reason = cmd["reasonmsg"].size() > 0 ? cmd["reasonmsg"].as<string>() : "";
this->disconnect(VREASON_SERVER_LEFT, reason, nullptr, true);
logMessage(this->getServerId(), "{} Got remote disconnect with the reason '{}'", CLIENT_STR_LOG_PREFIX, reason);
2020-01-25 23:42:37 +01:00
return command_result{error::ok};
}