@@ -18,7 +18,7 @@ import ContainerAPIClient
1818import ContainerOS
1919import ContainerPersistence
2020import ContainerizationError
21- import DNS
21+ import ContainerizationExtras
2222import DNSServer
2323import Foundation
2424import Logging
@@ -28,42 +28,41 @@ actor LocalhostDNSHandler: DNSHandler {
2828 private let ttl : UInt32
2929 private let watcher : DirectoryWatcher
3030
31- private let dns : Mutex < [ String : IPv4 ] >
31+ private var dns : [ String : IPv4Address ]
3232
3333 public init ( resolversURL: URL = HostDNSResolver . defaultConfigPath, ttl: UInt32 = 5 , log: Logger ) {
3434 self . ttl = ttl
3535
3636 self . watcher = DirectoryWatcher ( directoryURL: resolversURL, log: log)
37- self . dns = Mutex ( [ : ] )
37+ self . dns = [ : ]
3838 }
3939
4040 public func monitorResolvers( ) async {
41- await self . watcher. startWatching { fileURLs in
42- var dns : [ String : String ] = [ : ]
41+ await self . watcher. startWatching { [ weak self ] fileURLs in
42+ var dns : [ String : IPv4Address ] = [ : ]
4343 let regex = try Regex ( HostDNSResolver . localhostOptionsRegex)
4444
4545 for file in fileURLs. filter ( { $0. lastPathComponent. starts ( with: HostDNSResolver . containerizationPrefix) } ) {
4646 let content = try String ( contentsOf: file, encoding: . utf8)
4747
4848 if let match = content. firstMatch ( of: regex) ,
49- let ipv4 = ( match [ 1 ] . substring. map { String ( $0) } )
49+ let ipv4 = ( match [ 1 ] . substring. map { try ? IPv4Address ( String ( $0) ) } )
5050 {
5151 let name = String ( file. lastPathComponent. dropFirst ( HostDNSResolver . containerizationPrefix. count) )
5252 dns [ name + " . " ] = ipv4
5353 }
5454 }
55- self . dns . withLock { $0 = dns . compactMapValues { IPv4 ( $0 ) } }
55+ Task { await self ? . updateDNS ( dns ) }
5656 }
5757 }
5858
59- nonisolated public func answer( query: Message ) async throws -> Message ? {
59+ public func answer( query: Message ) async throws -> Message ? {
6060 let question = query. questions [ 0 ]
6161 var record : ResourceRecord ?
6262 switch question. type {
6363 case ResourceRecordType . host:
64- let dns = dns. withLock { $0 }
6564 if let ip = dns [ question. name] {
66- record = HostRecord < IPv4 > ( name: question. name, ttl: ttl, ip: ip)
65+ record = HostRecord < IPv4Address > ( name: question. name, ttl: ttl, ip: ip)
6766 }
6867 case ResourceRecordType . host6:
6968 return Message (
@@ -73,28 +72,11 @@ actor LocalhostDNSHandler: DNSHandler {
7372 questions: query. questions,
7473 answers: [ ]
7574 )
76- case ResourceRecordType . nameServer,
77- ResourceRecordType . alias,
78- ResourceRecordType . startOfAuthority,
79- ResourceRecordType . pointer,
80- ResourceRecordType . mailExchange,
81- ResourceRecordType . text,
82- ResourceRecordType . service,
83- ResourceRecordType . incrementalZoneTransfer,
84- ResourceRecordType . standardZoneTransfer,
85- ResourceRecordType . all:
86- return Message (
87- id: query. id,
88- type: . response,
89- returnCode: . notImplemented,
90- questions: query. questions,
91- answers: [ ]
92- )
9375 default :
9476 return Message (
9577 id: query. id,
9678 type: . response,
97- returnCode: . formatError ,
79+ returnCode: . notImplemented ,
9880 questions: query. questions,
9981 answers: [ ]
10082 )
@@ -112,4 +94,9 @@ actor LocalhostDNSHandler: DNSHandler {
11294 answers: [ record]
11395 )
11496 }
97+
98+ private func updateDNS( _ dns: [ String : IPv4Address ] ) {
99+ self . dns = dns
100+ }
101+
115102}
0 commit comments