summaryrefslogtreecommitdiff
path: root/Source/Core/UICommon/NetPlayIndex.cpp
blob: aaaa4549a8a0ddbc6a0d60308f32c57acdf17bfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2019 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "UICommon/NetPlayIndex.h"

#include <chrono>
#include <numeric>
#include <string>

#include <picojson.h>

#include "Common/Common.h"
#include "Common/HttpRequest.h"
#include "Common/Thread.h"
#include "Common/Version.h"

#include "Core/Config/NetplaySettings.h"

NetPlayIndex::NetPlayIndex() = default;

NetPlayIndex::~NetPlayIndex()
{
  if (!m_secret.empty())
    Remove();
}

static std::optional<picojson::value> ParseResponse(const std::vector<u8>& response)
{
  const std::string response_string(reinterpret_cast<const char*>(response.data()),
                                    response.size());

  picojson::value json;

  const auto error = picojson::parse(json, response_string);

  if (!error.empty())
    return {};

  return json;
}

std::optional<std::vector<NetPlaySession>>
NetPlayIndex::List(const std::map<std::string, std::string>& filters)
{
  Common::HttpRequest request;

  std::string list_url = Config::Get(Config::NETPLAY_INDEX_URL) + "/v0/list";

  if (!filters.empty())
  {
    list_url += '?';
    for (const auto& filter : filters)
    {
      list_url += filter.first + '=' + request.EscapeComponent(filter.second) + '&';
    }
    list_url.pop_back();
  }

  auto response =
      request.Get(list_url, {{"X-Is-Dolphin", "1"}}, Common::HttpRequest::AllowedReturnCodes::All);
  if (!response)
  {
    m_last_error = "NO_RESPONSE";
    return {};
  }

  auto json = ParseResponse(response.value());

  if (!json)
  {
    m_last_error = "BAD_JSON";
    return {};
  }

  const auto& status = json->get("status");

  if (status.to_str() != "OK")
  {
    m_last_error = status.to_str();
    return {};
  }

  const auto& entries = json->get("sessions");

  std::vector<NetPlaySession> sessions;

  for (const auto& entry : entries.get<picojson::array>())
  {
    const auto& name = entry.get("name");
    const auto& region = entry.get("region");
    const auto& method = entry.get("method");
    const auto& game_id = entry.get("game");
    const auto& server_id = entry.get("server_id");
    const auto& has_password = entry.get("password");
    const auto& player_count = entry.get("player_count");
    const auto& port = entry.get("port");
    const auto& in_game = entry.get("in_game");
    const auto& version = entry.get("version");

    if (!name.is<std::string>() || !region.is<std::string>() || !method.is<std::string>() ||
        !server_id.is<std::string>() || !game_id.is<std::string>() || !has_password.is<bool>() ||
        !player_count.is<double>() || !port.is<double>() || !in_game.is<bool>() ||
        !version.is<std::string>())
    {
      continue;
    }

    NetPlaySession session;
    session.name = name.to_str();
    session.region = region.to_str();
    session.game_id = game_id.to_str();
    session.server_id = server_id.to_str();
    session.method = method.to_str();
    session.version = version.to_str();
    session.has_password = has_password.get<bool>();
    session.player_count = static_cast<int>(player_count.get<double>());
    session.port = static_cast<int>(port.get<double>());
    session.in_game = in_game.get<bool>();

    sessions.push_back(std::move(session));
  }

  return sessions;
}

void NetPlayIndex::NotificationLoop()
{
  while (!m_session_thread_exit_event.WaitFor(std::chrono::seconds(5)))
  {
    Common::HttpRequest request;
    auto response = request.Get(
        Config::Get(Config::NETPLAY_INDEX_URL) + "/v0/session/active?secret=" + m_secret +
            "&player_count=" + std::to_string(m_player_count) +
            "&game=" + request.EscapeComponent(m_game) + "&in_game=" + std::to_string(m_in_game),
        {{"X-Is-Dolphin", "1"}}, Common::HttpRequest::AllowedReturnCodes::All);

    if (!response)
      continue;

    auto json = ParseResponse(response.value());

    if (!json)
    {
      m_last_error = "BAD_JSON";
      m_secret.clear();
      m_error_callback();
      return;
    }

    std::string status = json->get("status").to_str();

    if (status != "OK")
    {
      m_last_error = std::move(status);
      m_secret.clear();
      m_error_callback();
      return;
    }
  }
}

bool NetPlayIndex::Add(const NetPlaySession& session)
{
  Common::HttpRequest request;
  auto response = request.Get(
      Config::Get(Config::NETPLAY_INDEX_URL) +
          "/v0/session/add?name=" + request.EscapeComponent(session.name) +
          "&region=" + request.EscapeComponent(session.region) +
          "&game=" + request.EscapeComponent(session.game_id) +
          "&password=" + std::to_string(session.has_password) + "&method=" + session.method +
          "&server_id=" + session.server_id + "&in_game=" + std::to_string(session.in_game) +
          "&port=" + std::to_string(session.port) + "&player_count=" +
          std::to_string(session.player_count) + "&version=" + Common::GetScmDescStr(),
      {{"X-Is-Dolphin", "1"}}, Common::HttpRequest::AllowedReturnCodes::All);

  if (!response.has_value())
  {
    m_last_error = "NO_RESPONSE";
    return false;
  }

  auto json = ParseResponse(response.value());

  if (!json)
  {
    m_last_error = "BAD_JSON";
    return false;
  }

  std::string status = json->get("status").to_str();

  if (status != "OK")
  {
    m_last_error = std::move(status);
    return false;
  }

  m_secret = json->get("secret").to_str();
  m_in_game = session.in_game;
  m_player_count = session.player_count;
  m_game = session.game_id;

  m_session_thread_exit_event.Set();
  if (m_session_thread.joinable())
    m_session_thread.join();
  m_session_thread_exit_event.Reset();

  m_session_thread = std::thread([this] { NotificationLoop(); });

  return true;
}

void NetPlayIndex::SetInGame(bool in_game)
{
  m_in_game = in_game;
}

void NetPlayIndex::SetPlayerCount(int player_count)
{
  m_player_count = player_count;
}

void NetPlayIndex::SetGame(std::string game)
{
  m_game = std::move(game);
}

void NetPlayIndex::Remove()
{
  if (m_secret.empty())
    return;

  m_session_thread_exit_event.Set();

  if (m_session_thread.joinable())
    m_session_thread.join();

  // We don't really care whether this fails or not
  Common::HttpRequest request;
  request.Get(Config::Get(Config::NETPLAY_INDEX_URL) + "/v0/session/remove?secret=" + m_secret,
              {{"X-Is-Dolphin", "1"}}, Common::HttpRequest::AllowedReturnCodes::All);

  m_secret.clear();
}

std::vector<std::pair<std::string, std::string>> NetPlayIndex::GetRegions()
{
  return {
      {"EA", _trans("East Asia")},     {"CN", _trans("China")},         {"EU", _trans("Europe")},
      {"NA", _trans("North America")}, {"SA", _trans("South America")}, {"OC", _trans("Oceania")},
      {"AF", _trans("Africa")},
  };
}

// This encryption system uses simple XOR operations and a checksum
// It isn't very secure but is preferable to adding another dependency on mbedtls
// The encrypted data is encoded as nibbles with the character 'A' as the base offset

bool NetPlaySession::EncryptID(std::string_view password)
{
  if (password.empty())
    return false;

  std::string to_encrypt = server_id;

  // Calculate and append checksum to ID
  const u8 sum = std::accumulate(to_encrypt.begin(), to_encrypt.end(), u8{0});
  to_encrypt += sum;

  std::string encrypted_id;

  u8 i = 0;
  for (const char byte : to_encrypt)
  {
    char c = byte ^ password[i % password.size()];
    c += i;
    encrypted_id += 'A' + ((c & 0xF0) >> 4);
    encrypted_id += 'A' + (c & 0x0F);
    ++i;
  }

  server_id = std::move(encrypted_id);

  return true;
}

std::optional<std::string> NetPlaySession::DecryptID(std::string_view password) const
{
  if (password.empty())
    return {};

  // If the length of an encrypted session id is not divisble by two, it's invalid
  if (server_id.empty() || server_id.size() % 2 != 0)
    return {};

  std::string decoded;

  for (size_t i = 0; i < server_id.size(); i += 2)
  {
    char c = (server_id[i] - 'A') << 4 | (server_id[i + 1] - 'A');
    decoded.push_back(c);
  }

  u8 i = 0;
  for (auto& c : decoded)
  {
    c -= i;
    c ^= password[i % password.size()];
    ++i;
  }

  // Verify checksum
  const u8 expected_sum = decoded[decoded.size() - 1];

  decoded.pop_back();

  const u8 sum = std::accumulate(decoded.begin(), decoded.end(), u8{0});

  if (sum != expected_sum)
    return {};

  return decoded;
}

const std::string& NetPlayIndex::GetLastError() const
{
  return m_last_error;
}

bool NetPlayIndex::HasActiveSession() const
{
  return !m_secret.empty();
}

void NetPlayIndex::SetErrorCallback(std::function<void()> callback)
{
  m_error_callback = std::move(callback);
}