moonlight-qt/app/backend/nvhttp.cpp

434 lines
13 KiB
C++
Raw Normal View History

2018-04-28 22:39:50 +00:00
#include "nvhttp.h"
2018-06-24 07:14:23 +00:00
#include <Limelight.h>
2018-04-28 22:39:50 +00:00
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-06-27 06:49:44 +00:00
#include <QImageReader>
#include <QtEndian>
2018-11-04 22:13:56 +00:00
#include <QNetworkProxy>
2018-04-28 22:39:50 +00:00
2018-04-29 00:08:05 +00:00
#define REQUEST_TIMEOUT_MS 5000
2018-06-27 02:01:40 +00:00
NvHTTP::NvHTTP(QString address) :
m_Address(address)
2018-04-28 22:39:50 +00:00
{
Q_ASSERT(!address.isEmpty());
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-11-04 22:13:56 +00:00
// Never use a proxy server
QNetworkProxy noProxy(QNetworkProxy::NoProxy);
m_Nam.setProxy(noProxy);
2018-04-28 22:39:50 +00:00
}
2018-04-29 02:01:00 +00:00
QVector<int>
2018-07-06 06:12:55 +00:00
NvHTTP::parseQuad(QString quad)
2018-04-29 02:01:00 +00:00
{
QVector<int> ret;
// Return an empty vector for old GFE versions
// that were missing GfeVersion.
if (quad.isEmpty()) {
return ret;
}
QStringList parts = quad.split(".");
2018-04-29 02:01:00 +00:00
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");
if (serverState != nullptr && serverState.endsWith("_SERVER_BUSY"))
2018-04-29 02:01:00 +00:00
{
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(NvLogLevel logLevel)
2018-04-29 00:08:05 +00:00
{
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,
logLevel);
2018-04-29 00:08:05 +00:00
// 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,
logLevel);
2018-04-29 00:08:05 +00:00
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;
}
2018-06-24 07:14:23 +00:00
static QString
getSurroundAudioInfoString(int audioConfig)
{
int channelMask;
int channelCount;
switch (audioConfig)
{
case AUDIO_CONFIGURATION_STEREO:
channelCount = 2;
channelMask = 0x3;
break;
case AUDIO_CONFIGURATION_51_SURROUND:
channelCount = 6;
channelMask = 0xFC;
break;
default:
Q_ASSERT(false);
return 0;
}
return QString::number(channelMask << 16 | channelCount);
}
void
NvHTTP::launchApp(int appId,
PSTREAM_CONFIGURATION streamConfig,
bool sops,
bool localAudio,
int gamepadMask)
{
2018-07-22 02:16:08 +00:00
int riKeyId;
memcpy(&riKeyId, streamConfig->remoteInputAesIv, sizeof(riKeyId));
riKeyId = qFromBigEndian(riKeyId);
2018-06-24 07:14:23 +00:00
QString response =
openConnectionToString(m_BaseUrlHttps,
"launch",
"appid="+QString::number(appId)+
"&mode="+QString::number(streamConfig->width)+"x"+
QString::number(streamConfig->height)+"x"+
QString::number(streamConfig->fps)+
"&additionalStates=1&sops="+QString::number(sops ? 1 : 0)+
"&rikey="+QByteArray(streamConfig->remoteInputAesKey, sizeof(streamConfig->remoteInputAesKey)).toHex()+
2018-07-22 02:16:08 +00:00
"&rikeyid="+QString::number(riKeyId)+
2018-06-24 07:14:23 +00:00
(streamConfig->enableHdr ?
"&hdrMode=1&clientHdrCapVersion=0&clientHdrCapSupportedFlagsInUint32=0&clientHdrCapMetaDataId=NV_STATIC_METADATA_TYPE_1&clientHdrCapDisplayData=0x0x0x0x0x0x0x0x0x0x0" :
"")+
"&localAudioPlayMode="+QString::number(localAudio ? 1 : 0)+
"&surroundAudioInfo="+getSurroundAudioInfoString(streamConfig->audioConfiguration)+
"&remoteControllersBitmap="+QString::number(gamepadMask)+
"&gcmap="+QString::number(gamepadMask),
false);
// Throws if the request failed
verifyResponseStatus(response);
}
void
NvHTTP::resumeApp(PSTREAM_CONFIGURATION streamConfig)
{
2018-07-22 02:16:08 +00:00
int riKeyId;
memcpy(&riKeyId, streamConfig->remoteInputAesIv, sizeof(riKeyId));
riKeyId = qFromBigEndian(riKeyId);
2018-06-24 07:14:23 +00:00
QString response =
openConnectionToString(m_BaseUrlHttps,
"resume",
"rikey="+QString(QByteArray(streamConfig->remoteInputAesKey, sizeof(streamConfig->remoteInputAesKey)).toHex())+
2018-07-22 02:16:08 +00:00
"&rikeyid="+QString::number(riKeyId)+
2018-06-24 07:14:23 +00:00
"&surroundAudioInfo="+getSurroundAudioInfoString(streamConfig->audioConfiguration),
false);
// Throws if the request failed
verifyResponseStatus(response);
}
void
NvHTTP::quitApp()
{
QString response =
openConnectionToString(m_BaseUrlHttps,
"cancel",
nullptr,
false);
// Throws if the request failed
verifyResponseStatus(response);
// Newer GFE versions will just return success even if quitting fails
// if we're not the original requestor.
if (getCurrentGame(getServerInfo(NvHTTP::ERROR)) != 0) {
2018-06-24 07:14:23 +00:00
// Generate a synthetic GfeResponseException letting the caller know
// that they can't kill someone else's stream.
throw GfeHttpResponseException(599, "");
}
}
QVector<NvDisplayMode>
NvHTTP::getDisplayModeList(QString serverInfo)
{
QXmlStreamReader xmlReader(serverInfo);
QVector<NvDisplayMode> modes;
while (!xmlReader.atEnd()) {
while (xmlReader.readNextStartElement()) {
QStringRef name = xmlReader.name();
if (xmlReader.name() == "DisplayMode") {
modes.append(NvDisplayMode());
}
else if (xmlReader.name() == "Width") {
modes.last().width = xmlReader.readElementText().toInt();
}
else if (xmlReader.name() == "Height") {
modes.last().height = xmlReader.readElementText().toInt();
}
else if (xmlReader.name() == "RefreshRate") {
modes.last().refreshRate = xmlReader.readElementText().toInt();
}
}
}
return modes;
}
2018-06-27 06:39:28 +00:00
QVector<NvApp>
NvHTTP::getAppList()
{
QString appxml = openConnectionToString(m_BaseUrlHttps,
"applist",
nullptr,
2018-08-06 00:03:05 +00:00
true,
NvLogLevel::ERROR);
2018-06-27 06:39:28 +00:00
verifyResponseStatus(appxml);
QXmlStreamReader xmlReader(appxml);
QVector<NvApp> apps;
while (!xmlReader.atEnd()) {
while (xmlReader.readNextStartElement()) {
QStringRef name = xmlReader.name();
if (xmlReader.name() == "App") {
// We must have a valid app before advancing to the next one
if (!apps.isEmpty() && !apps.last().isInitialized()) {
qWarning() << "Invalid applist XML";
Q_ASSERT(false);
return QVector<NvApp>();
}
apps.append(NvApp());
}
else if (xmlReader.name() == "AppTitle") {
apps.last().name = xmlReader.readElementText();
}
else if (xmlReader.name() == "ID") {
apps.last().id = xmlReader.readElementText().toInt();
}
else if (xmlReader.name() == "IsHdrSupported") {
apps.last().hdrSupported = xmlReader.readElementText() == "1";
}
}
}
return apps;
}
2018-04-29 00:08:05 +00:00
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();
if (statusCode != 401) {
// 401 is expected for unpaired PCs when we fetch serverinfo over HTTPS
qWarning() << "Request failed:" << statusCode << statusMessage;
}
2018-04-29 00:08:05 +00:00
throw GfeHttpResponseException(statusCode, statusMessage);
}
}
}
}
2018-06-27 06:49:44 +00:00
QImage
NvHTTP::getBoxArt(int appId)
{
QNetworkReply* reply = openConnection(m_BaseUrlHttps,
"appasset",
"appid="+QString::number(appId)+
"&AssetType=2&AssetIdx=0",
true,
NvLogLevel::VERBOSE);
2018-06-27 06:49:44 +00:00
QImage image = QImageReader(reply).read();
delete reply;
return image;
}
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,
NvLogLevel logLevel)
2018-04-29 00:08:05 +00:00
{
QNetworkReply* reply = openConnection(baseUrl, command, arguments, enableTimeout, logLevel);
2018-04-29 00:08:05 +00:00
QString ret;
2018-07-27 05:15:52 +00:00
QTextStream stream(reply);
stream.setCodec("UTF-8");
ret = stream.readAll();
2018-04-29 00:08:05 +00:00
delete reply;
return ret;
}
2018-04-28 22:39:50 +00:00
QNetworkReply*
NvHTTP::openConnection(QUrl baseUrl,
QString command,
QString arguments,
bool enableTimeout,
NvLogLevel logLevel)
2018-04-28 22:39:50 +00:00
{
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);
2018-06-27 02:01:40 +00:00
url.setQuery("uniqueid=" + IdentityManager::get()->getUniqueId() +
2018-04-29 05:14:27 +00:00
"&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-06-27 02:01:40 +00:00
request.setSslConfiguration(IdentityManager::get()->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()));
}
if (logLevel >= NvLogLevel::VERBOSE) {
qInfo() << "Executing request:" << url.toString();
}
2018-04-29 00:08:05 +00:00
loop.exec(QEventLoop::ExcludeUserInputEvents);
// Abort the request if it timed out
if (!reply->isFinished())
{
if (logLevel >= NvLogLevel::ERROR) {
qWarning() << "Aborting timed out request for" << url.toString();
}
2018-04-29 00:08:05 +00:00
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)
{
if (logLevel >= NvLogLevel::ERROR) {
qWarning() << command << " request failed with error " << reply->error();
}
QtNetworkReplyException 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;
}