diff --git a/.gitignore b/.gitignore index f13af21ab..a772ebea8 100644 --- a/.gitignore +++ b/.gitignore @@ -271,4 +271,5 @@ blcrack/vcpkgs/ blcrack/out/ blcrack/tolua/ bin/hook/ -blcrack/patchelf/build/ \ No newline at end of file +blcrack/patchelf/build/ +blcrack/test/ \ No newline at end of file diff --git a/blcrack/http/updater.http b/blcrack/http/updater.http index 157f51c2e..54fb294fc 100644 --- a/blcrack/http/updater.http +++ b/blcrack/http/updater.http @@ -17,4 +17,9 @@ POST http://127.0.0.1:7541/get_hash ### POST http://127.0.0.1:7541/reload -BlCrackUpdater \ No newline at end of file +BlCrackUpdater + +### +POST http://127.0.0.1:7541/update?key=BlCrackUpdater&arch=arm64-v8a&file=libcracker.so + +< ../bin/Release-Android-arm64-v8a/libcracker.so \ No newline at end of file diff --git a/blcrack/updater/server.cpp b/blcrack/updater/server.cpp index c8af33712..2c3af6a0e 100644 --- a/blcrack/updater/server.cpp +++ b/blcrack/updater/server.cpp @@ -12,7 +12,7 @@ namespace fs = std::filesystem; -UpdateServer::UpdateServer() { +UpdateServer::UpdateServer() : m_assets_dir("assets") { update_files(); Post("/files", [this](const httplib::Request& req, httplib::Response& res) { @@ -85,6 +85,38 @@ UpdateServer::UpdateServer() { SPDLOG_WARN("Invalid key: {}", req.body); res.status = 400; }); + + Post("/update", [this](const httplib::Request& req, httplib::Response& res) { + SPDLOG_INFO("{} requested update", req.remote_addr); + const auto& key = req.params.equal_range("key").first->second; + const auto& arch = req.params.equal_range("arch").first->second; + const auto& file = req.params.equal_range("file").first->second; + + if (key == m_admin_key) { + FileData data(req.body.begin(), req.body.end()); + auto md5 = hash_file(data); + SPDLOG_INFO("Update {}/{} hash: {}", arch, file, md5); + + m_files[arch][file] = { + .data = std::move(data), + .hash = std::move(md5) + }; + + auto arch_path = m_assets_dir / arch; + if (!exists(arch_path)) { + create_directories(arch_path); + } + + std::ofstream out(arch_path / file, std::ios::binary); + out.write(data.data(), data.size()); + out.close(); + + res.status = 200; + return; + } + + res.status = 400; + }); } UpdateServer& UpdateServer::start(int port, const std::string& admin_key) { @@ -107,11 +139,10 @@ UpdateServer& UpdateServer::Instance() { void UpdateServer::update_files() { m_files.clear(); - fs::path assets_path = fs::current_path() / "assets"; - if (!exists(assets_path)) { + if (!exists(m_assets_dir)) { return; } - for (auto& arch : fs::directory_iterator(assets_path)) { + for (auto& arch : fs::directory_iterator(m_assets_dir)) { if (!fs::is_directory(arch)) { continue; } diff --git a/blcrack/updater/server.hpp b/blcrack/updater/server.hpp index 0ce240553..056f38907 100644 --- a/blcrack/updater/server.hpp +++ b/blcrack/updater/server.hpp @@ -6,6 +6,9 @@ #include #include #include +#include + +namespace fs = std::filesystem; class UpdateServer : protected httplib::Server { using ArchName = std::string; @@ -32,6 +35,7 @@ private: std::string m_admin_key; std::thread m_server_thread; + fs::path m_assets_dir; std::map> m_files; };