| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "UICommon/NetPlayIndex.h" |
| 5 | |
| 6 | #include <chrono> |
| 7 | #include <numeric> |
| 8 | #include <span> |
| 9 | #include <string> |
| 10 | |
| 11 | #include <picojson.h> |
| 12 | |
| 13 | #include "Common/Common.h" |
| 14 | #include "Common/HttpRequest.h" |
| 15 | #include "Common/Version.h" |
| 16 | |
| 17 | #include "Core/Config/NetplaySettings.h" |
| 18 | |
| 19 | NetPlayIndex::NetPlayIndex() = default; |
| 20 | |
| 21 | NetPlayIndex::~NetPlayIndex() |
| 22 | { |
| 23 | if (!m_secret.empty()) |
| 24 | Remove(); |
| 25 | } |
| 26 | |
| 27 | static std::optional<picojson::value> ParseResponse(std::span<const u8> response) |
| 28 | { |
| 29 | const char* data = reinterpret_cast<const char*>(response.data()); |
| 30 | |
| 31 | picojson::value json; |
| 32 | |
| 33 | const auto error = picojson::parse(json, data, data + response.size()); |
| 34 | |
| 35 | if (!error.empty()) |
| 36 | return {}; |
| 37 | |
| 38 | return json; |
| 39 | } |
| 40 | |
| 41 | std::optional<std::vector<NetPlaySession>> |
| 42 | NetPlayIndex::List(const std::map<std::string, std::string>& filters) |
| 43 | { |
| 44 | Common::HttpRequest request; |
| 45 | |
| 46 | std::string list_url = Config::Get(Config::NETPLAY_INDEX_URL) + "/v0/list"; |
| 47 | |
| 48 | if (!filters.empty()) |
| 49 | { |
| 50 | list_url += '?'; |
| 51 | for (const auto& filter : filters) |
| 52 | { |
| 53 | list_url += filter.first + '=' + request.EscapeComponent(filter.second) + '&'; |
| 54 | } |
| 55 | list_url.pop_back(); |
| 56 | } |
| 57 | |
| 58 | auto response = |
| 59 | request.Get(list_url, {{"X-Is-Dolphin", "1"}}, Common::HttpRequest::AllowedReturnCodes::All); |
| 60 | if (!response) |
| 61 | { |
| 62 | m_last_error = "NO_RESPONSE"; |
| 63 | return {}; |
| 64 | } |
| 65 | |
| 66 | auto json = ParseResponse(response.value()); |
| 67 | |
| 68 | if (!json) |
| 69 | { |
| 70 | m_last_error = "BAD_JSON"; |
| 71 | return {}; |
| 72 | } |
| 73 | |
| 74 | const auto& status = json->get("status"); |
| 75 | |
| 76 | if (status.to_str() != "OK") |
| 77 | { |
| 78 | m_last_error = status.to_str(); |
| 79 | return {}; |
| 80 | } |
| 81 | |
| 82 | const auto& entries = json->get("sessions"); |
| 83 | |
| 84 | std::vector<NetPlaySession> sessions; |
| 85 | |
| 86 | for (const auto& entry : entries.get<picojson::array>()) |
| 87 | { |
| 88 | const auto& name = entry.get("name"); |
| 89 | const auto& region = entry.get("region"); |
| 90 | const auto& method = entry.get("method"); |
| 91 | const auto& game_id = entry.get("game"); |
| 92 | const auto& server_id = entry.get("server_id"); |
| 93 | const auto& has_password = entry.get("password"); |
| 94 | const auto& player_count = entry.get("player_count"); |
| 95 | const auto& port = entry.get("port"); |
| 96 | const auto& in_game = entry.get("in_game"); |
| 97 | const auto& version = entry.get("version"); |
| 98 | |
| 99 | if (!name.is<std::string>() || !region.is<std::string>() || !method.is<std::string>() || |
| 100 | !server_id.is<std::string>() || !game_id.is<std::string>() || !has_password.is<bool>() || |
| 101 | !player_count.is<double>() || !port.is<double>() || !in_game.is<bool>() || |
| 102 | !version.is<std::string>()) |
| 103 | { |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | NetPlaySession session; |
| 108 | session.name = name.to_str(); |
| 109 | session.region = region.to_str(); |
| 110 | session.game_id = game_id.to_str(); |
| 111 | session.server_id = server_id.to_str(); |
| 112 | session.method = method.to_str(); |
| 113 | session.version = version.to_str(); |
| 114 | session.has_password = has_password.get<bool>(); |
| 115 | session.player_count = static_cast<int>(player_count.get<double>()); |
| 116 | session.port = static_cast<int>(port.get<double>()); |
| 117 | session.in_game = in_game.get<bool>(); |
| 118 | |
| 119 | sessions.push_back(std::move(session)); |
| 120 | } |
| 121 | |
| 122 | return sessions; |
| 123 | } |
| 124 | |
| 125 | void NetPlayIndex::NotificationLoop() |
| 126 | { |
| 127 | while (!m_session_thread_exit_event.WaitFor(std::chrono::seconds(5))) |
| 128 | { |
| 129 | Common::HttpRequest request; |
| 130 | auto response = request.Get( |
| 131 | Config::Get(Config::NETPLAY_INDEX_URL) + "/v0/session/active?secret=" + m_secret + |
| 132 | "&player_count=" + std::to_string(m_player_count) + |
| 133 | "&game=" + request.EscapeComponent(m_game) + "&in_game=" + std::to_string(m_in_game), |
| 134 | {{"X-Is-Dolphin", "1"}}, Common::HttpRequest::AllowedReturnCodes::All); |
| 135 | |
| 136 | if (!response) |
| 137 | continue; |
| 138 | |
| 139 | auto json = ParseResponse(response.value()); |
| 140 | |
| 141 | if (!json) |
| 142 | { |
| 143 | m_last_error = "BAD_JSON"; |
| 144 | m_secret.clear(); |
| 145 | m_error_callback(); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | std::string status = json->get("status").to_str(); |
| 150 | |
| 151 | if (status != "OK") |
| 152 | { |
| 153 | m_last_error = std::move(status); |
| 154 | m_secret.clear(); |
| 155 | m_error_callback(); |
| 156 | return; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | bool NetPlayIndex::Add(const NetPlaySession& session) |
| 162 | { |
| 163 | Common::HttpRequest request; |
| 164 | auto response = request.Get( |
| 165 | Config::Get(Config::NETPLAY_INDEX_URL) + |
| 166 | "/v0/session/add?name=" + request.EscapeComponent(session.name) + |
| 167 | "®ion=" + request.EscapeComponent(session.region) + |
| 168 | "&game=" + request.EscapeComponent(session.game_id) + |
| 169 | "&password=" + std::to_string(session.has_password) + "&method=" + session.method + |
| 170 | "&server_id=" + session.server_id + "&in_game=" + std::to_string(session.in_game) + |
| 171 | "&port=" + std::to_string(session.port) + "&player_count=" + |
| 172 | std::to_string(session.player_count) + "&version=" + Common::GetScmDescStr(), |
| 173 | {{"X-Is-Dolphin", "1"}}, Common::HttpRequest::AllowedReturnCodes::All); |
| 174 | |
| 175 | if (!response.has_value()) |
| 176 | { |
| 177 | m_last_error = "NO_RESPONSE"; |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | auto json = ParseResponse(response.value()); |
| 182 | |
| 183 | if (!json) |
| 184 | { |
| 185 | m_last_error = "BAD_JSON"; |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | std::string status = json->get("status").to_str(); |
| 190 | |
| 191 | if (status != "OK") |
| 192 | { |
| 193 | m_last_error = std::move(status); |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | m_secret = json->get("secret").to_str(); |
| 198 | m_in_game = session.in_game; |
| 199 | m_player_count = session.player_count; |
| 200 | m_game = session.game_id; |
| 201 | |
| 202 | m_session_thread_exit_event.Set(); |
| 203 | if (m_session_thread.joinable()) |
| 204 | m_session_thread.join(); |
| 205 | m_session_thread_exit_event.Reset(); |
| 206 | |
| 207 | m_session_thread = std::thread([this] { NotificationLoop(); }); |
| 208 | |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | void NetPlayIndex::SetInGame(bool in_game) |
| 213 | { |
| 214 | m_in_game = in_game; |
| 215 | } |
| 216 | |
| 217 | void NetPlayIndex::SetPlayerCount(int player_count) |
| 218 | { |
| 219 | m_player_count = player_count; |
| 220 | } |
| 221 | |
| 222 | void NetPlayIndex::SetGame(std::string game) |
| 223 | { |
| 224 | m_game = std::move(game); |
| 225 | } |
| 226 | |
| 227 | void NetPlayIndex::Remove() |
| 228 | { |
| 229 | if (m_secret.empty()) |
| 230 | return; |
| 231 | |
| 232 | m_session_thread_exit_event.Set(); |
| 233 | |
| 234 | if (m_session_thread.joinable()) |
| 235 | m_session_thread.join(); |
| 236 | |
| 237 | // We don't really care whether this fails or not |
| 238 | Common::HttpRequest request; |
| 239 | request.Get(Config::Get(Config::NETPLAY_INDEX_URL) + "/v0/session/remove?secret=" + m_secret, |
| 240 | {{"X-Is-Dolphin", "1"}}, Common::HttpRequest::AllowedReturnCodes::All); |
| 241 | |
| 242 | m_secret.clear(); |
| 243 | } |
| 244 | |
| 245 | std::vector<std::pair<std::string, std::string>> NetPlayIndex::GetRegions() |
| 246 | { |
| 247 | return { |
| 248 | {"EA", _trans("East Asia")}, {"CN", _trans("China")}, {"EU", _trans("Europe")}, |
| 249 | {"NA", _trans("North America")}, {"SA", _trans("South America")}, {"OC", _trans("Oceania")}, |
| 250 | {"AF", _trans("Africa")}, |
| 251 | }; |
| 252 | } |
| 253 | |
| 254 | // This encryption system uses simple XOR operations and a checksum |
| 255 | // It isn't very secure but is preferable to adding another dependency on mbedtls |
| 256 | // The encrypted data is encoded as nibbles with the character 'A' as the base offset |
| 257 | |
| 258 | bool NetPlaySession::EncryptID(std::string_view password) |
| 259 | { |
| 260 | if (password.empty()) |
| 261 | return false; |
| 262 | |
| 263 | std::string to_encrypt = server_id; |
| 264 | |
| 265 | // Calculate and append checksum to ID |
| 266 | const u8 sum = std::accumulate(to_encrypt.begin(), to_encrypt.end(), u8{0}); |
| 267 | to_encrypt += sum; |
| 268 | |
| 269 | std::string encrypted_id; |
| 270 | |
| 271 | u8 i = 0; |
| 272 | for (const char byte : to_encrypt) |
| 273 | { |
| 274 | char c = byte ^ password[i % password.size()]; |
| 275 | c += i; |
| 276 | encrypted_id += 'A' + ((c & 0xF0) >> 4); |
| 277 | encrypted_id += 'A' + (c & 0x0F); |
| 278 | ++i; |
| 279 | } |
| 280 | |
| 281 | server_id = std::move(encrypted_id); |
| 282 | |
| 283 | return true; |
| 284 | } |
| 285 | |
| 286 | std::optional<std::string> NetPlaySession::DecryptID(std::string_view password) const |
| 287 | { |
| 288 | if (password.empty()) |
| 289 | return {}; |
| 290 | |
| 291 | // If the length of an encrypted session id is not divisible by two, it's invalid |
| 292 | if (server_id.empty() || server_id.size() % 2 != 0) |
| 293 | return {}; |
| 294 | |
| 295 | std::string decoded; |
| 296 | |
| 297 | for (size_t i = 0; i < server_id.size(); i += 2) |
| 298 | { |
| 299 | char c = (server_id[i] - 'A') << 4 | (server_id[i + 1] - 'A'); |
| 300 | decoded.push_back(c); |
| 301 | } |
| 302 | |
| 303 | u8 i = 0; |
| 304 | for (auto& c : decoded) |
| 305 | { |
| 306 | c -= i; |
| 307 | c ^= password[i % password.size()]; |
| 308 | ++i; |
| 309 | } |
| 310 | |
| 311 | // Verify checksum |
| 312 | const u8 expected_sum = decoded[decoded.size() - 1]; |
| 313 | |
| 314 | decoded.pop_back(); |
| 315 | |
| 316 | const u8 sum = std::accumulate(decoded.begin(), decoded.end(), u8{0}); |
| 317 | |
| 318 | if (sum != expected_sum) |
| 319 | return {}; |
| 320 | |
| 321 | return decoded; |
| 322 | } |
| 323 | |
| 324 | const std::string& NetPlayIndex::GetLastError() const |
| 325 | { |
| 326 | return m_last_error; |
| 327 | } |
| 328 | |
| 329 | bool NetPlayIndex::HasActiveSession() const |
| 330 | { |
| 331 | return !m_secret.empty(); |
| 332 | } |
| 333 | |
| 334 | void NetPlayIndex::SetErrorCallback(std::function<void()> callback) |
| 335 | { |
| 336 | m_error_callback = std::move(callback); |
| 337 | } |