Expose file operations to trusted LAN clients

Refs #122
This commit is contained in:
agrynco committed 2026-11-25 21:26:55 +02:00
1 parent 93e672c71d
commit f5b7ddfb53
10 files changed
+885 -162

No files matched your search

+23 -82
View File
@@ -9,7 +9,7 @@
#include "lan_layout.h"
#include "lan_network.h"
#include "lan_wake.h"
#include "lan_clients.h"
#include "lan_client_files.h"
#include <atomic>
#include <mutex>
#include <thread>
@@ -183,24 +183,16 @@ struct LanService::Impl
~Impl() { clients.stop(); stop(); }
void startClients()
{
clients.start([this](const Json &request, const auto &allowed, const auto &send) {
const auto action = request.at("action").get<std::string>();
auto reply = [&](const Json &result) {
const auto text = result.dump();
send(text.data(), DWORD(text.size()));
clients.start([this](const Json &request, const auto &allowed, const auto &send, const auto &receive, const auto &asCaller) {
const auto reachable = [&](const Peer &peer) {
return enabled && peer.files && peer.verified && GetTickCount64() - peer.verified < lan::PeerLifetime;
};
lan::FilesPeer target;
std::string local, key;
auto machines = Json::array();
if (request.at("action") == "peers")
{
std::lock_guard gate(commandGate);
std::lock_guard lock(mutex);
const auto now = GetTickCount64();
const auto reachable = [&](const Peer &peer) {
return enabled && peer.files && peer.verified && now - peer.verified < lan::PeerLifetime;
};
if (action == "peers")
auto machines = Json::array();
{
std::lock_guard gate(commandGate);
std::lock_guard lock(mutex);
refreshFiles = true;
for (const auto &[peer, pair] : trusted.items())
{
@@ -209,74 +201,23 @@ struct LanService::Impl
{"online", found != peers.end() && reachable(found->second)}});
}
}
else
{
if (action != "drives" && action != "list" && action != "download")
throw std::runtime_error("Unsupported LAN client operation.");
const auto peer = request.at("peer").get<std::string>();
const auto found = peers.find(peer);
if (!trusted.contains(peer) || found == peers.end() || !reachable(found->second))
throw std::runtime_error("The paired computer is offline or file browsing is unavailable.");
char address[INET_ADDRSTRLEN]{};
auto ip = found->second.address.sin_addr;
if (!InetNtopA(AF_INET, &ip, address, sizeof(address)))
throw std::runtime_error("The paired computer has no usable address.");
target = {peer, address, wide(found->second.name), lan::FilesPort, true};
local = id;
key = trusted.at(peer).at("key").get<std::string>();
}
}
if (action == "peers")
{
reply(machines);
const auto result = machines.dump();
send(result.data(), DWORD(result.size()));
return;
}
lan::FilesConnection connection(target, local, key);
if (action == "drives")
{
const auto result = connection.call({{"action", "drives"}});
lan::checkFileReply(result);
reply(result.at("drives"));
return;
}
const auto path = lan::filePath(request.at("path"));
if (action == "list")
{
auto page = connection.call(lan::fileRequest("list", path));
auto entries = Json::array();
size_t bytes{};
for (;;)
{
lan::checkFileReply(page);
for (auto &entry : page.at("entries"))
{
bytes += entry.dump().size() + 1;
if (bytes > 8 * 1024 * 1024) throw std::runtime_error("The remote directory listing is too large.");
entries.push_back(std::move(entry));
}
if (!page.at("more").get<bool>()) break;
allowed();
page = connection.call({{"action", "listNext"}});
}
reply(entries);
return;
}
const auto file = connection.call(lan::fileRequest("stat", path));
lan::checkFileReply(file);
auto open = lan::fileRequest("openRead", path);
open["size"] = file.at("size");
open["modified"] = file.at("modified");
lan::checkFileReply(connection.call(open));
reply({{"size", file.at("size")}});
for (;;)
{
allowed();
const auto chunk = connection.call({{"action", "read"}});
lan::checkFileReply(chunk);
const auto &data = chunk.at("data").get_binary();
send(data.data(), DWORD(data.size()));
if (data.empty()) break;
}
lan::routeClientFiles(request, allowed, send, receive, asCaller, [&](const std::string &peer) {
std::lock_guard gate(commandGate);
std::lock_guard lock(mutex);
const auto found = peers.find(peer);
if (!trusted.contains(peer) || found == peers.end() || !reachable(found->second))
throw std::runtime_error("The paired computer is offline or file browsing is unavailable.");
char address[INET_ADDRSTRLEN]{};
auto ip = found->second.address.sin_addr;
if (!InetNtopA(AF_INET, &ip, address, sizeof(address)))
throw std::runtime_error("The paired computer has no usable address.");
return lan::ClientFilesPeer{{peer, address, wide(found->second.name), lan::FilesPort, true},
id, trusted.at(peer).at("key").get<std::string>()};
});
});
}
void save() { atomicWrite(path,protect(encoded({{"version",1},{"id",id},{"enabled",enabled},{"trusted",trusted},