2019-07-17 13:37:18 -04:00
# 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>
2020-03-02 07:33:57 -05:00
# include <shared/include/license/license.h>
2019-07-17 13:37:18 -04:00
# include <arpa/inet.h>
2020-02-28 05:24:07 -05:00
# include "DatabaseHandler.h"
2019-07-17 13:37:18 -04:00
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) */
2019-07-17 13:37:18 -04:00
event * writeEvent = nullptr ;
2020-02-06 07:49:39 -05:00
std : : mutex write_queue_lock { } ;
TAILQ_HEAD ( , ts : : buffer : : RawBuffer ) write_queue ;
2019-07-17 13:37:18 -04:00
} network ;
struct {
2020-02-06 07:49:39 -05:00
std : : mutex state_lock { } ;
2019-07-17 13:37:18 -04:00
protocol : : RequestState state = protocol : : UNCONNECTED ;
2020-02-06 07:49:39 -05:00
2019-07-17 13:37:18 -04: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 */
2019-07-17 13:37:18 -04:00
} 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 } ;
2019-07-17 13:37:18 -04:00
std : : string unique_identifier ;
2019-08-25 16:16:42 -04:00
bool invalid_license = false ;
2019-07-17 13:37:18 -04:00
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 ;
} ;
2019-07-17 13:37:18 -04:00
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 */ ) ;
2019-07-17 13:37:18 -04:00
~ LicenseServer ( ) ;
2020-02-06 07:49:39 -05:00
bool start ( ) ;
2019-07-17 13:37:18 -04:00
bool isRunning ( ) { return this - > running ; }
2020-02-06 07:49:39 -05:00
void stop ( ) ;
2019-07-17 13:37:18 -04:00
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-07-17 13:37:18 -04:00
}
2019-11-22 14:51:00 -05:00
std : : shared_ptr < WebCertificate > web_certificate { nullptr } ;
2019-07-17 13:37:18 -04:00
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 ;
2019-07-17 13:37:18 -04:00
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 ;
2019-07-17 13:37:18 -04:00
2020-02-06 07:49:39 -05:00
bool running = false ; /* also secured by client_lock */
2019-07-17 13:37:18 -04:00
2020-02-06 07:49:39 -05:00
sockaddr_in localAddr { } ;
int server_socket = 0 ;
2019-07-17 13:37:18 -04:00
event_base * evBase = nullptr ;
2020-02-06 07:49:39 -05:00
event * event_accept = nullptr ;
event * event_cleanup = nullptr ;
2019-07-17 13:37:18 -04:00
2020-02-06 07:49:39 -05:00
std : : thread event_base_dispatch { } ;
2019-07-17 13:37:18 -04:00
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 ) ;
2019-07-17 13:37:18 -04:00
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 ) ;
} ;
}