ui布局自适应

This commit is contained in:
tianmo committed 2026-11-09 10:08:29 +08:00
1 parent 98f44890f8
commit 3587129b90
40 files changed
+929 -142

No files matched your search

+38 -5
View File
@@ -4,11 +4,15 @@
#include "encoding.hpp"
#include "json.hpp"
#include "platform.hpp"
#ifdef __APPLE__
#include "local_certificate.hpp"
#endif
#include "workspace.hpp"
#include "zip.hpp"
#include <chrono>
#include <filesystem>
#include <iostream>
#include <map>
#include <mutex>
#include <random>
@@ -20,6 +24,7 @@ namespace {
constexpr const char* kVersion = "2.0.0";
constexpr unsigned short kPort = 27843;
constexpr unsigned short kSecurePort = 27844;
std::mutex pairing_mutex;
struct Pairing { std::string origin; std::chrono::steady_clock::time_point expires; };
std::map<std::string, Pairing> pairing;
@@ -125,10 +130,27 @@ std::pair<std::vector<std::uint8_t>, std::string> convert(const Json& payload, c
} // namespace
bool ConnectorService::start(std::string& error) {
return server_.start(kPort, [this](const HttpRequest& request) { return handle(request); }, error);
if (server_.running()) return true;
if (!server_.start(kPort, [this](const HttpRequest& request) { return handle(request); }, error)) return false;
#ifdef __APPLE__
std::string certificate_error;
if (!ensure_local_certificate(certificate_error)) {
secure_error_ = certificate_error;
std::cerr << "Safari HTTPS certificate error: " << secure_error_ << '\n';
} else if (!secure_server_.start(
kSecurePort, [this](const HttpRequest& request) { return handle(request); }, secure_error_,
local_identity_path())) {
// HTTP remains available for existing browsers even if the optional Safari endpoint cannot start.
std::cerr << "Safari HTTPS server error: " << secure_error_ << '\n';
}
#endif
return true;
}
void ConnectorService::stop() { server_.stop(); }
void ConnectorService::stop() {
secure_server_.stop();
server_.stop();
}
HttpResponse ConnectorService::handle(const HttpRequest& request) {
const std::string origin = header(request, "origin");
@@ -142,13 +164,21 @@ HttpResponse ConnectorService::handle(const HttpRequest& request) {
if (request.method == "GET" && request.path == "/v1/health") {
std::lock_guard<std::mutex> lock(config_mutex_);
Json::Array versions{Json("3.8"), Json("4.0"), Json("4.1"), Json("4.2"), Json("4.3")};
return json_response(200, {
Json::Object health = {
{"connectorVersion", Json(kVersion)},
{"authorized", Json(origin_allowed(origin))},
{"spineFound", Json(!find_spine_executable().empty())},
{"secureAvailable", Json(secure_server_.running())},
{"platform", Json(platform_name())},
{"supportedVersions", Json(std::move(versions))},
}, origin);
};
if (!origin_allowed(origin)) {
const std::string base = request.secure
? "https://127.0.0.1:" + std::to_string(kSecurePort)
: "http://127.0.0.1:" + std::to_string(kPort);
health["pairUrl"] = Json(base + "/pair?origin=" + url_encode(origin));
}
return json_response(200, std::move(health), origin);
}
if (request.method == "GET" && request.path == "/pair") {
const auto found = request.query.find("origin");
@@ -184,7 +214,10 @@ HttpResponse ConnectorService::handle(const HttpRequest& request) {
if (!origin_allowed(origin)) {
HttpResponse response = error_response(403, "ORIGIN_NOT_ALLOWED", "需要先授权当前网站使用本地 Spine 连接器", origin);
Json body = Json::parse(response.body);
body["pairUrl"] = Json("http://127.0.0.1:27843/pair?origin=" + url_encode(origin));
const std::string base = request.secure
? "https://127.0.0.1:" + std::to_string(kSecurePort)
: "http://127.0.0.1:" + std::to_string(kPort);
body["pairUrl"] = Json(base + "/pair?origin=" + url_encode(origin));
response.body = body.dump();
return response;
}