Teaspeak-Server/license/server/LicenseServer.h

131 lines
4.6 KiB
C
Raw Normal View History

#pragma once
#include <event.h>
#include <protocol/buffers.h>
#include <deque>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <ThreadPool/Thread.h>
#include <shared/License.h>
#include <arpa/inet.h>
2020-02-28 05:24:07 -05:00
#include "DatabaseHandler.h"
namespace license {
namespace web {
class WebStatistics;
}
namespace stats {
class StatisticManager;
}
class UserManager;
enum ClientType {
SERVER,
MANAGER
};
struct ConnectedClient {
public:
struct {
sockaddr_in remoteAddr;
int fileDescriptor = 0;
2020-02-06 07:49:39 -05:00
event* readEvent = nullptr; /* protected via state_lock (check state and the use these variables) */
event* writeEvent = nullptr;
2020-02-06 07:49:39 -05:00
std::mutex write_queue_lock{};
TAILQ_HEAD(, ts::buffer::RawBuffer) write_queue;
} network;
struct {
2020-02-06 07:49:39 -05:00
std::mutex state_lock{};
protocol::RequestState state = protocol::UNCONNECTED;
2020-02-06 07:49:39 -05:00
std::chrono::system_clock::time_point last_read;
std::string cryptKey = "";
2020-02-28 05:24:07 -05:00
int version{2}; /* current version is 3 */
} protocol;
ClientType type = ClientType::SERVER;
std::string username;
std::string key;
2020-02-28 05:24:07 -05:00
uint64_t key_pending_upgrade{0};
std::string unique_identifier;
2019-08-25 16:16:42 -04:00
bool invalid_license = false;
void init();
void uninit();
void sendPacket(const protocol::packet&);
inline std::string address() { return inet_ntoa(network.remoteAddr.sin_addr); }
};
2019-11-22 14:51:00 -05:00
struct WebCertificate {
std::string revision;
std::string key;
std::string certificate;
};
class LicenseServer {
public:
2020-02-28 05:24:07 -05:00
explicit LicenseServer(const sockaddr_in&, std::shared_ptr<server::database::DatabaseHandler> , std::shared_ptr<stats::StatisticManager> /* stats */, std::shared_ptr<web::WebStatistics> /* web stats */, std::shared_ptr<UserManager> /* user manager */);
~LicenseServer();
2020-02-06 07:49:39 -05:00
bool start();
bool isRunning(){ return this->running; }
2020-02-06 07:49:39 -05:00
void stop();
std::shared_ptr<ConnectedClient> findClient(int fileDescriptor);
void disconnectClient(const std::shared_ptr<ConnectedClient>&, const std::string& reason);
void closeConnection(const std::shared_ptr<ConnectedClient>&, bool blocking = false);
std::deque<std::shared_ptr<ConnectedClient>> getClients() {
2020-02-06 07:49:39 -05:00
std::lock_guard lock(this->client_lock);
return clients;
}
2019-11-22 14:51:00 -05:00
std::shared_ptr<WebCertificate> web_certificate{nullptr};
private:
void unregisterClient(const std::shared_ptr<ConnectedClient>&);
void cleanup_clients();
std::shared_ptr<web::WebStatistics> web_statistics;
std::shared_ptr<stats::StatisticManager> statistics;
2020-02-28 05:24:07 -05:00
std::shared_ptr<server::database::DatabaseHandler> manager;
std::shared_ptr<UserManager> user_manager;
2020-02-06 07:49:39 -05:00
std::mutex client_lock;
std::deque<std::shared_ptr<ConnectedClient>> clients;
2020-02-06 07:49:39 -05:00
bool running = false; /* also secured by client_lock */
2020-02-06 07:49:39 -05:00
sockaddr_in localAddr{};
int server_socket = 0;
event_base* evBase = nullptr;
2020-02-06 07:49:39 -05:00
event* event_accept = nullptr;
event* event_cleanup = nullptr;
2020-02-06 07:49:39 -05:00
std::thread event_base_dispatch{};
static void handleEventCleanup(int, short, void*);
static void handleEventAccept(int, short, void*);
static void handleEventRead(int, short, void*);
static void handleEventWrite(int, short, void*);
void handleMessage(std::shared_ptr<ConnectedClient>&, const std::string&);
bool handleDisconnect(std::shared_ptr<ConnectedClient>&, protocol::packet&, std::string& error);
bool handleHandshake(std::shared_ptr<ConnectedClient>&, protocol::packet&, std::string& error);
bool handleServerValidation(std::shared_ptr<ConnectedClient> &, protocol::packet &, std::string &error);
2020-02-28 05:24:07 -05:00
bool handlePacketLicenseUpgrade(std::shared_ptr<ConnectedClient> &client, protocol::packet &packet, std::string &error);
bool handlePacketPropertyUpdate(std::shared_ptr<ConnectedClient> &, protocol::packet &, std::string &error);
bool handlePacketAuth(std::shared_ptr<ConnectedClient> &, protocol::packet &, std::string &error);
bool handlePacketLicenseCreate(std::shared_ptr<ConnectedClient> &, protocol::packet &, std::string &error);
bool handlePacketLicenseList(std::shared_ptr<ConnectedClient> &, protocol::packet &, std::string &error);
bool handlePacketLicenseDelete(std::shared_ptr<ConnectedClient> &, protocol::packet &, std::string &error);
};
}