Compare commits
5 Commits
v2025.0626
...
v2025.0629
Author | SHA1 | Date | |
---|---|---|---|
0f5421630a | |||
50fb5f9da6 | |||
8e2611e362 | |||
a1b12fe177 | |||
902e68069a |
@ -36,13 +36,16 @@ target_include_directories(${PROJECT_NAME} PRIVATE
|
|||||||
src/common)
|
src/common)
|
||||||
|
|
||||||
# Find packages
|
# Find packages
|
||||||
find_package(OpenSSL REQUIRED)
|
|
||||||
find_package(Drogon CONFIG REQUIRED)
|
|
||||||
find_package(nlohmann_json REQUIRED)
|
find_package(nlohmann_json REQUIRED)
|
||||||
|
|
||||||
|
# Add module path for FindCPRStatic
|
||||||
|
list(APPEND CMAKE_MODULE_PATH "/usr/local/share/cmake/Modules")
|
||||||
|
|
||||||
|
# Find packages
|
||||||
|
find_package(nlohmann_json REQUIRED)
|
||||||
|
find_package(CPRStatic REQUIRED)
|
||||||
|
|
||||||
# Link libraries
|
# Link libraries
|
||||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||||
nlohmann_json::nlohmann_json Drogon::Drogon
|
nlohmann_json::nlohmann_json
|
||||||
/usr/local/lib/libpgcommon.a /usr/local/lib/libpgport.a
|
cpr::cpr_static)
|
||||||
lzma dl)
|
|
||||||
|
|
1
getpkg/debug_test.txt
Normal file
1
getpkg/debug_test.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
Debug content
|
@ -1,530 +1,317 @@
|
|||||||
#include "GetbinClient.hpp"
|
#include "GetbinClient.hpp"
|
||||||
#include <drogon/HttpClient.h>
|
#include <cpr/cpr.h>
|
||||||
#include <trantor/net/EventLoop.h>
|
|
||||||
#include <openssl/ssl.h>
|
|
||||||
#include <openssl/opensslconf.h>
|
|
||||||
#include <fstream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <string>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <thread>
|
#include <filesystem>
|
||||||
#include <chrono>
|
#include <sstream>
|
||||||
#include <cstdio>
|
|
||||||
#include <map>
|
|
||||||
#include <atomic>
|
|
||||||
#include <mutex>
|
|
||||||
#include <condition_variable>
|
|
||||||
#include <vector>
|
|
||||||
#include <ctime>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
static constexpr const char* SERVER_HOST = "getpkg.xyz";
|
const std::string GetbinClient::SERVER_HOST = "getpkg.xyz";
|
||||||
|
|
||||||
// Initialize SSL to use only secure protocols
|
GetbinClient::GetbinClient() {
|
||||||
static class SSLInitializer {
|
// Initialize CPR (done automatically, but we could add global config here)
|
||||||
public:
|
|
||||||
SSLInitializer() {
|
|
||||||
// Disable SSL 2.0, 3.0, TLS 1.0, and TLS 1.1
|
|
||||||
SSL_load_error_strings();
|
|
||||||
SSL_library_init();
|
|
||||||
// Note: This doesn't completely silence the warning but ensures we're using secure protocols
|
|
||||||
}
|
|
||||||
} ssl_init;
|
|
||||||
|
|
||||||
static std::string find_ca_certificates() {
|
|
||||||
// Common CA certificate locations across different Linux distributions
|
|
||||||
const std::vector<std::string> ca_paths = {
|
|
||||||
"/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Raspbian
|
|
||||||
"/etc/pki/tls/certs/ca-bundle.crt", // Fedora/RHEL/CentOS
|
|
||||||
"/etc/ssl/ca-bundle.pem", // OpenSUSE
|
|
||||||
"/etc/pki/tls/cert.pem", // Fedora/RHEL alternative
|
|
||||||
"/etc/ssl/certs/ca-bundle.crt", // Some distros
|
|
||||||
"/etc/ssl/cert.pem", // Alpine Linux
|
|
||||||
"/usr/local/share/certs/ca-root-nss.crt", // FreeBSD
|
|
||||||
"/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", // CentOS/RHEL 7+
|
|
||||||
"/etc/ca-certificates/extracted/tls-ca-bundle.pem" // Arch Linux
|
|
||||||
};
|
|
||||||
|
|
||||||
for (const auto& path : ca_paths) {
|
|
||||||
std::ifstream file(path);
|
|
||||||
if (file.good()) {
|
|
||||||
file.close();
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GetbinClient::GetbinClient() {}
|
std::string GetbinClient::getUserAgent() const {
|
||||||
|
return "getpkg/1.0";
|
||||||
|
}
|
||||||
|
|
||||||
bool GetbinClient::download(const std::string& toolName, const std::string& arch, const std::string& outPath) {
|
bool GetbinClient::download(const std::string& toolName, const std::string& arch, const std::string& outPath,
|
||||||
bool success = false;
|
ProgressCallback progressCallback) {
|
||||||
bool done = false;
|
try {
|
||||||
std::mutex mtx;
|
std::string url = "https://" + SERVER_HOST + "/object/" + toolName + ":" + arch;
|
||||||
std::condition_variable cv;
|
|
||||||
|
|
||||||
std::thread worker([&]() {
|
|
||||||
trantor::EventLoop loop;
|
|
||||||
|
|
||||||
auto client = drogon::HttpClient::newHttpClient(
|
cpr::Session session;
|
||||||
"https://" + std::string(SERVER_HOST),
|
session.SetUrl(cpr::Url{url});
|
||||||
&loop,
|
session.SetHeader(cpr::Header{{"User-Agent", getUserAgent()}});
|
||||||
false, // useOldTLS = false (disable TLS 1.0/1.1)
|
session.SetTimeout(cpr::Timeout{30000}); // 30 seconds
|
||||||
true // validateCert = true
|
session.SetVerifySsl(cpr::VerifySsl{true});
|
||||||
);
|
|
||||||
|
|
||||||
// Configure SSL certificates for HTTPS
|
// Add progress callback if provided
|
||||||
std::string ca_path = find_ca_certificates();
|
if (progressCallback) {
|
||||||
if (!ca_path.empty()) {
|
session.SetProgressCallback(cpr::ProgressCallback{[progressCallback](cpr::cpr_off_t downloadTotal, cpr::cpr_off_t downloadNow,
|
||||||
// Use addSSLConfigs with proper parameter names for OpenSSL
|
cpr::cpr_off_t uploadTotal, cpr::cpr_off_t uploadNow,
|
||||||
std::vector<std::pair<std::string, std::string>> sslConfigs;
|
intptr_t userdata) -> bool {
|
||||||
sslConfigs.push_back({"VerifyCAFile", ca_path});
|
return progressCallback(static_cast<size_t>(downloadNow), static_cast<size_t>(downloadTotal));
|
||||||
client->addSSLConfigs(sslConfigs);
|
}});
|
||||||
} else {
|
|
||||||
// If no CA certificates found, print warning but continue
|
|
||||||
std::cerr << "[GetbinClient] Warning: No system CA certificates found. SSL verification may fail." << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
client->enableCookies();
|
auto response = session.Get();
|
||||||
client->setUserAgent("getpkg/1.0");
|
|
||||||
|
|
||||||
std::string object_path = "/object/" + toolName + ":" + arch;
|
if (response.status_code == 200) {
|
||||||
|
std::ofstream ofs(outPath, std::ios::binary);
|
||||||
auto req = drogon::HttpRequest::newHttpRequest();
|
if (ofs) {
|
||||||
req->setMethod(drogon::Get);
|
ofs.write(response.text.data(), response.text.size());
|
||||||
req->setPath(object_path);
|
return ofs.good();
|
||||||
|
|
||||||
client->sendRequest(req, [&](drogon::ReqResult result, const drogon::HttpResponsePtr& response) {
|
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
|
||||||
if (result == drogon::ReqResult::Ok && response && response->getStatusCode() == drogon::k200OK) {
|
|
||||||
std::ofstream ofs(outPath, std::ios::binary);
|
|
||||||
if (ofs) {
|
|
||||||
const auto& body = response->getBody();
|
|
||||||
ofs.write(body.data(), body.size());
|
|
||||||
success = ofs.good();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
std::cerr << "[GetbinClient::download] HTTP request failed." << std::endl;
|
|
||||||
}
|
}
|
||||||
done = true;
|
} else if (response.status_code == 404) {
|
||||||
cv.notify_one();
|
// Not found - this is expected for arch fallback
|
||||||
loop.quit();
|
return false;
|
||||||
}, 30.0); // 30 second timeout
|
} else {
|
||||||
|
std::cerr << "[GetbinClient::download] HTTP " << response.status_code << ": " << response.error.message << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
loop.loop();
|
return false;
|
||||||
});
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "[GetbinClient::download] Exception: " << e.what() << std::endl;
|
||||||
// Wait for completion
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
|
||||||
cv.wait(lock, [&] { return done; });
|
|
||||||
}
|
|
||||||
|
|
||||||
worker.join();
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool GetbinClient::upload(const std::string& archivePath, std::string& outUrl, std::string& outHash, const std::string& token) {
|
|
||||||
// Read file first
|
|
||||||
std::ifstream ifs(archivePath, std::ios::binary);
|
|
||||||
if (!ifs) {
|
|
||||||
std::cerr << "[GetbinClient::upload] Failed to open archive file: " << archivePath << std::endl;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
std::string file_content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
|
}
|
||||||
|
|
||||||
// Compose metadata
|
bool GetbinClient::upload(const std::string& archivePath, std::string& outUrl, std::string& outHash,
|
||||||
json metadata = { {"labeltags", json::array()} };
|
const std::string& token, ProgressCallback progressCallback) {
|
||||||
std::string filename = archivePath.substr(archivePath.find_last_of("/\\") + 1);
|
try {
|
||||||
size_t dot = filename.find('.');
|
std::string url = "https://" + SERVER_HOST + "/upload";
|
||||||
std::string labeltag = dot != std::string::npos ? filename.substr(0, dot) : filename;
|
|
||||||
metadata["labeltags"].push_back(labeltag);
|
|
||||||
|
|
||||||
bool success = false;
|
|
||||||
bool done = false;
|
|
||||||
std::mutex mtx;
|
|
||||||
std::condition_variable cv;
|
|
||||||
|
|
||||||
std::thread worker([&]() {
|
|
||||||
trantor::EventLoop loop;
|
|
||||||
|
|
||||||
auto client = drogon::HttpClient::newHttpClient(
|
cpr::Session session;
|
||||||
"https://" + std::string(SERVER_HOST),
|
session.SetUrl(cpr::Url{url});
|
||||||
&loop,
|
session.SetHeader(cpr::Header{
|
||||||
false, // useOldTLS = false (disable TLS 1.0/1.1)
|
{"User-Agent", getUserAgent()},
|
||||||
true // validateCert = true
|
{"Authorization", "Bearer " + token}
|
||||||
);
|
});
|
||||||
|
session.SetTimeout(cpr::Timeout{300000}); // 5 minutes for uploads
|
||||||
|
session.SetVerifySsl(cpr::VerifySsl{true});
|
||||||
|
|
||||||
// Configure SSL certificates
|
|
||||||
std::string ca_path = find_ca_certificates();
|
|
||||||
std::vector<std::pair<std::string, std::string>> sslConfigs;
|
|
||||||
if (!ca_path.empty()) {
|
|
||||||
sslConfigs.push_back({"VerifyCAFile", ca_path});
|
|
||||||
}
|
|
||||||
// Configure SSL for secure connections
|
|
||||||
client->addSSLConfigs(sslConfigs);
|
|
||||||
|
|
||||||
if (ca_path.empty()) {
|
// Extract tool name and arch from archive path for labeltags
|
||||||
std::cerr << "[GetbinClient] Warning: No system CA certificates found. SSL verification may fail." << std::endl;
|
// Archive path format: /path/to/tool-name:arch.tgz or similar
|
||||||
|
std::string archiveName = std::filesystem::path(archivePath).filename().string();
|
||||||
|
std::string toolNameArch = archiveName;
|
||||||
|
if (toolNameArch.ends_with(".tgz")) {
|
||||||
|
toolNameArch = toolNameArch.substr(0, toolNameArch.length() - 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
client->enableCookies();
|
// Create metadata JSON with labeltags
|
||||||
client->setUserAgent("getpkg/1.0");
|
json metadata;
|
||||||
|
metadata["labeltags"] = json::array({toolNameArch});
|
||||||
|
|
||||||
// Create upload file from memory content
|
// Set up multipart form with file and metadata
|
||||||
// First save content to a temporary file since UploadFile expects a file path
|
session.SetMultipart(cpr::Multipart{
|
||||||
std::string temp_file = "/tmp/getpkg_upload_" + std::to_string(std::time(nullptr)) + ".tgz";
|
cpr::Part{"file", cpr::File{archivePath}},
|
||||||
std::ofstream temp_ofs(temp_file, std::ios::binary);
|
cpr::Part{"metadata", metadata.dump(), "application/json"}
|
||||||
if (!temp_ofs) {
|
});
|
||||||
std::cerr << "[GetbinClient::upload] Failed to create temporary file: " << temp_file << std::endl;
|
|
||||||
success = false;
|
// Add progress callback if provided
|
||||||
done = true;
|
if (progressCallback) {
|
||||||
cv.notify_one();
|
session.SetProgressCallback(cpr::ProgressCallback{[progressCallback](cpr::cpr_off_t downloadTotal, cpr::cpr_off_t downloadNow,
|
||||||
loop.quit();
|
cpr::cpr_off_t uploadTotal, cpr::cpr_off_t uploadNow,
|
||||||
return;
|
intptr_t userdata) -> bool {
|
||||||
|
return progressCallback(static_cast<size_t>(uploadNow), static_cast<size_t>(uploadTotal));
|
||||||
|
}});
|
||||||
}
|
}
|
||||||
temp_ofs.write(file_content.data(), file_content.size());
|
|
||||||
temp_ofs.close();
|
|
||||||
|
|
||||||
// Create upload request with file
|
auto response = session.Put();
|
||||||
drogon::UploadFile upload_file(temp_file);
|
|
||||||
|
|
||||||
auto req = drogon::HttpRequest::newFileUploadRequest({upload_file});
|
if (response.status_code == 200) {
|
||||||
req->setMethod(drogon::Put);
|
try {
|
||||||
req->setPath("/upload");
|
auto resp_json = json::parse(response.text);
|
||||||
req->addHeader("Authorization", "Bearer " + token);
|
if (resp_json.contains("hash") && resp_json.contains("result") && resp_json["result"] == "success") {
|
||||||
|
outUrl = "https://" + SERVER_HOST + "/object/" + resp_json["hash"].get<std::string>();
|
||||||
// Add metadata as form parameter
|
outHash = resp_json["hash"].get<std::string>();
|
||||||
req->setParameter("metadata", metadata.dump());
|
return true;
|
||||||
|
|
||||||
client->sendRequest(req, [&](drogon::ReqResult result, const drogon::HttpResponsePtr& response) {
|
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
|
||||||
if (result == drogon::ReqResult::Ok && response) {
|
|
||||||
int status_code = static_cast<int>(response->getStatusCode());
|
|
||||||
std::string response_body(response->getBody());
|
|
||||||
|
|
||||||
if (status_code == 200 || status_code == 201) {
|
|
||||||
try {
|
|
||||||
auto resp_json = json::parse(response_body);
|
|
||||||
if (resp_json.contains("url")) outUrl = resp_json["url"].get<std::string>();
|
|
||||||
if (resp_json.contains("hash")) outHash = resp_json["hash"].get<std::string>();
|
|
||||||
success = true;
|
|
||||||
} catch (const std::exception& e) {
|
|
||||||
std::cerr << "[GetbinClient::upload] Failed to parse JSON response: " << e.what() << std::endl;
|
|
||||||
std::cerr << "[GetbinClient::upload] Response body: " << response_body << std::endl;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
std::cerr << "[GetbinClient::upload] HTTP error: status code " << status_code << std::endl;
|
|
||||||
std::cerr << "[GetbinClient::upload] Response body: " << response_body << std::endl;
|
|
||||||
}
|
}
|
||||||
} else {
|
} catch (const json::exception& e) {
|
||||||
std::cerr << "[GetbinClient::upload] HTTP /upload request failed." << std::endl;
|
// Try to extract from plain text response
|
||||||
|
outUrl = "";
|
||||||
|
outHash = response.text;
|
||||||
|
// Remove trailing newline if present
|
||||||
|
if (!outHash.empty() && outHash.back() == '\n') {
|
||||||
|
outHash.pop_back();
|
||||||
|
}
|
||||||
|
return !outHash.empty();
|
||||||
}
|
}
|
||||||
done = true;
|
} else {
|
||||||
cv.notify_one();
|
std::cerr << "[GetbinClient::upload] HTTP " << response.status_code << ": " << response.error.message << std::endl;
|
||||||
loop.quit();
|
if (!response.text.empty()) {
|
||||||
}, 60.0); // 60 second timeout
|
std::cerr << "[GetbinClient::upload] Response: " << response.text << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
loop.loop();
|
return false;
|
||||||
|
} catch (const std::exception& e) {
|
||||||
// Clean up temporary file
|
std::cerr << "[GetbinClient::upload] Exception: " << e.what() << std::endl;
|
||||||
std::remove(temp_file.c_str());
|
return false;
|
||||||
});
|
|
||||||
|
|
||||||
// Wait for completion
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
|
||||||
cv.wait(lock, [&] { return done; });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
worker.join();
|
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GetbinClient::getHash(const std::string& toolName, const std::string& arch, std::string& outHash) {
|
bool GetbinClient::getHash(const std::string& toolName, const std::string& arch, std::string& outHash) {
|
||||||
bool success = false;
|
try {
|
||||||
bool done = false;
|
std::string url = "https://" + SERVER_HOST + "/hash/" + toolName + ":" + arch;
|
||||||
std::mutex mtx;
|
|
||||||
std::condition_variable cv;
|
|
||||||
|
|
||||||
std::thread worker([&]() {
|
|
||||||
trantor::EventLoop loop;
|
|
||||||
|
|
||||||
auto client = drogon::HttpClient::newHttpClient(
|
auto response = cpr::Get(cpr::Url{url},
|
||||||
"https://" + std::string(SERVER_HOST),
|
cpr::Header{{"User-Agent", getUserAgent()}},
|
||||||
&loop,
|
cpr::Timeout{10000}, // 10 seconds
|
||||||
false, // useOldTLS = false (disable TLS 1.0/1.1)
|
cpr::VerifySsl{true});
|
||||||
true // validateCert = true
|
|
||||||
);
|
|
||||||
|
|
||||||
// Configure SSL certificates
|
if (response.status_code == 200) {
|
||||||
std::string ca_path = find_ca_certificates();
|
try {
|
||||||
std::vector<std::pair<std::string, std::string>> sslConfigs;
|
// Try JSON first
|
||||||
if (!ca_path.empty()) {
|
auto resp_json = json::parse(response.text);
|
||||||
sslConfigs.push_back({"VerifyCAFile", ca_path});
|
if (resp_json.contains("hash")) {
|
||||||
}
|
outHash = resp_json["hash"].get<std::string>();
|
||||||
// Configure SSL for secure connections
|
return true;
|
||||||
client->addSSLConfigs(sslConfigs);
|
|
||||||
|
|
||||||
if (ca_path.empty()) {
|
|
||||||
std::cerr << "[GetbinClient] Warning: No system CA certificates found. SSL verification may fail." << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
client->enableCookies();
|
|
||||||
client->setUserAgent("getpkg/1.0");
|
|
||||||
|
|
||||||
std::string hash_path = "/hash/" + toolName + ":" + arch;
|
|
||||||
|
|
||||||
auto req = drogon::HttpRequest::newHttpRequest();
|
|
||||||
req->setMethod(drogon::Get);
|
|
||||||
req->setPath(hash_path);
|
|
||||||
|
|
||||||
client->sendRequest(req, [&](drogon::ReqResult result, const drogon::HttpResponsePtr& response) {
|
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
|
||||||
if (result == drogon::ReqResult::Ok && response && response->getStatusCode() == drogon::k200OK) {
|
|
||||||
std::string response_body(response->getBody());
|
|
||||||
|
|
||||||
// Try to parse hash from response body
|
|
||||||
try {
|
|
||||||
// Try JSON first
|
|
||||||
auto resp_json = json::parse(response_body);
|
|
||||||
if (resp_json.contains("hash")) {
|
|
||||||
outHash = resp_json["hash"].get<std::string>();
|
|
||||||
success = true;
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
// Not JSON, treat as plain text
|
|
||||||
outHash = response_body;
|
|
||||||
// Remove trailing newline if present
|
|
||||||
if (!outHash.empty() && outHash.back() == '\n') {
|
|
||||||
outHash.pop_back();
|
|
||||||
}
|
|
||||||
success = !outHash.empty();
|
|
||||||
}
|
}
|
||||||
|
} catch (const json::exception&) {
|
||||||
|
// Not JSON, treat as plain text
|
||||||
|
outHash = response.text;
|
||||||
|
// Remove trailing newline if present
|
||||||
|
if (!outHash.empty() && outHash.back() == '\n') {
|
||||||
|
outHash.pop_back();
|
||||||
|
}
|
||||||
|
return !outHash.empty();
|
||||||
}
|
}
|
||||||
done = true;
|
} else if (response.status_code == 404) {
|
||||||
cv.notify_one();
|
// Not found - this is expected for non-existent tools/archs
|
||||||
loop.quit();
|
return false;
|
||||||
}, 10.0); // 10 second timeout
|
} else {
|
||||||
|
std::cerr << "[GetbinClient::getHash] HTTP " << response.status_code << ": " << response.error.message << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
loop.loop();
|
return false;
|
||||||
});
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "[GetbinClient::getHash] Exception: " << e.what() << std::endl;
|
||||||
// Wait for completion
|
return false;
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
|
||||||
cv.wait(lock, [&] { return done; });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
worker.join();
|
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GetbinClient::deleteObject(const std::string& hash, const std::string& token) {
|
bool GetbinClient::deleteObject(const std::string& hash, const std::string& token) {
|
||||||
bool success = false;
|
try {
|
||||||
bool done = false;
|
std::string url = "https://" + SERVER_HOST + "/deleteobject?hash=" + hash;
|
||||||
std::mutex mtx;
|
|
||||||
std::condition_variable cv;
|
|
||||||
|
|
||||||
std::thread worker([&]() {
|
|
||||||
trantor::EventLoop loop;
|
|
||||||
|
|
||||||
auto client = drogon::HttpClient::newHttpClient(
|
auto response = cpr::Get(cpr::Url{url},
|
||||||
"https://" + std::string(SERVER_HOST),
|
cpr::Header{
|
||||||
&loop,
|
{"User-Agent", getUserAgent()},
|
||||||
false, // useOldTLS = false (disable TLS 1.0/1.1)
|
{"Authorization", "Bearer " + token}
|
||||||
true // validateCert = true
|
},
|
||||||
);
|
cpr::Timeout{30000}, // 30 seconds
|
||||||
|
cpr::VerifySsl{true});
|
||||||
|
|
||||||
// Configure SSL certificates
|
if (response.status_code == 200) {
|
||||||
std::string ca_path = find_ca_certificates();
|
return true;
|
||||||
std::vector<std::pair<std::string, std::string>> sslConfigs;
|
} else {
|
||||||
if (!ca_path.empty()) {
|
std::cerr << "[GetbinClient::deleteObject] HTTP " << response.status_code << ": " << response.error.message << std::endl;
|
||||||
sslConfigs.push_back({"VerifyCAFile", ca_path});
|
if (!response.text.empty()) {
|
||||||
}
|
std::cerr << "[GetbinClient::deleteObject] Response: " << response.text << std::endl;
|
||||||
// Configure SSL for secure connections
|
|
||||||
client->addSSLConfigs(sslConfigs);
|
|
||||||
|
|
||||||
if (ca_path.empty()) {
|
|
||||||
std::cerr << "[GetbinClient] Warning: No system CA certificates found. SSL verification may fail." << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
client->enableCookies();
|
|
||||||
client->setUserAgent("getpkg/1.0");
|
|
||||||
|
|
||||||
std::string delete_path = "/deleteobject?hash=" + hash;
|
|
||||||
|
|
||||||
auto req = drogon::HttpRequest::newHttpRequest();
|
|
||||||
req->setMethod(drogon::Get);
|
|
||||||
req->setPath(delete_path);
|
|
||||||
req->addHeader("Authorization", "Bearer " + token);
|
|
||||||
|
|
||||||
client->sendRequest(req, [&](drogon::ReqResult result, const drogon::HttpResponsePtr& response) {
|
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
|
||||||
if (result == drogon::ReqResult::Ok && response) {
|
|
||||||
int status_code = static_cast<int>(response->getStatusCode());
|
|
||||||
std::string response_body(response->getBody());
|
|
||||||
|
|
||||||
if (status_code == 200) {
|
|
||||||
// Check if the response indicates success
|
|
||||||
try {
|
|
||||||
auto resp_json = json::parse(response_body);
|
|
||||||
if (resp_json.contains("result") && resp_json["result"] == "success") {
|
|
||||||
success = true;
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
// If not JSON, assume success if 200 OK
|
|
||||||
success = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
std::cerr << "[GetbinClient::deleteObject] HTTP error: status code " << status_code << std::endl;
|
|
||||||
std::cerr << "[GetbinClient::deleteObject] Response body: " << response_body << std::endl;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
std::cerr << "[GetbinClient::deleteObject] HTTP request failed." << std::endl;
|
|
||||||
}
|
}
|
||||||
done = true;
|
|
||||||
cv.notify_one();
|
|
||||||
loop.quit();
|
|
||||||
}, 10.0); // 10 second timeout
|
|
||||||
|
|
||||||
loop.loop();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Wait for completion
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
|
||||||
cv.wait(lock, [&] { return done; });
|
|
||||||
}
|
|
||||||
|
|
||||||
worker.join();
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
bool GetbinClient::listPackages(std::vector<std::string>& outPackages) {
|
|
||||||
outPackages.clear();
|
|
||||||
|
|
||||||
// Set up SSL configuration
|
|
||||||
std::string ca_path = find_ca_certificates();
|
|
||||||
|
|
||||||
bool success = false;
|
|
||||||
bool done = false;
|
|
||||||
std::mutex mtx;
|
|
||||||
std::condition_variable cv;
|
|
||||||
|
|
||||||
std::thread worker([&]() {
|
|
||||||
trantor::EventLoop loop;
|
|
||||||
|
|
||||||
auto client = drogon::HttpClient::newHttpClient(
|
|
||||||
"https://" + std::string(SERVER_HOST),
|
|
||||||
&loop,
|
|
||||||
false, // useOldTLS = false (disable TLS 1.0/1.1)
|
|
||||||
true // validateCert = true
|
|
||||||
);
|
|
||||||
std::vector<std::pair<std::string, std::string>> sslConfigs;
|
|
||||||
if (!ca_path.empty()) {
|
|
||||||
sslConfigs.push_back({"VerifyCAFile", ca_path});
|
|
||||||
}
|
}
|
||||||
// Configure SSL for secure connections
|
|
||||||
client->addSSLConfigs(sslConfigs);
|
|
||||||
|
|
||||||
auto req = drogon::HttpRequest::newHttpRequest();
|
return false;
|
||||||
req->setMethod(drogon::Get);
|
} catch (const std::exception& e) {
|
||||||
req->setPath("/dir");
|
std::cerr << "[GetbinClient::deleteObject] Exception: " << e.what() << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GetbinClient::listPackages(std::vector<std::string>& outPackages) {
|
||||||
|
try {
|
||||||
|
std::string url = "https://" + SERVER_HOST + "/dir";
|
||||||
|
|
||||||
client->sendRequest(req, [&](drogon::ReqResult result, const drogon::HttpResponsePtr& response) {
|
auto response = cpr::Get(cpr::Url{url},
|
||||||
if (result == drogon::ReqResult::Ok) {
|
cpr::Header{{"User-Agent", getUserAgent()}},
|
||||||
int status_code = response->getStatusCode();
|
cpr::Timeout{30000}, // 30 seconds
|
||||||
std::string response_body = std::string(response->getBody());
|
cpr::VerifySsl{true});
|
||||||
|
|
||||||
if (status_code == 200) {
|
if (response.status_code == 200) {
|
||||||
try {
|
try {
|
||||||
json json_response = json::parse(response_body);
|
auto resp_json = json::parse(response.text);
|
||||||
|
if (resp_json.contains("entries") && resp_json["entries"].is_array()) {
|
||||||
if (json_response.contains("entries") && json_response["entries"].is_array()) {
|
outPackages.clear();
|
||||||
for (const auto& entry : json_response["entries"]) {
|
std::set<std::string> uniqueTools;
|
||||||
if (entry.contains("labeltags") && entry["labeltags"].is_array()) {
|
|
||||||
for (const auto& labeltag : entry["labeltags"]) {
|
for (const auto& entry : resp_json["entries"]) {
|
||||||
if (labeltag.is_string()) {
|
if (entry.contains("labeltags") && entry["labeltags"].is_array()) {
|
||||||
std::string name = labeltag.get<std::string>();
|
for (const auto& labeltag : entry["labeltags"]) {
|
||||||
// Extract tool name (remove architecture suffix if present)
|
if (labeltag.is_string()) {
|
||||||
size_t colon_pos = name.find(":");
|
std::string tag = labeltag.get<std::string>();
|
||||||
if (colon_pos != std::string::npos) {
|
// Extract tool name from "tool:arch" format
|
||||||
name = name.substr(0, colon_pos);
|
size_t colonPos = tag.find(":");
|
||||||
}
|
if (colonPos != std::string::npos) {
|
||||||
|
std::string toolName = tag.substr(0, colonPos);
|
||||||
// Skip empty names
|
if (!toolName.empty()) {
|
||||||
if (name.empty()) continue;
|
uniqueTools.insert(toolName);
|
||||||
|
|
||||||
// Add to list if not already present
|
|
||||||
if (std::find(outPackages.begin(), outPackages.end(), name) == outPackages.end()) {
|
|
||||||
outPackages.push_back(name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
success = true;
|
|
||||||
}
|
}
|
||||||
} catch (const std::exception& e) {
|
|
||||||
std::cerr << "[GetbinClient::listPackages] JSON parse error: " << e.what() << std::endl;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
std::cerr << "[GetbinClient::listPackages] HTTP error: status code " << status_code << std::endl;
|
// Convert set to vector
|
||||||
|
for (const auto& tool : uniqueTools) {
|
||||||
|
outPackages.push_back(tool);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} catch (const json::exception&) {
|
||||||
std::cerr << "[GetbinClient::listPackages] HTTP request failed." << std::endl;
|
// Try to parse as newline-separated list
|
||||||
}
|
outPackages.clear();
|
||||||
done = true;
|
std::istringstream stream(response.text);
|
||||||
cv.notify_one();
|
std::string line;
|
||||||
loop.quit();
|
while (std::getline(stream, line)) {
|
||||||
}, 10.0);
|
if (!line.empty()) {
|
||||||
|
outPackages.push_back(line);
|
||||||
loop.loop();
|
}
|
||||||
});
|
}
|
||||||
|
return !outPackages.empty();
|
||||||
// Wait for completion
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
|
||||||
cv.wait(lock, [&] { return done; });
|
|
||||||
}
|
|
||||||
|
|
||||||
worker.join();
|
|
||||||
|
|
||||||
// Filter out duplicates where we have both toolname and toolname-noarch
|
|
||||||
// Keep the base name and remove the -noarch variant
|
|
||||||
std::vector<std::string> filteredPackages;
|
|
||||||
std::set<std::string> baseNames;
|
|
||||||
|
|
||||||
// First pass: collect all base names (without -noarch)
|
|
||||||
for (const auto& pkg : outPackages) {
|
|
||||||
const std::string suffix = "-noarch";
|
|
||||||
if (pkg.length() < suffix.length() || pkg.substr(pkg.length() - suffix.length()) != suffix) {
|
|
||||||
baseNames.insert(pkg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Second pass: add packages, skipping -noarch variants if base exists
|
|
||||||
for (const auto& pkg : outPackages) {
|
|
||||||
const std::string suffix = "-noarch";
|
|
||||||
if (pkg.length() >= suffix.length() && pkg.substr(pkg.length() - suffix.length()) == suffix) {
|
|
||||||
std::string baseName = pkg.substr(0, pkg.length() - suffix.length());
|
|
||||||
if (baseNames.find(baseName) == baseNames.end()) {
|
|
||||||
filteredPackages.push_back(pkg); // Keep -noarch only if no base version
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
filteredPackages.push_back(pkg); // Always keep base versions
|
std::cerr << "[GetbinClient::listPackages] HTTP " << response.status_code << ": " << response.error.message << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "[GetbinClient::listPackages] Exception: " << e.what() << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GetbinClient::listAllEntries(std::vector<std::pair<std::string, std::vector<std::string>>>& outEntries) {
|
||||||
|
try {
|
||||||
|
std::string url = "https://" + SERVER_HOST + "/dir";
|
||||||
|
|
||||||
|
auto response = cpr::Get(cpr::Url{url},
|
||||||
|
cpr::Header{{"User-Agent", getUserAgent()}},
|
||||||
|
cpr::Timeout{30000}, // 30 seconds
|
||||||
|
cpr::VerifySsl{true});
|
||||||
|
|
||||||
|
if (response.status_code == 200) {
|
||||||
|
try {
|
||||||
|
auto resp_json = json::parse(response.text);
|
||||||
|
if (resp_json.contains("entries") && resp_json["entries"].is_array()) {
|
||||||
|
outEntries.clear();
|
||||||
|
|
||||||
|
for (const auto& entry : resp_json["entries"]) {
|
||||||
|
if (entry.contains("hash") && entry.contains("labeltags") &&
|
||||||
|
entry["hash"].is_string() && entry["labeltags"].is_array()) {
|
||||||
|
|
||||||
|
std::string hash = entry["hash"].get<std::string>();
|
||||||
|
std::vector<std::string> labeltags;
|
||||||
|
|
||||||
|
for (const auto& tag : entry["labeltags"]) {
|
||||||
|
if (tag.is_string()) {
|
||||||
|
labeltags.push_back(tag.get<std::string>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
outEntries.push_back({hash, labeltags});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} catch (const json::exception& e) {
|
||||||
|
std::cerr << "[GetbinClient::listAllEntries] JSON parse error: " << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
std::cerr << "[GetbinClient::listAllEntries] HTTP " << response.status_code << ": " << response.error.message << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "[GetbinClient::listAllEntries] Exception: " << e.what() << std::endl;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
outPackages = std::move(filteredPackages);
|
|
||||||
|
|
||||||
// Sort the packages for better display
|
|
||||||
std::sort(outPackages.begin(), outPackages.end());
|
|
||||||
|
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,25 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
class GetbinClient {
|
class GetbinClient {
|
||||||
public:
|
public:
|
||||||
GetbinClient();
|
GetbinClient();
|
||||||
bool download(const std::string& toolName, const std::string& arch, const std::string& outPath);
|
|
||||||
bool upload(const std::string& archivePath, std::string& outUrl, std::string& outHash, const std::string& token);
|
// Progress callback: (downloaded_bytes, total_bytes) -> should_continue
|
||||||
|
using ProgressCallback = std::function<bool(size_t, size_t)>;
|
||||||
|
|
||||||
|
bool download(const std::string& toolName, const std::string& arch, const std::string& outPath,
|
||||||
|
ProgressCallback progressCallback = nullptr);
|
||||||
|
bool upload(const std::string& archivePath, std::string& outUrl, std::string& outHash, const std::string& token,
|
||||||
|
ProgressCallback progressCallback = nullptr);
|
||||||
bool getHash(const std::string& toolName, const std::string& arch, std::string& outHash);
|
bool getHash(const std::string& toolName, const std::string& arch, std::string& outHash);
|
||||||
bool deleteObject(const std::string& hash, const std::string& token);
|
bool deleteObject(const std::string& hash, const std::string& token);
|
||||||
bool listPackages(std::vector<std::string>& outPackages);
|
bool listPackages(std::vector<std::string>& outPackages);
|
||||||
};
|
bool listAllEntries(std::vector<std::pair<std::string, std::vector<std::string>>>& outEntries);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const std::string SERVER_HOST;
|
||||||
|
std::string getUserAgent() const;
|
||||||
|
};
|
||||||
|
@ -200,27 +200,43 @@ int install_tool(int argc, char* argv[]) {
|
|||||||
// Download tool - try arch-specific version first, then universal fallback
|
// Download tool - try arch-specific version first, then universal fallback
|
||||||
GetbinClient getbin2;
|
GetbinClient getbin2;
|
||||||
std::string downloadArch = arch;
|
std::string downloadArch = arch;
|
||||||
//std::cout << "Downloading " << toolName << ":" << arch << "..." << std::endl;
|
|
||||||
if (!getbin2.download(toolName, arch, archivePath.string())) {
|
// Progress callback for downloads
|
||||||
|
auto progressCallback = [&toolName](size_t downloaded, size_t total) -> bool {
|
||||||
|
if (total > 0) {
|
||||||
|
int percent = (downloaded * 100) / total;
|
||||||
|
std::cout << "\rDownloading " << toolName << "... " << percent << "%" << std::flush;
|
||||||
|
} else {
|
||||||
|
std::cout << "\rDownloading " << toolName << "... " << downloaded << " bytes" << std::flush;
|
||||||
|
}
|
||||||
|
return true; // Continue download
|
||||||
|
};
|
||||||
|
|
||||||
|
std::cout << "Downloading " << toolName << "..." << std::flush;
|
||||||
|
if (!getbin2.download(toolName, arch, archivePath.string(), progressCallback)) {
|
||||||
// Try universal version as fallback
|
// Try universal version as fallback
|
||||||
//std::cout << "Arch-specific version not found, trying universal version..." << std::endl;
|
std::cout << "\rArch-specific version not found, trying universal..." << std::endl;
|
||||||
//std::cout << "Downloading " << toolName << ":universal..." << std::endl;
|
if (!getbin2.download(toolName, "universal", archivePath.string(), progressCallback)) {
|
||||||
if (!getbin2.download(toolName, "universal", archivePath.string())) {
|
std::cerr << "\rFailed to download tool archive (tried both " << arch << " and universal)." << std::endl;
|
||||||
std::cerr << "Failed to download tool archive (tried both " << arch << " and universal)." << std::endl;
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
downloadArch = "universal";
|
downloadArch = "universal";
|
||||||
}
|
}
|
||||||
|
std::cout << "\rDownloading " << toolName << "... done" << std::endl;
|
||||||
|
|
||||||
// Unpack tool
|
// Unpack tool
|
||||||
|
std::cout << "Unpacking..." << std::flush;
|
||||||
if (!common::unpack_tgz(archivePath.string(), binDir.string())) {
|
if (!common::unpack_tgz(archivePath.string(), binDir.string())) {
|
||||||
std::cerr << "Failed to unpack tool archive." << std::endl;
|
std::cerr << "\rFailed to unpack tool archive." << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
std::cout << "\rUnpacking... done" << std::endl;
|
||||||
|
|
||||||
// Add to PATH and autocomplete
|
// Add to PATH and autocomplete
|
||||||
|
std::cout << "Configuring..." << std::flush;
|
||||||
scriptManager.addToolEntry(toolName, binDir.string());
|
scriptManager.addToolEntry(toolName, binDir.string());
|
||||||
scriptManager.addAutocomplete(toolName);
|
scriptManager.addAutocomplete(toolName);
|
||||||
|
std::cout << "\rConfiguring... done" << std::endl;
|
||||||
|
|
||||||
// Get tool info
|
// Get tool info
|
||||||
std::string hash;
|
std::string hash;
|
||||||
@ -314,10 +330,24 @@ int publish_tool(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
GetbinClient getbin;
|
GetbinClient getbin;
|
||||||
std::string url, hash;
|
std::string url, hash;
|
||||||
if (!getbin.upload(archivePath.string(), url, hash, token)) {
|
|
||||||
std::cerr << "Failed to upload archive." << std::endl;
|
// Progress callback for upload
|
||||||
|
auto uploadProgressCallback = [](size_t uploaded, size_t total) -> bool {
|
||||||
|
if (total > 0) {
|
||||||
|
int percent = (uploaded * 100) / total;
|
||||||
|
std::cout << "\rUploading... " << percent << "%" << std::flush;
|
||||||
|
} else {
|
||||||
|
std::cout << "\rUploading... " << uploaded << " bytes" << std::flush;
|
||||||
|
}
|
||||||
|
return true; // Continue upload
|
||||||
|
};
|
||||||
|
|
||||||
|
std::cout << "Uploading..." << std::flush;
|
||||||
|
if (!getbin.upload(archivePath.string(), url, hash, token, uploadProgressCallback)) {
|
||||||
|
std::cerr << "\rFailed to upload archive." << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
std::cout << "\rUploading... done" << std::endl;
|
||||||
std::cout << "Published! URL: " << url << "\nHash: " << hash << std::endl;
|
std::cout << "Published! URL: " << url << "\nHash: " << hash << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -326,75 +356,163 @@ int update_tool(int argc, char* argv[]) {
|
|||||||
std::string home = get_home();
|
std::string home = get_home();
|
||||||
std::filesystem::path configDir = std::filesystem::path(home) / ".config/getpkg";
|
std::filesystem::path configDir = std::filesystem::path(home) / ".config/getpkg";
|
||||||
|
|
||||||
// Collect all installed tools
|
// Structure to hold tool information
|
||||||
std::vector<std::tuple<std::string, std::string, std::string>> updateResults; // name, status, version
|
struct ToolInfo {
|
||||||
|
std::string name;
|
||||||
// Capture stdout to process install_tool output
|
std::string localHash;
|
||||||
auto processToolUpdate = [&](const std::string& toolName) -> std::tuple<std::string, std::string> {
|
std::string remoteHash;
|
||||||
// Redirect stdout and stderr to capture output
|
std::string arch;
|
||||||
std::stringstream buffer;
|
std::string version;
|
||||||
std::stringstream errBuffer;
|
bool needsUpdate = false;
|
||||||
std::streambuf* oldOut = std::cout.rdbuf(buffer.rdbuf());
|
std::string status = "Up to date";
|
||||||
std::streambuf* oldErr = std::cerr.rdbuf(errBuffer.rdbuf());
|
|
||||||
|
|
||||||
char* toolArgv[] = {argv[0], (char*)"install", (char*)toolName.c_str()};
|
|
||||||
int result = install_tool(3, toolArgv);
|
|
||||||
|
|
||||||
// Restore stdout and stderr
|
|
||||||
std::cout.rdbuf(oldOut);
|
|
||||||
std::cerr.rdbuf(oldErr);
|
|
||||||
|
|
||||||
std::string output = buffer.str();
|
|
||||||
std::string status = "Failed";
|
|
||||||
std::string version = "-";
|
|
||||||
|
|
||||||
if (result == 0) {
|
|
||||||
if (output.find("is already up to date") != std::string::npos) {
|
|
||||||
status = "Up to date";
|
|
||||||
} else if (output.find("Installed " + toolName + " successfully") != std::string::npos) {
|
|
||||||
// Check if it was an update or fresh install
|
|
||||||
if (output.find("Updating " + toolName) != std::string::npos) {
|
|
||||||
status = "Updated";
|
|
||||||
} else {
|
|
||||||
status = "Installed";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to get version from config
|
|
||||||
std::filesystem::path toolInfoPath = configDir / (toolName + ".json");
|
|
||||||
if (std::filesystem::exists(toolInfoPath)) {
|
|
||||||
std::ifstream tfile(toolInfoPath);
|
|
||||||
json toolInfo;
|
|
||||||
tfile >> toolInfo;
|
|
||||||
version = toolInfo.value("version", "-");
|
|
||||||
if (!version.empty() && version.back() == '\n') version.pop_back();
|
|
||||||
// If version is empty, try to show something useful
|
|
||||||
if (version.empty() || version == "-") {
|
|
||||||
version = "installed";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::make_tuple(status, version);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// First update getpkg itself
|
std::vector<ToolInfo> tools;
|
||||||
auto [getpkgStatus, getpkgVersion] = processToolUpdate("getpkg");
|
|
||||||
updateResults.push_back(std::make_tuple("getpkg", getpkgStatus, getpkgVersion));
|
|
||||||
|
|
||||||
// Then update all other installed tools
|
// Collect all installed tools
|
||||||
if (std::filesystem::exists(configDir)) {
|
if (std::filesystem::exists(configDir)) {
|
||||||
for (const auto& entry : std::filesystem::directory_iterator(configDir)) {
|
for (const auto& entry : std::filesystem::directory_iterator(configDir)) {
|
||||||
if (entry.path().extension() == ".json") {
|
if (entry.path().extension() == ".json") {
|
||||||
std::string tname = entry.path().stem();
|
std::string tname = entry.path().stem();
|
||||||
if (tname != "getpkg") { // Skip getpkg since we already did it
|
|
||||||
auto [status, version] = processToolUpdate(tname);
|
ToolInfo tool;
|
||||||
updateResults.push_back(std::make_tuple(tname, status, version));
|
tool.name = tname;
|
||||||
|
|
||||||
|
// Read local tool info
|
||||||
|
std::ifstream tfile(entry.path());
|
||||||
|
if (tfile.good()) {
|
||||||
|
json toolInfo;
|
||||||
|
tfile >> toolInfo;
|
||||||
|
tool.localHash = toolInfo.value("hash", "");
|
||||||
|
tool.arch = toolInfo.value("arch", get_arch());
|
||||||
|
tool.version = toolInfo.value("version", "-");
|
||||||
|
if (!tool.version.empty() && tool.version.back() == '\n') {
|
||||||
|
tool.version.pop_back();
|
||||||
|
}
|
||||||
|
if (tool.version.empty() || tool.version == "-") {
|
||||||
|
tool.version = "installed";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tools.push_back(tool);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tools.empty()) {
|
||||||
|
std::cout << "No tools installed." << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 1: Check for updates (with progress)
|
||||||
|
std::cout << "Checking " << tools.size() << " tools for updates..." << std::endl;
|
||||||
|
|
||||||
|
GetbinClient getbin;
|
||||||
|
for (size_t i = 0; i < tools.size(); ++i) {
|
||||||
|
auto& tool = tools[i];
|
||||||
|
|
||||||
|
// Show progress
|
||||||
|
std::cout << "\r[" << (i + 1) << "/" << tools.size() << "] Checking " << tool.name << "..." << std::flush;
|
||||||
|
|
||||||
|
// Check remote hash
|
||||||
|
std::string remoteHash;
|
||||||
|
if (getbin.getHash(tool.name, tool.arch, remoteHash) && !remoteHash.empty()) {
|
||||||
|
tool.remoteHash = remoteHash;
|
||||||
|
if (tool.localHash != remoteHash) {
|
||||||
|
tool.needsUpdate = true;
|
||||||
|
tool.status = "Needs update";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
tool.status = "Check failed";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << "\r" << std::string(50, ' ') << "\r" << std::flush; // Clear progress line
|
||||||
|
|
||||||
|
// Step 2: Update tools that need updating
|
||||||
|
std::vector<std::tuple<std::string, std::string, std::string>> updateResults;
|
||||||
|
|
||||||
|
// First update getpkg if it needs updating
|
||||||
|
auto getpkgIt = std::find_if(tools.begin(), tools.end(),
|
||||||
|
[](const ToolInfo& t) { return t.name == "getpkg"; });
|
||||||
|
|
||||||
|
if (getpkgIt != tools.end() && getpkgIt->needsUpdate) {
|
||||||
|
std::cout << "Updating getpkg..." << std::flush;
|
||||||
|
|
||||||
|
// Use install_tool for actual update
|
||||||
|
std::stringstream buffer, errBuffer;
|
||||||
|
std::streambuf* oldOut = std::cout.rdbuf(buffer.rdbuf());
|
||||||
|
std::streambuf* oldErr = std::cerr.rdbuf(errBuffer.rdbuf());
|
||||||
|
|
||||||
|
char* toolArgv[] = {argv[0], (char*)"install", (char*)"getpkg"};
|
||||||
|
int result = install_tool(3, toolArgv);
|
||||||
|
|
||||||
|
std::cout.rdbuf(oldOut);
|
||||||
|
std::cerr.rdbuf(oldErr);
|
||||||
|
|
||||||
|
if (result == 0) {
|
||||||
|
getpkgIt->status = "Updated";
|
||||||
|
std::cout << " Updated" << std::endl;
|
||||||
|
} else {
|
||||||
|
getpkgIt->status = "Failed";
|
||||||
|
std::cout << " Failed" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update other tools
|
||||||
|
int toolsToUpdate = std::count_if(tools.begin(), tools.end(),
|
||||||
|
[](const ToolInfo& t) { return t.needsUpdate && t.name != "getpkg"; });
|
||||||
|
|
||||||
|
if (toolsToUpdate > 0) {
|
||||||
|
std::cout << "Updating " << toolsToUpdate << " tools..." << std::endl;
|
||||||
|
|
||||||
|
int updatedCount = 0;
|
||||||
|
for (auto& tool : tools) {
|
||||||
|
if (tool.needsUpdate && tool.name != "getpkg") {
|
||||||
|
updatedCount++;
|
||||||
|
std::cout << "[" << updatedCount << "/" << toolsToUpdate << "] Updating " << tool.name << "..." << std::flush;
|
||||||
|
|
||||||
|
// Use install_tool for actual update
|
||||||
|
std::stringstream buffer, errBuffer;
|
||||||
|
std::streambuf* oldOut = std::cout.rdbuf(buffer.rdbuf());
|
||||||
|
std::streambuf* oldErr = std::cerr.rdbuf(errBuffer.rdbuf());
|
||||||
|
|
||||||
|
char* toolArgv[] = {argv[0], (char*)"install", (char*)tool.name.c_str()};
|
||||||
|
int result = install_tool(3, toolArgv);
|
||||||
|
|
||||||
|
std::cout.rdbuf(oldOut);
|
||||||
|
std::cerr.rdbuf(oldErr);
|
||||||
|
|
||||||
|
if (result == 0) {
|
||||||
|
tool.status = "Updated";
|
||||||
|
std::cout << " Updated" << std::endl;
|
||||||
|
|
||||||
|
// Re-read version after update
|
||||||
|
std::filesystem::path toolInfoPath = configDir / (tool.name + ".json");
|
||||||
|
if (std::filesystem::exists(toolInfoPath)) {
|
||||||
|
std::ifstream tfile(toolInfoPath);
|
||||||
|
json toolInfo;
|
||||||
|
tfile >> toolInfo;
|
||||||
|
tool.version = toolInfo.value("version", tool.version);
|
||||||
|
if (!tool.version.empty() && tool.version.back() == '\n') {
|
||||||
|
tool.version.pop_back();
|
||||||
|
}
|
||||||
|
if (tool.version.empty() || tool.version == "-") {
|
||||||
|
tool.version = "installed";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
tool.status = "Failed";
|
||||||
|
std::cout << " Failed" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepare results for display
|
||||||
|
for (const auto& tool : tools) {
|
||||||
|
updateResults.push_back(std::make_tuple(tool.name, tool.status, tool.version));
|
||||||
|
}
|
||||||
|
|
||||||
// Display results in a table
|
// Display results in a table
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
std::cout << "+" << std::string(25, '-') << "+" << std::string(15, '-') << "+" << std::string(20, '-') << "+" << std::endl;
|
std::cout << "+" << std::string(25, '-') << "+" << std::string(15, '-') << "+" << std::string(20, '-') << "+" << std::endl;
|
||||||
@ -583,35 +701,34 @@ int unpublish_tool(int argc, char* argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// No specific architecture - unpublish all architectures
|
// No specific architecture - unpublish ALL entries with this tool name
|
||||||
std::vector<std::string> allArchitectures = {"x86_64", "aarch64", "universal"};
|
std::vector<std::pair<std::string, std::vector<std::string>>> allEntries;
|
||||||
std::vector<std::pair<std::string, std::string>> foundPackages;
|
std::vector<std::pair<std::string, std::string>> foundPackages; // (tag, hash)
|
||||||
|
|
||||||
std::cout << "Searching for " << toolName << " across all architectures..." << std::endl;
|
std::cout << "Searching for all entries with label '" << toolName << "'..." << std::endl;
|
||||||
|
|
||||||
// Find all existing versions
|
if (!getbin.listAllEntries(allEntries)) {
|
||||||
for (const auto& arch : allArchitectures) {
|
std::cerr << "Failed to get directory listing from server" << std::endl;
|
||||||
std::string archHash;
|
return 1;
|
||||||
if (getbin.getHash(toolName, arch, archHash) && !archHash.empty()) {
|
}
|
||||||
// Validate hash
|
|
||||||
bool validHash = true;
|
// Find all entries with labeltags starting with toolName:
|
||||||
for (char c : archHash) {
|
for (const auto& entry : allEntries) {
|
||||||
if (!std::isdigit(c)) {
|
const std::string& hash = entry.first;
|
||||||
validHash = false;
|
const std::vector<std::string>& labeltags = entry.second;
|
||||||
break;
|
|
||||||
}
|
for (const std::string& tag : labeltags) {
|
||||||
}
|
if (tag.find(toolName + ":") == 0) {
|
||||||
|
// Found a matching labeltag
|
||||||
if (validHash) {
|
foundPackages.push_back({tag, hash});
|
||||||
foundPackages.push_back({arch, archHash});
|
std::cout << " Found " << tag << " (hash: " << hash << ")" << std::endl;
|
||||||
std::cout << " Found " << toolName << ":" << arch << " (hash: " << archHash << ")" << std::endl;
|
break; // Only count each hash once even if it has multiple matching tags
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (foundPackages.empty()) {
|
if (foundPackages.empty()) {
|
||||||
std::cerr << "No packages found for " << toolName << std::endl;
|
std::cerr << "No packages found for " << toolName << std::endl;
|
||||||
std::cerr << "Searched architectures: x86_64, aarch64, universal" << std::endl;
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -623,7 +740,7 @@ int unpublish_tool(int argc, char* argv[]) {
|
|||||||
int failCount = 0;
|
int failCount = 0;
|
||||||
|
|
||||||
for (const auto& [arch, archHash] : foundPackages) {
|
for (const auto& [arch, archHash] : foundPackages) {
|
||||||
std::cout << " Unpublishing " << toolName << ":" << arch << "... ";
|
std::cout << " Unpublishing " << arch << "... ";
|
||||||
if (getbin.deleteObject(archHash, token)) {
|
if (getbin.deleteObject(archHash, token)) {
|
||||||
std::cout << "OK" << std::endl;
|
std::cout << "OK" << std::endl;
|
||||||
successCount++;
|
successCount++;
|
||||||
@ -706,7 +823,7 @@ int list_packages(int argc, char* argv[]) {
|
|||||||
for (const auto& packageName : availablePackages) {
|
for (const auto& packageName : availablePackages) {
|
||||||
std::string status = "Available";
|
std::string status = "Available";
|
||||||
std::string localVersion = "-";
|
std::string localVersion = "-";
|
||||||
std::string remoteStatus = "✓";
|
std::string remoteStatus = "-";
|
||||||
|
|
||||||
auto it = installedPackages.find(packageName);
|
auto it = installedPackages.find(packageName);
|
||||||
if (it != installedPackages.end()) {
|
if (it != installedPackages.end()) {
|
||||||
|
1
getpkg/test_debug/debug-test
Executable file
1
getpkg/test_debug/debug-test
Executable file
@ -0,0 +1 @@
|
|||||||
|
#!/bin/bash\necho debug
|
1
getpkg/test_debug2/debug-test2
Executable file
1
getpkg/test_debug2/debug-test2
Executable file
@ -0,0 +1 @@
|
|||||||
|
#!/bin/bash\necho debug2
|
1
getpkg/test_display/test-display
Executable file
1
getpkg/test_display/test-display
Executable file
@ -0,0 +1 @@
|
|||||||
|
#!/bin/bash\necho display test
|
1
getpkg/test_multi/test-multi
Executable file
1
getpkg/test_multi/test-multi
Executable file
@ -0,0 +1 @@
|
|||||||
|
#!/bin/bash\necho multi arch
|
1
getpkg/test_robust/test-robust
Executable file
1
getpkg/test_robust/test-robust
Executable file
@ -0,0 +1 @@
|
|||||||
|
#!/bin/bash\necho robust test
|
1
getpkg/test_upload.txt
Normal file
1
getpkg/test_upload.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
test content
|
Reference in New Issue
Block a user