Listen on IPv4 port 43112 by default

This commit is contained in:
SantaSpeen committed 2026-11-22 03:40:06 +03:00
1 parent 4801cfbe77
commit 0aad4f27eb
6 files changed
+44 -13

No files matched your search

+30 -5
View File
@@ -25,7 +25,7 @@ private struct VersionMatch: Encodable {
final class LocalServer {
private let listener: NWListener
private let queue = DispatchQueue(label: "ezserver.loopback")
private let queue = DispatchQueue(label: "ezserver.http")
private let maxHeaderSize = 8192
private let library: LibraryAPI
private let webConsole = WebConsoleAPI()
@@ -34,7 +34,8 @@ final class LocalServer {
init(port: UInt16, library: LibraryAPI) throws {
self.library = library
let parameters = NWParameters.tcp
parameters.requiredLocalEndpoint = .hostPort(host: "127.0.0.1", port: NWEndpoint.Port(rawValue: port)!)
(parameters.defaultProtocolStack.internetProtocol as? NWProtocolIP.Options)?.version = .v4
parameters.requiredLocalEndpoint = .hostPort(host: "0.0.0.0", port: NWEndpoint.Port(rawValue: port)!)
listener = try NWListener(using: parameters)
}
@@ -114,12 +115,11 @@ final class LocalServer {
guard requestLine.count == 3, ["HTTP/1.0", "HTTP/1.1"].contains(requestLine[2]),
requestLine[1].hasPrefix("/"), !requestLine[1].hasPrefix("//"),
let target = URLComponents(string: String(requestLine[1])),
hosts.count == 1,
hosts[0].range(of: #"\A(?:127\.0\.0\.1|localhost)(?::[0-9]{1,5})?\z"#,
options: [.regularExpression, .caseInsensitive]) != nil else {
hosts.count == 1, let host = Self.requestHost(hosts[0]) else {
send(["error": "Invalid request or host"], status: "400 Bad Request", on: connection)
return
}
let localConsole = Self.isLoopback(host: host, peer: connection.endpoint)
let method = String(requestLine[0])
let segments = target.path.split(separator: "/").map(String.init)
let authorization = headers["authorization"] ?? []
@@ -129,6 +129,10 @@ final class LocalServer {
if lower.starts(with: ["api", "v1"]) || apiPrefix == 3 {
let route = Array(lower.dropFirst(apiPrefix))
if webConsole.handles(route, method: method) {
guard localConsole else {
send(["error": "Web Console is local only"], status: "403 Forbidden", on: connection)
return
}
let reply: (Data) -> Void = { [self] body in
let response = webConsole.reply(path: route, method: method, body: body,
authorization: bearer, store: library.store)
@@ -170,6 +174,10 @@ final class LocalServer {
}
if target.path == "/webconsole" || target.path.hasPrefix("/webconsole/") {
guard localConsole else {
send(["error": "Web Console is local only"], status: "403 Forbidden", on: connection)
return
}
serveWebConsole(path: segments, on: connection)
return
}
@@ -207,6 +215,23 @@ final class LocalServer {
}
}
private static func requestHost(_ authority: String) -> String? {
let parts = authority.split(separator: ":", omittingEmptySubsequences: false)
guard let name = parts.first?.lowercased(),
name == "localhost" || IPv4Address(name) != nil,
parts.count == 1 || (parts.count == 2 && parts[1].utf8.count <= 5 &&
parts[1].utf8.allSatisfy({ (48...57).contains($0) }) &&
UInt16(parts[1]).map({ $0 > 0 }) == true) else { return nil }
return name
}
private static func isLoopback(host: String, peer: NWEndpoint) -> Bool {
guard host == "localhost" || IPv4Address(host)?.rawValue.first == 127,
case let .hostPort(address, _) = peer,
case let .ipv4(ip) = address else { return false }
return ip.rawValue.first == 127
}
private func serveWebConsole(path: [String], on connection: NWConnection) {
guard let root = Bundle.main.resourceURL?.appendingPathComponent("webconsole", isDirectory: true)
.resolvingSymlinksInPath() else {