mirror of
https://github.com/UltimMC/Launcher.git
synced 2025-12-24 12:32:42 +00:00
113 lines
4.7 KiB
C++
113 lines
4.7 KiB
C++
#include "AuthServer.h"
|
|
|
|
#include <QThread>
|
|
#include <QDebug>
|
|
#include <QTcpSocket>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
|
|
AuthServer::AuthServer(QObject *parent) : QObject(parent)
|
|
{
|
|
m_tcpServer.reset(new QTcpServer(this));
|
|
|
|
connect(m_tcpServer.get(), &QTcpServer::newConnection, this, &AuthServer::newConnection);
|
|
|
|
if (!m_tcpServer->listen(QHostAddress::LocalHost))
|
|
{
|
|
// TODO: think about stop launching when server start fails
|
|
qCritical() << "Auth server start failed";
|
|
}
|
|
}
|
|
|
|
quint16 AuthServer::port()
|
|
{
|
|
return m_tcpServer->serverPort();
|
|
}
|
|
|
|
void AuthServer::newConnection()
|
|
{
|
|
QTcpSocket *tcpSocket = m_tcpServer->nextPendingConnection();
|
|
|
|
connect(tcpSocket, &QTcpSocket::readyRead, this, [tcpSocket]()
|
|
{
|
|
// Not the best way to process queries, but it just works
|
|
QString rawRequest = tcpSocket->readAll().data();
|
|
QStringList requestLines = rawRequest.split("\r\n");
|
|
QString requestPath = requestLines[0].split(" ")[1];
|
|
|
|
int responseStatusCode = 500;
|
|
QString responseBody = "";
|
|
QStringList responseHeaders;
|
|
|
|
responseHeaders << "Connection: keep-alive";
|
|
|
|
if (requestPath == "/")
|
|
{
|
|
responseBody = "{\"Status\":\"OK\",\"Runtime-Mode\":\"productionMode\",\"Application-Author\":\"Mojang Web Force\",\"Application-Description\":\"Mojang Public API.\",\"Specification-Version\":\"3.58.0\",\"Application-Name\":\"yggdrasil.accounts.restlet.server.public\",\"Implementation-Version\":\"3.58.0_build194\",\"Application-Owner\":\"Mojang\"}";
|
|
responseStatusCode = 200;
|
|
responseHeaders << "Content-Type: application/json; charset=utf-8";
|
|
}
|
|
else if (requestPath == "/sessionserver/session/minecraft/join" || requestPath == "/sessionserver/session/minecraft/hasJoined")
|
|
{
|
|
responseStatusCode = 204;
|
|
}
|
|
else if (requestPath == "/auth/authenticate")
|
|
{
|
|
QString body = rawRequest.mid(rawRequest.indexOf("\r\n\r\n") + 4);
|
|
auto doc = QJsonDocument::fromJson(body.toUtf8());
|
|
auto json = doc.object();
|
|
QString clientToken = json.value("clientToken").toString();
|
|
QString username = json.value("username").toString();
|
|
|
|
QString profile = ((QString)"{\"id\":\"%1\",\"name\":\"%2\"}").arg(clientToken, username);
|
|
|
|
responseStatusCode = 200;
|
|
responseBody = ((QString)"{\"accessToken\":\"%1\",\"clientToken\":\"%2\",\"availableProfiles\":[%3], \"selectedProfile\": %3}").arg(username, clientToken, profile);
|
|
}
|
|
else if (requestPath == "/auth/refresh")
|
|
{
|
|
qDebug() << "Request process222";
|
|
QString body = rawRequest.mid(rawRequest.indexOf("\r\n\r\n") + 4);
|
|
auto doc = QJsonDocument::fromJson(body.toUtf8());
|
|
auto json = doc.object();
|
|
QString clientToken = json.value("clientToken").toString();
|
|
QString username = json.value("accessToken").toString();
|
|
|
|
|
|
QString profile = ((QString) "{\"id\":\"%1\",\"name\":\"%2\"}").arg(clientToken, username);
|
|
|
|
responseStatusCode = 200;
|
|
responseBody = ((QString) "{\"accessToken\":\"%1\",\"clientToken\":\"%2\",\"availableProfiles\":[%3], \"selectedProfile\": %3}").arg(username, clientToken, profile);
|
|
}
|
|
else
|
|
{
|
|
qDebug() << "Request failed" << requestPath;
|
|
responseBody = "Not found";
|
|
responseStatusCode = 404;
|
|
}
|
|
|
|
QString responseStatusText = "Internal Server Error";
|
|
if (responseStatusCode == 200)
|
|
responseStatusText = "OK";
|
|
else if (responseStatusCode == 204)
|
|
responseStatusText = "No Content";
|
|
else if (responseStatusCode == 404)
|
|
responseStatusText = "Not Found";
|
|
|
|
if (responseBody.length() != 0)
|
|
{
|
|
responseHeaders << ((QString) "Content-Length: %1").arg(responseBody.length());
|
|
}
|
|
qDebug() << responseBody;
|
|
|
|
tcpSocket->write(((QString) "HTTP/1.1 %1 %2\r\nConnection: keep-alive\r\n").arg(responseStatusCode).arg(responseStatusText).toUtf8());
|
|
tcpSocket->write(responseHeaders.join("\r\n").toUtf8());
|
|
tcpSocket->write("\r\n\r\n");
|
|
tcpSocket->write(responseBody.toUtf8());
|
|
});
|
|
connect(tcpSocket, &QTcpSocket::disconnected, this, [tcpSocket]()
|
|
{
|
|
tcpSocket->close();
|
|
});
|
|
}
|