-
Notifications
You must be signed in to change notification settings - Fork 657
Host connection #1078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Host connection #1078
Changes from all commits
c227fd3
e15fa7a
098f18c
8af9d63
2994e48
0b264d7
08110c7
5aff090
ad4cf92
83d430e
08d2dc3
d8291cb
67394aa
cbbe91e
04a7eb0
c239480
297ec66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| import ArgumentParser | ||
| import ContainerAPIClient | ||
| import ContainerizationError | ||
| import ContainerizationExtras | ||
| import Foundation | ||
|
|
||
| extension Application { | ||
|
|
@@ -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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?)") | ||
| } | ||
|
|
@@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
| } | ||
| } | ||
| 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() | ||
| } | ||
| } |
| 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] | ||
| ) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.