Restore original Web Console login and navigation

This commit is contained in:
SantaSpeen committed 2026-11-22 03:23:44 +03:00
1 parent d748f7cc69
commit 81a86d0e6c
5 files changed
+290 -7

No files matched your search

+36 -1
View File
@@ -28,6 +28,7 @@ final class LocalServer {
private let queue = DispatchQueue(label: "ezserver.loopback")
private let maxHeaderSize = 8192
private let library: LibraryAPI
private let webConsole = WebConsoleAPI()
private var connections: [ObjectIdentifier: NWConnection] = [:]
init(port: UInt16, library: LibraryAPI) throws {
@@ -123,6 +124,35 @@ final class LocalServer {
let segments = target.path.split(separator: "/").map(String.init)
let authorization = headers["authorization"] ?? []
let bearer = authorization.count == 1 ? authorization[0] : nil
let lower = segments.map { $0.lowercased() }
let apiPrefix = lower.starts(with: ["api", "index.php", "v1"]) ? 3 : 2
if lower.starts(with: ["api", "v1"]) || apiPrefix == 3 {
let route = Array(lower.dropFirst(apiPrefix))
if webConsole.handles(route, method: method) {
let reply: (Data) -> Void = { [self] body in
let response = webConsole.reply(path: route, method: method, body: body,
authorization: bearer, store: library.store)
guard let json = try? JSONSerialization.data(withJSONObject: response.body) else {
connection.cancel()
return
}
sendBody(json, status: response.status, on: connection)
}
if method == "GET" {
reply(Data())
} else {
guard headers["content-type"]?.first?.lowercased().hasPrefix("application/json") == true else {
send(["errorMessage": "Expected application/json"],
status: "415 Unsupported Media Type", on: connection)
return
}
acceptBody(headers, initial: initialBody, maximum: 32 * 1024, on: connection) { url in
reply(try Data(contentsOf: url))
}
}
return
}
}
if segments.starts(with: ["mac", "v1", "patients"]) {
let path = Array(segments.dropFirst(3))
if method == "GET" {
@@ -193,7 +223,8 @@ final class LocalServer {
return
}
let type = UTType(filenameExtension: file.pathExtension)?.preferredMIMEType ?? "application/octet-stream"
let headers = responseHeaders(status: "200 OK", type: type, bytes: bytes, extra: "")
let policy = "Content-Security-Policy: connect-src 'self'; frame-ancestors 'none'; object-src 'none'\r\n"
let headers = responseHeaders(status: "200 OK", type: type, bytes: bytes, extra: policy)
connection.send(content: headers, completion: .contentProcessed { [weak self] error in
guard let self, error == nil else { connection.cancel(); return }
self.stream(handle, remaining: bytes, on: connection)
@@ -377,6 +408,10 @@ final class LocalServer {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .iso8601
guard let body = try? encoder.encode(response) else { connection.cancel(); return }
sendBody(body, status: status, extraHeaders: extraHeaders, on: connection)
}
private func sendBody(_ body: Data, status: String, extraHeaders: String = "", on connection: NWConnection) {
let headers = responseHeaders(status: status, type: "application/json; charset=utf-8",
bytes: body.count, extra: extraHeaders)
connection.send(content: headers + body, completion: .contentProcessed { _ in