Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions Sources/ContainerCommands/System/DNS/DNSCreate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import ArgumentParser
import ContainerAPIClient
import ContainerPersistence
import ContainerizationError
import ContainerizationExtras
import Foundation
Expand All @@ -30,22 +31,54 @@ extension Application {
@OptionGroup
public var logOptions: Flags.Logging

@Option(name: .long, help: "Set the ip address to be redirected to localhost")
var localhost: String?

@Argument(help: "The local domain name")
var domainName: String

public init() {}

public func run() async throws {
var localhostIP: IPAddress? = nil
if let localhost {
localhostIP = try? IPAddress(localhost)
guard let localhostIP, case .v4(_) = localhostIP else {
throw ContainerizationError(.invalidArgument, message: "invalid IPv4 address: \(localhost)")
}
}

let resolver: HostDNSResolver = HostDNSResolver()
do {
try resolver.createDomain(name: domainName)
print(domainName)
try resolver.createDomain(name: domainName, localhost: localhostIP)
} catch let error as ContainerizationError {
throw error
} catch {
throw ContainerizationError(.invalidState, message: "cannot create domain (try sudo?)")
}

let pf = PacketFilter()
if let from = localhostIP {
let to = try! IPAddress("127.0.0.1")
do {
try pf.createRedirectRule(from: from, to: to, domain: domainName)
} catch {
_ = try resolver.deleteDomain(name: domainName)
throw error
}
}
print(domainName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(domainName)
// Output the created resource ID after domain (and optional packet filter) configurations exist.
// Just provide diagnostic messages for failed service restarts.
print(domainName)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by resource ID? Is it in the context of ManagedResource?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but you suggest an interesting point. When we implement the ManagedResource stuff and if we did make domains a managed resource, the domain name property might be independent of its resource ID.

That's something for another PR though, the comment will be helpful to someone who's looking at the code for the first time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, once we use resource ID, we should update this to use resource ID also.


if localhostIP != nil {
do {
try pf.reinitialize()
} catch let error as ContainerizationError {
throw error
} catch {
throw ContainerizationError(.invalidState, message: "failed loading pf rules")
}
}

do {
try HostDNSResolver.reinitialize()
} catch {
Expand Down
22 changes: 20 additions & 2 deletions Sources/ContainerCommands/System/DNS/DNSDelete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import ArgumentParser
import ContainerAPIClient
import ContainerizationError
import ContainerizationExtras
import Foundation

extension Application {
Expand All @@ -37,9 +38,9 @@ extension Application {

public func run() async throws {
let resolver = HostDNSResolver()
var localhostIP: IPAddress?
do {
try resolver.deleteDomain(name: domainName)
print(domainName)
localhostIP = try resolver.deleteDomain(name: domainName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we do the managed resource refactoring, let's plan to update the domain code to use the managed resource protocol, and then we'll use more conventional semantics for DELETE.

No need for changes in this PR.

} catch {
throw ContainerizationError(.invalidState, message: "cannot delete domain (try sudo?)")
}
Expand All @@ -49,6 +50,23 @@ extension Application {
} catch {
throw ContainerizationError(.invalidState, message: "mDNSResponder restart failed, run `sudo killall -HUP mDNSResponder` to deactivate domain")
}

guard let localhostIP else {
print(domainName)
return
}

let pf = PacketFilter()
try pf.removeRedirectRule(from: localhostIP, to: try! IPAddress("127.0.0.1"), domain: domainName)

do {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DNS manager makes it the responsibility of the caller to perform the reinitialize side effects for both create and delete.

For the packet filter, which is it the responsibility of the caller for create, and the callee for delete?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packet filter the same, it is the responsibility of caller to reinitialize for both create and delete.

try pf.reinitialize()
} catch let error as ContainerizationError {
throw error
} catch {
throw ContainerizationError(.invalidState, message: "failed loading pf rules")
}
print(domainName)
}
}
}
23 changes: 22 additions & 1 deletion Sources/Helpers/APIServer/APIServer+Start.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extension APIServer {
)

static let listenAddress = "127.0.0.1"
static let localhostDNSPort = 1053
static let dnsPort = 2053

@Flag(name: .long, help: "Enable debug logging")
Expand Down Expand Up @@ -97,13 +98,33 @@ extension APIServer {
let hostsQueryValidator = StandardQueryValidator(handler: compositeResolver)
let dnsServer: DNSServer = DNSServer(handler: hostsQueryValidator, log: log)
log.info(
"starting DNS host query resolver",
"starting DNS resolver for container hostnames",
metadata: [
"host": "\(Self.listenAddress)",
"port": "\(Self.dnsPort)",
]
)
try await dnsServer.run(host: Self.listenAddress, port: Self.dnsPort)

}

// start up realhost DNS
group.addTask {
let localhostResolver = LocalhostDNSHandler(log: log)
try localhostResolver.monitorResolvers()

let nxDomainResolver = NxDomainResolver()
let compositeResolver = CompositeResolver(handlers: [localhostResolver, nxDomainResolver])
let hostsQueryValidator = StandardQueryValidator(handler: compositeResolver)
let dnsServer: DNSServer = DNSServer(handler: hostsQueryValidator, log: log)
log.info(
"starting DNS resolver for localhost",
metadata: [
"host": "\(Self.listenAddress)",
"port": "\(Self.localhostDNSPort)",
]
)
try await dnsServer.run(host: Self.listenAddress, port: Self.localhostDNSPort)
}
}
} catch {
Expand Down
78 changes: 78 additions & 0 deletions Sources/Helpers/APIServer/DirectoryWatcher.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//===----------------------------------------------------------------------===//
// Copyright © 2026 Apple Inc. and the container project authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//

import ContainerizationError
import Foundation
import Logging

public class DirectoryWatcher {
public let directoryURL: URL

private let monitorQueue: DispatchQueue
private var source: DispatchSourceFileSystemObject?

private let log: Logger

init(directoryURL: URL, log: Logger) {
self.directoryURL = directoryURL
self.monitorQueue = DispatchQueue(label: "monitor:\(directoryURL.path)")
self.log = log
}

public func startWatching(handler: @escaping ([URL]) throws -> Void) throws {
guard source == nil else {
throw ContainerizationError(.invalidState, message: "already watching on \(directoryURL.path)")
}

do {
let files = try FileManager.default.contentsOfDirectory(atPath: directoryURL.path)
try handler(files.map { directoryURL.appending(path: $0) })
} catch {
throw ContainerizationError(.invalidState, message: "failed to start watching on \(directoryURL.path)")
}

log.info("starting directory watcher for \(directoryURL.path)")

let descriptor = open(directoryURL.path, O_EVTONLY)

source = DispatchSource.makeFileSystemObjectSource(
fileDescriptor: descriptor,
eventMask: .write,
queue: monitorQueue
)

source?.setEventHandler { [weak self] in
guard let self else { return }

do {
let files = try FileManager.default.contentsOfDirectory(atPath: directoryURL.path)
try? handler(files.map { directoryURL.appending(path: $0) })
} catch {
self.log.info("failed to run handler for \(directoryURL.path)")
}
}

source?.resume()
}

deinit {
guard let source else {
return
}

source.cancel()
}
}
112 changes: 112 additions & 0 deletions Sources/Helpers/APIServer/LocalhostDNSHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//===----------------------------------------------------------------------===//
// Copyright © 2026 Apple Inc. and the container project authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//

import ContainerAPIClient
import ContainerPersistence
import ContainerizationError
import DNS
import DNSServer
import Foundation
import Logging

class LocalhostDNSHandler: DNSHandler {
private let ttl: UInt32
private let watcher: DirectoryWatcher

private var dns: [String: IPv4]

public init(resolversURL: URL = HostDNSResolver.defaultConfigPath, ttl: UInt32 = 5, log: Logger) {
self.ttl = ttl

self.watcher = DirectoryWatcher(directoryURL: resolversURL, log: log)
self.dns = [:]
}

public func monitorResolvers() throws {
try self.watcher.startWatching { fileURLs in
var dns: [String: IPv4] = [:]
let regex = try Regex(HostDNSResolver.localhostOptionsRegex)

for file in fileURLs.filter({ $0.lastPathComponent.starts(with: HostDNSResolver.containerizationPrefix) }) {
let content = try String(contentsOf: file, encoding: .utf8)

if let match = content.firstMatch(of: regex),
let ipv4 = IPv4(String(match[1].substring ?? ""))
{
let name = String(file.lastPathComponent.dropFirst(HostDNSResolver.containerizationPrefix.count))
dns[name + "."] = ipv4
}
}
self.dns = dns
}
}

public func answer(query: Message) async throws -> Message? {
let question = query.questions[0]
var record: ResourceRecord?
switch question.type {
case ResourceRecordType.host:
if let ip = dns[question.name] {
record = HostRecord<IPv4>(name: question.name, ttl: ttl, ip: ip)
}
case ResourceRecordType.host6:
return Message(
id: query.id,
type: .response,
returnCode: .noError,
questions: query.questions,
answers: []
)
case ResourceRecordType.nameServer,
ResourceRecordType.alias,
ResourceRecordType.startOfAuthority,
ResourceRecordType.pointer,
ResourceRecordType.mailExchange,
ResourceRecordType.text,
ResourceRecordType.service,
ResourceRecordType.incrementalZoneTransfer,
ResourceRecordType.standardZoneTransfer,
ResourceRecordType.all:
return Message(
id: query.id,
type: .response,
returnCode: .notImplemented,
questions: query.questions,
answers: []
)
default:
return Message(
id: query.id,
type: .response,
returnCode: .formatError,
questions: query.questions,
answers: []
)
}

guard let record else {
return nil
}

return Message(
id: query.id,
type: .response,
returnCode: .noError,
questions: query.questions,
answers: [record]
)
}
}
Loading