Log EzDent HTTP errors without request data
This commit is contained in:
1 parent
230743be33
commit
05e31e7f11
1 file changed
+18
@@ -1,5 +1,6 @@
|
||||
import Foundation
|
||||
import Network
|
||||
import OSLog
|
||||
import UniformTypeIdentifiers
|
||||
|
||||
private struct APIVersion: Encodable {
|
||||
@@ -27,9 +28,15 @@ final class LocalServer {
|
||||
private let listener: NWListener
|
||||
private let queue = DispatchQueue(label: "ezserver.http")
|
||||
private let maxHeaderSize = 8192
|
||||
private let diagnostics = Logger(subsystem: "EzServerMac", category: "HTTP")
|
||||
private let library: LibraryAPI
|
||||
private let webConsole = WebConsoleAPI()
|
||||
private var connections: [ObjectIdentifier: NWConnection] = [:]
|
||||
private var diagnosticRoutes: [ObjectIdentifier: String] = [:]
|
||||
private static let legacyGroups: Set<String> = [
|
||||
"auth", "info", "e2ds", "e3ds", "fs", "settings", "analytics", "consultds",
|
||||
"implantds", "iosensor", "orthods", "thirdparty", "device", "pay",
|
||||
]
|
||||
|
||||
init(port: UInt16, library: LibraryAPI) throws {
|
||||
self.library = library
|
||||
@@ -58,6 +65,7 @@ final class LocalServer {
|
||||
switch state {
|
||||
case .cancelled, .failed:
|
||||
self?.connections.removeValue(forKey: id)
|
||||
self?.diagnosticRoutes.removeValue(forKey: id)
|
||||
default:
|
||||
break
|
||||
}
|
||||
@@ -75,6 +83,7 @@ final class LocalServer {
|
||||
listener.cancel()
|
||||
connections.values.forEach { $0.cancel() }
|
||||
connections.removeAll()
|
||||
diagnosticRoutes.removeAll()
|
||||
completion?()
|
||||
}
|
||||
}
|
||||
@@ -128,6 +137,11 @@ final class LocalServer {
|
||||
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 route.count >= 2, Self.legacyGroups.contains(route[0]),
|
||||
route[1].range(of: #"\A[a-z][a-z0-9]{0,63}\z"#, options: .regularExpression) != nil,
|
||||
["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"].contains(method) {
|
||||
diagnosticRoutes[ObjectIdentifier(connection)] = "\(method) /api/v1/\(route[0])/\(route[1])"
|
||||
}
|
||||
if webConsole.handles(route, method: method) {
|
||||
guard localConsole || webConsole.allowsNetworkAccess(route, method: method) else {
|
||||
send(["error": "Web Console is local only"], status: "403 Forbidden", on: connection)
|
||||
@@ -439,6 +453,10 @@ final class LocalServer {
|
||||
}
|
||||
|
||||
private func sendBody(_ body: Data, status: String, extraHeaders: String = "", on connection: NWConnection) {
|
||||
if let route = diagnosticRoutes[ObjectIdentifier(connection)],
|
||||
let code = Int(status.prefix(3)), code >= 400 {
|
||||
diagnostics.notice("\(route, privacy: .public) → \(code, privacy: .public)")
|
||||
}
|
||||
let headers = responseHeaders(status: status, type: "application/json; charset=utf-8",
|
||||
bytes: body.count, extra: extraHeaders)
|
||||
connection.send(content: headers + body, completion: .contentProcessed { _ in
|
||||
|
||||
Reference in new issue
Block a user