diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d0039c..713a581 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,8 @@ cmake_minimum_required(VERSION 3.5) -set(CMAKE_TOOLCHAIN_FILE "$ENV{OPENORBIS}/cmake/daemon.cmake") - project(ezremote-server) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDONT_HAVE_STRUPR") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDONT_HAVE_STRUPR -fno-builtin") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpermissive") set(APP_VERSION "1.00") @@ -27,24 +25,20 @@ add_executable(${PROJECT_NAME}.elf source/clients/webdav.cpp source/config.cpp source/crypt.c + source/dbglogger.c source/fs.cpp - source/getentropy.c - source/orbis_jbc.c + source/memccpy.c source/main.cpp) -add_self(${PROJECT_NAME}.elf) - -add_pkg(${PROJECT_NAME}.elf ${CMAKE_SOURCE_DIR}/data "EZSR00001" "ezRemote Server" "01.00" 32 0) - target_compile_definitions(${PROJECT_NAME}.elf PRIVATE EZREMOTE_VERSION=${APP_VERSION} CPPHTTPLIB_THREAD_POOL_COUNT=64) target_link_libraries(${PROJECT_NAME}.elf - dbglogger z + c + c++ crypto ssl json-c - jbc smb2 ssh2 nfs diff --git a/data/sce_sys/icon0.png b/data/sce_sys/icon0.png deleted file mode 100644 index 7e3de07..0000000 Binary files a/data/sce_sys/icon0.png and /dev/null differ diff --git a/source/clients/baseclient.cpp b/source/clients/baseclient.cpp index 78f47d3..77c7675 100644 --- a/source/clients/baseclient.cpp +++ b/source/clients/baseclient.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include "clients/remote_client.h" #include "clients/baseclient.h" #include "config.h" @@ -91,7 +90,6 @@ int BaseClient::Get(const std::string &outputfile, const std::string &path, uint { file_stream.write(data, data_length); *g_bytes_transfered = *g_bytes_transfered + data_length; - sceSystemServicePowerTick(); return true; })) { @@ -119,7 +117,6 @@ int BaseClient::GetRange(const std::string &path, DataSink &sink, uint64_t size, { bytes_read += data_length; bool ok = sink.write(data, data_length); - sceSystemServicePowerTick(); return ok; })) { diff --git a/source/clients/nfsclient.cpp b/source/clients/nfsclient.cpp index 30012db..8bf2f12 100644 --- a/source/clients/nfsclient.cpp +++ b/source/clients/nfsclient.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include "clients/nfsclient.h" #include "config.h" #include "fs.h" @@ -124,7 +123,7 @@ int NfsClient::Get(const std::string &outputfile, const std::string &ppath, uint nfs_lseek(nfs, nfsfh, offset, SEEK_SET, NULL); } - while ((count = nfs_read(nfs, nfsfh, BUF_SIZE, buff)) > 0) + while ((count = nfs_read(nfs, nfsfh, buff, BUF_SIZE)) > 0) { if (count < 0) { @@ -136,7 +135,6 @@ int NfsClient::Get(const std::string &outputfile, const std::string &ppath, uint } FS::Write(out, buff, count); *g_bytes_transfered += count; - sceSystemServicePowerTick(); } FS::Close(out); @@ -179,7 +177,7 @@ int NfsClient::GetRange(void *fp, DataSink &sink, uint64_t size, uint64_t offset do { size_t bytes_to_read = std::min(BUF_SIZE, bytes_remaining); - count = nfs_read(nfs, nfsfh, bytes_to_read, buff); + count = nfs_read(nfs, nfsfh, buff, bytes_to_read); if (count > 0) { bytes_remaining -= count; diff --git a/source/clients/sftpclient.cpp b/source/clients/sftpclient.cpp index 3459e25..57e6781 100644 --- a/source/clients/sftpclient.cpp +++ b/source/clients/sftpclient.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include "clients/remote_client.h" #include "clients/sftpclient.h" #include "fs.h" @@ -225,7 +224,6 @@ int SFTPClient::Get(const std::string &outputfile, const std::string &path, uint { *g_bytes_transfered += rc; FS::Write(out, buff, rc); - sceSystemServicePowerTick(); } else { diff --git a/source/clients/smbclient.cpp b/source/clients/smbclient.cpp index e784ddd..8db3f72 100644 --- a/source/clients/smbclient.cpp +++ b/source/clients/smbclient.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include "config.h" #include "fs.h" #include "clients/smbclient.h" @@ -135,7 +134,6 @@ int SmbClient::Get(const std::string &outputfile, const std::string &ppath, uint } FS::Write(out, buff, count); *g_bytes_transfered += count; - sceSystemServicePowerTick(); } FS::Close(out); diff --git a/source/config.h b/source/config.h index d974019..8f08db1 100644 --- a/source/config.h +++ b/source/config.h @@ -15,7 +15,6 @@ #define PKG_INSTALL_HISTORY_PATH DATA_PATH "/pkg_install_history.json" #define BG_DOWNLOAD_HISTORY_PATH DATA_PATH "/bg_download_history.json" #define DEBUG_SERVER_LOG_PATH DATA_PATH "/ezremote_server.log" -#define NOTIFY_ICON_FILE "/user" DATA_PATH "/sce_sys/icon0.png" #define CLIENT_ELF_PATH DATA_PATH "/ezremote_client.elf" #define HTTP_SERVER_APACHE "Apache" diff --git a/source/http/httplib.h b/source/http/httplib.h index 45fd248..1ef3a2a 100644 --- a/source/http/httplib.h +++ b/source/http/httplib.h @@ -173,6 +173,7 @@ using socket_t = SOCKET; #include #if !defined(_AIX) && !defined(__MVS__) +#include #include #endif #ifdef __MVS__ diff --git a/source/main.cpp b/source/main.cpp index 26fec91..613ed26 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -7,59 +7,93 @@ #include #include #include -#include -#include -#include -#include #include "server/http_server.h" #include "config.h" #include "util.h" -#include "dbglogger.h" extern "C" { -#include "orbis_jbc.h" + int sceNetInit(void); + int sceNetPoolCreate(const char *name, int size, int flags); + int sceNetPoolDestroy(int memid); + int sceNetTerm(void); + int sceKernelSendNotificationRequest(int, void *, size_t, int); } -static void terminate() +static int g_libnet_mem_id = -1; +static volatile sig_atomic_t g_running = 1; + +static int NetInit(void) { - terminate_jbc(); - sceSystemServiceLoadExec("exit", NULL); + if (sceNetInit() != 0) + { + errno = EIO; + return -1; + } + + g_libnet_mem_id = sceNetPoolCreate("nanodns", 5 * 1024 * 1024, 0); + if (g_libnet_mem_id < 0) + { + errno = EIO; + if (sceNetTerm() != 0) + { + errno = EIO; + } + return -1; + } + + return 0; +} + +static void NetTerm(void) +{ + if (g_libnet_mem_id >= 0) + { + if (sceNetPoolDestroy(g_libnet_mem_id) != 0) + { + errno = EIO; + } + g_libnet_mem_id = -1; + } + + if (sceNetTerm() != 0) + { + errno = EIO; + } +} + +static void OnSignal(int signo) +{ + (void)signo; + g_running = 0; } int main(int argc, char *argv[]) { - dbglogger_init(); - dbglogger_log("If you see this you've set up dbglogger correctly."); + signal(SIGINT, OnSignal); + signal(SIGTERM, OnSignal); - if (!initialize_jbc()) + if (NetInit() != 0) { - terminate(); + NetTerm(); + return -1; } - atexit(terminate); - - if (sceSysmoduleLoadModuleInternal(ORBIS_SYSMODULE_INTERNAL_SYSTEM_SERVICE) < 0) return 0; - if (sceSysmoduleLoadModuleInternal(ORBIS_SYSMODULE_INTERNAL_USER_SERVICE) < 0) return 0; - if (sceSysmoduleLoadModuleInternal(ORBIS_SYSMODULE_INTERNAL_NET) < 0 || sceNetInit() != 0) return 0; - CONFIG::LoadPackageInstallHostData(); CONFIG::LoadBgDownloadData(); if (HttpServer::IsStarted()) { Util::Notify("ezRemote Server already started"); - terminate(); + NetTerm(); return 0; } - dbglogger_log(" Registering Daemon..."); - sceSystemServiceRegisterDaemon(); - HttpServer::StartDownloadThread(); HttpServer::Start(); Util::Notify("ezRemote Server stopped."); + NetTerm(); return 0; } diff --git a/source/memccpy.c b/source/memccpy.c new file mode 100644 index 0000000..8d960fb --- /dev/null +++ b/source/memccpy.c @@ -0,0 +1,15 @@ +#include + +void *memccpy(void *restrict dest, const void *restrict src, int c, size_t n) { + unsigned char *d = dest; + const unsigned char *s = src; + unsigned char ch = (unsigned char)c; + + for (size_t i = 0; i < n; i++) { + d[i] = s[i]; + if (s[i] == ch) { + return d + i + 1; + } + } + return NULL; +} \ No newline at end of file diff --git a/source/server/http_server.cpp b/source/server/http_server.cpp index 207115c..eb26eeb 100644 --- a/source/server/http_server.cpp +++ b/source/server/http_server.cpp @@ -217,7 +217,7 @@ namespace HttpServer CONFIG::SaveBgDownloadData(); snprintf(temp_file, sizeof(temp_file), "%s.tmp", bg_download_list[i].dest_path.c_str()); - Util::RichNotify(bg_download_list[i].id, "Started download %s", bg_download_list[i].dest_path.c_str()); + Util::Notify("Started download %s", bg_download_list[i].dest_path.c_str()); ret = tmp_client->Get(temp_file, bg_download_list[i].src_path); @@ -225,11 +225,11 @@ namespace HttpServer if (ret == 0) { bg_download_list[i].state = STATE_FAILED; - Util::RichNotify(bg_download_list[i].id, "Failed to download %s", bg_download_list[i].dest_path.c_str()); + Util::Notify("Failed to download %s", bg_download_list[i].dest_path.c_str()); } else { - Util::RichNotify(bg_download_list[i].id, "Completed download %s", bg_download_list[i].dest_path.c_str()); + Util::Notify("Completed download %s", bg_download_list[i].dest_path.c_str()); bg_download_list[i].state = STATE_SUCCESS; } CONFIG::SaveBgDownloadData(); @@ -253,7 +253,7 @@ namespace HttpServer snprintf(temp_file, sizeof(temp_file), "%s.tmp", bg_download_list[i].dest_path.c_str()); // Check if temp file still exists, if exists then resume download - Util::RichNotify(bg_download_list[i].id, "Resuming download %s", bg_download_list[i].dest_path.c_str()); + Util::Notify("Resuming download %s", bg_download_list[i].dest_path.c_str()); if (FS::FileExists(temp_file)) { tmp_file_size = FS::GetSize(temp_file); @@ -270,11 +270,11 @@ namespace HttpServer if (ret == 0) { bg_download_list[i].state = STATE_FAILED; - Util::RichNotify(bg_download_list[i].id, "Failed to download %s", bg_download_list[i].dest_path.c_str()); + Util::Notify("Failed to download %s", bg_download_list[i].dest_path.c_str()); } else { - Util::RichNotify(bg_download_list[i].id, "Completed download %s", bg_download_list[i].dest_path.c_str()); + Util::Notify("Completed download %s", bg_download_list[i].dest_path.c_str()); bg_download_list[i].state = STATE_SUCCESS; } CONFIG::SaveBgDownloadData(); diff --git a/source/util.h b/source/util.h index cb3efe0..4954806 100644 --- a/source/util.h +++ b/source/util.h @@ -12,21 +12,14 @@ #include "openssl/md5.h" #include "config.h" -#define SCE_NOTIFICATION_LOCAL_USER_ID_SYSTEM 0xFE - -typedef struct notify_request -{ - char useless1[45]; - char message[3075]; -} notify_request_t; - -/* -extern "C" -{ - int sceKernelSendNotificationRequest(int, notify_request_t *, size_t, int); - int sceNotificationSend(int userId, bool isLogged, const char* payload); +extern "C" { +int sceKernelSendNotificationRequest(int, void *, size_t, int); } -*/ + +typedef struct notify_request { + char useless1[45]; + char message[3075]; +} notify_request_t; namespace Util { @@ -181,153 +174,15 @@ namespace Util static void Notify(const char *fmt, ...) { - OrbisNotificationRequest request; + notify_request_t req; + memset(&req, 0, sizeof(req)); va_list args; va_start(args, fmt); - vsprintf(request.message, fmt, args); + vsprintf(req.message, fmt, args); va_end(args); - request.type = OrbisNotificationRequestType::NotificationRequest; - request.unk3 = 0; - request.useIconImageUri = 0; - request.targetId = -1; - sceKernelSendNotificationRequest(0, &request, sizeof(request), 0); - } - - static void append_json_escaped(char *dst, size_t dst_size, const char *src) - { - size_t used = strlen(dst); - if (used >= dst_size) - return; - - for (; *src != '\0' && used + 1 < dst_size; ++src) - { - const char *escape = NULL; - char single[2] = {0}; - - switch (*src) - { - case '\\': - escape = "\\\\"; - break; - case '"': - escape = "\\\""; - break; - case '\n': - escape = "\\n"; - break; - case '\r': - escape = "\\r"; - break; - case '\t': - escape = "\\t"; - break; - default: - single[0] = *src; - escape = single; - break; - } - - size_t escape_len = strlen(escape); - if (used + escape_len >= dst_size) - break; - memcpy(dst + used, escape, escape_len); - used += escape_len; - dst[used] = '\0'; - } - } - - static bool RichNotify(uint64_t id, const char *fmt, ...) - { - /* - va_list args; - char message[3072]; - char escaped_message[4096]; - char payload[8192]; - char created_at[32]; - char notification_id[32]; - - va_start(args, fmt); - vsnprintf(message, sizeof message, fmt, args); - va_end(args); - - escaped_message[0] = '\0'; - append_json_escaped(escaped_message, sizeof(escaped_message), message); - - struct tm tm_utc; - time_t now = time(NULL); - gmtime_r(&now, &tm_utc); - strftime(created_at, 32, "%Y-%m-%dT%H:%M:%S.000Z", &tm_utc); - sprintf(notification_id, "%lu", id); - - int len = snprintf( - payload, sizeof(payload), - "{\n" - " \"rawData\": {\n" - " \"viewTemplateType\": \"InteractiveToastTemplateB\",\n" - " \"channelType\": \"ServiceFeedback\",\n" - " \"bundleName\": \"ezRemoteClientWelcome\",\n" - " \"useCaseId\": \"IDC\",\n" - " \"soundEffect\": \"none\",\n" - " \"toastOverwriteType\": \"InQueue\",\n" - " \"isImmediate\": true,\n" - " \"priority\": 100,\n" - " \"viewData\": {\n" - " \"icon\": {\n" - " \"type\": \"Url\",\n" - " \"parameters\": {\n" - " \"url\": \"" NOTIFY_ICON_FILE "\"\n" - " }\n" - " },\n" - " \"message\": {\n" - " \"body\": \"%s\"\n" - " },\n" - " \"subMessage\": {\n" - " \"body\": \"ezRemote Client\"\n" - " },\n" - " \"actions\": [\n" - " {\n" - " \"actionName\": \"Go to ezRemote Client\",\n" - " \"actionType\": \"DeepLink\",\n" - " \"defaultFocus\": true,\n" - " \"parameters\": {\n" - " \"actionUrl\": \"http://localhost:8080/hbldr?path=%s\"\n" - " }\n" - " }\n" - " ]\n" - " },\n" - " \"platformViews\": {\n" - " \"previewDisabled\": {\n" - " \"viewData\": {\n" - " \"icon\": {\n" - " \"type\": \"Predefined\",\n" - " \"parameters\": {\n" - " \"icon\": \"community\"\n" - " }\n" - " },\n" - " \"message\": {\n" - " \"body\": \"%s\"\n" - " }\n" - " }\n" - " }\n" - " }\n" - " },\n" - " \"createdDateTime\": \"%s\",\n" - " \"localNotificationId\": \"%s\"\n" - "}", - escaped_message, EZREMOTE_VERSION, CLIENT_ELF_PATH, escaped_message, created_at, - notification_id); - - if (len < 0 || (size_t)len >= sizeof(payload)) - return false; - - int rc = sceNotificationSend(SCE_NOTIFICATION_LOCAL_USER_ID_SYSTEM, true, - payload); - return rc == 0; - */ - - return 0; + sceKernelSendNotificationRequest(0, &req, sizeof(req), 0); } static size_t NthOccurrence(const std::string &str, const std::string &findMe, int nth, size_t start_pos = 0, size_t end_pos = INT_MAX)