//-----------------------------------------------------------------------------
// Uploads a screenshot to a particular listener
//-----------------------------------------------------------------------------
void CServerRemoteAccess::UploadScreenshot( const char *pFileName )
{
#ifndef SWDS
	if ( m_nScreenshotListener < 0 )
		return;

	CUtlBuffer buf( 128 * 1024, 0 );
	if ( g_pFullFileSystem->ReadFile( pFileName, "MOD", buf ) )
	{
		HZIP hZip = CreateZipZ( 0, 1024 * 1024, ZIP_MEMORY );
		void *pMem;
		unsigned long nLen;
		ZipAdd( hZip, "screenshot.jpg", buf.Base(), buf.TellMaxPut(), ZIP_MEMORY );
		ZipGetMemory( hZip, &pMem, &nLen );
		SendResponseToClient( m_nScreenshotListener, SERVERDATA_SCREENSHOT_RESPONSE, pMem, nLen );
		CloseZip( hZip );
	}
	else
	{
		LogCommand( m_nScreenshotListener, "Failed to read screenshot!\n" );
		RespondString( m_nScreenshotListener, 0, "Failed to read screenshot!\n" );
	}

	m_nScreenshotListener = -1;
#endif
}
//-----------------------------------------------------------------------------
// Purpose: handles a request
//-----------------------------------------------------------------------------
void CServerRemoteAccess::WriteDataRequest( CRConServer *pNetworkListener, ra_listener_id listener, const void *buffer, int bufferSize)
{
	m_iBytesReceived += bufferSize;
	// ConMsg("RemoteAccess: bytes received: %d\n", m_iBytesReceived);

	if ( bufferSize < 2*sizeof(int) ) // check that the buffer contains at least the id and type
	{
		return;
	}

	CUtlBuffer cmd(buffer, bufferSize, CUtlBuffer::READ_ONLY);
	bool invalidRequest = false;

	while ( invalidRequest == false && (int)cmd.TellGet() < (int)(cmd.Size() - 2 * sizeof(int) ) ) // while there is commands to read
	{
		// parse out the buffer
		int requestID = cmd.GetInt();
		pNetworkListener->SetRequestID( listener, requestID ); // tell the rcon server the ID so it can reflect it when the console redirect flushes
		int requestType = cmd.GetInt();

		switch (requestType)
		{
			case SERVERDATA_REQUESTVALUE:
				{
					if ( IsAuthenticated(listener) )
					{
						char variable[256];
						if ( !GetStringHelper( cmd, variable, sizeof(variable) ) )
						{
							invalidRequest = true;
							break;
						}
						RequestValue( listener, requestID, variable);
						if ( !GetStringHelper( cmd, variable, sizeof(variable) ) )
						{
							invalidRequest = true;
							break;
						}
					}
					else
					{
						char variable[256];
						if ( !GetStringHelper( cmd, variable, sizeof(variable) ) )
						{
							invalidRequest = true;
							break;
						}
						if ( !GetStringHelper( cmd, variable, sizeof(variable) ) )
						{
							invalidRequest = true;
							break;
						}
					}
				}
				break;

			case SERVERDATA_SETVALUE:
				{
					if ( IsAuthenticated(listener) )
					{
						char variable[256];
						char value[256];
						if ( !GetStringHelper( cmd, variable, sizeof(variable) ) )
						{
							invalidRequest = true;
							break;
						}
						if ( !GetStringHelper( cmd, value, sizeof(value) ) )
						{
							invalidRequest = true;
							break;
						}
						SetValue(variable, value);
					}
					else
					{
						char command[512];
						if ( !GetStringHelper( cmd, command, sizeof(command) ) )
						{
							invalidRequest = true;
							break;
						}
						if ( !GetStringHelper( cmd, command, sizeof(command) ) )
						{
							invalidRequest = true;
							break;
						}
					}
				}
				break;

			case SERVERDATA_EXECCOMMAND:
				{
					if ( IsAuthenticated(listener) )
					{
						char command[512];
						if ( !GetStringHelper( cmd, command, sizeof(command) ) )
						{
							invalidRequest = true;
							break;
						}
						
						ExecCommand(command);
						
						if ( listener != m_AdminUIID )
						{
							LogCommand( listener, va( "command \"%s\"", command) );
						}
						if ( !GetStringHelper( cmd, command, sizeof(command) ) )
						{
							invalidRequest = true;
							break;
						}
					}
					else
					{
						char command[512];
						if ( !GetStringHelper( cmd, command, sizeof(command) ) )
						{
							invalidRequest = true;
							break;
						}
						if ( !GetStringHelper( cmd, command, sizeof(command) ) )
						{
							invalidRequest = true;
							break;
						}
						LogCommand( listener, "Bad Password" );
					}
				}
				break;

			case SERVERDATA_AUTH:
				{
					char password[512];
					if ( !GetStringHelper( cmd, password, sizeof(password) ) )
					{
						invalidRequest = true;
						break;
					}
					CheckPassword( pNetworkListener, listener, requestID, password );
					if ( !GetStringHelper( cmd, password, sizeof(password) ) )
					{
						invalidRequest = true;
						break;
					}

					if ( m_ListenerIDs[ listener ].authenticated )
					{
						// if the second string has a non-zero value, it is a userid.
						int userID = atoi( password );
						const ConCommandBase *var = g_pCVar->GetCommands();
						while ( var )
						{
							if ( var->IsCommand() )
							{
								if ( Q_stricmp( var->GetName(), "mp_disable_autokick" ) == 0 )
								{
									Cbuf_AddText( va( "mp_disable_autokick %d\n", userID ) );
									Cbuf_Execute();
									break;
								}
							}
							var = var->GetNext();
						}
					}
				}
				break;

			case SERVERDATA_TAKE_SCREENSHOT:
#ifndef SWDS
				m_nScreenshotListener = listener;
				CL_TakeJpeg( );
#endif
				break;

			case SERVERDATA_SEND_CONSOLE_LOG:
				{
#ifndef SWDS
					const char *pLogFile = GetConsoleLogFilename();
					CUtlBuffer buf( 1024, 0, CUtlBuffer::TEXT_BUFFER );
					if ( g_pFullFileSystem->ReadFile( pLogFile, "GAME", buf ) )
					{
						HZIP hZip = CreateZipZ( 0, 1024 * 1024, ZIP_MEMORY );
						void *pMem;
						unsigned long nLen;
						ZipAdd( hZip, "console.log", buf.Base(), buf.TellMaxPut(), ZIP_MEMORY );
						ZipGetMemory( hZip, &pMem, &nLen );
						SendResponseToClient( listener, SERVERDATA_CONSOLE_LOG_RESPONSE, pMem, nLen );
						CloseZip( hZip );
					}
					else
					{
						LogCommand( listener, "Failed to read console log!\n" );
						RespondString( listener, requestID, "Failed to read console log!\n" );
					}
#endif
				}
				break;

#ifdef VPROF_ENABLED
			case SERVERDATA_VPROF:
				{
					char password[25];
					if ( !GetStringHelper( cmd, password, sizeof(password) ) )
					{
						invalidRequest = true;
						break;
					}
					if ( !GetStringHelper( cmd, password, sizeof(password) ) )
					{
						invalidRequest = true;
						break;
					}
					if ( IsAuthenticated(listener) )
					{
						RegisterVProfDataListener( listener );
						LogCommand( listener, "Remote VProf started!\n" );
						RespondString( listener, requestID, "Remote VProf started!\n" );
					}
				}
				break;
		
			case SERVERDATA_REMOVE_VPROF:
				{
					char password[25];
					if ( !GetStringHelper( cmd, password, sizeof(password) ) )
					{
						invalidRequest = true;
						break;
					}
					if ( !GetStringHelper( cmd, password, sizeof(password) ) )
					{
						invalidRequest = true;
						break;
					}
					if ( IsAuthenticated(listener) )
					{
						RemoveVProfDataListener( listener );
						LogCommand( listener, "Remote VProf finished!\n" );
						RespondString( listener, requestID, "Remote VProf finished!\n" );
					}
				}
				break;
#endif

			default:
				Assert(!("Unknown requestType in CServerRemoteAccess::WriteDataRequest()"));
				cmd.Purge();
				invalidRequest = true;
				break;
		};
	}
}
示例#3
0
void main()
{ CreateFiles();
  HZIP hz; DWORD writ;

  // EXAMPLE 1 - create a zipfile from existing files
  hz = CreateZip(_T("\\simple1.zip"),0);
  ZipAdd(hz,_T("znsimple.bmp"), _T("\\simple.bmp"));
  ZipAdd(hz,_T("znsimple.txt"), _T("\\simple.txt"));
  CloseZip(hz);
  _tprintf(_T("Created '\\simple1.zip'\n"));


  // EXAMPLE 2 - unzip it with the names suggested in the zip
  hz = OpenZip(_T("\\simple1.zip"),0);
  SetUnzipBaseDir(hz,_T("\\"));
  ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index;
  for (int zi=0; zi<numitems; zi++)
  { GetZipItem(hz,zi,&ze);
    UnzipItem(hz,zi,ze.name);
  }
  CloseZip(hz);
  _tprintf(_T("Unzipped 'znsimple.bmp' and 'znsimple.txt' from 'simple1.zip'\n"));



  // EXAMPLE 3 - create an auto-allocated pagefile-based zip file from various sources
  // the second argument says how much address space to reserve for it. We can
  // afford to be generous: no address-space is allocated unless it's actually needed.
  hz = CreateZip(0,100000,"password");
  // adding a conventional file...
  ZipAdd(hz,_T("znsimple.txt"),  _T("\\simple.txt"));
  // adding something from memory...
  char buf[1000]; for (int zj=0; zj<1000; zj++) buf[zj]=(char)(zj&0x7F);
  ZipAdd(hz,_T("simple.dat"),  buf,1000);
  // adding something from a pipe...
#ifndef UNDER_CE
  HANDLE hread,hwrite; CreatePipe(&hread,&hwrite,NULL,0);
  DWORD WINAPI Ex3ThreadFunc(void *dat);
  HANDLE hthread = CreateThread(0,0,Ex3ThreadFunc,(void*)hwrite,0,0);
  ZipAddHandle(hz,_T("simple3.dat"),  hread,1000);  // the '1000' is optional, but it makes for nicer zip files if sizes are known in advance.
  WaitForSingleObject(hthread,INFINITE);
  CloseHandle(hthread); CloseHandle(hread); // the thread will close hwrite
#endif
  //
  // and now that the zip is created in pagefile, let's do something with it:
  void *zbuf; unsigned long zlen; ZipGetMemory(hz,&zbuf,&zlen);
  HANDLE hfz = CreateFile(_T("\\simple3 - pwd is 'password'.zip"),GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
  WriteFile(hfz,zbuf,zlen,&writ,NULL);
  CloseHandle(hfz);
  CloseZip(hz);
  _tprintf(_T("Created 'simple3.zip' via pagefile\n"));


  // EXAMPLE 4 - unzip directly from resource into a file
  // resource RT_RCDATA/#1 happens to be a zip file...
  HINSTANCE hInstance=GetModuleHandle(0);
  HRSRC hrsrc = FindResource(hInstance,MAKEINTRESOURCE(1),RT_RCDATA);
  HANDLE hglob = LoadResource(hInstance,hrsrc);
  void *zipbuf=LockResource(hglob);
  unsigned int ziplen=SizeofResource(hInstance,hrsrc);
  hz = OpenZip(zipbuf, ziplen, 0);  SetUnzipBaseDir(hz,_T("\\"));
  int i; FindZipItem(hz,_T("simple.jpg"),true,&i,&ze);
  //   - unzip to a file -
  UnzipItem(hz,i,ze.name);
  //   - unzip to a membuffer -
  char *ibuf = new char[ze.unc_size];
  UnzipItem(hz,i, ibuf, ze.unc_size);
  delete[] ibuf;
  //   - unzip to a fixed membuff, bit by bit -
  char fbuf[1024]; ZRESULT zr=ZR_MORE; unsigned long totsize=0;
  while (zr==ZR_MORE)
  { zr = UnzipItem(hz,i, fbuf,1024);
    unsigned long bufsize=1024; if (zr==ZR_OK) bufsize=ze.unc_size-totsize;
    // now do something with this chunk which we just unzipped...
    totsize+=bufsize;
  }
  //   - finished -
  CloseZip(hz);
  // note: no need to free resources obtained through Find/Load/LockResource
  _tprintf(_T("Unzipped 'simple.jpg' from resource zipfile\n"));


  
  DeleteFile(_T("\\simple.txt"));
  DeleteFile(_T("\\simple.bmp"));
  _tprintf(_T("Deleted 'simple.txt' and 'simple.bmp'\n"));
}
示例#4
0
ZIP_API ZRESULT ZipGetData(HZIP zip_handle, void **buffer, unsigned long* length){return ZipGetMemory(zip_handle, buffer, length);}