RemoteConsoleCore.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/PlatformDef.h>
  9. #include <AzCore/Socket/AzSocket.h>
  10. #include <AzFramework/StringFunc/StringFunc.h>
  11. #include <platform.h>
  12. #include <IConsole.h>
  13. #include <ILevelSystem.h>
  14. #include <ISystem.h>
  15. #include "RemoteConsoleCore.h"
  16. #include <RemoteConsole_Traits_Platform.h>
  17. const int defaultRemoteConsolePort = 4600; // externed in the header to expose publicly
  18. namespace
  19. {
  20. static const int kDefaultBufferSize = 32768;
  21. static const AZ::u16 kMaxBindPorts = 8; // max range of ports to bind to for remote console
  22. static const char* kServerThreadName = "RemoteConsoleServer";
  23. static const char* kClientThreadName = "RemoteConsoleClient";
  24. }
  25. bool RCON_IsRemoteAllowedToConnect(const AZ::AzSock::AzSocketAddress& connectee)
  26. {
  27. if ((!gEnv) || (!gEnv->pConsole))
  28. {
  29. CryLog("Cannot allow incoming connection for remote console, because we do not yet have a console or an environment.");
  30. return false;
  31. }
  32. ICVar* remoteConsoleAllowedHostList = gEnv->pConsole->GetCVar("log_RemoteConsoleAllowedAddresses");
  33. if (!remoteConsoleAllowedHostList)
  34. {
  35. CryLog("Cannot allow incoming connection for remote console, because there is no registered log_RemoteConsoleAllowedAddresses console variable.");
  36. return false;
  37. }
  38. const char* value = remoteConsoleAllowedHostList->GetString();
  39. // the default or empty string indicates localhost.
  40. if (!value)
  41. {
  42. value = "";
  43. }
  44. AZStd::vector<AZStd::string> addresses;
  45. AzFramework::StringFunc::Tokenize(value, addresses, ',');
  46. if (addresses.empty())
  47. {
  48. addresses.push_back("127.0.0.1");
  49. }
  50. AZ::AzSock::AzSocketAddress testAddress;
  51. for (const AZStd::string& address : addresses)
  52. {
  53. // test the approved addresses with connectee's port to see if we have a match
  54. testAddress.SetAddress(address.c_str(), connectee.GetAddrPort());
  55. if (testAddress == connectee)
  56. {
  57. // its an exact match.
  58. if (gEnv->pLog)
  59. {
  60. gEnv->pLog->LogToConsole("Remote console connected from ip %s (matches: %s)", connectee.GetAddress().c_str(), address.c_str());
  61. }
  62. return true;
  63. }
  64. }
  65. if (gEnv->pLog)
  66. {
  67. gEnv->pLog->LogToConsole("An attempt to connect to remote console from ip %s failed because it is not on the ApprovedList.", connectee.GetAddress().c_str());
  68. gEnv->pLog->LogToConsole("Add to the ApprovedList using the CVAR log_RemoteConsoleAllowedAddresses (comma separated IPs or hostnames)");
  69. gEnv->pLog->LogToConsole("Example: log_RemoteConsoleAllowedAddresses localhost,joescomputer");
  70. }
  71. return false; // return false by default, so you MUST pass an above check for you to be allowed in.
  72. }
  73. /////////////////////////////////////////////////////////////////////////////////////////////
  74. /////////////////////////////////////////////////////////////////////////////////////////////
  75. /////////////////////////////////////////////////////////////////////////////////////////////
  76. void SRemoteThreadedObject::Start(const char* name)
  77. {
  78. AZStd::thread_desc desc;
  79. desc.m_name = name;
  80. auto function = AZStd::bind(&SRemoteThreadedObject::ThreadFunction, this);
  81. m_thread = AZStd::thread(desc, function);
  82. }
  83. void SRemoteThreadedObject::WaitForThread()
  84. {
  85. if (m_thread.joinable())
  86. {
  87. m_thread.join();
  88. }
  89. }
  90. void SRemoteThreadedObject::ThreadFunction()
  91. {
  92. Run();
  93. Terminate();
  94. }
  95. /////////////////////////////////////////////////////////////////////////////////////////////
  96. /////////////////////////////////////////////////////////////////////////////////////////////
  97. /////////////////////////////////////////////////////////////////////////////////////////////
  98. void SRemoteServer::StartServer()
  99. {
  100. StopServer();
  101. m_bAcceptClients = true;
  102. Start(kServerThreadName);
  103. }
  104. /////////////////////////////////////////////////////////////////////////////////////////////
  105. void SRemoteServer::StopServer()
  106. {
  107. m_bAcceptClients = false;
  108. AZ::AzSock::CloseSocket(m_socket);
  109. m_socket = SOCKET_ERROR;
  110. AZStd::unique_lock<AZStd::recursive_mutex> lock(m_mutex);
  111. for (TClients::iterator it = m_clients.begin(); it != m_clients.end(); ++it)
  112. {
  113. it->pClient->StopClient();
  114. }
  115. m_stopCondition.wait(lock, [this] { return m_clients.empty(); });
  116. }
  117. /////////////////////////////////////////////////////////////////////////////////////////////
  118. void SRemoteServer::ClientDone(SRemoteClient* pClient)
  119. {
  120. AZStd::scoped_lock lock(m_mutex);
  121. for (TClients::iterator it = m_clients.begin(); it != m_clients.end(); ++it)
  122. {
  123. if (it->pClient == pClient)
  124. {
  125. delete it->pClient;
  126. delete it->pEvents;
  127. m_clients.erase(it);
  128. break;
  129. }
  130. }
  131. if (m_clients.empty())
  132. {
  133. m_stopCondition.notify_all();
  134. }
  135. }
  136. /////////////////////////////////////////////////////////////////////////////////////////////
  137. void SRemoteServer::Terminate()
  138. {
  139. }
  140. /////////////////////////////////////////////////////////////////////////////////////////////
  141. void SRemoteServer::Run()
  142. {
  143. AZ_TRAIT_REMOTECONSOLE_SET_THREAD_AFFINITY
  144. AZSOCKET sClient;
  145. AZ::AzSock::AzSocketAddress local;
  146. int result = 0;
  147. if (AZ::AzSock::SocketErrorOccured(AZ::AzSock::Startup()))
  148. {
  149. gEnv->pLog->LogError("[RemoteKeyboard] Failed to load Winsock!\n");
  150. return;
  151. }
  152. m_socket = AZ::AzSock::Socket();
  153. if (!AZ::AzSock::IsAzSocketValid(m_socket))
  154. {
  155. CryLog("Remote console FAILED. socket() => SOCKET_ERROR");
  156. return;
  157. }
  158. // this CVAR is optional.
  159. AZ::u16 remotePort = defaultRemoteConsolePort;
  160. ICVar* remoteConsolePort = gEnv->pConsole->GetCVar("log_RemoteConsolePort");
  161. if (remoteConsolePort)
  162. {
  163. remotePort = static_cast<AZ::u16>(remoteConsolePort->GetIVal());
  164. }
  165. //
  166. // There may be multiple processes running, and each process will require a unique port for remote console to work.
  167. // So we need to be able to bind to ascending ports so that automated tests can connect to each process. QA's Automated
  168. // tools depend on this behavior for successful testing to occur.
  169. // Please check with ly-networking, ly-systems or ly-qa before changing this.
  170. // Thanks.
  171. //
  172. bool bindOk = false;
  173. for (AZ::u16 port=remotePort; port < (remotePort + kMaxBindPorts); port++)
  174. {
  175. local.SetAddrPort(port);
  176. result = AZ::AzSock::Bind(m_socket, local);
  177. if (!AZ::AzSock::SocketErrorOccured(result))
  178. {
  179. bindOk = true;
  180. break;
  181. }
  182. }
  183. if ( !bindOk )
  184. {
  185. CryLog("Failed to bind Remote Console to ports %hu to %hu", remotePort, static_cast<AZ::u16>(remotePort + kMaxBindPorts - 1) );
  186. return;
  187. }
  188. AZ::AzSock::Listen(m_socket, 8);
  189. AZ::AzSock::AzSocketAddress sockName;
  190. result = AZ::AzSock::GetSockName(m_socket, sockName);
  191. if (!AZ::AzSock::SocketErrorOccured(result))
  192. {
  193. CryLog("Remote console listening on: %d\n", sockName.GetAddrPort());
  194. }
  195. else
  196. {
  197. CryLog("Remote console FAILED to listen on: %d\n", sockName.GetAddrPort());
  198. }
  199. while (m_bAcceptClients)
  200. {
  201. AZTIMEVAL timeout { 1, 0 };
  202. if (!AZ::AzSock::IsRecvPending(m_socket, &timeout))
  203. {
  204. continue;
  205. }
  206. AZ::AzSock::AzSocketAddress clientAddress;
  207. sClient = AZ::AzSock::Accept(m_socket, clientAddress);
  208. if (!m_bAcceptClients || !AZ::AzSock::IsAzSocketValid(sClient))
  209. {
  210. break;
  211. }
  212. if (!RCON_IsRemoteAllowedToConnect(clientAddress))
  213. {
  214. AZ::AzSock::CloseSocket(sClient);
  215. continue;
  216. }
  217. AZStd::scoped_lock lock(m_mutex);
  218. SRemoteClient* pClient = new SRemoteClient(this);
  219. m_clients.push_back(SRemoteClientInfo(pClient));
  220. pClient->StartClient(sClient);
  221. }
  222. AZ::AzSock::CloseSocket(m_socket);
  223. CryLog("Remote console terminating.\n");
  224. //AZ::AzSock::Shutdown();
  225. }
  226. /////////////////////////////////////////////////////////////////////////////////////////////
  227. void SRemoteServer::AddEvent(IRemoteEvent* pEvent)
  228. {
  229. AZStd::scoped_lock lock(m_mutex);
  230. for (TClients::iterator it = m_clients.begin(); it != m_clients.end(); ++it)
  231. {
  232. it->pEvents->push_back(pEvent->Clone());
  233. }
  234. delete pEvent;
  235. }
  236. /////////////////////////////////////////////////////////////////////////////////////////////
  237. void SRemoteServer::GetEvents(TEventBuffer& buffer)
  238. {
  239. AZStd::scoped_lock lock(m_mutex);
  240. buffer = m_eventBuffer;
  241. m_eventBuffer.clear();
  242. }
  243. /////////////////////////////////////////////////////////////////////////////////////////////
  244. bool SRemoteServer::WriteBuffer(SRemoteClient* pClient, char* buffer, int& size)
  245. {
  246. IRemoteEvent* pEvent = nullptr;
  247. {
  248. AZStd::scoped_lock lock(m_mutex);
  249. for (TClients::iterator it = m_clients.begin(); it != m_clients.end(); ++it)
  250. {
  251. if (it->pClient == pClient)
  252. {
  253. TEventBuffer* pEvents = it->pEvents;
  254. if (!pEvents->empty())
  255. {
  256. pEvent = pEvents->front();
  257. pEvents->pop_front();
  258. }
  259. break;
  260. }
  261. }
  262. }
  263. const bool res = (pEvent != nullptr);
  264. if (pEvent)
  265. {
  266. SRemoteEventFactory::GetInst()->WriteToBuffer(pEvent, buffer, size, kDefaultBufferSize);
  267. delete pEvent;
  268. }
  269. return res;
  270. }
  271. /////////////////////////////////////////////////////////////////////////////////////////////
  272. bool SRemoteServer::ReadBuffer(const char* buffer, int data)
  273. {
  274. bool result = true;
  275. // Sometimes multiple events can come in a single buffer, so make sure we look
  276. // at the entire thing.
  277. int bytesRemaining = data;
  278. const char* curBuffer = buffer;
  279. while (bytesRemaining > 0)
  280. {
  281. // Create the event from the current sub string in the buffer.
  282. IRemoteEvent* event = SRemoteEventFactory::GetInst()->CreateEventFromBuffer(curBuffer, bytesRemaining);
  283. result &= (event != nullptr);
  284. if (event)
  285. {
  286. if (event->GetType() != eCET_Noop)
  287. {
  288. AZStd::scoped_lock lock(m_mutex);
  289. m_eventBuffer.push_back(event);
  290. }
  291. else
  292. {
  293. delete event;
  294. }
  295. }
  296. // Advance to the next null terminated string in the buffer
  297. const int currentSize = static_cast<int>(strnlen(curBuffer, bytesRemaining));
  298. bytesRemaining -= currentSize + 1;
  299. curBuffer += currentSize + 1;
  300. }
  301. return result;
  302. }
  303. /////////////////////////////////////////////////////////////////////////////////////////////
  304. /////////////////////////////////////////////////////////////////////////////////////////////
  305. /////////////////////////////////////////////////////////////////////////////////////////////
  306. void SRemoteClient::StartClient(AZSOCKET socket)
  307. {
  308. m_socket = socket;
  309. Start(kClientThreadName);
  310. }
  311. /////////////////////////////////////////////////////////////////////////////////////////////
  312. void SRemoteClient::StopClient()
  313. {
  314. AZ::AzSock::CloseSocket(m_socket);
  315. m_socket = AZ_SOCKET_INVALID;
  316. }
  317. /////////////////////////////////////////////////////////////////////////////////////////////
  318. void SRemoteClient::Terminate()
  319. {
  320. m_pServer->ClientDone(this);
  321. }
  322. /////////////////////////////////////////////////////////////////////////////////////////////
  323. void SRemoteClient::Run()
  324. {
  325. AZ_TRAIT_REMOTECONSOLE_SET_THREAD_AFFINITY
  326. char szBuff[kDefaultBufferSize];
  327. int size;
  328. SNoDataEvent<eCET_Req> reqEvt;
  329. AZStd::vector<AZStd::string> autoCompleteList;
  330. FillAutoCompleteList(autoCompleteList);
  331. bool ok = true;
  332. bool autoCompleteDoneSent = false;
  333. // Send a message that is used to verify that the Remote Console connected
  334. SNoDataEvent<eCET_ConnectMessage> connectMessage;
  335. SRemoteEventFactory::GetInst()->WriteToBuffer(&connectMessage, szBuff, size, kDefaultBufferSize);
  336. ok &= SendPackage(szBuff, size);
  337. ok &= RecvPackage(szBuff, size);
  338. ok &= m_pServer->ReadBuffer(szBuff, size);
  339. while (ok)
  340. {
  341. // read data
  342. SRemoteEventFactory::GetInst()->WriteToBuffer(&reqEvt, szBuff, size, kDefaultBufferSize);
  343. ok &= SendPackage(szBuff, size);
  344. ok &= RecvPackage(szBuff, size);
  345. ok &= m_pServer->ReadBuffer(szBuff, size);
  346. for (int i = 0; i < 20 && !autoCompleteList.empty(); ++i)
  347. {
  348. SStringEvent<eCET_AutoCompleteList> autoCompleteListEvt(autoCompleteList.back().c_str());
  349. SRemoteEventFactory::GetInst()->WriteToBuffer(&autoCompleteListEvt, szBuff, size, kDefaultBufferSize);
  350. ok &= SendPackage(szBuff, size);
  351. ok &= RecvPackage(szBuff, size);
  352. ok &= m_pServer->ReadBuffer(szBuff, size);
  353. autoCompleteList.pop_back();
  354. }
  355. if (autoCompleteList.empty() && !autoCompleteDoneSent)
  356. {
  357. SNoDataEvent<eCET_AutoCompleteListDone> autoCompleteDone;
  358. SRemoteEventFactory::GetInst()->WriteToBuffer(&autoCompleteDone, szBuff, size, kDefaultBufferSize);
  359. ok &= SendPackage(szBuff, size);
  360. ok &= RecvPackage(szBuff, size);
  361. ok &= m_pServer->ReadBuffer(szBuff, size);
  362. autoCompleteDoneSent = true;
  363. }
  364. // send data
  365. while (ok && m_pServer->WriteBuffer(this, szBuff, size))
  366. {
  367. ok &= SendPackage(szBuff, size);
  368. ok &= RecvPackage(szBuff, size);
  369. ok &= m_pServer->ReadBuffer(szBuff, size);
  370. }
  371. }
  372. }
  373. /////////////////////////////////////////////////////////////////////////////////////////////
  374. bool SRemoteClient::RecvPackage(char* buffer, int& size)
  375. {
  376. size = 0;
  377. int ret, idx = 0;
  378. do
  379. {
  380. ret = AZ::AzSock::Recv(m_socket, buffer + idx, kDefaultBufferSize - idx, 0);
  381. if (AZ::AzSock::SocketErrorOccured(ret))
  382. {
  383. return false;
  384. }
  385. idx += ret;
  386. } while (buffer[idx - 1] != '\0');
  387. size = idx;
  388. return true;
  389. }
  390. /////////////////////////////////////////////////////////////////////////////////////////////
  391. bool SRemoteClient::SendPackage(const char* buffer, int size)
  392. {
  393. int ret, idx = 0;
  394. int left = size + 1;
  395. assert(buffer[size] == '\0');
  396. while (left > 0)
  397. {
  398. ret = AZ::AzSock::Send(m_socket, &buffer[idx], left, 0);
  399. if (AZ::AzSock::SocketErrorOccured(ret))
  400. {
  401. return false;
  402. }
  403. left -= ret;
  404. idx += ret;
  405. }
  406. return true;
  407. }
  408. /////////////////////////////////////////////////////////////////////////////////////////////
  409. void SRemoteClient::FillAutoCompleteList(AZStd::vector<AZStd::string>& list)
  410. {
  411. AZStd::vector<AZStd::string_view> cmds;
  412. size_t count = gEnv->pConsole->GetSortedVars(cmds);
  413. cmds.resize(count);
  414. count = gEnv->pConsole->GetSortedVars(cmds);
  415. for (size_t i = 0; i < count; ++i)
  416. {
  417. list.push_back(cmds[i]);
  418. }
  419. if (!gEnv->pSystem || !gEnv->pSystem->GetILevelSystem())
  420. {
  421. return;
  422. }
  423. for (int i = 0, end = gEnv->pSystem->GetILevelSystem()->GetLevelCount(); i < end; ++i)
  424. {
  425. ILevelInfo* pLevel = gEnv->pSystem->GetILevelSystem()->GetLevelInfo(i);
  426. AZStd::string item = "map ";
  427. const char* levelName = pLevel->GetName();
  428. int start = 0;
  429. for (int k = 0, kend = static_cast<int>(strlen(levelName)); k < kend; ++k)
  430. {
  431. if ((levelName[k] == '\\' || levelName[k] == '/') && k + 1 < kend)
  432. {
  433. start = k + 1;
  434. }
  435. }
  436. item += levelName + start;
  437. list.push_back(item);
  438. }
  439. }
  440. /////////////////////////////////////////////////////////////////////////////////////////////
  441. /////////////////////////////////// Event factory ///////////////////////////////////////////
  442. /////////////////////////////////////////////////////////////////////////////////////////////
  443. #define REGISTER_EVENT_NODATA(evt) RegisterEvent(new SNoDataEvent<evt>());
  444. #define REGISTER_EVENT_STRING(evt) RegisterEvent(new SStringEvent<evt>(""));
  445. /////////////////////////////////////////////////////////////////////////////////////////////
  446. SRemoteEventFactory::SRemoteEventFactory()
  447. {
  448. REGISTER_EVENT_NODATA(eCET_Noop);
  449. REGISTER_EVENT_NODATA(eCET_Req);
  450. REGISTER_EVENT_STRING(eCET_LogMessage);
  451. REGISTER_EVENT_STRING(eCET_LogWarning);
  452. REGISTER_EVENT_STRING(eCET_LogError);
  453. REGISTER_EVENT_STRING(eCET_ConsoleCommand);
  454. REGISTER_EVENT_STRING(eCET_AutoCompleteList);
  455. REGISTER_EVENT_NODATA(eCET_AutoCompleteListDone);
  456. REGISTER_EVENT_NODATA(eCET_Strobo_GetThreads);
  457. REGISTER_EVENT_STRING(eCET_Strobo_ThreadAdd);
  458. REGISTER_EVENT_NODATA(eCET_Strobo_ThreadDone);
  459. REGISTER_EVENT_NODATA(eCET_Strobo_GetResult);
  460. REGISTER_EVENT_NODATA(eCET_Strobo_ResultStart);
  461. REGISTER_EVENT_NODATA(eCET_Strobo_ResultDone);
  462. REGISTER_EVENT_NODATA(eCET_Strobo_StatStart);
  463. REGISTER_EVENT_STRING(eCET_Strobo_StatAdd);
  464. REGISTER_EVENT_NODATA(eCET_Strobo_ThreadInfoStart);
  465. REGISTER_EVENT_STRING(eCET_Strobo_ThreadInfoAdd);
  466. REGISTER_EVENT_NODATA(eCET_Strobo_SymStart);
  467. REGISTER_EVENT_STRING(eCET_Strobo_SymAdd);
  468. REGISTER_EVENT_NODATA(eCET_Strobo_CallstackStart);
  469. REGISTER_EVENT_STRING(eCET_Strobo_CallstackAdd);
  470. REGISTER_EVENT_STRING(eCET_GameplayEvent);
  471. REGISTER_EVENT_NODATA(eCET_Strobo_FrameInfoStart);
  472. REGISTER_EVENT_STRING(eCET_Strobo_FrameInfoAdd);
  473. REGISTER_EVENT_NODATA(eCET_ConnectMessage);
  474. }
  475. /////////////////////////////////////////////////////////////////////////////////////////////
  476. SRemoteEventFactory::~SRemoteEventFactory()
  477. {
  478. for (TPrototypes::iterator it = m_prototypes.begin(), end = m_prototypes.end(); it != end; ++it)
  479. {
  480. delete it->second;
  481. }
  482. }
  483. /////////////////////////////////////////////////////////////////////////////////////////////
  484. IRemoteEvent* SRemoteEventFactory::CreateEventFromBuffer(const char* buffer, int size)
  485. {
  486. if (size > 1 && buffer[size - 1] == '\0')
  487. {
  488. EConsoleEventType type = EConsoleEventType(buffer[0] - '0');
  489. TPrototypes::const_iterator it = m_prototypes.find(type);
  490. if (it != m_prototypes.end())
  491. {
  492. IRemoteEvent* pEvent = it->second->CreateFromBuffer(buffer + 1, size - 1);
  493. return pEvent;
  494. }
  495. }
  496. return nullptr;
  497. }
  498. /////////////////////////////////////////////////////////////////////////////////////////////
  499. void SRemoteEventFactory::WriteToBuffer(IRemoteEvent* pEvent, char* buffer, int& size, int maxsize)
  500. {
  501. assert(m_prototypes.find(pEvent->GetType()) != m_prototypes.end());
  502. buffer[0] = '0' + (char)pEvent->GetType();
  503. pEvent->WriteToBuffer(buffer + 1, size, maxsize - 2);
  504. buffer[++size] = '\0';
  505. }
  506. /////////////////////////////////////////////////////////////////////////////////////////////
  507. void SRemoteEventFactory::RegisterEvent(IRemoteEvent* pEvent)
  508. {
  509. assert(m_prototypes.find(pEvent->GetType()) == m_prototypes.end());
  510. m_prototypes[pEvent->GetType()] = pEvent;
  511. }