moonlight-qt/app/http/nvhttp.cpp

263 lines
7 KiB
C++
Raw Normal View History

2018-04-28 22:39:50 +00:00
#include "nvhttp.h"
2018-04-29 07:55:18 +00:00
#include <QDebug>
2018-04-28 22:39:50 +00:00
#include <QUuid>
#include <QtNetwork/QNetworkReply>
2018-04-29 00:08:05 +00:00
#include <QEventLoop>
#include <QTimer>
#include <QXmlStreamReader>
2018-04-29 06:29:45 +00:00
#include <QSslKey>
2018-04-28 22:39:50 +00:00
2018-04-29 00:08:05 +00:00
#define REQUEST_TIMEOUT_MS 5000
2018-04-29 05:14:27 +00:00
NvHTTP::NvHTTP(QString address, IdentityManager im) :
m_Address(address),
m_Im(im)
2018-04-28 22:39:50 +00:00
{
m_BaseUrlHttp.setScheme("http");
m_BaseUrlHttps.setScheme("https");
m_BaseUrlHttp.setHost(address);
m_BaseUrlHttps.setHost(address);
m_BaseUrlHttp.setPort(47989);
m_BaseUrlHttps.setPort(47984);
}
2018-04-29 00:08:05 +00:00
NvComputer
NvHTTP::getComputerInfo()
{
NvComputer computer;
QString serverInfo = getServerInfo();
computer.m_Name = getXmlString(serverInfo, "hostname");
if (computer.m_Name == nullptr)
{
computer.m_Name = "UNKNOWN";
}
computer.m_Uuid = getXmlString(serverInfo, "uniqueid");
computer.m_MacAddress = getXmlString(serverInfo, "mac");
// If there's no LocalIP field, use the address we hit the server on
computer.m_LocalAddress = getXmlString(serverInfo, "LocalIP");
if (computer.m_LocalAddress == nullptr)
{
computer.m_LocalAddress = m_Address;
}
// If there's no ExternalIP field, use the address we hit the server on
computer.m_RemoteAddress = getXmlString(serverInfo, "ExternalIP");
if (computer.m_RemoteAddress == nullptr)
{
computer.m_RemoteAddress = m_Address;
}
computer.m_PairState = getXmlString(serverInfo, "PairStatus") == "1" ?
NvComputer::PS_PAIRED : NvComputer::PS_NOT_PAIRED;
computer.m_RunningGameId = getCurrentGame(serverInfo);
computer.m_State = NvComputer::CS_ONLINE;
return computer;
}
2018-04-29 02:01:00 +00:00
QVector<int>
NvHTTP::getServerVersionQuad(QString serverInfo)
{
QString quad = getXmlString(serverInfo, "appversion");
QStringList parts = quad.split(".");
QVector<int> ret;
for (int i = 0; i < 4; i++)
{
ret.append(parts.at(i).toInt());
}
2018-04-29 05:14:27 +00:00
return ret;
2018-04-29 02:01:00 +00:00
}
2018-04-29 00:08:05 +00:00
int
NvHTTP::getCurrentGame(QString serverInfo)
{
// GFE 2.8 started keeping currentgame set to the last game played. As a result, it no longer
// has the semantics that its name would indicate. To contain the effects of this change as much
// as possible, we'll force the current game to zero if the server isn't in a streaming session.
QString serverState = getXmlString(serverInfo, "state");
2018-04-29 02:01:00 +00:00
if (serverState != nullptr && serverState.endsWith("_SERVER_BUSY"))
{
2018-04-29 00:08:05 +00:00
return getXmlString(serverInfo, "currentgame").toInt();
}
2018-04-29 02:01:00 +00:00
else
{
2018-04-29 00:08:05 +00:00
return 0;
}
}
QString
NvHTTP::getServerInfo()
{
QString serverInfo;
try
{
// Always try HTTPS first, since it properly reports
// pairing status (and a few other attributes).
serverInfo = openConnectionToString(m_BaseUrlHttps,
"serverinfo",
nullptr,
true);
// Throws if the request failed
verifyResponseStatus(serverInfo);
}
2018-04-29 05:14:27 +00:00
catch (const GfeHttpResponseException& e)
2018-04-29 00:08:05 +00:00
{
if (e.getStatusCode() == 401)
{
// Certificate validation error, fallback to HTTP
2018-04-29 06:29:45 +00:00
serverInfo = openConnectionToString(m_BaseUrlHttp,
2018-04-29 00:08:05 +00:00
"serverinfo",
nullptr,
true);
verifyResponseStatus(serverInfo);
}
2018-04-29 05:14:27 +00:00
else
{
// Rethrow real errors
throw e;
}
2018-04-29 00:08:05 +00:00
}
return serverInfo;
}
void
NvHTTP::verifyResponseStatus(QString xml)
{
QXmlStreamReader xmlReader(xml);
while (xmlReader.readNextStartElement())
{
if (xmlReader.name() == "root")
{
int statusCode = xmlReader.attributes().value("status_code").toInt();
if (statusCode == 200)
{
// Successful
return;
}
else
{
QString statusMessage = xmlReader.attributes().value("status_message").toString();
qDebug() << "Request failed: " << statusCode << " " << statusMessage;
throw GfeHttpResponseException(statusCode, statusMessage);
}
}
}
}
2018-04-29 08:48:41 +00:00
QByteArray
NvHTTP::getXmlStringFromHex(QString xml,
QString tagName)
{
QString str = getXmlString(xml, tagName);
if (str == nullptr)
{
return nullptr;
}
return QByteArray::fromHex(str.toLatin1());
}
2018-04-29 00:08:05 +00:00
QString
NvHTTP::getXmlString(QString xml,
QString tagName)
{
QXmlStreamReader xmlReader(xml);
2018-04-29 07:55:18 +00:00
while (!xmlReader.atEnd())
2018-04-29 00:08:05 +00:00
{
2018-04-29 07:55:18 +00:00
if (xmlReader.readNext() != QXmlStreamReader::StartElement)
{
continue;
}
2018-04-29 00:08:05 +00:00
if (xmlReader.name() == tagName)
{
2018-04-29 07:55:18 +00:00
return xmlReader.readElementText();
2018-04-29 00:08:05 +00:00
}
}
return nullptr;
}
QString
NvHTTP::openConnectionToString(QUrl baseUrl,
QString command,
QString arguments,
bool enableTimeout)
{
QNetworkReply* reply = openConnection(baseUrl, command, arguments, enableTimeout);
QString ret;
ret = QTextStream(reply).readAll();
delete reply;
return ret;
}
2018-04-28 22:39:50 +00:00
QNetworkReply*
NvHTTP::openConnection(QUrl baseUrl,
QString command,
QString arguments,
bool enableTimeout)
{
2018-04-29 00:08:05 +00:00
// Build a URL for the request
2018-04-28 22:39:50 +00:00
QUrl url(baseUrl);
2018-04-29 05:14:27 +00:00
url.setPath("/" + command);
url.setQuery("uniqueid=" + m_Im.getUniqueId() +
"&uuid=" + QUuid::createUuid().toRfc4122().toHex() +
((arguments != nullptr) ? ("&" + arguments) : ""));
2018-04-28 22:39:50 +00:00
2018-04-29 06:29:45 +00:00
QNetworkRequest request = QNetworkRequest(url);
// Add our client certificate
2018-05-06 04:59:30 +00:00
request.setSslConfiguration(m_Im.getSslConfig());
2018-04-29 06:29:45 +00:00
QNetworkReply* reply = m_Nam.get(request);
2018-04-29 00:08:05 +00:00
// Ignore self-signed certificate errors (since GFE uses them)
2018-04-29 06:29:45 +00:00
reply->ignoreSslErrors();
2018-04-28 22:39:50 +00:00
2018-04-29 00:08:05 +00:00
// Run the request with a timeout if requested
QEventLoop loop;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
if (enableTimeout)
{
QTimer::singleShot(REQUEST_TIMEOUT_MS, &loop, SLOT(quit()));
}
qDebug() << "Executing request: " << url.toString();
loop.exec(QEventLoop::ExcludeUserInputEvents);
// Abort the request if it timed out
if (!reply->isFinished())
{
qDebug() << "Aborting timed out request for " << url.toString();
reply->abort();
}
2018-06-24 06:46:16 +00:00
// We must clear out cached authentication and connections or
// GFE will puke next time
m_Nam.clearAccessCache();
2018-04-29 00:08:05 +00:00
// Handle error
if (reply->error() != QNetworkReply::NoError)
{
2018-04-29 05:14:27 +00:00
qDebug() << command << " request failed with error " << reply->error();
GfeHttpResponseException exception(reply->error(), reply->errorString());
2018-04-29 00:08:05 +00:00
delete reply;
2018-04-29 02:01:00 +00:00
throw exception;
2018-04-29 00:08:05 +00:00
}
2018-04-28 22:39:50 +00:00
return reply;
}