mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-27 00:11:35 +00:00
feat(dashboard): the zone file API, with zone edit locks
/api/v1/zones/{zoneId}: send a zone's edited files (multipart), follow the reload,
list, restore and clear the zone's sets, and take, release and look at its lock.
Every request needs an API key that names the zones_publish permission, an owner at
zone_publish_gm_level, and HTTPS or the server's own machine; refusals are counted
per address and every use is in the audit log. A publish is checked at its headers
before any of its body is taken, the files are checked and written on a worker
thread, and the zone's instances are replaced through the world reload, held while
players are there unless told to move them.
The Worlds page lists each zone's editors and lock holder for an admin.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include "PermissionGrantsLoader.h"
|
||||
#include "RequireAuthMiddleware.h"
|
||||
#include "RouteUtils.h"
|
||||
#include "ZonePublishAuth.h"
|
||||
|
||||
using namespace RouteUtils;
|
||||
using AccountRules::eAccountAction;
|
||||
@@ -213,7 +214,12 @@ namespace ApiKeyRoutes {
|
||||
key.permissions = std::string(ApiKeys::ALL_PERMISSIONS);
|
||||
} else if (requested.is_array()) {
|
||||
std::set<std::string> permissions;
|
||||
for (const auto& permission : requested) if (permission.is_string()) permissions.insert(permission.get<std::string>());
|
||||
for (const auto& permission : requested) {
|
||||
if (!permission.is_string()) continue;
|
||||
// The zone file API's scope is written zones.publish; permission names have no dots
|
||||
auto name = permission.get<std::string>();
|
||||
permissions.insert(name == ZonePublishAuth::SCOPE_ALIAS ? std::string(ZonePublishAuth::SCOPE) : name);
|
||||
}
|
||||
if (permissions.empty()) return JsonError(reply, eHTTPStatusCode::BAD_REQUEST, "Pick at least one permission");
|
||||
// Staff can't hand a key more than they have
|
||||
const auto refused = Permissions::NotGrantable(context.gmLevel, permissions, context.grants.get());
|
||||
|
||||
@@ -21,6 +21,7 @@ set(DASHBOARDROUTES_SOURCES
|
||||
"LiveWorld.cpp"
|
||||
"LiveUpdateRoutes.cpp"
|
||||
"WorldReloadRoutes.cpp"
|
||||
"ZonePublishRoutes.cpp"
|
||||
"SettingsRoutes.cpp"
|
||||
"Background.cpp"
|
||||
"DatabaseUsage.cpp"
|
||||
|
||||
@@ -14,7 +14,8 @@
|
||||
* description in the house style become query parameters and a request body:
|
||||
* "Query: ?range=24h|7d|30d, &files= (newest per server)" or "Query: after (the last id), limit (max 500)"
|
||||
* "Body: {name, enabled, parent (default root.xml)}" (also "Body (optional): {...}", "Body adds {...}")
|
||||
* Routes described as "(DataTables)" take the DataTables paging body. Pure (routes in, JSON out) so it can be tested.
|
||||
* Routes described as "(DataTables)" take the DataTables paging body; ones described as "(multipart)" take
|
||||
* multipart/form-data: the fields of their "Body: {...}" and file parts. Pure (routes in, JSON out) so it can be tested.
|
||||
*/
|
||||
namespace OpenApi {
|
||||
struct Route {
|
||||
@@ -203,7 +204,18 @@ namespace OpenApi {
|
||||
properties[param.name] = schema;
|
||||
example[param.name] = param.isList ? nlohmann::json::array() : nlohmann::json(param.choices.empty() ? "" : param.choices.front());
|
||||
}
|
||||
operation["requestBody"] = { {"required", false}, {"content", { {"application/json", { {"schema", { {"type", "object"}, {"properties", properties} }}, {"example", example} }} }} };
|
||||
if (route.description.find("(multipart)") != std::string::npos) {
|
||||
// File uploads: the fields from "Body: {...}", and any number of file parts
|
||||
properties["files"] = { {"type", "array"}, {"items", { {"type", "string"}, {"format", "binary"} }},
|
||||
{"description", "One part per file; each part's filename is the file's path"} };
|
||||
operation["requestBody"] = { {"required", true}, {"content", { {"multipart/form-data", { {"schema", { {"type", "object"}, {"properties", properties} }} }} }} };
|
||||
operation["responses"]["409"] = { {"description", "Conflict: {error, message}"} };
|
||||
operation["responses"]["413"] = { {"description", "Too large"} };
|
||||
operation["responses"]["422"] = { {"description", "Refused: {error: validation_failed, files: [{path, ok, errors: [{code, message, offset}]}]}"} };
|
||||
operation["responses"]["429"] = { {"description", "Too many requests"} };
|
||||
} else {
|
||||
operation["requestBody"] = { {"required", false}, {"content", { {"application/json", { {"schema", { {"type", "object"}, {"properties", properties} }}, {"example", example} }} }} };
|
||||
}
|
||||
}
|
||||
paths[path][method] = operation;
|
||||
}
|
||||
|
||||
@@ -92,6 +92,10 @@ namespace RouteUtils {
|
||||
Register(method, path, std::move(middleware), std::move(handler));
|
||||
}
|
||||
|
||||
void Document(eHTTPMethod method, const std::string& path, const std::string& permission, const std::string& description) {
|
||||
g_RouteDocs.push_back({ std::string(magic_enum::enum_name(method)), path, Permissions::Level(permission), description, permission, method == eHTTPMethod::GET });
|
||||
}
|
||||
|
||||
bool Can(const HTTPContext& context, const std::string& permission) {
|
||||
return context.isAuthenticated && Permissions::Allowed(context.gmLevel, permission, context.apiKey.get(), context.grants.get());
|
||||
}
|
||||
|
||||
@@ -260,6 +260,10 @@ namespace RouteUtils {
|
||||
void Route(eHTTPMethod method, const std::string& path, int16_t minGmLevel, const std::string& description, Handler handler);
|
||||
void Route(eHTTPMethod method, const std::string& path, const Perm& permission, const std::string& description, Handler handler);
|
||||
|
||||
// List a route that was registered with its own middleware (not through Route) in the API documentation;
|
||||
// permission: the permission its callers need
|
||||
void Document(eHTTPMethod method, const std::string& path, const std::string& permission, const std::string& description);
|
||||
|
||||
// Routes registered while one of these lives only read, even POSTs (lookups that send their query as a body), so
|
||||
// read-only API keys may use them. POST /api/tables/... (DataTables) always counts as a read.
|
||||
struct ReadRoutes {
|
||||
|
||||
1064
dDashboardServer/routes/ZonePublishRoutes.cpp
Normal file
1064
dDashboardServer/routes/ZonePublishRoutes.cpp
Normal file
File diff suppressed because it is too large
Load Diff
64
dDashboardServer/routes/ZonePublishRoutes.h
Normal file
64
dDashboardServer/routes/ZonePublishRoutes.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "json.hpp"
|
||||
|
||||
struct WorldFilesStatus;
|
||||
|
||||
/**
|
||||
* The zone file API (/api/v1/zones/..., docs/ZonePublish.md): an external level editor sends a zone's edited files,
|
||||
* which are checked, kept in the override folder and loaded by replacing the zone's instances. A thin layer: who
|
||||
* may is ZonePublishAuth (the key) and ZoneEditLock (the zone's lock), checking a set is ZonePublish, keeping it is
|
||||
* ZoneOverrideStore, and the reload is the world reload master already does (WorldReloader).
|
||||
*
|
||||
* Routes and their database use run on the main thread; reading and writing the files runs on one worker thread,
|
||||
* which touches nothing else and hands its result back to the main thread (Update).
|
||||
*/
|
||||
namespace ZonePublishRoutes {
|
||||
void RegisterRoutes();
|
||||
|
||||
// Master's world file status (WORLD_FILES_STATUS): how far the reloads asked for here have come
|
||||
void HandleStatus(const WorldFilesStatus& status);
|
||||
|
||||
// Main loop: finish what the worker did
|
||||
void Update();
|
||||
|
||||
// Stops the worker (a set being written finishes first)
|
||||
void Shutdown();
|
||||
|
||||
// What master counts per zone (WorldFilesStatus::Zone); present: the zone has instances that reported files
|
||||
struct ReloadCounters {
|
||||
bool present{};
|
||||
uint32_t requests{};
|
||||
uint32_t acted{};
|
||||
uint32_t playersMoved{};
|
||||
uint32_t failed{};
|
||||
uint16_t pending{};
|
||||
};
|
||||
|
||||
struct ReloadProgress {
|
||||
bool done{};
|
||||
uint32_t instances{};
|
||||
uint32_t playersMoved{};
|
||||
uint32_t failed{};
|
||||
};
|
||||
|
||||
/**
|
||||
* What a reload asked for when the counters were `before` has done by `now`: done once master took the request
|
||||
* and nothing of the zone is being replaced any more, or the zone has no instance left. Pure.
|
||||
*/
|
||||
inline ReloadProgress ProgressOf(const ReloadCounters& before, const ReloadCounters& now) {
|
||||
ReloadProgress progress;
|
||||
// No instance left (they were all stopped), or the counts started over: nothing more will come
|
||||
if (!now.present || now.requests < before.requests || now.acted < before.acted) {
|
||||
progress.done = true;
|
||||
return progress;
|
||||
}
|
||||
progress.instances = now.acted - before.acted;
|
||||
progress.playersMoved = now.playersMoved >= before.playersMoved ? now.playersMoved - before.playersMoved : 0;
|
||||
progress.failed = now.failed >= before.failed ? now.failed - before.failed : 0;
|
||||
progress.done = now.requests > before.requests && now.pending == 0;
|
||||
return progress;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user