2017-10-31 09:13:21 +00:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <chrono>
|
2018-11-24 08:26:52 +00:00
|
|
|
#include <fstream>
|
2017-10-31 09:13:21 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
#include <regex>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
2019-11-09 07:42:37 +00:00
|
|
|
#include <cryptopp/base64.h>
|
2017-10-31 09:13:21 +00:00
|
|
|
#include <glad/glad.h>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
// windows.h needs to be included before shellapi.h
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <shellapi.h>
|
|
|
|
#endif
|
|
|
|
|
2019-11-03 02:16:37 +00:00
|
|
|
#include "common/common_paths.h"
|
2017-10-31 09:13:21 +00:00
|
|
|
#include "common/common_types.h"
|
2018-09-12 16:22:48 +00:00
|
|
|
#include "common/detached_tasks.h"
|
2019-11-03 02:16:37 +00:00
|
|
|
#include "common/file_util.h"
|
|
|
|
#include "common/logging/backend.h"
|
|
|
|
#include "common/logging/log.h"
|
2017-10-31 09:13:21 +00:00
|
|
|
#include "common/scm_rev.h"
|
2018-11-24 08:26:52 +00:00
|
|
|
#include "common/string_util.h"
|
2017-10-31 09:13:21 +00:00
|
|
|
#include "core/announce_multiplayer_session.h"
|
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/settings.h"
|
|
|
|
#include "network/network.h"
|
2018-11-24 08:26:52 +00:00
|
|
|
#include "network/room.h"
|
2018-10-27 07:46:58 +00:00
|
|
|
#include "network/verify_user.h"
|
|
|
|
|
|
|
|
#ifdef ENABLE_WEB_SERVICE
|
|
|
|
#include "web_service/verify_user_jwt.h"
|
|
|
|
#endif
|
2017-10-31 09:13:21 +00:00
|
|
|
|
2019-04-15 21:32:47 +00:00
|
|
|
#undef _UNICODE
|
|
|
|
#include <getopt.h>
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2017-10-31 09:13:21 +00:00
|
|
|
static void PrintHelp(const char* argv0) {
|
|
|
|
std::cout << "Usage: " << argv0
|
|
|
|
<< " [options] <filename>\n"
|
|
|
|
"--room-name The name of the room\n"
|
2018-04-30 07:40:51 +00:00
|
|
|
"--room-description The room description\n"
|
2017-10-31 09:13:21 +00:00
|
|
|
"--port The port used for the room\n"
|
|
|
|
"--max_members The maximum number of players for this room\n"
|
|
|
|
"--password The password for the room\n"
|
2017-11-05 19:26:55 +00:00
|
|
|
"--preferred-game The preferred game for this room\n"
|
|
|
|
"--preferred-game-id The preferred game-id for this room\n"
|
2017-10-31 09:13:21 +00:00
|
|
|
"--username The username used for announce\n"
|
|
|
|
"--token The token used for announce\n"
|
2018-09-12 17:07:06 +00:00
|
|
|
"--web-api-url Citra Web API url\n"
|
2018-11-24 08:26:52 +00:00
|
|
|
"--ban-list-file The file for storing the room ban list\n"
|
2019-11-03 02:16:37 +00:00
|
|
|
"--log-file The file for storing the room log\n"
|
2018-12-15 14:47:07 +00:00
|
|
|
"--enable-citra-mods Allow Citra Community Moderators to moderate on your room\n"
|
2017-10-31 09:13:21 +00:00
|
|
|
"-h, --help Display this help and exit\n"
|
|
|
|
"-v, --version Output version information and exit\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintVersion() {
|
|
|
|
std::cout << "Citra dedicated room " << Common::g_scm_branch << " " << Common::g_scm_desc
|
|
|
|
<< " Libnetwork: " << Network::network_version << std::endl;
|
|
|
|
}
|
|
|
|
|
2018-11-24 08:26:52 +00:00
|
|
|
/// The magic text at the beginning of a citra-room ban list file.
|
|
|
|
static constexpr char BanListMagic[] = "CitraRoom-BanList-1";
|
|
|
|
|
2019-11-09 07:42:37 +00:00
|
|
|
static constexpr char token_delimiter{':'};
|
|
|
|
|
|
|
|
static std::string UsernameFromDisplayToken(const std::string& display_token) {
|
|
|
|
std::string unencoded_display_token;
|
|
|
|
CryptoPP::StringSource ss(
|
|
|
|
display_token, true,
|
|
|
|
new CryptoPP::Base64Decoder(new CryptoPP::StringSink(unencoded_display_token)));
|
|
|
|
return unencoded_display_token.substr(0, unencoded_display_token.find(token_delimiter));
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string TokenFromDisplayToken(const std::string& display_token) {
|
|
|
|
std::string unencoded_display_token;
|
|
|
|
CryptoPP::StringSource ss(
|
|
|
|
display_token, true,
|
|
|
|
new CryptoPP::Base64Decoder(new CryptoPP::StringSink(unencoded_display_token)));
|
|
|
|
return unencoded_display_token.substr(unencoded_display_token.find(token_delimiter) + 1);
|
|
|
|
}
|
|
|
|
|
2018-11-24 08:26:52 +00:00
|
|
|
static Network::Room::BanList LoadBanList(const std::string& path) {
|
|
|
|
std::ifstream file;
|
|
|
|
OpenFStream(file, path, std::ios_base::in);
|
|
|
|
if (!file || file.eof()) {
|
|
|
|
std::cout << "Could not open ban list!\n\n";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::string magic;
|
|
|
|
std::getline(file, magic);
|
|
|
|
if (magic != BanListMagic) {
|
|
|
|
std::cout << "Ban list is not valid!\n\n";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// false = username ban list, true = ip ban list
|
|
|
|
bool ban_list_type = false;
|
|
|
|
Network::Room::UsernameBanList username_ban_list;
|
|
|
|
Network::Room::IPBanList ip_ban_list;
|
|
|
|
while (!file.eof()) {
|
|
|
|
std::string line;
|
|
|
|
std::getline(file, line);
|
|
|
|
line.erase(std::remove(line.begin(), line.end(), '\0'), line.end());
|
|
|
|
line = Common::StripSpaces(line);
|
|
|
|
if (line.empty()) {
|
|
|
|
// An empty line marks start of the IP ban list
|
|
|
|
ban_list_type = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ban_list_type) {
|
|
|
|
ip_ban_list.emplace_back(line);
|
|
|
|
} else {
|
|
|
|
username_ban_list.emplace_back(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {username_ban_list, ip_ban_list};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SaveBanList(const Network::Room::BanList& ban_list, const std::string& path) {
|
|
|
|
std::ofstream file;
|
|
|
|
OpenFStream(file, path, std::ios_base::out);
|
|
|
|
if (!file) {
|
|
|
|
std::cout << "Could not save ban list!\n\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file << BanListMagic << "\n";
|
|
|
|
|
|
|
|
// Username ban list
|
|
|
|
for (const auto& username : ban_list.first) {
|
|
|
|
file << username << "\n";
|
|
|
|
}
|
|
|
|
file << "\n";
|
|
|
|
|
|
|
|
// IP ban list
|
|
|
|
for (const auto& ip : ban_list.second) {
|
|
|
|
file << ip << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
file.flush();
|
|
|
|
}
|
|
|
|
|
2019-11-03 02:16:37 +00:00
|
|
|
static void InitializeLogging(const std::string& log_file) {
|
|
|
|
Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>());
|
|
|
|
|
|
|
|
const std::string& log_dir = FileUtil::GetUserPath(FileUtil::UserPath::LogDir);
|
|
|
|
FileUtil::CreateFullPath(log_dir);
|
|
|
|
Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + log_file));
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
Log::AddBackend(std::make_unique<Log::DebuggerBackend>());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-10-31 09:13:21 +00:00
|
|
|
/// Application entry point
|
|
|
|
int main(int argc, char** argv) {
|
2018-09-12 16:22:48 +00:00
|
|
|
Common::DetachedTasks detached_tasks;
|
2017-10-31 09:13:21 +00:00
|
|
|
int option_index = 0;
|
|
|
|
char* endarg;
|
|
|
|
|
|
|
|
// This is just to be able to link against core
|
2018-04-06 19:27:47 +00:00
|
|
|
gladLoadGL();
|
2017-10-31 09:13:21 +00:00
|
|
|
|
|
|
|
std::string room_name;
|
2018-04-30 07:40:51 +00:00
|
|
|
std::string room_description;
|
2017-10-31 09:13:21 +00:00
|
|
|
std::string password;
|
|
|
|
std::string preferred_game;
|
|
|
|
std::string username;
|
|
|
|
std::string token;
|
2018-09-12 17:07:06 +00:00
|
|
|
std::string web_api_url;
|
2018-11-24 08:26:52 +00:00
|
|
|
std::string ban_list_file;
|
2019-11-03 02:16:37 +00:00
|
|
|
std::string log_file = "citra-room.log";
|
2017-10-31 09:13:21 +00:00
|
|
|
u64 preferred_game_id = 0;
|
2018-03-29 01:44:54 +00:00
|
|
|
u32 port = Network::DefaultRoomPort;
|
2017-10-31 09:13:21 +00:00
|
|
|
u32 max_members = 16;
|
2018-12-15 14:47:07 +00:00
|
|
|
bool enable_citra_mods = false;
|
2017-10-31 09:13:21 +00:00
|
|
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"room-name", required_argument, 0, 'n'},
|
2018-04-30 07:40:51 +00:00
|
|
|
{"room-description", required_argument, 0, 'd'},
|
2017-10-31 09:13:21 +00:00
|
|
|
{"port", required_argument, 0, 'p'},
|
|
|
|
{"max_members", required_argument, 0, 'm'},
|
|
|
|
{"password", required_argument, 0, 'w'},
|
|
|
|
{"preferred-game", required_argument, 0, 'g'},
|
|
|
|
{"preferred-game-id", required_argument, 0, 'i'},
|
2019-11-09 07:42:37 +00:00
|
|
|
{"username", optional_argument, 0, 'u'},
|
2017-10-31 09:13:21 +00:00
|
|
|
{"token", required_argument, 0, 't'},
|
2018-09-12 17:07:06 +00:00
|
|
|
{"web-api-url", required_argument, 0, 'a'},
|
2018-11-24 08:26:52 +00:00
|
|
|
{"ban-list-file", required_argument, 0, 'b'},
|
2019-11-03 02:16:37 +00:00
|
|
|
{"log-file", required_argument, 0, 'l'},
|
2018-12-15 14:47:07 +00:00
|
|
|
{"enable-citra-mods", no_argument, 0, 'e'},
|
2017-10-31 09:13:21 +00:00
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{"version", no_argument, 0, 'v'},
|
|
|
|
{0, 0, 0, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
while (optind < argc) {
|
2019-11-03 02:16:37 +00:00
|
|
|
int arg = getopt_long(argc, argv, "n:d:p:m:w:g:u:t:a:i:l:hv", long_options, &option_index);
|
2017-10-31 09:13:21 +00:00
|
|
|
if (arg != -1) {
|
2019-03-15 23:19:24 +00:00
|
|
|
switch (static_cast<char>(arg)) {
|
2017-10-31 09:13:21 +00:00
|
|
|
case 'n':
|
|
|
|
room_name.assign(optarg);
|
|
|
|
break;
|
2018-04-30 07:40:51 +00:00
|
|
|
case 'd':
|
|
|
|
room_description.assign(optarg);
|
|
|
|
break;
|
2017-10-31 09:13:21 +00:00
|
|
|
case 'p':
|
|
|
|
port = strtoul(optarg, &endarg, 0);
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
max_members = strtoul(optarg, &endarg, 0);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
password.assign(optarg);
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
preferred_game.assign(optarg);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
preferred_game_id = strtoull(optarg, &endarg, 16);
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
username.assign(optarg);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
token.assign(optarg);
|
|
|
|
break;
|
|
|
|
case 'a':
|
2018-09-12 17:07:06 +00:00
|
|
|
web_api_url.assign(optarg);
|
2017-10-31 09:13:21 +00:00
|
|
|
break;
|
2018-11-24 08:26:52 +00:00
|
|
|
case 'b':
|
|
|
|
ban_list_file.assign(optarg);
|
|
|
|
break;
|
2019-11-03 02:16:37 +00:00
|
|
|
case 'l':
|
|
|
|
log_file.assign(optarg);
|
|
|
|
break;
|
2018-12-15 14:47:07 +00:00
|
|
|
case 'e':
|
|
|
|
enable_citra_mods = true;
|
|
|
|
break;
|
2017-10-31 09:13:21 +00:00
|
|
|
case 'h':
|
|
|
|
PrintHelp(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 'v':
|
|
|
|
PrintVersion();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (room_name.empty()) {
|
|
|
|
std::cout << "room name is empty!\n\n";
|
|
|
|
PrintHelp(argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (preferred_game.empty()) {
|
2018-03-11 11:58:00 +00:00
|
|
|
std::cout << "preferred game is empty!\n\n";
|
2017-10-31 09:13:21 +00:00
|
|
|
PrintHelp(argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (preferred_game_id == 0) {
|
2018-03-11 11:58:00 +00:00
|
|
|
std::cout << "preferred-game-id not set!\nThis should get set to allow users to find your "
|
|
|
|
"room.\nSet with --preferred-game-id id\n\n";
|
2017-10-31 09:13:21 +00:00
|
|
|
}
|
2018-12-10 19:21:32 +00:00
|
|
|
if (max_members > Network::MaxConcurrentConnections || max_members < 2) {
|
2017-10-31 09:13:21 +00:00
|
|
|
std::cout << "max_members needs to be in the range 2 - "
|
|
|
|
<< Network::MaxConcurrentConnections << "!\n\n";
|
|
|
|
PrintHelp(argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (port > 65535) {
|
|
|
|
std::cout << "port needs to be in the range 0 - 65535!\n\n";
|
|
|
|
PrintHelp(argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-11-24 08:26:52 +00:00
|
|
|
if (ban_list_file.empty()) {
|
|
|
|
std::cout << "Ban list file not set!\nThis should get set to load and save room ban "
|
|
|
|
"list.\nSet with --ban-list-file <file>\n\n";
|
|
|
|
}
|
2017-10-31 09:13:21 +00:00
|
|
|
bool announce = true;
|
|
|
|
if (token.empty() && announce) {
|
|
|
|
announce = false;
|
|
|
|
std::cout << "token is empty: Hosting a private room\n\n";
|
|
|
|
}
|
2018-09-12 17:07:06 +00:00
|
|
|
if (web_api_url.empty() && announce) {
|
2017-10-31 09:13:21 +00:00
|
|
|
announce = false;
|
2018-08-27 21:09:29 +00:00
|
|
|
std::cout << "endpoint url is empty: Hosting a private room\n\n";
|
2017-10-31 09:13:21 +00:00
|
|
|
}
|
|
|
|
if (announce) {
|
2019-11-09 07:42:37 +00:00
|
|
|
if (username.empty()) {
|
|
|
|
std::cout << "Hosting a public room\n\n";
|
|
|
|
Settings::values.web_api_url = web_api_url;
|
|
|
|
Settings::values.citra_username = UsernameFromDisplayToken(token);
|
2019-11-17 14:56:20 +00:00
|
|
|
username = Settings::values.citra_username;
|
2019-11-09 07:42:37 +00:00
|
|
|
Settings::values.citra_token = TokenFromDisplayToken(token);
|
|
|
|
} else {
|
|
|
|
std::cout << "Hosting a public room\n\n";
|
|
|
|
Settings::values.web_api_url = web_api_url;
|
|
|
|
Settings::values.citra_username = username;
|
|
|
|
Settings::values.citra_token = token;
|
|
|
|
}
|
2017-10-31 09:13:21 +00:00
|
|
|
}
|
2018-12-15 14:47:07 +00:00
|
|
|
if (!announce && enable_citra_mods) {
|
|
|
|
enable_citra_mods = false;
|
|
|
|
std::cout << "Can not enable Citra Moderators for private rooms\n\n";
|
|
|
|
}
|
2017-10-31 09:13:21 +00:00
|
|
|
|
2019-11-03 02:16:37 +00:00
|
|
|
InitializeLogging(log_file);
|
|
|
|
|
2018-11-24 08:26:52 +00:00
|
|
|
// Load the ban list
|
|
|
|
Network::Room::BanList ban_list;
|
|
|
|
if (!ban_list_file.empty()) {
|
|
|
|
ban_list = LoadBanList(ban_list_file);
|
|
|
|
}
|
|
|
|
|
2018-10-27 07:46:58 +00:00
|
|
|
std::unique_ptr<Network::VerifyUser::Backend> verify_backend;
|
|
|
|
if (announce) {
|
|
|
|
#ifdef ENABLE_WEB_SERVICE
|
|
|
|
verify_backend = std::make_unique<WebService::VerifyUserJWT>(Settings::values.web_api_url);
|
|
|
|
#else
|
|
|
|
std::cout
|
|
|
|
<< "Citra Web Services is not available with this build: validation is disabled.\n\n";
|
|
|
|
verify_backend = std::make_unique<Network::VerifyUser::NullBackend>();
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
verify_backend = std::make_unique<Network::VerifyUser::NullBackend>();
|
|
|
|
}
|
|
|
|
|
2017-10-31 09:13:21 +00:00
|
|
|
Network::Init();
|
|
|
|
if (std::shared_ptr<Network::Room> room = Network::GetRoom().lock()) {
|
2018-11-24 08:26:52 +00:00
|
|
|
if (!room->Create(room_name, room_description, "", port, password, max_members, username,
|
2018-12-15 14:47:07 +00:00
|
|
|
preferred_game, preferred_game_id, std::move(verify_backend), ban_list,
|
|
|
|
enable_citra_mods)) {
|
2017-10-31 09:13:21 +00:00
|
|
|
std::cout << "Failed to create room: \n\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
std::cout << "Room is open. Close with Q+Enter...\n\n";
|
|
|
|
auto announce_session = std::make_unique<Core::AnnounceMultiplayerSession>();
|
|
|
|
if (announce) {
|
|
|
|
announce_session->Start();
|
|
|
|
}
|
|
|
|
while (room->GetState() == Network::Room::State::Open) {
|
|
|
|
std::string in;
|
|
|
|
std::cin >> in;
|
|
|
|
if (in.size() > 0) {
|
2019-11-10 21:54:05 +00:00
|
|
|
break;
|
2017-10-31 09:13:21 +00:00
|
|
|
}
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
}
|
|
|
|
if (announce) {
|
|
|
|
announce_session->Stop();
|
|
|
|
}
|
|
|
|
announce_session.reset();
|
2018-11-24 08:26:52 +00:00
|
|
|
// Save the ban list
|
|
|
|
if (!ban_list_file.empty()) {
|
|
|
|
SaveBanList(room->GetBanList(), ban_list_file);
|
|
|
|
}
|
2017-10-31 09:13:21 +00:00
|
|
|
room->Destroy();
|
|
|
|
}
|
|
|
|
Network::Shutdown();
|
2018-09-12 16:22:48 +00:00
|
|
|
detached_tasks.WaitForAllTasks();
|
2017-10-31 09:13:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|