diff --git a/license/CMakeLists.txt b/license/CMakeLists.txt index 3c19b20..bdaa693 100644 --- a/license/CMakeLists.txt +++ b/license/CMakeLists.txt @@ -14,100 +14,65 @@ include_directories(../shared/src) add_definitions(-DLTM_DESC) set(LICENCE_SOURCE_FILES - shared/LicenseRequest.cpp - shared/LicenseRequestHandler.cpp - shared/License.cpp - shared/LicenseServerClient.cpp - ../shared/src/log/LogUtils.cpp + shared/src/license.cpp + shared/src/client.cpp ) #Protobuf find_package(Protobuf REQUIRED) include_directories(${Protobuf_INCLUDE_DIRS}) -include_directories(${CMAKE_CURRENT_BINARY_DIR}) -protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS packets/LicenseRequest.proto packets/LicenseManager.proto) +protobuf_generate_cpp(PROTO_SRCS TeaLicenseHelper_PROTOCOL_HEADERS shared/packets/LicenseRequest.proto shared/packets/LicenseManager.proto) -#The actual librarie -add_library(TeaLicenseHelper STATIC ${LICENCE_SOURCE_FILES} ${PROTO_SRCS} ${PROTO_HDRS}) -target_link_libraries(TeaLicenseHelper PUBLIC TeaSpeak libevent::core libevent::pthreads) +#The actual shared library +add_library(TeaLicenseHelper STATIC ${LICENCE_SOURCE_FILES} ${PROTO_SRCS}) +target_link_libraries(TeaLicenseHelper PUBLIC + TeaSpeak + protobuf::libprotobuf + libevent::core libevent::pthreads + openssl::ssl::shared + openssl::crypto::shared + ${StringVariable_LIBRARIES_STATIC} + ${ed25519_LIBRARIES_STATIC} + stdc++fs +) +target_include_directories(TeaLicenseHelper PUBLIC shared/include/) +target_include_directories(TeaLicenseHelper PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) #The license server -add_executable(TeaLicenseServer ${LICENCE_SOURCE_FILES} ${PROTO_SRCS} ${PROTO_HDRS} +add_executable(TeaLicenseServer ${LICENCE_SOURCE_FILES} server/KeyIdCache.cpp server/LicenseServer.cpp server/LicenseServerHandler.cpp server/DatabaseHandler.cpp - LicenseServerMain.cpp server/WebAPI.cpp server/StatisticManager.cpp server/UserManager.cpp + LicenseServerMain.cpp MySQLLibSSLFix.c ) -target_compile_options(TeaLicenseServer PRIVATE "-Wl,--unresolved-symbols=ignore-in-object-files") target_link_libraries(TeaLicenseServer + TeaSpeak + TeaLicenseHelper #Static threadpool::static #Static - TeaSpeak #Static - TeaLicenseHelper #Static CXXTerminal::static #Static - ${StringVariable_LIBRARIES_STATIC} - # stdc++fs libevent::core libevent::pthreads - yaml-cpp - ${LIBRARY_PATH_PROTOBUF} - ${ed25519_LIBRARIES_STATIC} - #We're forsed to use boringssl caused by the fact that boringssl is already within webrtc! - - #Require a so - sqlite3 - protobuf::libprotobuf - - tomcrypt::static - tommath::static - - mysqlclient.a + mysqlclient jsoncpp_lib - ${DataPipes_LIBRARIES_SHARED} # Also includes glib2.0 + DataPipes::core::shared openssl::ssl::shared openssl::crypto::shared + pthread dl z ) -if (COMPILE_WEB_CLIENT) - target_link_libraries(TeaLicenseServer ${glib20_DIR}/lib/x86_64-linux-gnu/libffi.so.7 ${nice_DIR}/lib/libnice.so.10) -endif () - -include_directories(${LIBRARY_PATH}/boringssl/include/) #The test license client -add_executable(TeaLicenseClient - LicenseClientMain.cpp - ${LICENCE_SOURCE_FILES} ${PROTO_SRCS} ${PROTO_HDRS}) -target_link_libraries(TeaLicenseClient - TeaSpeak #Static - - protobuf::libprotobuf - stdc++fs - libevent::core libevent::pthreads - ${ed25519_LIBRARIES_STATIC} - ${StringVariable_LIBRARIES_STATIC} - - CXXTerminal::static #Static - - tomcrypt::static - tommath::static - - ${DataPipes_LIBRARIES_SHARED} # Also includes glib2.0 - ${glib20_DIR}/lib/x86_64-linux-gnu/libffi.so.7 ${nice_DIR}/lib/libnice.so.10 - openssl::ssl::shared - openssl::crypto::shared - pthread - dl - z -) +add_executable(TeaLicenseClient LicenseClientMain.cpp) +target_link_libraries(TeaLicenseClient TeaLicenseHelper) #The license manager if(NOT DISABLE_QT) @@ -162,17 +127,6 @@ add_executable(LicenseCLI manager/ServerConnection.cpp manager/ServerConnectionExecutor.cpp manager/ServerConnectionHandler.cpp - shared/License.cpp - ${PROTO_SRCS} ${PROTO_HDRS} ) -target_link_libraries(LicenseCLI - TeaSpeak - libevent::core libevent::pthreads - threadpool::static #Static - tomcrypt::static - tommath::static - protobuf::libprotobuf - ${ed25519_LIBRARIES_STATIC} - pthread -) \ No newline at end of file +target_link_libraries(LicenseCLI TeaLicenseHelper) \ No newline at end of file diff --git a/license/LicenseClientMain.cpp b/license/LicenseClientMain.cpp index 5c6c494..2bb1b39 100644 --- a/license/LicenseClientMain.cpp +++ b/license/LicenseClientMain.cpp @@ -1,8 +1,8 @@ #include -#include -#include +#include +#include #include -#include "shared/LicenseServerClient.h" +#include "shared/include/license/client.h" #include #include diff --git a/license/LicenseManager.cpp b/license/LicenseManager.cpp index 6912036..16655ab 100644 --- a/license/LicenseManager.cpp +++ b/license/LicenseManager.cpp @@ -1,6 +1,6 @@ #include -#include -#include +#include +#include #include #include #include diff --git a/license/manager/ServerConnection.cpp b/license/manager/ServerConnection.cpp index 18f98b5..f800b7b 100644 --- a/license/manager/ServerConnection.cpp +++ b/license/manager/ServerConnection.cpp @@ -3,8 +3,8 @@ // #include -#include -#include +#include +#include #include #include #include diff --git a/license/manager/ServerConnection.h b/license/manager/ServerConnection.h index a5d7a9f..48a0217 100644 --- a/license/manager/ServerConnection.h +++ b/license/manager/ServerConnection.h @@ -6,7 +6,7 @@ #include #include #include -#include "shared/License.h" +#include "shared/include/license/license.h" #define FLSUCCESS(listener, object) \ do { \ diff --git a/license/manager/ServerConnectionHandler.cpp b/license/manager/ServerConnectionHandler.cpp index 6398d6d..b821f92 100644 --- a/license/manager/ServerConnectionHandler.cpp +++ b/license/manager/ServerConnectionHandler.cpp @@ -2,8 +2,8 @@ #include #include #include -#include -#include +#include +#include #include #include "ServerConnection.h" diff --git a/license/server/DatabaseHandler.h b/license/server/DatabaseHandler.h index a1d907d..4b4b082 100644 --- a/license/server/DatabaseHandler.h +++ b/license/server/DatabaseHandler.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include namespace license::server::database { diff --git a/license/server/LicenseServer.cpp b/license/server/LicenseServer.cpp index 182d64e..fef87f8 100644 --- a/license/server/LicenseServer.cpp +++ b/license/server/LicenseServer.cpp @@ -8,8 +8,8 @@ #include #include #include -#include -#include +#include +#include #include #include "LicenseServer.h" #include "UserManager.h" @@ -363,10 +363,11 @@ void LicenseServer::handleMessage(shared_ptr& client, const std memcpy(&packet.header, message.data(), sizeof(protocol::packet::header)); packet.data = message.substr(sizeof(protocol::packet::header), packet.header.length); - if(!client->protocol.cryptKey.empty()) + if(!client->protocol.cryptKey.empty()) { xorBuffer((char*) packet.data.data(), packet.data.length(), client->protocol.cryptKey.data(), client->protocol.cryptKey.length()); + } - bool success = false; + bool success{false}; string error; try { if(packet.header.packetId == protocol::PACKET_CLIENT_HANDSHAKE) { diff --git a/license/server/LicenseServer.h b/license/server/LicenseServer.h index d1837f8..55a8969 100644 --- a/license/server/LicenseServer.h +++ b/license/server/LicenseServer.h @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include "DatabaseHandler.h" diff --git a/license/server/LicenseServerHandler.cpp b/license/server/LicenseServerHandler.cpp index da21193..7db025d 100644 --- a/license/server/LicenseServerHandler.cpp +++ b/license/server/LicenseServerHandler.cpp @@ -3,9 +3,9 @@ #include #include #include -#include +#include #include "LicenseRequest.pb.h" -#include "shared/License.h" +#include "shared/include/license/license.h" #include "LicenseServer.h" #include "WebAPI.h" #include "StatisticManager.h" diff --git a/license/server/WebAPI.h b/license/server/WebAPI.h index 4b14bb2..78e476e 100644 --- a/license/server/WebAPI.h +++ b/license/server/WebAPI.h @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/license/shared/LicenseRequest.cpp b/license/shared/LicenseRequest.cpp deleted file mode 100644 index 5fdef57..0000000 --- a/license/shared/LicenseRequest.cpp +++ /dev/null @@ -1,306 +0,0 @@ -#include -#include -#include -#include -#include "crypt.h" - -#define DEFINE_HELPER -#include "LicenseRequest.h" -#include "License.h" -#include -#include -#include - -using namespace std; -using namespace std::chrono; -using namespace ts; -using namespace license; - -//#define DEBUG_LICENSE_CLIENT -#define CERR(message) LICENSE_FERR(this, CouldNotConnectException, message) - - -LicenceRequest::LicenceRequest(const std::shared_ptr & license, const sockaddr_in& remoteAddr) : data{license} { -#ifdef DEBUG_LICENSE_CLIENT - memtrack::allocated(this); -#endif - memcpy(&this->remote_address, &remoteAddr, sizeof(remoteAddr)); - - assert(license->info); -} - -LicenceRequest::~LicenceRequest() { -#ifdef DEBUG_LICENSE_CLIENT - memtrack::freed(this); -#endif - this->abortRequest(); - - threads::save_join(this->closeThread); - delete this->currentFuture; - this->currentFuture = nullptr; -} - -threads::Future> LicenceRequest::requestInfo() { - { - lock_guard lock(this->lock); - if(this->currentFuture) return *this->currentFuture; - this->currentFuture = new threads::Future>(); - } - - this->beginRequest(); - return *this->currentFuture; -} - - -//Basic IO -void LicenceRequest::handleEventWrite(int fd, short event, void* ptrClient) { - auto* client = static_cast(ptrClient); - - buffer::RawBuffer* buffer = nullptr; - { - lock_guard lock(client->lock); - if((event & EV_TIMEOUT) > 0) { //Connect timeout - LICENSE_FERR(client, ConnectionException, "Connect timeout"); - return; - } - - if(client->state == protocol::CONNECTING){ - client->handleConnected(); - } - - if(client->state == protocol::UNCONNECTED || !client->event_write) - return; - - buffer = TAILQ_FIRST(&client->writeQueue); - if(!buffer) return; - - auto writtenBytes = send(fd, &buffer->buffer[buffer->index], buffer->length - buffer->index, MSG_NOSIGNAL | MSG_DONTWAIT); - buffer->index += writtenBytes; - - if(buffer->index >= buffer->length) { - TAILQ_REMOVE(&client->writeQueue, buffer, tail); - delete buffer; - } - if(!TAILQ_EMPTY(&client->writeQueue)) - event_add(client->event_write, nullptr); - } -} - -void LicenceRequest::sendPacket(const protocol::packet& packet) { - if(this->state == protocol::UNCONNECTED || this->state == protocol::DISCONNECTING) { - if(this->verbose) - logError(LOG_GENERAL, "Tried to send a packet to an unconnected remote!"); - return; - } - packet.prepare(); - - auto buffer = new buffer::RawBuffer(packet.data.length() + sizeof(packet.header)); - memcpy(buffer->buffer, &packet.header, sizeof(packet.header)); - memcpy(&buffer->buffer[sizeof(packet.header)], packet.data.data(), packet.data.length()); - - if(!this->cryptKey.empty()) - xorBuffer(&buffer->buffer[sizeof(packet.header)], packet.data.length(), this->cryptKey.data(), this->cryptKey.length()); - - { - lock_guard lock(this->lock); - TAILQ_INSERT_TAIL(&this->writeQueue, buffer, tail); - if(this->event_write) - event_add(this->event_write, nullptr); - } -} - -void LicenceRequest::handleEventRead(int fd, short, void* ptrClient) { - auto* client = static_cast(ptrClient); - - auto buffer = std::unique_ptr{malloc(1024), free}; - sockaddr_in remoteAddr{}; - socklen_t remoteAddrSize = sizeof(remoteAddr); - - auto read = recvfrom(fd, buffer.get(), 1024, MSG_NOSIGNAL | MSG_DONTWAIT, reinterpret_cast(&remoteAddr), &remoteAddrSize); - - if(read < 0){ - if(errno == EWOULDBLOCK) return; - if(client->event_read) - event_del_noblock(client->event_read); - LICENSE_FERR(client, ConnectionException, "Invalid read: " + string(strerror(errno)) + "/" + to_string(errno)); - return; - } else if(read == 0) { - if(client->event_read) - event_del_noblock(client->event_read); - LICENSE_FERR(client, ConnectionException, "IO error (" + to_string(errno) + "): " + string(strerror(errno))); - return; - } - - client->handleMessage(string((char*) buffer.get(), read)); -} - - -static int enabled = 1; -static int disabled = 0; -void LicenceRequest::beginRequest() { - lock_guard lock(this->lock); - TAILQ_INIT(&this->writeQueue); - - this->file_descriptor = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); - if(this->file_descriptor < 0) CERR("Socket setup failed"); - - signal(SIGPIPE, SIG_IGN); - - auto connect_state = ::connect(this->file_descriptor, reinterpret_cast(&this->remote_address), sizeof(this->remote_address)); - if(connect_state < 0 && errno != EINPROGRESS) CERR("connect() failed (" + string(strerror(errno)) + ")"); - - if(setsockopt(this->file_descriptor, SOL_SOCKET, SO_REUSEADDR, &enabled, sizeof(enabled)) < 0); //CERR("could not set reuse addr"); - if(setsockopt(this->file_descriptor, IPPROTO_TCP, TCP_CORK, &disabled, sizeof(disabled)) < 0); // CERR("could not set no push"); - - if(fcntl(this->file_descriptor, F_SETFD, fcntl(this->file_descriptor, F_GETFL, 0) | FD_CLOEXEC | O_NONBLOCK) < 0); // CERR("Failed to set FD_CLOEXEC and O_NONBLOCK (" + std::to_string(errno) + ")"); - - this->event_base = event_base_new(); - this->event_read = event_new(this->event_base, this->file_descriptor, EV_READ | EV_PERSIST, LicenceRequest::handleEventRead, this); - this->event_write = event_new(this->event_base, this->file_descriptor, EV_WRITE, LicenceRequest::handleEventWrite, this); - - this->state = protocol::CONNECTING; //First set connected, then we could enable the event loop - - event_dispatch = std::thread([&]() { - signal(SIGPIPE, SIG_IGN); - - { /* now we could start listening */ - lock_guard _lock(this->lock); - if(!this->event_read || !this->event_write) return; - - event_add(this->event_read, nullptr); - timeval connect_timeout{5, 0}; - event_add(this->event_write, &connect_timeout); - } - - event_base_dispatch(this->event_base); - }); -} - -void LicenceRequest::handleConnected() { - this->state = protocol::HANDSCHAKE; - - uint8_t handshakeBuffer[4]; - handshakeBuffer[0] = 0xC0; - handshakeBuffer[1] = 0xFF; - handshakeBuffer[2] = 0xEE; - handshakeBuffer[3] = LICENSE_PROT_VERSION; - this->sendPacket(protocol::packet{protocol::PACKET_CLIENT_HANDSHAKE, string((const char*) handshakeBuffer, 4)}); //Initialise packet -} - -void LicenceRequest::handleMessage(const std::string& message) { - this->buffer += message; - if(this->buffer.length() < sizeof(protocol::packet::header)) - return; - - protocol::packet packet{protocol::PACKET_DISCONNECT, ""}; - memcpy(&packet.header, this->buffer.data(), sizeof(protocol::packet::header)); - if(packet.header.length <= this->buffer.length() - sizeof(protocol::packet::header)) { - packet.data = this->buffer.substr(sizeof(protocol::packet::header), packet.header.length); - this->buffer = this->buffer.substr(sizeof(protocol::packet::header) + packet.header.length); - } else { - return; - } - - if(!this->cryptKey.empty()) { - xorBuffer((char*) packet.data.data(), packet.data.length(), this->cryptKey.data(), this->cryptKey.length()); - } - - if(packet.header.packetId == protocol::PACKET_SERVER_HANDSHAKE) { - this->handlePacketHandshake(packet.data); - } else if(packet.header.packetId == protocol::PACKET_DISCONNECT) { - this->handlePacketDisconnect(packet.data); - } else if(packet.header.packetId == protocol::PACKET_SERVER_VALIDATION_RESPONSE) { - this->handlePacketLicenseInfo(packet.data); - } else if(packet.header.packetId == protocol::PACKET_SERVER_PROPERTY_ADJUSTMENT) { - this->handlePacketInfoAdjustment(packet.data); - } else - LICENSE_FERR(this, ConnectionException, "Invalid packet id (" + to_string(packet.header.packetId) + ")"); - - if(!this->buffer.empty() && this->state != protocol::DISCONNECTING && this->state != protocol::UNCONNECTED) - this->handleMessage(""); -} - -void LicenceRequest::disconnect(const std::string& message) { - if(this->state != protocol::UNCONNECTED && this->state != protocol::DISCONNECTING) - this->sendPacket({protocol::PACKET_DISCONNECT, message}); - this->closeConnection(); - //TODO flush? -} - -void LicenceRequest::closeConnection() { - event *event_read{nullptr}, *event_write{nullptr}; - { - lock_guard slock(this->lock); - if(this->state == protocol::UNCONNECTED) return; - - if(this->event_dispatch.get_id() == this_thread::get_id()) { //We could not close in the same thread as we read/write (we're joining it later) - if(this->state == protocol::DISCONNECTING) return; - - this->state = protocol::DISCONNECTING; - this->closeThread = std::thread(&LicenceRequest::closeConnection, this); -#ifdef DEBUG_LICENSE_CLIENT - if(this->verbose) { - debugMessage(LOG_GENERAL,"Running close in a new thread"); - this->closeThread->name("License request close"); - } -#endif - return; - } - this->state = protocol::UNCONNECTED; - - std::swap(this->event_read, event_read); - std::swap(this->event_write, event_write); - } - - if(event_read) { - event_del_block(event_read); - event_free(event_read); - } - - if(event_write) { - event_del_block(event_write); - event_free(event_write); - } - - /* close before base shutdown (else epoll hangup) */ - if(this->file_descriptor > 0) { - shutdown(this->file_descriptor, SHUT_RDWR); - ::close(this->file_descriptor); - } - this->file_descriptor = 0; - - { - lock_guard lock(this->lock); - ts::buffer::RawBuffer* buffer; - while ((buffer = TAILQ_FIRST(&this->writeQueue))) { - TAILQ_REMOVE(&this->writeQueue, buffer, tail); - delete buffer; - } - } - - { - if(this->event_base) { - timeval seconds{1, 0}; - event_base_loopexit(this->event_base, &seconds); - event_base_loopexit(this->event_base, nullptr); - } - - if(this->event_dispatch.joinable()) { - this->event_dispatch.join(); - } - - if(this->event_base) { - event_base_free(this->event_base); - this->event_base = nullptr; - } - } - -#ifdef DEBUG_LICENSE_CLIENT - if(this->verbose) - debugMessage("Executing close done"); -#endif -} - -void LicenceRequest::abortRequest(const std::chrono::system_clock::time_point &timeout) { - this->closeConnection(); -} diff --git a/license/shared/LicenseRequest.h b/license/shared/LicenseRequest.h deleted file mode 100644 index f1c49a5..0000000 --- a/license/shared/LicenseRequest.h +++ /dev/null @@ -1,140 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include "License.h" - -#ifdef DEFINE_HELPER - #define LICENSE_FERR(this, class, message) \ - do { \ - this->currentException = std::make_shared(message); \ - if(this->currentFuture && this->currentFuture->state() == threads::FutureState::WORKING) this->currentFuture->executionFailed(); \ - this->disconnect("internal error"); \ - return; \ - } while(0) -#endif - -namespace license { - namespace exceptions { - class CouldNotConnectException : public LicenseException { - public: - explicit CouldNotConnectException(const std::string &message) : LicenseException(message) {} - }; - - class ConnectionException : public LicenseException { - public: - explicit ConnectionException(const std::string &message) : LicenseException(message) {} - }; - - class UnexcpectedDisconnectException : public LicenseException { - public: - explicit UnexcpectedDisconnectException(const std::string &message) : LicenseException(message) {} - }; - - class InvalidResponseException : public LicenseException { - public: - explicit InvalidResponseException(const std::string &message) : LicenseException(message) {} - }; - } - - struct ServerInfo { - std::string unique_identifier; - int64_t timestamp; - std::string uname; - std::string version; - }; - - struct LicenseRequestResponse { - std::shared_ptr license; - bool license_valid; - - int64_t speach_varianz_adjustment; - bool speach_reset; - bool properties_valid; - - std::chrono::system_clock::time_point age; - }; - - struct LicenseRequestData { - std::shared_ptr license{nullptr}; - std::shared_ptr info{nullptr}; - - std::string web_certificate_revision{}; - - int64_t speach_total = 0; - int64_t speach_dead = 0; - int64_t speach_online = 0; - int64_t speach_varianz = 0; - - int64_t client_online = 0; - int64_t web_clients_online = 0; - int64_t bots_online = 0; - int64_t queries_online = 0; - int64_t servers_online = 0; - }; - - struct WebCertificate { - std::string revision; - std::string key; - std::string certificate; - }; - - class LicenceRequest { - public: - typedef threads::Future> ResponseFuture; - LicenceRequest(const std::shared_ptr&, const sockaddr_in&); - ~LicenceRequest(); - - std::shared_ptr exception(){ return this->currentException; } - void clearExceptions(){ this->currentException = nullptr; } - ResponseFuture requestInfo(); - void abortRequest(const std::chrono::system_clock::time_point& timeout = std::chrono::system_clock::time_point()); - - void sendPacket(const protocol::packet&); - - std::function callback_update_certificate{nullptr}; - std::function callback_update_license{nullptr}; - bool verbose = true; - private: - std::shared_ptr data; - threads::Future>* currentFuture = nullptr; - std::shared_ptr response = nullptr; - std::shared_ptr currentException; - - std::recursive_mutex lock; - protocol::RequestState state = protocol::UNCONNECTED; - - sockaddr_in remote_address; - - std::string buffer{}; - - int file_descriptor{0}; - std::thread event_dispatch{}; - std::thread closeThread{}; - struct event_base* event_base{nullptr}; - struct event* event_read{nullptr}; - struct event* event_write{nullptr}; - - TAILQ_HEAD(, ts::buffer::RawBuffer) writeQueue; - - std::string cryptKey = ""; - - void beginRequest(); - void handleConnected(); - - static void handleEventRead(int, short, void*); - static void handleEventWrite(int, short, void*); - - void handleMessage(const std::string&); - void disconnect(const std::string&); - void closeConnection(); - - void handlePacketHandshake(const std::string&); - void handlePacketDisconnect(const std::string&); - void handlePacketLicenseInfo(const std::string&); - void handlePacketInfoAdjustment(const std::string&); - }; -} \ No newline at end of file diff --git a/license/shared/LicenseRequestHandler.cpp b/license/shared/LicenseRequestHandler.cpp deleted file mode 100644 index e96ff5e..0000000 --- a/license/shared/LicenseRequestHandler.cpp +++ /dev/null @@ -1,133 +0,0 @@ -#include -#include - -#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) { - /* - * We got an early disconnect. - * Test if we have a result. If so and the license is invalid then we could use that result - */ - if(this->response && !this->response->license_valid) { - try { - this->currentFuture->executionSucceed(this->response); - } catch(threads::InvalidFutureException& ex) {} - } else { - LICENSE_FERR(this, UnexcpectedDisconnectException, "Remote side closed the connection unexpectedly (" + message + ")"); - } - } -} - -void LicenceRequest::handlePacketHandshake(const std::string& data) { - if(this->state != protocol::HANDSCHAKE) 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(); - auto licenseInfo = make_shared(); - if(!response.has_license_info() && this->data->license && response.valid() && response.blacklist().state() == ts::proto::license::VALID) - 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; - - if(response.has_update_pending()) { - - } - - 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); - - infos.set_web_cert_revision(this->data->web_certificate_revision); - 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(); - - if(response.has_web_certificate() && this->callback_update_certificate) { - WebCertificate cert{}; - cert.revision = response.web_certificate().revision(); - cert.key = response.web_certificate().key(); - cert.certificate = response.web_certificate().certificate(); - this->callback_update_certificate(cert); - } - - try { - this->currentFuture->executionSucceed(this->response); - } catch(threads::InvalidFutureException& ex) {} - this->response = nullptr; - - this->disconnect("query succeeded!"); -} \ No newline at end of file diff --git a/license/shared/LicenseServerClient.h b/license/shared/include/license/client.h similarity index 99% rename from license/shared/LicenseServerClient.h rename to license/shared/include/license/client.h index 7719825..c9aa1ee 100644 --- a/license/shared/LicenseServerClient.h +++ b/license/shared/include/license/client.h @@ -5,7 +5,7 @@ #include #include -#include "./License.h" +#include "license.h" namespace license::client { class LicenseServerClient { diff --git a/license/shared/License.h b/license/shared/include/license/license.h similarity index 100% rename from license/shared/License.h rename to license/shared/include/license/license.h diff --git a/license/packets/LicenseManager.proto b/license/shared/packets/LicenseManager.proto similarity index 100% rename from license/packets/LicenseManager.proto rename to license/shared/packets/LicenseManager.proto diff --git a/license/packets/LicenseRequest.proto b/license/shared/packets/LicenseRequest.proto similarity index 100% rename from license/packets/LicenseRequest.proto rename to license/shared/packets/LicenseRequest.proto diff --git a/license/shared/LicenseServerClient.cpp b/license/shared/src/client.cpp similarity index 99% rename from license/shared/LicenseServerClient.cpp rename to license/shared/src/client.cpp index b2d31e8..52c2f8d 100644 --- a/license/shared/LicenseServerClient.cpp +++ b/license/shared/src/client.cpp @@ -7,7 +7,7 @@ #include #include #include -#include "LicenseServerClient.h" +#include "shared/include/license/client.h" #include "crypt.h" using namespace license::client; diff --git a/license/shared/crypt.h b/license/shared/src/crypt.h similarity index 100% rename from license/shared/crypt.h rename to license/shared/src/crypt.h diff --git a/license/shared/License.cpp b/license/shared/src/license.cpp similarity index 99% rename from license/shared/License.cpp rename to license/shared/src/license.cpp index d4d15f7..6c07339 100644 --- a/license/shared/License.cpp +++ b/license/shared/src/license.cpp @@ -10,7 +10,7 @@ #include #include #include "crypt.h" -#include "License.h" +#include "shared/include/license/license.h" using namespace std; using namespace std::chrono; diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 24acbf6..4f1005b 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -263,10 +263,10 @@ target_link_libraries(TeaSpeakServer libevent::core libevent::pthreads opus::static yaml-cpp - ${LIBRARY_PATH_PROTOBUF} #Require a so sqlite3 + DataPipes::rtc::shared breakpad::static protobuf::libprotobuf @@ -278,7 +278,6 @@ target_link_libraries(TeaSpeakServer mysqlclient.a jsoncpp_lib ${ed25519_LIBRARIES_STATIC} - ${DataPipes_LIBRARIES_SHARED} # Also includes glib2.0 ) if (COMPILE_WEB_CLIENT) @@ -308,16 +307,4 @@ if (NOT DISABLE_JEMALLOC) jemalloc ) add_definitions(-DHAVE_JEMALLOC) -endif () - -#Fix RPATH -#patchelf --set-rpath ./libs/ TeaSpeakServer -#patchelf --remove-rpath TeaSpeakServer - -#add_custom_command( -# TARGET TeaSpeakServer -## COMMAND bash -c "patchelf --set-rpath ./libs/ ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}TeaSpeakServer" -# COMMAND bash -c "patchelf --remove-rpath ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}TeaSpeakServer" -# WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} -# COMMENT "Cleaning RPATH" -#) +endif () \ No newline at end of file diff --git a/server/repro/generate_libraries.sh b/server/repro/generate_libraries.sh index 51219b7..d6e8668 100755 --- a/server/repro/generate_libraries.sh +++ b/server/repro/generate_libraries.sh @@ -50,8 +50,8 @@ library_path=$(realpath "${library_base}/boringssl/${build_path}/crypto/libcrypt cp "$library_path" . || { echo "failed to copy libcrypto.so"; exit 1; } # Setting up DataPipes -library_path=$(realpath "${library_base}//DataPipes/${build_path}/lib/libDataPipes.so") -cp "$library_path" . || { echo "failed to copy libDataPipes.so"; exit 1; } +library_path=$(realpath "${library_base}/DataPipes/${build_path}/lib/libDataPipes-RTC.so") +cp "$library_path" . || { echo "failed to copy libDataPipes-RTC.so"; exit 1; } _dp_path="$library_path" # Setting up Sqlite3 diff --git a/server/src/Configuration.cpp b/server/src/Configuration.cpp index 22b6b0a..750aa16 100644 --- a/server/src/Configuration.cpp +++ b/server/src/Configuration.cpp @@ -2,7 +2,7 @@ #include "Configuration.h" #include "build.h" -#include "../../license/shared/License.h" +#include "../../license/shared/include/license/license.h" #include #include #include diff --git a/server/src/Configuration.h b/server/src/Configuration.h index 91181b8..eac2250 100644 --- a/server/src/Configuration.h +++ b/server/src/Configuration.h @@ -8,7 +8,7 @@ #endif #include #include "geo/GeoLocation.h" -#include "../../license/shared/License.h" +#include "../../license/shared/include/license/license.h" namespace YAML { class Node; diff --git a/server/src/InstanceHandler.cpp b/server/src/InstanceHandler.cpp index b80f8bd..a5e862d 100644 --- a/server/src/InstanceHandler.cpp +++ b/server/src/InstanceHandler.cpp @@ -4,7 +4,6 @@ #define XREALLOC undefined_realloc #include -#include "../../license/shared/LicenseRequest.h" #include "src/weblist/WebListManager.h" #include #include "InstanceHandler.h" @@ -23,6 +22,7 @@ #include #include #include +#include #ifndef _POSIX_SOURCE #define _POSIX_SOURCE diff --git a/server/src/lincense/LicenseService.cpp b/server/src/lincense/LicenseService.cpp index 029d71e..69d67b8 100644 --- a/server/src/lincense/LicenseService.cpp +++ b/server/src/lincense/LicenseService.cpp @@ -7,11 +7,11 @@ #include #include #include -#include "../../../license/shared/LicenseServerClient.h" -#include "../../../cmake-build-debug-wsl/license/LicenseRequest.pb.h" +#include #include "src/InstanceHandler.h" +#include "LicenseRequest.pb.h" -#define DO_LOCAL_REQUEST +//#define DO_LOCAL_REQUEST using namespace ts::server::license; LicenseService::LicenseService() { @@ -184,7 +184,7 @@ void LicenseService::handle_check_fail(const std::string &error) { std::lock_guard rlock{this->request_lock}; this->abort_request(rlock, "request failed"); - if(config::license->isPremium()) { + if(config::license->isPremium() && (this->timings.last_succeeded.time_since_epoch().count() == 0 || this->timings.failed_count < 6)) { logCritical(LOG_INSTANCE, strobf("Failed to validate license:").string()); logCritical(LOG_INSTANCE, error); logCritical(LOG_INSTANCE, strobf("Stopping server!").string()); diff --git a/server/src/manager/ConversationManager.cpp b/server/src/manager/ConversationManager.cpp index ff93416..c404f46 100644 --- a/server/src/manager/ConversationManager.cpp +++ b/server/src/manager/ConversationManager.cpp @@ -193,8 +193,8 @@ namespace fs = std::experimental::filesystem; 0x555555c41ed7 ret #endif __attribute__((optimize("-O3"), always_inline)) void apply_crypt(void* source, void* target, size_t length, uint64_t base_key) { - uint64_t crypt_key = base_key; - size_t length_left = length; + uint64_t crypt_key{base_key}; + size_t length_left{length}; auto source_ptr = (uint8_t*) source; auto dest_ptr = (uint8_t*) target; diff --git a/server/src/weblist/WebListManager.h b/server/src/weblist/WebListManager.h index 37faa99..e764ff9 100644 --- a/server/src/weblist/WebListManager.h +++ b/server/src/weblist/WebListManager.h @@ -5,6 +5,7 @@ #include #include #include +#include struct event_base; namespace ts {