From 81a5143f2f4ffce3a18af31737273087dbb7485d Mon Sep 17 00:00:00 2001 From: cy33hc Date: Sat, 30 May 2026 04:08:00 -0700 Subject: [PATCH] first working bg install --- CMakeLists.txt | 3 +- ps4-ezremote-server | 2 +- source/actions.cpp | 58 ++++---------- source/actions.h | 1 - source/clients/ftpclient.cpp | 85 ++++++++++++++------- source/clients/github.cpp | 16 ++++ source/clients/github.h | 2 + source/clients/nfsclient.cpp | 40 ++++++++-- source/clients/smbclient.cpp | 126 ++++++++++++++++++++---------- source/daemon.cpp | 139 ---------------------------------- source/daemon.h | 9 --- source/installer.cpp | 49 +++++++----- source/installer.h | 1 + source/main.cpp | 7 +- source/server/http_server.cpp | 6 +- source/split_file.cpp | 25 +++--- source/split_file.h | 2 +- source/windows.cpp | 2 +- 18 files changed, 263 insertions(+), 310 deletions(-) delete mode 100644 source/daemon.cpp delete mode 100644 source/daemon.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ec7f66b..3eef0c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,7 +50,6 @@ add_executable(ezremote_client source/actions.cpp source/config.cpp source/crypt.c - source/daemon.cpp source/fs.cpp source/gui.cpp source/getentropy.c @@ -117,7 +116,7 @@ target_link_libraries(ezremote_client ) add_self(ezremote_client) -add_pkg(ezremote_client ${CMAKE_SOURCE_DIR}/data "RMTC00001" "ezRemote Client" "01.40" 32 0) +add_pkg(ezremote_client ${CMAKE_SOURCE_DIR}/data "RMTC00001" "ezRemote Client" "01.41" 32 0) add_custom_target(package COMMAND mkdir -p ${PROJECT_SOURCE_DIR}/data/daemon diff --git a/ps4-ezremote-server b/ps4-ezremote-server index 4212e37..59739ac 160000 --- a/ps4-ezremote-server +++ b/ps4-ezremote-server @@ -1 +1 @@ -Subproject commit 4212e371682cdaf863b836ebddec19113e0dfaf7 +Subproject commit 59739ac76509d2205bd93d3bc43bbedbcb647971 diff --git a/source/actions.cpp b/source/actions.cpp index 78f994c..87994a5 100644 --- a/source/actions.cpp +++ b/source/actions.cpp @@ -654,39 +654,6 @@ namespace Actions else files.push_back(selected_remote_file); - bool download_and_install = false; - if (remote_settings->enable_rpi) - { - std::string url = INSTALLER::getRemoteUrl(files.begin()->path); - sprintf(activity_message, "%s", lang_strings[STR_CHECKING_REMOTE_SERVER_MSG]); - file_transfering = false; - if (!INSTALLER::canInstallRemotePkg(url)) - { - confirm_state = CONFIRM_WAIT; - action_to_take = selected_action; - activity_inprogess = false; - while (confirm_state == CONFIRM_WAIT) - { - sceKernelUsleep(100000); - } - activity_inprogess = true; - selected_action = action_to_take; - - if (confirm_state == CONFIRM_YES) - { - download_and_install = true; - } - else - { - goto finish; - } - } - } - else - { - download_and_install = true; - } - for (std::vector::iterator it = files.begin(); it != files.end(); ++it) { if (stop_activity) @@ -708,7 +675,7 @@ namespace Actions { if (BE32(header.pkg_magic) == PKG_MAGIC) { - if (download_and_install) + if (!remote_settings->enable_rpi) { if (DownloadAndInstallPkg(it->path, &header) == 0) failed++; @@ -728,11 +695,11 @@ namespace Actions SplitFile *sp = new SplitFile(install_pkg_path, INSTALL_ARCHIVE_PKG_SPLIT_SIZE/2); install_data->split_file = sp; - install_data->remote_client = remoteclient; + install_data->remote_client = INSTALLER::GetRemoteClient(remote_settings); install_data->path = it->path; remoteclient->Size(it->path, &install_data->size); install_data->stop_write_thread = false; - install_data->delete_client = false; + install_data->delete_client = true; int ret = pthread_create(&install_data->thread, NULL, DownloadSplitPkg, install_data); @@ -745,10 +712,15 @@ namespace Actions { std::string url = INSTALLER::getRemoteUrl(it->path, true); std::string title = INSTALLER::GetRemotePkgTitle(remoteclient, it->path, &header); - if (INSTALLER::InstallRemotePkg(url, &header, title, true) == 0) + if (INSTALLER::InstallRemotePkg(url, &header, title) == 0) failed++; else success++; + + if (it != files.end()) + { + sleep(3); + } } } } @@ -764,6 +736,8 @@ namespace Actions { while (entry != nullptr) { + snprintf(activity_message, 1023, "%s %s", lang_strings[STR_INSTALLING], entry->filename.c_str()); + ArchivePkgInstallData *install_data = (ArchivePkgInstallData*) malloc(sizeof(ArchivePkgInstallData)); memset(install_data, 0, sizeof(ArchivePkgInstallData)); @@ -1913,21 +1887,15 @@ namespace Actions void RestartServer() { - StopServer(); + INSTALLER::StopEzRemoteServer(); sleep(2); INSTALLER::StartEzRemoteServer(); sleep(2); } - void StopServer() - { - httplib::Client client = httplib::Client("http://localhost:" + std::to_string(http_int_server_port)); - client.Get("/stop"); - } - void GetBackgroundDownloadProgress() { - httplib::Client client = httplib::Client("http://localhost:" + std::to_string(http_int_server_port)); + httplib::Client client = httplib::Client("http://127.0.0.1:" + std::to_string(http_int_server_port)); if (auto res = client.Get("/get_download_state")) { diff --git a/source/actions.h b/source/actions.h index d15de28..967f948 100644 --- a/source/actions.h +++ b/source/actions.h @@ -123,7 +123,6 @@ namespace Actions void *ExtractArchivePkg(void *argp); void *DownloadSplitPkg(void *argp); void RestartServer(); - void StopServer(); void GetBackgroundDownloadProgress(); } diff --git a/source/clients/ftpclient.cpp b/source/clients/ftpclient.cpp index b84afd5..13e3abd 100644 --- a/source/clients/ftpclient.cpp +++ b/source/clients/ftpclient.cpp @@ -897,7 +897,8 @@ int FtpClient::FtpXfer(SplitFile *split_file, const std::string &path, ftphandle dbuf = static_cast(malloc(FTP_CLIENT_BUFSIZ)); while ((l = FtpRead(dbuf, FTP_CLIENT_BUFSIZ, nData)) > 0) { - split_file->Write(dbuf, l); + if (split_file->Write(dbuf, l) < 0) + break; } free(dbuf); @@ -1499,9 +1500,16 @@ int FtpClient::ParseDirEntry(char *line, DirEntry *dirEntry) dirEntry->modified.minutes = (uint8_t)strtoul(token + 3, NULL, 10); // The PM period covers the 12 hours from noon to midnight + // Correct 12-hour clock: 12:xx AM = 00:xx, 12:xx PM = 12:xx if (strstr(token, "PM") != NULL) { - dirEntry->modified.hours += 12; + if (dirEntry->modified.hours != 12) + dirEntry->modified.hours += 12; + } + else + { + if (dirEntry->modified.hours == 12) + dirEntry->modified.hours = 0; } } else @@ -1525,7 +1533,7 @@ int FtpClient::ParseDirEntry(char *line, DirEntry *dirEntry) else { // Save the size of the file - dirEntry->file_size = strtoul(token, NULL, 10); + dirEntry->file_size = strtoull(token, NULL, 10); } // Read filename field @@ -1547,8 +1555,8 @@ int FtpClient::ParseDirEntry(char *line, DirEntry *dirEntry) // Unix listing format? else { - // Check file permissions - if (strchr(token, 'd') != NULL) + // Check file permissions — 'd' must be at position 0 of the permissions field + if (token[0] == 'd') { dirEntry->isDir = true; } @@ -1578,7 +1586,7 @@ int FtpClient::ParseDirEntry(char *line, DirEntry *dirEntry) return -1; // Save the size of the file - dirEntry->file_size = strtoul(token, NULL, 10); + dirEntry->file_size = strtoull(token, NULL, 10); // Read modification time (month) token = strtok_r(NULL, " ", &p); @@ -1619,12 +1627,13 @@ int FtpClient::ParseDirEntry(char *line, DirEntry *dirEntry) // The format of the year is yyyy dirEntry->modified.year = (uint16_t)strtoul(token, NULL, 10); } - else if (strlen(token) == 5) + else if (strchr(token, ':') != NULL) { - // The format of the time hh:mm - token[2] = '\0'; + // The format of the time is hh:mm or h:mm + char *colon = strchr(token, ':'); + *colon = '\0'; dirEntry->modified.hours = (uint8_t)strtoul(token, NULL, 10); - dirEntry->modified.minutes = (uint8_t)strtoul(token + 3, NULL, 10); + dirEntry->modified.minutes = (uint8_t)strtoul(colon + 1, NULL, 10); dirEntry->modified.year = cur_time.tm_year + 1900; } else @@ -1639,6 +1648,11 @@ int FtpClient::ParseDirEntry(char *line, DirEntry *dirEntry) if (token == NULL) return -1; + // For symlinks, strip the " -> target" suffix (e.g. "linkname -> /some/target") + char *arrow = strstr(token, " -> "); + if (arrow != NULL) + *arrow = '\0'; + // Retrieve the length of the filename n = strlen(token); // Limit the number of characters to copy @@ -1650,6 +1664,10 @@ int FtpClient::ParseDirEntry(char *line, DirEntry *dirEntry) dirEntry->name[n] = '\0'; } + // Exclude hidden files and folders (names starting with '.') + if (!show_hidden_files && dirEntry->name[0] == '.') + return -1; + // The directory entry is valid return 1; } @@ -1660,37 +1678,48 @@ int FtpClient::ParseMLSDDirEntry(char *line, DirEntry *dirEntry) char *token; char *facts; char *keypair; + char *factsSave; char key[128]; char value[128]; - // Spilt string by space + // Split string by first space: facts portion and name portion facts = strtok_r(line, " ", &p); - // path is the rest of the line after space + // path is the rest of the line after the space, strip trailing CR/LF token = strtok_r(p, "\r\n", &p); snprintf(dirEntry->name, 256, "%s", token); - // split properties by semi-colon and get the key value pair - while ((keypair = strtok_r(facts, ";", &facts))) + // Split facts by semicolon and parse each key=value pair + factsSave = NULL; + keypair = strtok_r(facts, ";", &factsSave); + while (keypair != NULL) { - sscanf(keypair, "%[^=]=%s", key, value); - if (strcasecmp(key, "type") == 0) + key[0] = '\0'; + value[0] = '\0'; + if (sscanf(keypair, "%127[^=]=%127s", key, value) == 2) { - dirEntry->isDir = false; - if (strcasecmp(value, "dir") == 0) + if (strcasecmp(key, "type") == 0) { - dirEntry->isDir = true; + dirEntry->isDir = false; + // dir, cdir (current dir), and pdir (parent dir) are all directory types + if (strcasecmp(value, "dir") == 0 || + strcasecmp(value, "cdir") == 0 || + strcasecmp(value, "pdir") == 0) + { + dirEntry->isDir = true; + } + } + else if (strcasecmp(key, "size") == 0) + { + dirEntry->file_size = atoll(value); + } + else if (strcasecmp(key, "modify") == 0) + { + sscanf(value, "%4d%2d%2d%2d%2d%2d", &dirEntry->modified.year, &dirEntry->modified.month, &dirEntry->modified.day, + &dirEntry->modified.hours, &dirEntry->modified.minutes, &dirEntry->modified.seconds); } } - else if (strcasecmp(key, "size") == 0) - { - dirEntry->file_size = atoll(value); - } - else if (strcasecmp(key, "modify") == 0) - { - sscanf(value, "%4d%2d%2d%2d%2d%2d", &dirEntry->modified.year, &dirEntry->modified.month, &dirEntry->modified.day, - &dirEntry->modified.hours, &dirEntry->modified.minutes, &dirEntry->modified.seconds); - } + keypair = strtok_r(NULL, ";", &factsSave); } return 1; } diff --git a/source/clients/github.cpp b/source/clients/github.cpp index a865e62..ec4938c 100644 --- a/source/clients/github.cpp +++ b/source/clients/github.cpp @@ -21,6 +21,7 @@ int GithubClient::Connect(const std::string &url, const std::string &username, c this->base_path = "/repos" + url.substr(18); Util::Rtrim(this->base_path, "/"); this->base_path += "/releases"; + this->m_download_url = "https://github.com"; client = new httplib::Client(this->host_url); if (username.length() > 0) @@ -258,3 +259,18 @@ bool GithubClient::ParseReleases() return 1; } + +std::string GithubClient::GetDownloadUrl(const std::string &path) +{ + if (!ParseReleases()) + return ""; + + std::vector path_parts = Util::Split(path, "/"); + + if (path_parts.size() != 2) + { + return ""; + } + + return this->m_download_url + Escape(m_assets[path_parts[0]][path_parts[1]].url); +} diff --git a/source/clients/github.h b/source/clients/github.h index 0f3bb61..2f3fd05 100644 --- a/source/clients/github.h +++ b/source/clients/github.h @@ -19,6 +19,7 @@ public: int GetRange(const std::string &path, void *buffer, uint64_t size, uint64_t offset); int GetRange(const std::string &path, DataSink &sink, uint64_t size, uint64_t offset); int Head(const std::string &path, void *buffer, uint64_t len); + std::string GetDownloadUrl(const std::string &path); private: struct GitAsset @@ -38,6 +39,7 @@ private: std::vector m_releases; std::map> m_assets; bool releases_parsed = false; + std::string m_download_url; BaseClient m_client; bool ParseReleases(); diff --git a/source/clients/nfsclient.cpp b/source/clients/nfsclient.cpp index 46610c1..5d2760e 100644 --- a/source/clients/nfsclient.cpp +++ b/source/clients/nfsclient.cpp @@ -247,7 +247,7 @@ int NfsClient::Get(SplitFile *split_file, const std::string &ppath, uint64_t off void *buff = malloc(BUF_SIZE); int count = 0; - while ((count = nfs_read(nfs, nfsfh, BUF_SIZE, buff)) > 0) + while ((count = nfs_read(nfs, nfsfh, BUF_SIZE, buff)) != 0) { if (count < 0) { @@ -256,7 +256,13 @@ int NfsClient::Get(SplitFile *split_file, const std::string &ppath, uint64_t off free((void *)buff); return 0; } - split_file->Write((char *)buff, count); + ret = split_file->Write((char *)buff, count); + if (ret < 0) + { + nfs_close(nfs, nfsfh); + free((void *)buff); + return 0; + } } nfs_close(nfs, nfsfh); free((void *)buff); @@ -343,10 +349,28 @@ int NfsClient::GetRange(void *fp, void *buffer, uint64_t size, uint64_t offset) return 0; } - int count = nfs_read(nfs, nfsfh, size, buffer); - if (count != size) - return 0; + size_t bytes_remaining = size; + char *buff = (char*)buffer; + int total = 0; + int count = 0; + do + { + count = nfs_read(nfs, nfsfh, bytes_remaining, buff); + if (count > 0) + { + bytes_remaining -= count; + buff += count; + total += count; + } + else + { + break; + } + } while (1); + if (total != size) + return 0; + return 1; } @@ -411,14 +435,14 @@ int NfsClient::Put(const std::string &inputfile, const std::string &ppath, uint6 } void *buff = malloc(BUF_SIZE); - uint64_t count = 0; + int count = 0; bytes_transfered = 0; sceRtcGetCurrentTick(&prev_tick); - while ((count = FS::Read(in, buff, BUF_SIZE)) > 0) + while ((count = FS::Read(in, buff, BUF_SIZE)) != 0) { if (count < 0) { - sprintf(response, "%s", lang_strings[STR_FAILED]); + snprintf(response, sizeof(response), "%s", lang_strings[STR_FAILED]); FS::Close(in); nfs_close(nfs, nfsfh); free(buff); diff --git a/source/clients/smbclient.cpp b/source/clients/smbclient.cpp index 35e466d..a963b5c 100644 --- a/source/clients/smbclient.cpp +++ b/source/clients/smbclient.cpp @@ -31,27 +31,36 @@ int SmbClient::Connect(const std::string &url, const std::string &user, const st smb2 = smb2_init_context(); if (smb2 == NULL) { - sprintf(response, "Failed to init SMB context"); + snprintf(response, sizeof(response), "Failed to init SMB context"); return 0; } smb_url = smb2_parse_url(smb2, url.c_str()); if (smb_url == NULL || smb_url->share == NULL || strlen(smb_url->share) == 0) { - sprintf(response, "Invalid SMB Url"); + if (smb_url != NULL) + smb2_destroy_url(smb_url); + smb2_destroy_context(smb2); + smb2 = NULL; + snprintf(response, sizeof(response), "Invalid SMB Url"); return 0; } if (pass.length() > 0) smb2_set_password(smb2, pass.c_str()); smb2_set_security_mode(smb2, SMB2_NEGOTIATE_SIGNING_ENABLED); + smb2_set_version(smb2, SMB2_VERSION_ANY); smb2_set_timeout(smb2, 30); if (smb2_connect_share(smb2, smb_url->server, smb_url->share, user.c_str()) < 0) { - sprintf(response, "%s", smb2_get_error(smb2)); + snprintf(response, sizeof(response), "%s", smb2_get_error(smb2)); + smb2_destroy_url(smb_url); + smb2_destroy_context(smb2); + smb2 = NULL; return 0; } + smb2_destroy_url(smb_url); max_read_size = smb2_get_max_read_size(smb2); max_write_size = smb2_get_max_write_size(smb2); @@ -109,7 +118,7 @@ int SmbClient::Mkdir(const std::string &ppath) path = Util::Trim(path, "/"); if (smb2_mkdir(smb2, path.c_str()) != 0) { - sprintf(response, "%s", smb2_get_error(smb2)); + snprintf(response, sizeof(response), "%s", smb2_get_error(smb2)); return 0; } return 1; @@ -126,7 +135,7 @@ int SmbClient::_Rmdir(const std::string &ppath) path = Util::Trim(path, "/"); if (smb2_rmdir(smb2, path.c_str()) != 0) { - sprintf(response, "%s", smb2_get_error(smb2)); + snprintf(response, sizeof(response), "%s", smb2_get_error(smb2)); return 0; } return 1; @@ -193,37 +202,45 @@ int SmbClient::Get(const std::string &outputfile, const std::string &ppath, uint path = Util::Trim(path, "/"); if (!Size(path.c_str(), &bytes_to_download)) { - sprintf(response, "%s", smb2_get_error(smb2)); + snprintf(response, sizeof(response), "%s", smb2_get_error(smb2)); return 0; } - struct smb2fh *in = smb2_open(smb2, path.c_str(), O_RDONLY); + struct smb2fh* in = smb2_open(smb2, path.c_str(), O_RDONLY); if (in == NULL) { - sprintf(response, "%s", smb2_get_error(smb2)); + snprintf(response, sizeof(response), "%s", smb2_get_error(smb2)); return 0; } - FILE *out = FS::Create(outputfile); + FILE* out = FS::Create(outputfile); if (out == NULL) { - sprintf(response, "%s", lang_strings[STR_FAILED]); + snprintf(response, sizeof(response), "%s", lang_strings[STR_FAILED]); + smb2_close(smb2, in); return 0; } - uint8_t *buff = (uint8_t *)malloc(max_read_size); + uint8_t *buff = (uint8_t*)malloc(max_read_size); + if (buff == NULL) + { + snprintf(response, sizeof(response), "%s", lang_strings[STR_FAILED]); + FS::Close(out); + smb2_close(smb2, in); + return 0; + } int count = 0; bytes_transfered = 0; sceRtcGetCurrentTick(&prev_tick); - while ((count = smb2_read(smb2, in, buff, max_read_size)) > 0) + while ((count = smb2_read(smb2, in, buff, max_read_size)) != 0) { if (count < 0) { - sprintf(response, "%s", smb2_get_error(smb2)); + snprintf(response, sizeof(response), "%s", smb2_get_error(smb2)); FS::Close(out); smb2_close(smb2, in); - free((void *)buff); + free((void*)buff); return 0; } FS::Write(out, buff, count); @@ -231,7 +248,7 @@ int SmbClient::Get(const std::string &outputfile, const std::string &ppath, uint } FS::Close(out); smb2_close(smb2, in); - free((void *)buff); + free((void*)buff); return 1; } @@ -243,23 +260,33 @@ int SmbClient::Get(SplitFile *split_file, const std::string &ppath, uint64_t off struct smb2fh *in = smb2_open(smb2, path.c_str(), O_RDONLY); if (in == NULL) { - sprintf(response, "%s", smb2_get_error(smb2)); + snprintf(response, sizeof(response), "%s", smb2_get_error(smb2)); return 0; } uint8_t *buff = (uint8_t *)malloc(max_read_size); + if (buff == NULL) + { + smb2_close(smb2, in); + return 0; + } int count = 0; - while ((count = smb2_read(smb2, in, buff, max_read_size)) > 0) + while ((count = smb2_read(smb2, in, buff, max_read_size)) != 0) { if (count < 0) { - sprintf(response, "%s", smb2_get_error(smb2)); + snprintf(response, sizeof(response), "%s", smb2_get_error(smb2)); + smb2_close(smb2, in); + free((void *)buff); + return 0; + } + if (split_file->Write((char*)buff, count) < 0) + { smb2_close(smb2, in); free((void *)buff); return 0; } - split_file->Write((char*)buff, count); } smb2_close(smb2, in); @@ -345,24 +372,27 @@ int SmbClient::GetRange(void *fp, void *buffer, uint64_t size, uint64_t offset) smb2_lseek(smb2, in, offset, SEEK_SET, NULL); - uint8_t *buff = (uint8_t *)buffer; - int count = 0; size_t bytes_remaining = size; + uint8_t *buff = (uint8_t*)buffer; + int count = 0; + uint64_t total = 0; do { - size_t bytes_to_read = std::min(max_read_size, bytes_remaining); - count = smb2_read(smb2, in, buff, bytes_to_read); + count = smb2_read(smb2, in, buff, bytes_remaining); if (count > 0) { bytes_remaining -= count; buff += count; + total += count; } else { break; } } while (1); - + + if (total != size) + return 0; return 1; } @@ -425,7 +455,7 @@ bool SmbClient::FileExists(const std::string &ppath) int ret = smb2_stat(smb2, path.c_str(), &st); if (ret != 0) { - sprintf(response, "%s", smb2_get_error(smb2)); + snprintf(response, sizeof(response), "%s", smb2_get_error(smb2)); return false; } @@ -449,21 +479,29 @@ int SmbClient::Put(const std::string &inputfile, const std::string &ppath, uint6 return 0; } - FILE *in = FS::OpenRead(inputfile); + FILE* in = FS::OpenRead(inputfile); if (in == NULL) { - sprintf(response, "%s", lang_strings[STR_FAILED]); + snprintf(response, sizeof(response), "%s", lang_strings[STR_FAILED]); return 0; } - - struct smb2fh *out = smb2_open(smb2, path.c_str(), O_WRONLY | O_CREAT | O_TRUNC); + + struct smb2fh* out = smb2_open(smb2, path.c_str(), O_WRONLY | O_CREAT | O_TRUNC); if (out == NULL) { - sprintf(response, "%s", smb2_get_error(smb2)); + snprintf(response, sizeof(response), "%s", smb2_get_error(smb2)); + FS::Close(in); return 0; } - uint8_t *buff = (uint8_t *)malloc(max_write_size); + uint8_t* buff = (uint8_t*)malloc(max_write_size); + if (buff == NULL) + { + snprintf(response, sizeof(response), "%s", lang_strings[STR_FAILED]); + FS::Close(in); + smb2_close(smb2, out); + return 0; + } int count = 0; bytes_transfered = 0; sceRtcGetCurrentTick(&prev_tick); @@ -472,13 +510,20 @@ int SmbClient::Put(const std::string &inputfile, const std::string &ppath, uint6 { if (count < 0) { - sprintf(response, "%s", lang_strings[STR_FAILED]); + snprintf(response, sizeof(response), "%s", lang_strings[STR_FAILED]); + FS::Close(in); + smb2_close(smb2, out); + free(buff); + return 0; + } + if (smb2_write(smb2, out, buff, count) < 0) + { + snprintf(response, sizeof(response), "%s", smb2_get_error(smb2)); FS::Close(in); smb2_close(smb2, out); free(buff); return 0; } - smb2_write(smb2, out, buff, count); bytes_transfered += count; } FS::Close(in); @@ -496,7 +541,7 @@ int SmbClient::Rename(const std::string &src, const std::string &dst) path2 = Util::Trim(path2, "/"); if (smb2_rename(smb2, path1.c_str(), path2.c_str()) != 0) { - sprintf(response, "%s", smb2_get_error(smb2)); + snprintf(response, sizeof(response), "%s", smb2_get_error(smb2)); return 0; } @@ -509,7 +554,7 @@ int SmbClient::Delete(const std::string &ppath) path = Util::Trim(path, "/"); if (smb2_unlink(smb2, path.c_str()) != 0) { - sprintf(response, "%s", smb2_get_error(smb2)); + snprintf(response, sizeof(response), "%s", smb2_get_error(smb2)); return 0; } @@ -523,7 +568,7 @@ int SmbClient::Size(const std::string &ppath, int64_t *size) smb2_stat_64 st; if (smb2_stat(smb2, path.c_str(), &st) != 0) { - sprintf(response, "%s", smb2_get_error(smb2)); + snprintf(response, sizeof(response), "%s", smb2_get_error(smb2)); return 0; } *size = st.smb2_size; @@ -592,7 +637,8 @@ std::vector SmbClient::ListDir(const std::string &path) sprintf(entry.display_size, "%s", lang_strings[STR_FOLDER]); break; } - if (strcmp(entry.name, "..") != 0 && strcmp(entry.name, ".") != 0) + if (strcmp(entry.name, "..") != 0 && strcmp(entry.name, ".") != 0 && + (show_hidden_files || entry.name[0] != '.')) out.push_back(entry); } smb2_closedir(smb2, dir); @@ -619,15 +665,15 @@ int SmbClient::Head(const std::string &ppath, void *buffer, uint64_t len) return 0; } - struct smb2fh *in = smb2_open(smb2, path.c_str(), O_RDONLY); + struct smb2fh* in = smb2_open(smb2, path.c_str(), O_RDONLY); if (in == NULL) { return 0; } - int count = smb2_read(smb2, in, (uint8_t *)buffer, len); + uint64_t count = smb2_read(smb2, in, (uint8_t*)buffer, len); smb2_close(smb2, in); - if (count != len) + if (count < 0 || count != len) return 0; return 1; diff --git a/source/daemon.cpp b/source/daemon.cpp deleted file mode 100644 index 6fcdb21..0000000 --- a/source/daemon.cpp +++ /dev/null @@ -1,139 +0,0 @@ -#include -#include "system.h" -#include "fs.h" -#include "dbglogger.h" - -extern "C" -{ -#include "orbis_jbc.h" - int sceLncUtilGetAppId(const char *tid); -} - -#define EZREMOTE_SERVER_TITLEID "EZSR00001" -#define EZREMOTE_CLIENT_TITLEID "RMTC00001" - -#define DAEMON_PATH "/system/vsh/app/" EZREMOTE_SERVER_TITLEID -#define DAEMON_SRC_PATH "/mnt/sandbox/pfsmnt/" EZREMOTE_CLIENT_TITLEID "-app0/daemon" - -namespace Daemon -{ - int copyFile(const char *sourcefile, const char *destfile) - { - int src = sceKernelOpen(sourcefile, 0x0000, 0); - if (src > 0) - { - int out = sceKernelOpen(destfile, 0x0001 | 0x0200 | 0x0400, 0777); - if (out > 0) - { - size_t bytes; - char *buffer = (char *)malloc(65536); - if (buffer != NULL) - { - while (0 < (bytes = sceKernelRead(src, buffer, 65536))) - sceKernelWrite(out, buffer, bytes); - free(buffer); - } - sceKernelClose(out); - } - else - return -1; - - sceKernelClose(src); - return 0; - } - - dbglogger_log("[ELFLOADER] fuxking error"); - dbglogger_log("[Itemz-loader:%s:%i] ----- src fd = %i---", __FUNCTION__, __LINE__, src); - - return -1; - } - - int MD5_hash_compare(const std::string &file1, const std::string &file2) - { - std::vector file1_content = FS::Load(file1); - std::vector file2_content = FS::Load(file2); - - std::string str1 = std::string(file1_content.data(), file1_content.size()); - std::string str2 = std::string(file1_content.data(), file1_content.size()); - - return str1.compare(str2); - } - - bool IsDaemonOutdated(void) - { - bool res = true; - if (FS::FileExists(DAEMON_PATH "/eboot.md5")) - { - res = MD5_hash_compare(DAEMON_PATH "/daemon.md5", DAEMON_SRC_PATH "/deamon.md5"); - dbglogger_log("Daemon Is Outdated?: %s", res ? "Yes" : "No"); - } - - return res; - } - - uint32_t LaunchDaemon(const char *TITLE_ID) - { - uint32_t userId = -1; - - LncAppParam param; - param.size = sizeof(LncAppParam); - param.user_id = userId; - param.app_opt = 0; - param.crash_report = 0; - param.LaunchAppCheck_flag = LaunchApp_SkipSystemUpdate; - - return sceLncUtilLaunchApp(TITLE_ID, NULL, ¶m); - - return 0; - } - - bool BootDaemonServices() - { - dbglogger_log("Booting Daemon Services"); - - if (!FS::FolderExists(DAEMON_PATH) || IsDaemonOutdated()) - { - if (mount_large_fs("/dev/da0x4.crypt", "/system", "exfatfs", "511", MNT_UPDATE) != 0) - { - dbglogger_log("mounting /system failed with %s.", strerror(errno)); - return false; - } - else - { - - dbglogger_log("Remount Successful"); - // Delete the folder and all its files - FS::RmRecursive(DAEMON_PATH); - FS::MkDirs(DAEMON_PATH); - FS::MkDirs(DAEMON_PATH "/sce_sys"); - - if (copyFile(DAEMON_SRC_PATH "/param", DAEMON_PATH "/sce_sys/param.sfo") != -1) - { - if (copyFile(DAEMON_SRC_PATH "/daemon.self", DAEMON_PATH "/eboot.bin") != 0 || - copyFile(DAEMON_SRC_PATH "/daemon.md5", DAEMON_PATH "/daemon.md5") != 0) - { - dbglogger_log("Creating the Daemon eboot failed to create: %s", strerror(errno)); - return false; - } - } - else - { - dbglogger_log("Copying Daemon files failed"); - return false; - } - } - } - - int32_t appid = sceLncUtilGetAppId(EZREMOTE_SERVER_TITLEID); - // Launch Daemon with silent - if ((appid & ~0xFFFFFF) != 0x60000000) - { - appid = LaunchDaemon(EZREMOTE_SERVER_TITLEID); - dbglogger_log("Launched Daemon AppId: %x", appid); - } - else - dbglogger_log("Found Daemon AppId: %x", appid); - - return true; - } -} \ No newline at end of file diff --git a/source/daemon.h b/source/daemon.h deleted file mode 100644 index df464a5..0000000 --- a/source/daemon.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef EZ_DAEMON_H -#define EZ_DAEMON_H - -namespace Daemon -{ - bool BootDaemonServices(); -} - -#endif \ No newline at end of file diff --git a/source/installer.cpp b/source/installer.cpp index dbca34e..b646f7f 100644 --- a/source/installer.cpp +++ b/source/installer.cpp @@ -16,6 +16,7 @@ #include "clients/smbclient.h" #include "clients/sftpclient.h" #include "clients/ftpclient.h" +#include "clients/github.h" #include "clients/nfsclient.h" #include "clients/webdav.h" #include "clients/apache.h" @@ -26,7 +27,6 @@ #include "clients/nginx.h" #include "clients/npxserve.h" #include "clients/rclone.h" -#include "dbglogger.h" #include "server/http_server.h" #include "installer.h" @@ -38,6 +38,8 @@ #include "fs.h" #include "sfo.h" +#define SERVER_ELF_PATH "/mnt/sandbox/pfsmnt/RMTC00001-app0/daemon/daemon.elf" + #define BGFT_HEAP_SIZE (1 * 1024 * 1024) struct BgProgressCheck @@ -238,7 +240,7 @@ namespace INSTALLER const char *params_str = json_object_to_json_string(history_item_obj); - httplib::Client client = httplib::Client(std::string("http://localhost:") + std::to_string(http_int_server_port)); + httplib::Client client = httplib::Client(std::string("http://127.0.0.1:") + std::to_string(http_int_server_port)); client.set_connection_timeout(1); if (auto res = client.Post("/store_bg_install_data", params_str, "application/json")) @@ -256,9 +258,17 @@ namespace INSTALLER std::string getRemoteUrl(const std::string path, bool encodeUrl) { - if (strlen(remote_settings->username) == 0 && strlen(remote_settings->password) == 0 && - (remoteclient->clientType() == CLIENT_TYPE_WEBDAV || - (remoteclient->clientType() == CLIENT_TYPE_HTTP_SERVER && strcmp(remote_settings->http_server_type, HTTP_SERVER_GITHUB) != 0))) + if (remote_settings->type == CLIENT_TYPE_HTTP_SERVER && strcmp(remote_settings->http_server_type, HTTP_SERVER_GITHUB) == 0) + { + GithubClient *tmp_client = (GithubClient*) remoteclient; + return tmp_client->GetDownloadUrl(path); + } + if ( strlen(remote_settings->username) == 0 && + strlen(remote_settings->password) == 0 && + (remote_settings->type == CLIENT_TYPE_WEBDAV || + (remote_settings->type == CLIENT_TYPE_HTTP_SERVER && strcmp(remote_settings->http_server_type, HTTP_SERVER_ARCHIVEORG) == 0) + ) + ) { std::string full_url = WebDAVClient::GetHttpUrl(remote_settings->server + path); size_t scheme_pos = full_url.find("://"); @@ -277,9 +287,8 @@ namespace INSTALLER } else { - std::string encoded_path = httplib::detail::encode_url(path); - std::string encoded_site_name = httplib::detail::encode_url(remote_settings->site_name); - std::string full_url = std::string("http://localhost:") + std::to_string(http_server_port) + "/rmt_inst/" + encoded_site_name + encoded_path; + std::string hash = StoreBgInstallHostData(remote_settings, path); + std::string full_url = std::string("http://127.0.0.1:") + std::to_string(http_int_server_port) + "/bg_install/" + hash; return full_url; } @@ -910,7 +919,7 @@ namespace INSTALLER } std::string hash = Util::UrlHash(path); - std::string full_url = std::string("http://localhost:") + std::to_string(http_server_port) + "/archive_inst/" + hash; + std::string full_url = std::string("http://127.0.0.1:") + std::to_string(http_server_port) + "/archive_inst/" + hash; AddArchivePkgInstallData(hash, pkg_data); OrbisBgftTaskProgress progress_info; @@ -1097,7 +1106,7 @@ namespace INSTALLER } std::string hash = Util::UrlHash(path); - std::string full_url = std::string("http://localhost:") + std::to_string(http_server_port) + "/split_inst/" + hash; + std::string full_url = std::string("http://127.0.0.1:") + std::to_string(http_server_port) + "/split_inst/" + hash; AddSplitPkgInstallData(hash, pkg_data); OrbisBgftTaskProgress progress_info; @@ -1229,10 +1238,11 @@ namespace INSTALLER std::string EzRemoteServerVersion() { - httplib::Client client = httplib::Client(std::string("http://localhost:") + std::to_string(http_int_server_port)); - client.set_connection_timeout(1); + httplib::Client tmp_client = httplib::Client("http://127.0.0.1:6701"); - if (auto res = client.Get("/version")) + tmp_client.set_connection_timeout(1); + + if (auto res = tmp_client.Get("/version")) { if (HTTP_SUCCESS(res->status)) { @@ -1245,7 +1255,6 @@ namespace INSTALLER int StartEzRemoteServer() { - /* char buffer[8192]; in_addr_t in_addr; in_addr_t server_addr; @@ -1255,10 +1264,7 @@ namespace INSTALLER ssize_t read_return; struct hostent *hostent; struct sockaddr_in sockaddr_in; - unsigned short server_port = 9021; - - if (!EzRemoteServerVersion().empty()) - return 0; + unsigned short server_port = 9090; filefd = open(SERVER_ELF_PATH, O_RDONLY); if (filefd == -1) @@ -1310,11 +1316,16 @@ namespace INSTALLER close(filefd); close(sockfd); - */ return 0; } + void StopEzRemoteServer() + { + httplib::Client tmp_client = httplib::Client("http://127.0.0.1:6701"); + tmp_client.Get("/stop"); + } + RemoteClient *GetRemoteClient(int site_idx) { RemoteClient *tmp_client = nullptr; diff --git a/source/installer.h b/source/installer.h index df87ac6..213c384 100644 --- a/source/installer.h +++ b/source/installer.h @@ -167,6 +167,7 @@ namespace INSTALLER bool InstallSplitPkg(const std::string &path, SplitPkgInstallData* pkg_data, bool bg = false); std::string EzRemoteServerVersion(); int StartEzRemoteServer(); + void StopEzRemoteServer(); std::string StoreBgInstallHostData(RemoteSettings *remote_settings, const std::string &path); RemoteClient *GetRemoteClient(int site_idx); RemoteClient *GetRemoteClient(RemoteSettings *settings); diff --git a/source/main.cpp b/source/main.cpp index 63ee7b5..46d6ba7 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -20,7 +20,6 @@ #include "server/http_server.h" #include "clients/gdrive.h" #include "config.h" -#include "daemon.h" #include "lang.h" #include "gui.h" #include "util.h" @@ -301,7 +300,6 @@ int main() return 0; CONFIG::LoadConfig(); - HttpServer::Start(); // Create a window context window = SDL_CreateWindow("main", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, FRAME_WIDTH, FRAME_HEIGHT, 0); @@ -325,13 +323,14 @@ int main() terminate(); } - Daemon::BootDaemonServices(); - if (load_sys_modules() != 0) return 0; atexit(terminate); + HttpServer::Start(); + INSTALLER::StartEzRemoteServer(); + GUI::RenderLoop(renderer); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); diff --git a/source/server/http_server.cpp b/source/server/http_server.cpp index 5c36e9b..d760be3 100644 --- a/source/server/http_server.cpp +++ b/source/server/http_server.cpp @@ -1004,7 +1004,7 @@ namespace HttpServer std::string post_data = std::string("code=") + auth_code + "&client_id=" + gg_app.client_id + "&client_secret=" + gg_app.client_secret + - "&redirect_uri=http%3A//localhost%3A" + std::to_string(http_server_port) + "/google_auth" + "&redirect_uri=http%3A//127.0.0.1%3A" + std::to_string(http_server_port) + "/google_auth" "&grant_type=authorization_code"; if (auto result = client.Post(url, post_data.c_str(), post_data.length(), "application/x-www-form-urlencoded")) @@ -1376,7 +1376,7 @@ namespace HttpServer if (!use_disk_cache) { - std::string remote_install_url = std::string("http://localhost:") + std::to_string(http_server_port) + "/rmt_inst/Site%2099/" + hash; + std::string remote_install_url = std::string("http://127.0.0.1:") + std::to_string(http_server_port) + "/rmt_inst/Site%2099/" + hash; int rc = INSTALLER::InstallRemotePkg(remote_install_url, &header, title, false); if (rc == 0) { @@ -1536,7 +1536,7 @@ namespace HttpServer json_object_object_add(params, "id", json_object_new_uint64(tick.mytick)); const char *params_str = json_object_to_json_string(params); - httplib::Client tmp_client = httplib::Client(std::string("http://localhost:") + std::to_string(http_int_server_port)); + httplib::Client tmp_client = httplib::Client(std::string("http://127.0.0.1:") + std::to_string(http_int_server_port)); std::string download_req_url = + "/download_url"; if (auto resp = tmp_client.Post(download_req_url, params_str, strlen(params_str), "application/json")) diff --git a/source/split_file.cpp b/source/split_file.cpp index 308e83e..0a0ff08 100644 --- a/source/split_file.cpp +++ b/source/split_file.cpp @@ -24,7 +24,7 @@ SplitFile::~SplitFile() fclose(this->file_blocks[i]->fd); } remove(this->file_blocks[i]->block_file.c_str()); - free(this->file_blocks[i]); + delete this->file_blocks[i]; } } sem_destroy(&this->block_ready); @@ -33,8 +33,6 @@ SplitFile::~SplitFile() int SplitFile::Open() { this->block_in_progress = NewBlock(); - this->block_in_progress->fd = fopen(block_in_progress->block_file.c_str(), "w"); - return (block_in_progress->fd == nullptr); } @@ -62,6 +60,10 @@ size_t SplitFile::Read(char *buf, size_t buf_size, size_t offset) sem_timedwait(&this->block_ready, &ts); } + // If complete and block_num is past the end, the requested offset is beyond EOF + if (block_num >= this->file_blocks.size()) + return 0; + block = this->file_blocks[block_num]; if (block->status == BLOCK_STATUS_DELETED) { @@ -121,7 +123,7 @@ size_t SplitFile::Read(char *buf, size_t buf_size, size_t offset) block_offset = 0; while ((block_num > this->file_blocks.size() - 1 && !this->complete) || - this->file_blocks[block_num]->status == BLOCK_STATUS_NOT_EXISTS) + (block_num < this->file_blocks.size() && this->file_blocks[block_num]->status == BLOCK_STATUS_NOT_EXISTS)) { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); @@ -129,6 +131,10 @@ size_t SplitFile::Read(char *buf, size_t buf_size, size_t offset) sem_timedwait(&this->block_ready, &ts); } + // If complete and block_num is past the end, no more data + if (block_num >= this->file_blocks.size()) + break; + block = this->file_blocks[block_num]; } @@ -136,7 +142,7 @@ size_t SplitFile::Read(char *buf, size_t buf_size, size_t offset) // forward and won't read previously already read blocks. For safety, keeping only current block and 2 previous blocks for (int j=0; j < first_block_num - 13; j++) { - if (this->file_blocks[j]->status == BLOCK_STATUS_CREATED) + if (this->file_blocks[j] != nullptr && this->file_blocks[j]->status == BLOCK_STATUS_CREATED) { if (this->file_blocks[j]->fd != nullptr) { @@ -145,6 +151,8 @@ size_t SplitFile::Read(char *buf, size_t buf_size, size_t offset) } this->file_blocks[j]->status = BLOCK_STATUS_DELETED; remove(this->file_blocks[j]->block_file.c_str()); + delete (this->file_blocks[j]); + this->file_blocks[j] = nullptr; } } @@ -152,14 +160,14 @@ size_t SplitFile::Read(char *buf, size_t buf_size, size_t offset) return total_bytes_read; } -size_t SplitFile::Write(char *buf, size_t buf_size) +ssize_t SplitFile::Write(char *buf, size_t buf_size) { size_t bytes_written = 0; size_t block_space_remaining; size_t bytes_to_write; char *p = buf; - size_t total_bytes_written = 0; + ssize_t total_bytes_written = 0; size_t remaining_to_write = buf_size; if (this->IsClosed()) @@ -249,8 +257,7 @@ bool SplitFile::IsClosed() FileBlock *SplitFile::NewBlock() { - FileBlock *block = (FileBlock *)malloc(sizeof(FileBlock)); - memset(block, 0, sizeof(FileBlock)); + FileBlock *block = new FileBlock{}; block->is_last = false; block->size = 0; diff --git a/source/split_file.h b/source/split_file.h index 273051f..06f99b9 100644 --- a/source/split_file.h +++ b/source/split_file.h @@ -29,7 +29,7 @@ public: SplitFile(const std::string& path, size_t block_size); ~SplitFile(); size_t Read(char* buf, size_t buf_size, size_t offset); - size_t Write(char* buf, size_t buf_size); + ssize_t Write(char* buf, size_t buf_size); int Open(); int Close(); bool IsClosed(); diff --git a/source/windows.cpp b/source/windows.cpp index 1b89f2c..b0a820a 100644 --- a/source/windows.cpp +++ b/source/windows.cpp @@ -2085,7 +2085,7 @@ namespace Windows sprintf(id, "%s##settings", lang_strings[STR_STOP_SERVER]); if (ImGui::Button(id, ImVec2(410, 0))) { - Actions::StopServer(); + INSTALLER::StopEzRemoteServer(); is_server_started = !INSTALLER::EzRemoteServerVersion().empty(); } ImGui::PopStyleColor(2);