-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathLCU.cpp
More file actions
330 lines (287 loc) · 9.41 KB
/
Copy pathLCU.cpp
File metadata and controls
330 lines (287 loc) · 9.41 KB
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
#include <json/json.h>
#include <thread>
#include "Utils.h"
#include "LCU.h"
[[maybe_unused]] static void FixCachingProblem();
std::string LCU::Request(const std::string& method, const std::string& endpoint, const std::string& body)
{
if (league.port == 0)
return "Not connected to League";
std::string sURL = endpoint;
if (sURL.find("https://127.0.0.1") == std::string::npos)
{
if (sURL.find("https://") == std::string::npos && sURL.find("http://") == std::string::npos)
{
while (sURL[0] == ' ')
sURL.erase(sURL.begin());
if (sURL[0] != '/')
sURL.insert(0, "/");
sURL.insert(0, "https://127.0.0.1:" + std::to_string(league.port));
}
}
else if (sURL.find("https://127.0.0.1:") == std::string::npos)
{
sURL.insert(strlen("https://127.0.0.1"), ":" + std::to_string(league.port));
}
cpr::Response r = {};
session.SetUrl(sURL);
session.SetBody(body);
if (const std::string upperMethod = Utils::ToUpper(method); upperMethod == "GET")
{
r = session.Get();
}
else if (upperMethod == "POST")
{
r = session.Post();
}
else if (upperMethod == "OPTIONS")
{
r = session.Options();
}
else if (upperMethod == "DELETE")
{
r = session.Delete();
}
else if (upperMethod == "PUT")
{
r = session.Put();
}
else if (upperMethod == "HEAD")
{
r = session.Head();
}
else if (upperMethod == "PATCH")
{
r = session.Patch();
}
return r.text;
}
bool LCU::SetRiotClientInfo(const ClientInfo& info)
{
riot = info;
if (riot.port == 0 || riot.token == "")
return false;
riot.header = Auth::MakeRiotHeader(info);
return true;
}
bool LCU::SetRiotClientInfo()
{
const auto riotClients = Auth::GetAllProcessIds(L"Riot Client.exe");
for (const DWORD& clientPid : riotClients)
{
const auto info = Auth::GetClientInfo(clientPid);
if (info.port == 0)
continue;
return SetRiotClientInfo(info);
}
return false;
}
bool LCU::SetLeagueClientInfo(const ClientInfo& info)
{
isCurrentRiotInfoSet = false;
league = info;
if (league.port == 0 || league.token.empty())
return false;
league.header = Auth::MakeLeagueHeader(info);
session = cpr::Session();
session.SetVerifySsl(false);
session.SetHeader(Utils::StringToHeader(league.header));
return true;
}
bool LCU::SetLeagueClientInfo()
{
if (!IsProcessGood())
return false;
return SetLeagueClientInfo(Auth::GetClientInfo(leagueProcesses[indexLeagueProcesses].first));
}
bool LCU::SetCurrentClientRiotInfo()
{
if (isCurrentRiotInfoSet)
return true;
const bool isSet = SetRiotClientInfo(Auth::GetClientInfo(leagueProcesses[indexLeagueProcesses].first, true));
if (isSet)
isCurrentRiotInfoSet = true;
return isSet;
}
void LCU::GetLeagueProcesses()
{
const std::vector<DWORD> allProcessIds = Auth::GetAllProcessIds(L"LeagueClientUx.exe");
// remove unexisting clients
for (size_t i = 0; i < leagueProcesses.size(); i++)
{
bool exists = false;
for (const DWORD& proc : allProcessIds)
{
if (proc == leagueProcesses[i].first)
{
exists = true;
break;
}
}
if (!exists)
{
leagueProcesses.erase(leagueProcesses.begin() + i);
if (i == indexLeagueProcesses)
SetLeagueClientInfo();
}
}
for (const DWORD& proc : allProcessIds)
{
int foundIndex = -1;
for (size_t i = 0; i < leagueProcesses.size(); i++)
{
if (leagueProcesses[i].first == proc)
{
foundIndex = static_cast<int>(i);
break;
}
}
if (foundIndex != -1 && !leagueProcesses[foundIndex].second.empty())
continue;
ClientInfo currentInfo = Auth::GetClientInfo(proc);
currentInfo.header = Auth::MakeLeagueHeader(currentInfo);
size_t currentIndex = foundIndex;
if (foundIndex == -1)
{
currentIndex = leagueProcesses.size();
std::pair<DWORD, std::string> temp = { proc, "" };
leagueProcesses.emplace_back(temp);
}
std::thread t([currentIndex, currentInfo]() {
short sessionFailCount = 0;
while (true)
{
cpr::Session session;
session.SetVerifySsl(false);
session.SetHeader(Utils::StringToHeader(currentInfo.header));
session.SetUrl(std::format("https://127.0.0.1:{}/lol-login/v1/session", currentInfo.port));
std::string procSession = session.Get().text;
// probably legacy client
if (procSession.find("errorCode") != std::string::npos)
{
sessionFailCount++;
if (sessionFailCount > 5)
{
leagueProcesses[currentIndex].second = "!FAILED!";
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(300));
continue;
}
Json::CharReaderBuilder builder;
const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
JSONCPP_STRING err;
Json::Value root;
if (reader->parse(procSession.c_str(), procSession.c_str() + static_cast<int>(procSession.length()), &root, &err))
{
std::string currentSummId = root["summonerId"].asString();
// player has summId when client is loaded
if (!currentSummId.empty())
{
session.SetUrl(std::format("https://127.0.0.1:{}/lol-summoner/v1/summoners/{}", currentInfo.port, currentSummId));
std::string currentSummoner = session.Get().text;
if (reader->parse(currentSummoner.c_str(), currentSummoner.c_str() + static_cast<int>(currentSummoner.length()), &root, &err))
{
leagueProcesses[currentIndex].second = std::string(root["gameName"].asString().substr(0, 25)) + "#" + std::string(root["tagLine"].asString());
break;
}
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(300));
}
});
t.detach();
}
}
bool LCU::IsProcessGood()
{
return !leagueProcesses.empty() && indexLeagueProcesses < leagueProcesses.size();
}
// TODO: sync with LCU header, it's almost the same
std::string LCU::GetStoreHeader()
{
std::string storeUrl = Request("GET", "/lol-store/v1/getStoreUrl");
std::erase(storeUrl, '"');
std::string accessToken = Request("GET", "/lol-rso-auth/v1/authorization/access-token");
Json::CharReaderBuilder builder;
const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
JSONCPP_STRING err;
Json::Value root;
if (reader->parse(accessToken.c_str(), accessToken.c_str() + static_cast<int>(accessToken.length()), &root, &err))
{
std::string storeToken = root["token"].asString();
std::string storeHost;
if (auto n = storeUrl.find("https://"); n != std::string::npos)
{
storeHost = storeUrl.substr(n + strlen("https://"));
}
std::string storeHeader = "Host: " + storeHost + "\r\n" +
"Connection: keep-alive\r\n" +
"AUTHORIZATION: Bearer " + storeToken + "\r\n" +
"Accept: application/json" + "\r\n" +
"Accept-Language: en-US,en;q=0.9" + "\r\n" +
"Content-Type: application/json" + "\r\n" +
"Origin: https://127.0.0.1:" + std::to_string(league.port) + "\r\n" +
"User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) LeagueOfLegendsClient/" + league.version +
" (CEF 91) Safari/537.36" + "\r\n" +
//X-B3-SpanId:
//X-B3-TraceId:
R"(sec-ch-ua: "Chromium";v="91")" + "\r\n" +
"sec-ch-ua-mobile: ?0" + "\r\n" +
"Sec-Fetch-Site: same-origin" + "\r\n" +
"Sec-Fetch-Mode: no-cors" + "\r\n" +
"Sec-Fetch-Dest: empty" + "\r\n" +
"Referer: https://127.0.0.1:" + std::to_string(league.port) + "\r\n" +
"Accept-Encoding: "/*gzip,*/ + "deflate, br";
return storeHeader;
}
return "";
}
/**
* \brief Fixes Wininet error 12158, #80 in GitHub issues
*/
static void FixCachingProblem()
{
using tRegOpenKeyExA = LSTATUS(WINAPI*)(HKEY hKey, LPCSTR lpSubKey, DWORD ulOptions,
REGSAM samDesired, PHKEY phkResult);
static auto RegOpenKeyExA = reinterpret_cast<tRegOpenKeyExA>(GetProcAddress(LoadLibraryW(L"advapi32.dll"), "RegOpenKeyExA"));
using tRegQueryValueExA = LSTATUS(WINAPI*)(HKEY hKey, LPCSTR lpValueName,
LPDWORD lpReserved, LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbDatan);
static auto RegQueryValueExA = reinterpret_cast<tRegQueryValueExA>(GetProcAddress(LoadLibraryW(L"advapi32.dll"), "RegQueryValueExA"));
using tRegSetValueExA = LSTATUS(WINAPI*)(HKEY hKey, LPCSTR lpValueName, DWORD Reserved,
DWORD dwType, const BYTE* lpData, DWORD cbData);
static auto RegSetValueExA = reinterpret_cast<tRegSetValueExA>(GetProcAddress(LoadLibraryW(L"advapi32.dll"), "RegSetValueExA"));
using tRegCloseKey = LSTATUS(WINAPI*)(HKEY hKe);
static auto RegCloseKey = reinterpret_cast<tRegCloseKey>(GetProcAddress(LoadLibraryW(L"advapi32.dll"), "RegCloseKey"));
bool bChanged = false;
HKEY hkResult;
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, R"(Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings)", 0, KEY_READ | KEY_WRITE,
&hkResult) == ERROR_SUCCESS)
{
DWORD value;
DWORD newValue = 0;
DWORD dwSize = sizeof(value);
if (const LSTATUS regQuery = RegQueryValueExA(hkResult, "DisableCachingOfSSLPages", nullptr, nullptr, reinterpret_cast<LPBYTE>(&value),
&dwSize); regQuery == ERROR_SUCCESS)
{
if (value == 0x1)
{
RegSetValueExA(hkResult, "DisableCachingOfSSLPages", 0, REG_DWORD, reinterpret_cast<LPBYTE>(&newValue), dwSize);
bChanged = true;
}
}
else if (regQuery == ERROR_FILE_NOT_FOUND) // if key doesnt exist, create it
{
RegSetValueExA(hkResult, "DisableCachingOfSSLPages", 0, REG_DWORD, reinterpret_cast<LPBYTE>(&newValue), dwSize);
bChanged = true;
}
RegCloseKey(hkResult);
}
if (bChanged == true)
{
MessageBoxA(nullptr, "Restart the program\n\nIf this pop-up window keeps showing up: Open \"Internet Options\", "
"Go to \"Advanced\" tab and disable \"Do not save encrypted pages to disk\". Press \"Apply\" and \"OK\"",
"Updated faulty options", MB_OK);
exit(EXIT_SUCCESS); // ConcurrencyMtUnsafe)
}
}