RemoteConsoleCore.cpp 19 KB

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