Teaspeak-Server/license/shared/License.h

186 lines
5.2 KiB
C
Raw Normal View History

#pragma once
#include <string>
#include <chrono>
#include <memory>
#include <utility>
#include <ThreadPool/Mutex.h>
#include <ThreadPool/Future.h>
#include <Variable.h>
#define LICENSE_VERSION 1
#define LICENSE_PROT_VERSION 2
#define MAGIC_NUMER 0xBADC0DED
namespace license {
namespace exceptions {
class LicenseException : public std::exception {
public:
LicenseException() = delete;
LicenseException(std::string message) : errorMessage(std::move(message)) {}
LicenseException(const LicenseException& ref) : errorMessage(ref.errorMessage) {}
LicenseException(LicenseException&& ref) : errorMessage(std::move(ref.errorMessage)) {}
const char* what() const noexcept override;
private:
std::string errorMessage;
};
}
struct LicenseHeader {
uint16_t version;
const char cryptKey[64]; //The dummy key for data de/encryption
} __attribute__ ((__packed__));
/*
namespace v2 {
namespace data {
struct ChainHead {
uint32_t chain_version;
uint32_t chain_magic;
uint8_t sign[32];
} __attribute__ ((__packed__));
struct ChainEntryHead {
uint8_t entry_type; //sign bit = contains private
int64_t entry_begin;
int32_t entry_length;
uint8_t key[32];
} __attribute__ ((__packed__));
}
struct LicenseIssuer {
std::string name;
std::string email;
};
struct LicenseChainEntry {
data::ChainEntryHead head;
struct {
bool contains;
uint8_t key[32];
} prv_key;
LicenseIssuer issuer;
};
struct LicenseChain {
public:
data::ChainHead head;
std::deque<LicenseChainEntry> entries;
private:
};
}
*/
enum LicenseType : uint8_t {
INVALID,
DEMO,
PREMIUM,
HOSTER,
PRIVATE,
};
inline bool isPremiumLicense(LicenseType type){ return type == HOSTER || type == PREMIUM || type == PRIVATE; }
#define LT_NAMES {"Invalid", "Demo", "Premium", "Hoster", "Private"};
extern std::string LicenseTypeNames[5];
struct License {
LicenseHeader header;
//Crypted part
struct {
LicenseType type;
int64_t endTimestamp;
const char licenceKey[64]; //The actual key
const char licenceOwner[64];
} __attribute__ ((__packed__)) data;
inline std::string key() { return std::string(data.licenceKey, 64); }
inline std::chrono::time_point<std::chrono::system_clock> end() { return std::chrono::system_clock::time_point() + std::chrono::milliseconds(this->data.endTimestamp); }
inline std::string owner() { return std::string(this->data.licenceOwner); }
inline bool isValid() { return data.endTimestamp == 0 || std::chrono::system_clock::now() < this->end(); }
inline bool isPremium(){ return isPremiumLicense(data.type); }
} __attribute__ ((__packed__));
struct LicenseInfo {
LicenseType type;
std::string username;
std::string first_name;
std::string last_name;
std::string email;
std::chrono::system_clock::time_point start;
std::chrono::system_clock::time_point end;
std::chrono::system_clock::time_point creation;
bool deleted = false;
inline bool isValid() { return (end.time_since_epoch().count() == 0 || std::chrono::system_clock::now() < this->end); }
};
extern std::shared_ptr<License> readLocalLicence(const std::string &, std::string &);
extern std::string exportLocalLicense(const std::shared_ptr<License>&);
extern std::string createLocalLicence(LicenseType type, std::chrono::time_point<std::chrono::system_clock> until, std::string licenseOwner);
namespace protocol {
enum RequestState {
UNCONNECTED,
CONNECTING,
HANDSCAKE,
SERVER_VALIDATION,
LICENSE_INFO,
PROPERTY_ADJUSTMENT,
MANAGER_AUTHORIZATION,
MANAGER_CONNECTED,
DISCONNECTING
};
enum PacketType : uint8_t {
PACKET_CLIENT_HANDSHAKE,
PACKET_SERVER_HANDSHAKE,
PACKET_CLIENT_SERVER_VALIDATION,
PACKET_SERVER_VALIDATION_RESPONSE,
PACKET_CLIENT_PROPERTY_ADJUSTMENT,
PACKET_SERVER_PROPERTY_ADJUSTMENT,
PACKET_CLIENT_AUTH_REQUEST,
PACKET_SERVER_AUTH_RESPONSE,
PACKET_CLIENT_LICENSE_CREATE_REQUEST,
PACKET_SERVER_LICENSE_CREATE_RESPONSE,
PACKET_CLIENT_LIST_REQUEST,
PACKET_SERVER_LIST_RESPONSE,
PACKET_CLIENT_DELETE_REQUEST,
PACKET_CLIENT_DELETE_RESPONSE,
PACKET_PING = 0xF0,
PACKET_DISCONNECT = 0xFF
};
struct packet {
struct {
PacketType packetId;
mutable uint16_t length;
} header;
std::string data;
inline void prepare() const {
this->header.length = (uint16_t) data.length();
}
packet(PacketType packetId, std::string data) : data(std::move(data)), header({packetId, 0}) {}
#ifdef GOOGLE_PROTOBUF_MESSAGE_H__
packet(PacketType packetId, const ::google::protobuf::Message&);
#endif
packet(PacketType packetId, nullptr_t);
};
}
}
//DEFINE_TRANSFORMS(license::LicenseType, uint8_t);