yuzu_room.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <chrono>
  4. #include <fstream>
  5. #include <iostream>
  6. #include <memory>
  7. #include <regex>
  8. #include <string>
  9. #include <thread>
  10. #ifdef _WIN32
  11. // windows.h needs to be included before shellapi.h
  12. #include <windows.h>
  13. #include <shellapi.h>
  14. #endif
  15. #include <mbedtls/base64.h>
  16. #include "common/common_types.h"
  17. #include "common/detached_tasks.h"
  18. #include "common/fs/file.h"
  19. #include "common/fs/fs.h"
  20. #include "common/fs/path_util.h"
  21. #include "common/logging/backend.h"
  22. #include "common/logging/log.h"
  23. #include "common/scm_rev.h"
  24. #include "common/settings.h"
  25. #include "common/string_util.h"
  26. #include "core/core.h"
  27. #include "network/announce_multiplayer_session.h"
  28. #include "network/network.h"
  29. #include "network/room.h"
  30. #include "network/verify_user.h"
  31. #ifdef ENABLE_WEB_SERVICE
  32. #include "web_service/verify_user_jwt.h"
  33. #endif
  34. #undef _UNICODE
  35. #include <getopt.h>
  36. #ifndef _MSC_VER
  37. #include <unistd.h>
  38. #endif
  39. static void PrintHelp(const char* argv0) {
  40. LOG_INFO(Network,
  41. "Usage: {}"
  42. " [options] <filename>\n"
  43. "--room-name The name of the room\n"
  44. "--room-description The room description\n"
  45. "--port The port used for the room\n"
  46. "--max_members The maximum number of players for this room\n"
  47. "--password The password for the room\n"
  48. "--preferred-game The preferred game for this room\n"
  49. "--preferred-game-id The preferred game-id for this room\n"
  50. "--username The username used for announce\n"
  51. "--token The token used for announce\n"
  52. "--web-api-url yuzu Web API url\n"
  53. "--ban-list-file The file for storing the room ban list\n"
  54. "--log-file The file for storing the room log\n"
  55. "--enable-yuzu-mods Allow yuzu Community Moderators to moderate on your room\n"
  56. "-h, --help Display this help and exit\n"
  57. "-v, --version Output version information and exit\n",
  58. argv0);
  59. }
  60. static void PrintVersion() {
  61. LOG_INFO(Network, "yuzu dedicated room {} {} Libnetwork: {}", Common::g_scm_branch,
  62. Common::g_scm_desc, Network::network_version);
  63. }
  64. /// The magic text at the beginning of a yuzu-room ban list file.
  65. static constexpr char BanListMagic[] = "YuzuRoom-BanList-1";
  66. static constexpr char token_delimiter{':'};
  67. static void PadToken(std::string& token) {
  68. std::size_t outlen = 0;
  69. std::array<unsigned char, 512> output{};
  70. std::array<unsigned char, 2048> roundtrip{};
  71. for (size_t i = 0; i < 3; i++) {
  72. mbedtls_base64_decode(output.data(), output.size(), &outlen,
  73. reinterpret_cast<const unsigned char*>(token.c_str()),
  74. token.length());
  75. mbedtls_base64_encode(roundtrip.data(), roundtrip.size(), &outlen, output.data(), outlen);
  76. if (memcmp(roundtrip.data(), token.data(), token.size()) == 0) {
  77. break;
  78. }
  79. token.push_back('=');
  80. }
  81. }
  82. static std::string UsernameFromDisplayToken(const std::string& display_token) {
  83. std::size_t outlen;
  84. std::array<unsigned char, 512> output{};
  85. mbedtls_base64_decode(output.data(), output.size(), &outlen,
  86. reinterpret_cast<const unsigned char*>(display_token.c_str()),
  87. display_token.length());
  88. std::string decoded_display_token(reinterpret_cast<char*>(&output), outlen);
  89. return decoded_display_token.substr(0, decoded_display_token.find(token_delimiter));
  90. }
  91. static std::string TokenFromDisplayToken(const std::string& display_token) {
  92. std::size_t outlen;
  93. std::array<unsigned char, 512> output{};
  94. mbedtls_base64_decode(output.data(), output.size(), &outlen,
  95. reinterpret_cast<const unsigned char*>(display_token.c_str()),
  96. display_token.length());
  97. std::string decoded_display_token(reinterpret_cast<char*>(&output), outlen);
  98. return decoded_display_token.substr(decoded_display_token.find(token_delimiter) + 1);
  99. }
  100. static Network::Room::BanList LoadBanList(const std::string& path) {
  101. std::ifstream file;
  102. Common::FS::OpenFileStream(file, path, std::ios_base::in);
  103. if (!file || file.eof()) {
  104. LOG_ERROR(Network, "Could not open ban list!");
  105. return {};
  106. }
  107. std::string magic;
  108. std::getline(file, magic);
  109. if (magic != BanListMagic) {
  110. LOG_ERROR(Network, "Ban list is not valid!");
  111. return {};
  112. }
  113. // false = username ban list, true = ip ban list
  114. bool ban_list_type = false;
  115. Network::Room::UsernameBanList username_ban_list;
  116. Network::Room::IPBanList ip_ban_list;
  117. while (!file.eof()) {
  118. std::string line;
  119. std::getline(file, line);
  120. line.erase(std::remove(line.begin(), line.end(), '\0'), line.end());
  121. line = Common::StripSpaces(line);
  122. if (line.empty()) {
  123. // An empty line marks start of the IP ban list
  124. ban_list_type = true;
  125. continue;
  126. }
  127. if (ban_list_type) {
  128. ip_ban_list.emplace_back(line);
  129. } else {
  130. username_ban_list.emplace_back(line);
  131. }
  132. }
  133. return {username_ban_list, ip_ban_list};
  134. }
  135. static void SaveBanList(const Network::Room::BanList& ban_list, const std::string& path) {
  136. std::ofstream file;
  137. Common::FS::OpenFileStream(file, path, std::ios_base::out);
  138. if (!file) {
  139. LOG_ERROR(Network, "Could not save ban list!");
  140. return;
  141. }
  142. file << BanListMagic << "\n";
  143. // Username ban list
  144. for (const auto& username : ban_list.first) {
  145. file << username << "\n";
  146. }
  147. file << "\n";
  148. // IP ban list
  149. for (const auto& ip : ban_list.second) {
  150. file << ip << "\n";
  151. }
  152. }
  153. static void InitializeLogging(const std::string& log_file) {
  154. Common::Log::Initialize();
  155. Common::Log::SetColorConsoleBackendEnabled(true);
  156. Common::Log::Start();
  157. }
  158. /// Application entry point
  159. int main(int argc, char** argv) {
  160. Common::DetachedTasks detached_tasks;
  161. int option_index = 0;
  162. char* endarg;
  163. std::string room_name;
  164. std::string room_description;
  165. std::string password;
  166. std::string preferred_game;
  167. std::string username;
  168. std::string token;
  169. std::string web_api_url;
  170. std::string ban_list_file;
  171. std::string log_file = "yuzu-room.log";
  172. u64 preferred_game_id = 0;
  173. u32 port = Network::DefaultRoomPort;
  174. u32 max_members = 16;
  175. bool enable_yuzu_mods = false;
  176. static struct option long_options[] = {
  177. {"room-name", required_argument, 0, 'n'},
  178. {"room-description", required_argument, 0, 'd'},
  179. {"port", required_argument, 0, 'p'},
  180. {"max_members", required_argument, 0, 'm'},
  181. {"password", required_argument, 0, 'w'},
  182. {"preferred-game", required_argument, 0, 'g'},
  183. {"preferred-game-id", required_argument, 0, 'i'},
  184. {"username", optional_argument, 0, 'u'},
  185. {"token", required_argument, 0, 't'},
  186. {"web-api-url", required_argument, 0, 'a'},
  187. {"ban-list-file", required_argument, 0, 'b'},
  188. {"log-file", required_argument, 0, 'l'},
  189. {"enable-yuzu-mods", no_argument, 0, 'e'},
  190. {"help", no_argument, 0, 'h'},
  191. {"version", no_argument, 0, 'v'},
  192. {0, 0, 0, 0},
  193. };
  194. InitializeLogging(log_file);
  195. while (optind < argc) {
  196. int arg = getopt_long(argc, argv, "n:d:p:m:w:g:u:t:a:i:l:hv", long_options, &option_index);
  197. if (arg != -1) {
  198. switch (static_cast<char>(arg)) {
  199. case 'n':
  200. room_name.assign(optarg);
  201. break;
  202. case 'd':
  203. room_description.assign(optarg);
  204. break;
  205. case 'p':
  206. port = strtoul(optarg, &endarg, 0);
  207. break;
  208. case 'm':
  209. max_members = strtoul(optarg, &endarg, 0);
  210. break;
  211. case 'w':
  212. password.assign(optarg);
  213. break;
  214. case 'g':
  215. preferred_game.assign(optarg);
  216. break;
  217. case 'i':
  218. preferred_game_id = strtoull(optarg, &endarg, 16);
  219. break;
  220. case 'u':
  221. username.assign(optarg);
  222. break;
  223. case 't':
  224. token.assign(optarg);
  225. break;
  226. case 'a':
  227. web_api_url.assign(optarg);
  228. break;
  229. case 'b':
  230. ban_list_file.assign(optarg);
  231. break;
  232. case 'l':
  233. log_file.assign(optarg);
  234. break;
  235. case 'e':
  236. enable_yuzu_mods = true;
  237. break;
  238. case 'h':
  239. PrintHelp(argv[0]);
  240. return 0;
  241. case 'v':
  242. PrintVersion();
  243. return 0;
  244. }
  245. }
  246. }
  247. if (room_name.empty()) {
  248. LOG_ERROR(Network, "Room name is empty!");
  249. PrintHelp(argv[0]);
  250. return -1;
  251. }
  252. if (preferred_game.empty()) {
  253. LOG_ERROR(Network, "Preferred game is empty!");
  254. PrintHelp(argv[0]);
  255. return -1;
  256. }
  257. if (preferred_game_id == 0) {
  258. LOG_ERROR(Network,
  259. "preferred-game-id not set!\nThis should get set to allow users to find your "
  260. "room.\nSet with --preferred-game-id id");
  261. }
  262. if (max_members > Network::MaxConcurrentConnections || max_members < 2) {
  263. LOG_ERROR(Network, "max_members needs to be in the range 2 - {}!",
  264. Network::MaxConcurrentConnections);
  265. PrintHelp(argv[0]);
  266. return -1;
  267. }
  268. if (port > UINT16_MAX) {
  269. LOG_ERROR(Network, "Port needs to be in the range 0 - 65535!");
  270. PrintHelp(argv[0]);
  271. return -1;
  272. }
  273. if (ban_list_file.empty()) {
  274. LOG_ERROR(Network, "Ban list file not set!\nThis should get set to load and save room ban "
  275. "list.\nSet with --ban-list-file <file>");
  276. }
  277. bool announce = true;
  278. if (token.empty() && announce) {
  279. announce = false;
  280. LOG_INFO(Network, "Token is empty: Hosting a private room");
  281. }
  282. if (web_api_url.empty() && announce) {
  283. announce = false;
  284. LOG_INFO(Network, "Endpoint url is empty: Hosting a private room");
  285. }
  286. if (announce) {
  287. if (username.empty()) {
  288. LOG_INFO(Network, "Hosting a public room");
  289. Settings::values.web_api_url = web_api_url;
  290. PadToken(token);
  291. Settings::values.yuzu_username = UsernameFromDisplayToken(token);
  292. username = Settings::values.yuzu_username.GetValue();
  293. Settings::values.yuzu_token = TokenFromDisplayToken(token);
  294. } else {
  295. LOG_INFO(Network, "Hosting a public room");
  296. Settings::values.web_api_url = web_api_url;
  297. Settings::values.yuzu_username = username;
  298. Settings::values.yuzu_token = token;
  299. }
  300. }
  301. if (!announce && enable_yuzu_mods) {
  302. enable_yuzu_mods = false;
  303. LOG_INFO(Network, "Can not enable yuzu Moderators for private rooms");
  304. }
  305. // Load the ban list
  306. Network::Room::BanList ban_list;
  307. if (!ban_list_file.empty()) {
  308. ban_list = LoadBanList(ban_list_file);
  309. }
  310. std::unique_ptr<Network::VerifyUser::Backend> verify_backend;
  311. if (announce) {
  312. #ifdef ENABLE_WEB_SERVICE
  313. verify_backend =
  314. std::make_unique<WebService::VerifyUserJWT>(Settings::values.web_api_url.GetValue());
  315. #else
  316. LOG_INFO(Network,
  317. "yuzu Web Services is not available with this build: validation is disabled.");
  318. verify_backend = std::make_unique<Network::VerifyUser::NullBackend>();
  319. #endif
  320. } else {
  321. verify_backend = std::make_unique<Network::VerifyUser::NullBackend>();
  322. }
  323. Network::RoomNetwork network{};
  324. network.Init();
  325. if (auto room = network.GetRoom().lock()) {
  326. AnnounceMultiplayerRoom::GameInfo preferred_game_info{.name = preferred_game,
  327. .id = preferred_game_id};
  328. if (!room->Create(room_name, room_description, "", port, password, max_members, username,
  329. preferred_game_info, std::move(verify_backend), ban_list,
  330. enable_yuzu_mods)) {
  331. LOG_INFO(Network, "Failed to create room: ");
  332. return -1;
  333. }
  334. LOG_INFO(Network, "Room is open. Close with Q+Enter...");
  335. auto announce_session = std::make_unique<Core::AnnounceMultiplayerSession>(network);
  336. if (announce) {
  337. announce_session->Start();
  338. }
  339. while (room->GetState() == Network::Room::State::Open) {
  340. std::string in;
  341. std::cin >> in;
  342. if (in.size() > 0) {
  343. break;
  344. }
  345. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  346. }
  347. if (announce) {
  348. announce_session->Stop();
  349. }
  350. announce_session.reset();
  351. // Save the ban list
  352. if (!ban_list_file.empty()) {
  353. SaveBanList(room->GetBanList(), ban_list_file);
  354. }
  355. room->Destroy();
  356. }
  357. network.Shutdown();
  358. detached_tasks.WaitForAllTasks();
  359. return 0;
  360. }