This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiclient.cpp
365 lines (298 loc) · 10.9 KB
/
apiclient.cpp
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "apiclient.h"
#include "fillrand.h"
#include <QtEndian>
#include <QUrlQuery>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkCookie>
#include <QNetworkCookieJar>
QString VRChad::ApiClient::genAuthCookie(const QString& username, const QString& password)
{
return "Basic " + (QUrl::toPercentEncoding(username) + ":" + QUrl::toPercentEncoding(password)).toBase64();
}
QString VRChad::ApiClient::genHardwareId()
{
QByteArray bytes;
bytes.resize(42);
VRChad::Utils::FillRandHex(bytes.data(), bytes.size());
return bytes;
}
VRChad::ApiClient::ApiClient(QObject* parent)
: QObject(parent)
, m_baseUrl("https://api.vrchat.cloud")
, m_apiPath("/api/1/")
, m_networkManager(new QNetworkAccessManager(this))
, m_loginStatus(LoginStatus::LoggedOut)
, m_pendingAuth()
, m_hwid(genHardwareId())
, m_userAgent("VRC.Core.BestHTTP")
, m_unityVersion("2018.4.20f1")
, m_clientVersion()
, m_photonServerName()
, m_apiGotConfig(false)
, m_photonGotConfig(false)
, m_serverName()
, m_buildVersionTag()
, m_token_apiKey()
, m_token_2fa()
, m_token_auth()
, m_updateRateMsMaximum()
, m_updateRateMsMinimum()
, m_updateRateMsNormal()
, m_updateRateMsUdonManual()
, m_releaseServerVersionStandalone()
, m_photonAuthToken()
, m_currentUserId()
, m_currentAvatarId()
{
}
VRChad::ApiClient::LoginStatus VRChad::ApiClient::loginStatus() const
{
return m_loginStatus;
}
QString VRChad::ApiClient::currentHwid() const
{
return m_hwid;
}
QString VRChad::ApiClient::currentUserId() const
{
return m_currentUserId;
}
QString VRChad::ApiClient::photonAuthToken() const
{
return m_photonAuthToken;
}
QString VRChad::ApiClient::clientVersion() const
{
return m_clientVersion;
}
QString VRChad::ApiClient::photonServerName() const
{
return m_photonServerName;
}
void VRChad::ApiClient::login(QString authCookie)
{
if (loginStatus() == LoginStatus::LoggedOut) {
setLoginStatus(LoginStatus::LoggingIn);
m_pendingAuth = authCookie;
ensureConfigs(std::bind(&VRChad::ApiClient::apiGetLogin, this));
}
}
void VRChad::ApiClient::setLoginStatus(VRChad::ApiClient::LoginStatus status)
{
if (m_loginStatus != status) {
m_loginStatus = status;
emit loginStatusChanged(status);
}
}
QNetworkRequest VRChad::ApiClient::createApiRequest(const QString& ext, HttpContentType contentType, std::uint32_t contentLength, bool addQueries)
{
QUrl url = m_baseUrl.resolved(m_apiPath + ext);
if (addQueries) {
QUrlQuery query;
query.addQueryItem("organization", "vrchat");
if (!m_token_apiKey.isEmpty()) {
query.addQueryItem("apiKey", m_token_apiKey);
}
url.setQuery(query);
}
QNetworkRequest req(url);
const char* contentTypeStr;
switch (contentType) {
case HttpContentType::UrlEncoded:
contentTypeStr = "application/x-www-form-urlencoded";
break;
case HttpContentType::Json:
contentTypeStr = "application/json";
break;
}
// Set headers
req.setHeader(QNetworkRequest::UserAgentHeader, m_userAgent.toUtf8());
req.setHeader(QNetworkRequest::ContentTypeHeader, contentTypeStr);
req.setHeader(QNetworkRequest::ContentLengthHeader, contentLength);
req.setRawHeader("Accept", "*/*");
req.setRawHeader("Accept-Encoding", "identity");
//req.setRawHeader("X-MacAddress", m_hwid);
return req;
}
void VRChad::ApiClient::ensureConfigs(std::function<void()> needsConfigs)
{
if (!m_photonGotConfig) {
getPhotonConfig(needsConfigs);
return;
}
if (!m_apiGotConfig) {
apiGetConfig(needsConfigs);
return;
}
needsConfigs();
}
void VRChad::ApiClient::getPhotonConfig(std::function<void()> needsConfig)
{
QNetworkReply* reply = m_networkManager->get(QNetworkRequest (QUrl("https://api.ripper.store/config")));
connect(reply, &QNetworkReply::finished, [this, reply, needsConfig]() {
auto data = reply->readAll();
auto doc = QJsonDocument::fromJson(data);
if (doc.isObject()) {
m_clientVersion = doc["x_client_version"].toString();
m_photonServerName = doc["photon_server"].toString();
m_photonGotConfig = true;
qDebug() << "Got Photon config!";
emit gotPhotonConfig();
ensureConfigs(needsConfig);
}
else {
m_photonGotConfig = false;
qDebug() << "Failed to parse photonconfig response!";
qDebug() << data;
}
});
connect(reply, &QNetworkReply::sslErrors, this, &VRChad::ApiClient::onSslError);
connect(reply, &QNetworkReply::errorOccurred, this, &VRChad::ApiClient::onNetworkError);
}
void VRChad::ApiClient::apiGetConfig(std::function<void()> needsConfig)
{
auto req = createApiRequest("config", HttpContentType::UrlEncoded, 0, true);
QNetworkReply* reply = m_networkManager->get(req);
connect(reply, &QNetworkReply::finished, [this, reply, needsConfig]() {
auto data = reply->readAll();
auto doc = QJsonDocument::fromJson(data);
if (doc.isObject()) {
m_token_apiKey = doc["apiKey"].toString();
m_serverName = doc["serverName"].toString();
m_buildVersionTag = doc["buildVersionTag"].toString();
m_updateRateMsMaximum = doc["updateRateMsMaximum"].toInt();
m_updateRateMsMinimum = doc["updateRateMsMinimum"].toInt();
m_updateRateMsNormal = doc["updateRateMsNormal"].toInt();
m_updateRateMsUdonManual = doc["updateRateMsUdonManual"].toInt();
m_releaseServerVersionStandalone = doc["releaseServerVersionStandalone"].toString();
m_networkManager->cookieJar()->updateCookie(QNetworkCookie("apiKey", m_token_apiKey.toUtf8()));
m_apiGotConfig = true;
qDebug() << "Got API config!";
emit gotApiConfig();
ensureConfigs(needsConfig);
}
else {
m_apiGotConfig = false;
qDebug() << "Failed to parse json response!";
qDebug() << data;
}
});
connect(reply, &QNetworkReply::sslErrors, this, &VRChad::ApiClient::onSslError);
connect(reply, &QNetworkReply::errorOccurred, this, &VRChad::ApiClient::onNetworkError);
}
void VRChad::ApiClient::apiGetLogin()
{
if (m_pendingAuth.isEmpty()) {
return;
}
m_token_auth.clear();
auto req = createApiRequest("auth/user", HttpContentType::UrlEncoded, 0, true);
req.setRawHeader("Authorization", m_pendingAuth.toUtf8());
QNetworkReply* reply = m_networkManager->get(req);
connect(reply, &QNetworkReply::finished, [this, reply]() {
auto data = reply->readAll();
auto doc = QJsonDocument::fromJson(data);
if (doc.isObject()) {
auto err = doc["error"];
if (!err.isObject()) {
m_token_auth = m_pendingAuth;
m_pendingAuth.clear();
QVariant cookieVar = reply->header(QNetworkRequest::SetCookieHeader);
if (cookieVar.isValid()) {
QList<QNetworkCookie> cookies = cookieVar.value<QList<QNetworkCookie>>();
foreach (QNetworkCookie cookie, cookies) {
if (cookie.name() == "auth") {
m_photonAuthToken = cookie.value();
break;
}
}
}
m_currentUserId = doc["id"].toString();
m_currentAvatarId = doc["currentAvatar"].toString();
setLoginStatus(LoginStatus::LoggedIn);
apiGetUserInfo(m_currentUserId);
}
else {
qDebug() << QString("[%1] error: %2").arg(err["status_code"].toInt()).arg(err["message"].toString());
setLoginStatus(LoginStatus::LoggedOut);
}
}
else {
qDebug() << "Failed to parse json response!";
qDebug() << data;
setLoginStatus(LoginStatus::LoggedOut);
}
});
connect(reply, &QNetworkReply::sslErrors, this, &VRChad::ApiClient::onSslError);
connect(reply, &QNetworkReply::errorOccurred, this, &VRChad::ApiClient::onNetworkError);
}
void VRChad::ApiClient::apiGetUserInfo(const QString& userId)
{
auto req = createApiRequest("users/" + userId, HttpContentType::UrlEncoded, 0, true);
QNetworkReply* reply = m_networkManager->get(req);
connect(reply, &QNetworkReply::finished, [this, reply, userId]() {
auto data = reply->readAll();
auto doc = QJsonDocument::fromJson(data);
if (doc.isObject()) {
qDebug() << "Got user info!";
}
else {
qDebug() << "Failed to parse json response!";
qDebug() << data;
}
});
connect(reply, &QNetworkReply::sslErrors, this, &VRChad::ApiClient::onSslError);
connect(reply, &QNetworkReply::errorOccurred, this, &VRChad::ApiClient::onNetworkError);
}
void VRChad::ApiClient::apiGetWorldMetadata(const QString& worldId)
{
auto req = createApiRequest("worlds/" + worldId + "/metadata", HttpContentType::UrlEncoded, 0, true);
QNetworkReply* reply = m_networkManager->get(req);
connect(reply, &QNetworkReply::finished, [this, reply, worldId]() {
auto data = reply->readAll();
auto doc = QJsonDocument::fromJson(data);
if (doc.isObject()) {
qDebug() << "Got world metadata!";
}
else {
qDebug() << "Failed to parse json response!";
qDebug() << data;
}
});
connect(reply, &QNetworkReply::sslErrors, this, &VRChad::ApiClient::onSslError);
connect(reply, &QNetworkReply::errorOccurred, this, &VRChad::ApiClient::onNetworkError);
}
void VRChad::ApiClient::apiPutPingWorld(const QString& userId, const QString& worldId)
{
QJsonObject obj;
obj.insert("userId", userId);
obj.insert("worldId", worldId);
QByteArray json = QJsonDocument(obj).toJson(QJsonDocument::Compact);
auto req = createApiRequest("visits", HttpContentType::Json, json.length(), true);
QNetworkReply* reply = m_networkManager->put(req, json);
connect(reply, &QNetworkReply::finished, [this, reply, userId]() {
auto data = reply->readAll();
auto doc = QJsonDocument::fromJson(data);
if (doc.isObject()) {
qDebug() << "World ping success!";
}
else {
qDebug() << "Failed to parse json response!";
qDebug() << data;
}
});
connect(reply, &QNetworkReply::sslErrors, this, &VRChad::ApiClient::onSslError);
connect(reply, &QNetworkReply::errorOccurred, this, &VRChad::ApiClient::onNetworkError);
}
void VRChad::ApiClient::onSslError(const QList<QSslError>& errors)
{
for (const QSslError& err : errors)
{
qDebug() << "SslError:" << err;
}
}
void VRChad::ApiClient::onNetworkError(QNetworkReply::NetworkError err)
{
qDebug() << "NetworkError:" << err;
}