2013-03-01 16:58:05 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2013-03-02 07:11:34 +00:00
|
|
|
#include "Common/StdThread.h"
|
2013-03-01 16:58:05 +00:00
|
|
|
#include "Core/Config.h"
|
|
|
|
#include "Core/System.h"
|
|
|
|
|
|
|
|
#include "net/http_client.h"
|
|
|
|
#include "net/resolve.h"
|
|
|
|
#include "base/buffer.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string>
|
2013-03-02 07:11:34 +00:00
|
|
|
#include <cstdarg>
|
2013-03-02 06:34:02 +00:00
|
|
|
|
2013-03-01 16:58:05 +00:00
|
|
|
namespace Reporting
|
|
|
|
{
|
|
|
|
const int DEFAULT_PORT = 80;
|
2013-03-02 07:11:34 +00:00
|
|
|
const u32 SPAM_LIMIT = 100;
|
2013-03-02 06:34:02 +00:00
|
|
|
const int PAYLOAD_BUFFER_SIZE = 100;
|
2013-03-01 20:56:51 +00:00
|
|
|
|
|
|
|
// Internal limiter on number of requests per instance.
|
|
|
|
static u32 spamProtectionCount = 0;
|
|
|
|
// Temporarily stores a reference to the hostname.
|
|
|
|
static std::string lastHostname;
|
2013-03-01 16:58:05 +00:00
|
|
|
|
2013-03-02 06:34:02 +00:00
|
|
|
enum RequestType
|
|
|
|
{
|
|
|
|
MESSAGE,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Payload
|
|
|
|
{
|
|
|
|
RequestType type;
|
2013-03-04 07:36:24 +00:00
|
|
|
std::string string1;
|
|
|
|
std::string string2;
|
2013-03-02 06:34:02 +00:00
|
|
|
};
|
|
|
|
static Payload payloadBuffer[PAYLOAD_BUFFER_SIZE];
|
|
|
|
static int payloadBufferPos = 0;
|
|
|
|
|
2013-03-01 16:58:05 +00:00
|
|
|
static size_t ServerHostnameLength()
|
|
|
|
{
|
|
|
|
if (g_Config.sReportHost.empty())
|
|
|
|
return g_Config.sReportHost.npos;
|
|
|
|
|
|
|
|
// IPv6 literal?
|
|
|
|
if (g_Config.sReportHost[0] == '[')
|
|
|
|
{
|
|
|
|
size_t length = g_Config.sReportHost.find("]:");
|
|
|
|
if (length != g_Config.sReportHost.npos)
|
|
|
|
++length;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return g_Config.sReportHost.find(':');
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *ServerHostname()
|
|
|
|
{
|
|
|
|
if (g_Config.sReportHost.empty())
|
|
|
|
return NULL;
|
|
|
|
// Disabled by default for now.
|
|
|
|
if (g_Config.sReportHost.compare("default") == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
size_t length = ServerHostnameLength();
|
|
|
|
if (length == g_Config.sReportHost.npos)
|
|
|
|
return g_Config.sReportHost.c_str();
|
|
|
|
|
|
|
|
lastHostname = g_Config.sReportHost.substr(0, length);
|
|
|
|
return lastHostname.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ServerPort()
|
|
|
|
{
|
|
|
|
if (g_Config.sReportHost.empty())
|
|
|
|
return 0;
|
|
|
|
// Disabled by default for now.
|
|
|
|
if (g_Config.sReportHost.compare("default") == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
size_t offset = ServerHostnameLength();
|
|
|
|
if (offset == g_Config.sReportHost.npos)
|
|
|
|
return DEFAULT_PORT;
|
|
|
|
|
|
|
|
// Skip the colon.
|
|
|
|
std::string port = g_Config.sReportHost.substr(offset + 1);
|
|
|
|
return atoi(port.c_str());
|
|
|
|
}
|
|
|
|
|
2013-03-01 20:56:51 +00:00
|
|
|
// Should only be called once per request.
|
|
|
|
bool CheckSpamLimited()
|
|
|
|
{
|
|
|
|
return ++spamProtectionCount >= SPAM_LIMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SendReportRequest(const char *uri, const std::string &data, Buffer *output = NULL)
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
http::Client http;
|
|
|
|
Buffer theVoid;
|
|
|
|
|
|
|
|
if (output == NULL)
|
|
|
|
output = &theVoid;
|
|
|
|
|
|
|
|
net::Init();
|
|
|
|
if (http.Resolve(ServerHostname(), ServerPort()))
|
|
|
|
{
|
|
|
|
http.Connect();
|
|
|
|
http.POST("/report/message", data, "application/x-www-urlencoded", output);
|
|
|
|
http.Disconnect();
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
net::Shutdown();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-02 06:34:02 +00:00
|
|
|
int Process(int pos)
|
|
|
|
{
|
|
|
|
Payload &payload = payloadBuffer[pos];
|
|
|
|
|
|
|
|
const int PARAM_BUFFER_SIZE = 4096;
|
|
|
|
char temp[PARAM_BUFFER_SIZE];
|
|
|
|
|
|
|
|
// TODO: Need to escape these values, add more.
|
|
|
|
snprintf(temp, PARAM_BUFFER_SIZE - 1, "version=%s&game=%s_%s",
|
|
|
|
PPSSPP_GIT_VERSION,
|
|
|
|
g_paramSFO.GetValueString("DISC_ID").c_str(),
|
|
|
|
g_paramSFO.GetValueString("DISC_VERSION").c_str());
|
|
|
|
|
|
|
|
std::string data;
|
|
|
|
switch (payload.type)
|
|
|
|
{
|
|
|
|
case MESSAGE:
|
|
|
|
// TODO: Escape.
|
2013-03-04 07:36:24 +00:00
|
|
|
data = std::string(temp) + "&message=" + payload.string1 + "&value=" + payload.string2;
|
|
|
|
payload.string1.clear();
|
|
|
|
payload.string2.clear();
|
2013-03-02 06:34:02 +00:00
|
|
|
|
|
|
|
SendReportRequest("/report/message", data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-01 16:58:05 +00:00
|
|
|
void ReportMessage(const char *message, ...)
|
|
|
|
{
|
2013-03-01 20:56:51 +00:00
|
|
|
if (g_Config.sReportHost.empty() || CheckSpamLimited())
|
2013-03-01 16:58:05 +00:00
|
|
|
return;
|
|
|
|
// Disabled by default for now.
|
|
|
|
if (g_Config.sReportHost.compare("default") == 0)
|
|
|
|
return;
|
|
|
|
|
2013-03-01 17:51:10 +00:00
|
|
|
const int MESSAGE_BUFFER_SIZE = 32768;
|
|
|
|
char temp[MESSAGE_BUFFER_SIZE];
|
2013-03-01 16:58:05 +00:00
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, message);
|
2013-03-02 06:34:02 +00:00
|
|
|
vsnprintf(temp, MESSAGE_BUFFER_SIZE - 1, message, args);
|
|
|
|
temp[MESSAGE_BUFFER_SIZE - 1] = '\0';
|
2013-03-01 16:58:05 +00:00
|
|
|
va_end(args);
|
|
|
|
|
2013-03-02 06:34:02 +00:00
|
|
|
int pos = payloadBufferPos++ % PAYLOAD_BUFFER_SIZE;
|
|
|
|
Payload &payload = payloadBuffer[pos];
|
|
|
|
payload.type = MESSAGE;
|
2013-03-04 07:36:24 +00:00
|
|
|
payload.string1 = message;
|
|
|
|
payload.string2 = temp;
|
2013-03-02 06:34:02 +00:00
|
|
|
|
|
|
|
std::thread th(Process, pos);
|
|
|
|
th.detach();
|
2013-03-01 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|