Listen on IPv4 port 43112 by default
This commit is contained in:
1 parent
4801cfbe77
commit
0aad4f27eb
6 files changed
+44
-13
No files matched your search
@@ -34,7 +34,7 @@ func runCommandLine(_ arguments: [String]) {
|
||||
server.start { state in
|
||||
switch state {
|
||||
case let .success(port):
|
||||
print("EzServer Mac is listening on http://127.0.0.1:\(port)")
|
||||
print("EzServer Mac is listening on http://0.0.0.0:\(port)")
|
||||
fflush(stdout)
|
||||
case let .failure(error):
|
||||
FileHandle.standardError.write(Data("Could not start EzServer: \(error.localizedDescription)\n".utf8))
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -22,7 +22,7 @@ private final class ServerControl: ObservableObject {
|
||||
@Published var starting = false
|
||||
@Published var stopping = false
|
||||
@Published var copying = false
|
||||
@Published var port = "55005"
|
||||
@Published var port = String(ServerConfiguration.defaultPort)
|
||||
@Published var notice: String?
|
||||
|
||||
private var configuration: ServerConfiguration?
|
||||
@@ -33,7 +33,12 @@ private final class ServerControl: ObservableObject {
|
||||
do {
|
||||
let configuration = try ServerConfiguration()
|
||||
self.configuration = configuration
|
||||
port = UserDefaults.standard.string(forKey: "listenPort") ?? String(configuration.port)
|
||||
let savedPort = UserDefaults.standard.string(forKey: "listenPort")
|
||||
if ProcessInfo.processInfo.environment["EZSERVER_PORT"] != nil || savedPort == "55005" {
|
||||
port = String(configuration.port)
|
||||
} else {
|
||||
port = savedPort ?? String(configuration.port)
|
||||
}
|
||||
start()
|
||||
} catch {
|
||||
notice = error.localizedDescription
|
||||
@@ -153,7 +158,7 @@ private struct ServerPanel: View {
|
||||
Divider()
|
||||
HStack {
|
||||
Text("Порт")
|
||||
TextField("55005", text: $model.port)
|
||||
TextField(String(ServerConfiguration.defaultPort), text: $model.port)
|
||||
.frame(width: 75)
|
||||
.disabled(model.running || model.starting || model.stopping)
|
||||
.accessibilityLabel("Порт HTTP")
|
||||
|
||||
@@ -2,12 +2,13 @@ import Foundation
|
||||
import Security
|
||||
|
||||
struct ServerConfiguration {
|
||||
static let defaultPort: UInt16 = 43112
|
||||
let store: ServerStore
|
||||
let token: String
|
||||
let port: UInt16
|
||||
|
||||
init(environment: [String: String] = ProcessInfo.processInfo.environment, imagesParent: URL? = nil) throws {
|
||||
let rawPort = environment["EZSERVER_PORT"] ?? "55005"
|
||||
let rawPort = environment["EZSERVER_PORT"] ?? String(Self.defaultPort)
|
||||
guard let port = UInt16(rawPort), port > 0 else { throw Failure.invalidPort }
|
||||
self.port = port
|
||||
let directory: URL
|
||||
|
||||
Reference in new issue
Block a user