2018-04-21 20:51:18 +00:00
|
|
|
// Copyright (c) 2014- 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/.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
2020-09-29 10:19:22 +00:00
|
|
|
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/Net/HTTPClient.h"
|
|
|
|
#include "Common/Net/HTTPServer.h"
|
|
|
|
#include "Common/Net/Sinks.h"
|
2020-10-01 07:27:25 +00:00
|
|
|
#include "Common/Thread/ThreadUtil.h"
|
2020-09-29 10:19:22 +00:00
|
|
|
#include "Common/Log.h"
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/File/FileUtil.h"
|
|
|
|
#include "Common/File/FileDescriptor.h"
|
2020-08-15 18:53:08 +00:00
|
|
|
#include "Common/TimeUtil.h"
|
2020-09-29 10:19:22 +00:00
|
|
|
#include "Common/StringUtils.h"
|
2018-04-21 20:51:18 +00:00
|
|
|
#include "Core/Config.h"
|
2018-04-13 05:11:11 +00:00
|
|
|
#include "Core/Debugger/WebSocket.h"
|
2018-04-21 20:51:18 +00:00
|
|
|
#include "Core/WebServer.h"
|
|
|
|
|
|
|
|
enum class ServerStatus {
|
|
|
|
STOPPED,
|
|
|
|
STARTING,
|
|
|
|
RUNNING,
|
|
|
|
STOPPING,
|
2020-04-23 04:18:39 +00:00
|
|
|
FINISHED,
|
2018-04-21 20:51:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char *REPORT_HOSTNAME = "report.ppsspp.org";
|
|
|
|
static const int REPORT_PORT = 80;
|
|
|
|
|
|
|
|
static std::thread serverThread;
|
|
|
|
static ServerStatus serverStatus;
|
|
|
|
static std::mutex serverStatusLock;
|
|
|
|
static int serverFlags;
|
|
|
|
|
|
|
|
static void UpdateStatus(ServerStatus s) {
|
|
|
|
std::lock_guard<std::mutex> guard(serverStatusLock);
|
|
|
|
serverStatus = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ServerStatus RetrieveStatus() {
|
|
|
|
std::lock_guard<std::mutex> guard(serverStatusLock);
|
|
|
|
return serverStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This reports the local IP address to report.ppsspp.org, which can then
|
|
|
|
// relay that address to a mobile device searching for the server.
|
|
|
|
static void RegisterServer(int port) {
|
|
|
|
http::Client http;
|
|
|
|
Buffer theVoid;
|
|
|
|
|
|
|
|
char resource4[1024] = {};
|
|
|
|
if (http.Resolve(REPORT_HOSTNAME, REPORT_PORT, net::DNSType::IPV4)) {
|
|
|
|
if (http.Connect()) {
|
|
|
|
std::string ip = fd_util::GetLocalIP(http.sock());
|
|
|
|
snprintf(resource4, sizeof(resource4) - 1, "/match/update?local=%s&port=%d", ip.c_str(), port);
|
|
|
|
|
|
|
|
http.GET(resource4, &theVoid);
|
|
|
|
theVoid.Skip(theVoid.size());
|
|
|
|
http.Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (http.Resolve(REPORT_HOSTNAME, REPORT_PORT, net::DNSType::IPV6)) {
|
|
|
|
// We register both IPv4 and IPv6 in case the other client is using a different one.
|
|
|
|
if (resource4[0] != 0 && http.Connect()) {
|
|
|
|
http.GET(resource4, &theVoid);
|
|
|
|
theVoid.Skip(theVoid.size());
|
|
|
|
http.Disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently, we're not using keepalive, so gotta reconnect...
|
|
|
|
if (http.Connect()) {
|
|
|
|
char resource6[1024] = {};
|
|
|
|
std::string ip = fd_util::GetLocalIP(http.sock());
|
|
|
|
snprintf(resource6, sizeof(resource6) - 1, "/match/update?local=%s&port=%d", ip.c_str(), port);
|
|
|
|
|
|
|
|
http.GET(resource6, &theVoid);
|
|
|
|
theVoid.Skip(theVoid.size());
|
|
|
|
http.Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 21:53:38 +00:00
|
|
|
bool RemoteISOFileSupported(const std::string &filename) {
|
|
|
|
// Disc-like files.
|
|
|
|
if (endsWithNoCase(filename, ".cso") || endsWithNoCase(filename, ".iso")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// May work - but won't have supporting files.
|
|
|
|
if (endsWithNoCase(filename, ".pbp")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Debugging files.
|
|
|
|
if (endsWithNoCase(filename, ".ppdmp")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-25 19:55:10 +00:00
|
|
|
static std::string RemotePathForRecent(const std::string &filename) {
|
2018-04-21 20:51:18 +00:00
|
|
|
#ifdef _WIN32
|
2019-08-25 19:55:10 +00:00
|
|
|
static const std::string sep = "\\/";
|
2018-04-21 20:51:18 +00:00
|
|
|
#else
|
2019-08-25 19:55:10 +00:00
|
|
|
static const std::string sep = "/";
|
2018-04-21 20:51:18 +00:00
|
|
|
#endif
|
2019-08-25 19:55:10 +00:00
|
|
|
size_t basepos = filename.find_last_of(sep);
|
|
|
|
std::string basename = "/" + (basepos == filename.npos ? filename : filename.substr(basepos + 1));
|
|
|
|
|
|
|
|
if (basename == "/EBOOT.PBP") {
|
|
|
|
// Go up one more folder.
|
|
|
|
size_t nextpos = filename.find_last_of(sep, basepos - 1);
|
|
|
|
basename = "/" + (nextpos == filename.npos ? filename : filename.substr(nextpos + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's not serve directories, since they won't work. Only single files.
|
|
|
|
// Maybe can do PBPs and other files later. Would be neat to stream virtual disc filesystems.
|
|
|
|
if (RemoteISOFileSupported(basename)) {
|
|
|
|
return ReplaceAll(basename, " ", "%20");
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2018-04-21 20:51:18 +00:00
|
|
|
|
2019-08-25 19:55:10 +00:00
|
|
|
static std::string LocalFromRemotePath(const std::string &path) {
|
|
|
|
for (const std::string &filename : g_Config.recentIsos) {
|
|
|
|
std::string basename = RemotePathForRecent(filename);
|
|
|
|
if (basename == path) {
|
|
|
|
return filename;
|
2018-04-21 20:51:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-25 19:55:10 +00:00
|
|
|
return "";
|
|
|
|
}
|
2018-04-21 20:51:18 +00:00
|
|
|
|
2019-08-25 20:08:04 +00:00
|
|
|
static void DiscHandler(const http::Request &request, const std::string &filename) {
|
2019-08-25 19:55:10 +00:00
|
|
|
s64 sz = File::GetFileSize(filename);
|
|
|
|
|
|
|
|
std::string range;
|
|
|
|
if (request.Method() == http::RequestHeader::HEAD) {
|
|
|
|
request.WriteHttpResponseHeader("1.0", 200, sz, "application/octet-stream", "Accept-Ranges: bytes\r\n");
|
|
|
|
} else if (request.GetHeader("range", &range)) {
|
|
|
|
s64 begin = 0, last = 0;
|
|
|
|
if (sscanf(range.c_str(), "bytes=%lld-%lld", &begin, &last) != 2) {
|
|
|
|
request.WriteHttpResponseHeader("1.0", 400, -1, "text/plain");
|
|
|
|
request.Out()->Push("Could not understand range request.");
|
|
|
|
return;
|
|
|
|
}
|
2018-04-21 20:51:18 +00:00
|
|
|
|
2019-08-25 19:55:10 +00:00
|
|
|
if (begin < 0 || begin > last || last >= sz) {
|
|
|
|
request.WriteHttpResponseHeader("1.0", 416, -1, "text/plain");
|
|
|
|
request.Out()->Push("Range goes outside of file.");
|
|
|
|
return;
|
|
|
|
}
|
2018-04-21 20:51:18 +00:00
|
|
|
|
2019-08-25 19:55:10 +00:00
|
|
|
FILE *fp = File::OpenCFile(filename, "rb");
|
|
|
|
if (!fp || fseek(fp, begin, SEEK_SET) != 0) {
|
|
|
|
request.WriteHttpResponseHeader("1.0", 500, -1, "text/plain");
|
|
|
|
request.Out()->Push("File access failed.");
|
|
|
|
if (fp) {
|
|
|
|
fclose(fp);
|
2018-04-21 20:51:18 +00:00
|
|
|
}
|
2019-08-25 19:55:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-21 20:51:18 +00:00
|
|
|
|
2019-08-25 19:55:10 +00:00
|
|
|
s64 len = last - begin + 1;
|
|
|
|
char contentRange[1024];
|
|
|
|
sprintf(contentRange, "Content-Range: bytes %lld-%lld/%lld\r\n", begin, last, sz);
|
|
|
|
request.WriteHttpResponseHeader("1.0", 206, len, "application/octet-stream", contentRange);
|
|
|
|
|
|
|
|
const size_t CHUNK_SIZE = 16 * 1024;
|
|
|
|
char *buf = new char[CHUNK_SIZE];
|
|
|
|
for (s64 pos = 0; pos < len; pos += CHUNK_SIZE) {
|
|
|
|
s64 chunklen = std::min(len - pos, (s64)CHUNK_SIZE);
|
|
|
|
if (fread(buf, chunklen, 1, fp) != 1)
|
|
|
|
break;
|
|
|
|
request.Out()->Push(buf, chunklen);
|
2018-04-21 20:51:18 +00:00
|
|
|
}
|
2019-08-25 19:55:10 +00:00
|
|
|
fclose(fp);
|
|
|
|
delete[] buf;
|
|
|
|
request.Out()->Flush();
|
|
|
|
} else {
|
|
|
|
request.WriteHttpResponseHeader("1.0", 418, -1, "text/plain");
|
|
|
|
request.Out()->Push("This server only supports range requests.");
|
|
|
|
}
|
|
|
|
}
|
2018-04-21 20:51:18 +00:00
|
|
|
|
2019-08-25 20:08:04 +00:00
|
|
|
static void HandleListing(const http::Request &request) {
|
|
|
|
request.WriteHttpResponseHeader("1.0", 200, -1, "text/plain");
|
|
|
|
request.Out()->Printf("/\n");
|
|
|
|
if (serverFlags & (int)WebServerFlags::DISCS) {
|
|
|
|
// List the current discs in their recent order.
|
|
|
|
for (const std::string &filename : g_Config.recentIsos) {
|
|
|
|
std::string basename = RemotePathForRecent(filename);
|
|
|
|
if (!basename.empty()) {
|
|
|
|
request.Out()->Printf("%s\n", basename.c_str());
|
|
|
|
}
|
2019-08-25 19:55:10 +00:00
|
|
|
}
|
2018-04-21 20:51:18 +00:00
|
|
|
}
|
2019-08-25 20:08:04 +00:00
|
|
|
if (serverFlags & (int)WebServerFlags::DEBUGGER) {
|
|
|
|
request.Out()->Printf("/debugger\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleFallback(const http::Request &request) {
|
2019-08-25 20:14:49 +00:00
|
|
|
if (serverFlags & (int)WebServerFlags::DISCS) {
|
|
|
|
std::string filename = LocalFromRemotePath(request.resource());
|
|
|
|
if (!filename.empty()) {
|
|
|
|
DiscHandler(request, filename);
|
|
|
|
return;
|
|
|
|
}
|
2019-08-25 20:08:04 +00:00
|
|
|
}
|
2019-08-25 20:14:49 +00:00
|
|
|
|
|
|
|
static const std::string payload = "404 not found\r\n";
|
|
|
|
request.WriteHttpResponseHeader("1.0", 404, (int)payload.size(), "text/plain");
|
|
|
|
request.Out()->Push(payload);
|
2019-08-25 20:08:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ForwardDebuggerRequest(const http::Request &request) {
|
|
|
|
if (serverFlags & (int)WebServerFlags::DEBUGGER) {
|
|
|
|
HandleDebuggerRequest(request);
|
|
|
|
} else {
|
|
|
|
HandleFallback(request);
|
|
|
|
}
|
2018-04-21 20:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ExecuteWebServer() {
|
|
|
|
setCurrentThreadName("HTTPServer");
|
|
|
|
|
|
|
|
auto http = new http::Server(new threading::NewThreadExecutor());
|
2019-08-25 20:08:04 +00:00
|
|
|
http->RegisterHandler("/", &HandleListing);
|
|
|
|
// This lists all the (current) recent ISOs.
|
|
|
|
http->SetFallbackHandler(&HandleFallback);
|
|
|
|
http->RegisterHandler("/debugger", &ForwardDebuggerRequest);
|
2018-04-21 20:51:18 +00:00
|
|
|
|
|
|
|
if (!http->Listen(g_Config.iRemoteISOPort)) {
|
|
|
|
if (!http->Listen(0)) {
|
|
|
|
ERROR_LOG(FILESYS, "Unable to listen on any port");
|
2020-04-23 04:18:39 +00:00
|
|
|
UpdateStatus(ServerStatus::FINISHED);
|
2018-04-21 20:51:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UpdateStatus(ServerStatus::RUNNING);
|
|
|
|
|
|
|
|
g_Config.iRemoteISOPort = http->Port();
|
|
|
|
RegisterServer(http->Port());
|
2020-09-24 21:52:03 +00:00
|
|
|
double lastRegister = time_now_d();
|
2018-04-21 20:51:18 +00:00
|
|
|
while (RetrieveStatus() == ServerStatus::RUNNING) {
|
|
|
|
http->RunSlice(1.0);
|
|
|
|
|
2020-09-24 21:52:03 +00:00
|
|
|
double now = time_now_d();
|
2018-04-21 20:51:18 +00:00
|
|
|
if (now > lastRegister + 540.0) {
|
|
|
|
RegisterServer(http->Port());
|
|
|
|
lastRegister = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
http->Stop();
|
2018-04-21 22:33:34 +00:00
|
|
|
StopAllDebuggers();
|
|
|
|
delete http;
|
2018-04-21 20:51:18 +00:00
|
|
|
|
2020-04-23 04:18:39 +00:00
|
|
|
UpdateStatus(ServerStatus::FINISHED);
|
2018-04-21 20:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StartWebServer(WebServerFlags flags) {
|
|
|
|
std::lock_guard<std::mutex> guard(serverStatusLock);
|
|
|
|
switch (serverStatus) {
|
|
|
|
case ServerStatus::RUNNING:
|
|
|
|
if ((serverFlags & (int)flags) == (int)flags) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
serverFlags |= (int)flags;
|
|
|
|
return true;
|
|
|
|
|
2020-04-23 04:18:39 +00:00
|
|
|
case ServerStatus::FINISHED:
|
|
|
|
serverThread.join();
|
|
|
|
// Intentional fallthrough.
|
2018-04-21 20:51:18 +00:00
|
|
|
case ServerStatus::STOPPED:
|
|
|
|
serverStatus = ServerStatus::STARTING;
|
|
|
|
serverFlags = (int)flags;
|
|
|
|
serverThread = std::thread(&ExecuteWebServer);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StopWebServer(WebServerFlags flags) {
|
|
|
|
std::lock_guard<std::mutex> guard(serverStatusLock);
|
2019-08-25 20:14:49 +00:00
|
|
|
if (serverStatus != ServerStatus::RUNNING) {
|
2018-04-21 20:51:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
serverFlags &= ~(int)flags;
|
|
|
|
if (serverFlags == 0) {
|
|
|
|
serverStatus = ServerStatus::STOPPING;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebServerStopping(WebServerFlags flags) {
|
|
|
|
std::lock_guard<std::mutex> guard(serverStatusLock);
|
|
|
|
return serverStatus == ServerStatus::STOPPING;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebServerStopped(WebServerFlags flags) {
|
|
|
|
std::lock_guard<std::mutex> guard(serverStatusLock);
|
|
|
|
if (serverStatus == ServerStatus::RUNNING) {
|
|
|
|
return (serverFlags & (int)flags) == 0;
|
|
|
|
}
|
2020-04-23 04:18:39 +00:00
|
|
|
return serverStatus == ServerStatus::STOPPED || serverStatus == ServerStatus::FINISHED;
|
2018-04-21 20:51:18 +00:00
|
|
|
}
|
2019-09-28 18:12:12 +00:00
|
|
|
|
|
|
|
void ShutdownWebServer() {
|
|
|
|
StopWebServer(WebServerFlags::ALL);
|
2020-04-23 04:18:39 +00:00
|
|
|
|
|
|
|
if (serverStatus != ServerStatus::STOPPED)
|
2019-09-28 18:12:12 +00:00
|
|
|
serverThread.join();
|
2020-04-23 04:18:39 +00:00
|
|
|
serverStatus = ServerStatus::STOPPED;
|
2019-09-28 18:12:12 +00:00
|
|
|
}
|