orlandos-nl / dnsclient Goto Github PK
View Code? Open in Web Editor NEWA DNS Resolver in Swift
License: MIT License
A DNS Resolver in Swift
License: MIT License
It would be great if you could publish this package on Cocoapods as well. Some old applications that are still using it for package management would be able to use this.
Hey there,
These modifications are necessary to allow packages using MongoKitten + Meow to target iOS which otherwise would result in a failing build.
The modifications needed are as follows:
https://github.com/Wabi-Studios/NioDNS/commit/f8c52ff6287b2935671bb0bff79429940ac6f70d
https://github.com/Wabi-Studios/NioDNS/commit/5e3b84b3c926e3b578befd69ceeffc7a50cd5fb3
for consistency with other packages like swift-nio
, swift-nio-ssl
, this package should be renamed swift-nio-dns
to follow swift package naming conventions.
Using XCode 14.2 and DNSClient 2.3.2
When resolving the packages and dependencies, XCode runs into a problem with the NioDNS package. The error message is as follows:
'niodns' dependency on 'https://github.com/Joannis/swift-nio-transport-services.git' conflicts with dependency on 'https://github.com/apple/swift-nio-transport-services.git' which has the same identity 'swift-nio-transport-services'. this will be escalated to an error in future versions of SwiftPM.
XCode seems to resolve the dependency graph without error, but this causes the underlying code from the DNSClient package to stop working. In the swift file "DNSClient+Connect," it can no longer find the reference "NIOTSDatagramBootstrap".
Specifically, the error occurs in the DNSClient extension:
extension DNSClient {
/// Connect to the dns server using TCP using NIOTransportServices. This is only available on iOS 12 and above.
/// - parameters:
/// - group: EventLoops to use
/// - config: DNS servers to use
/// - returns: Future with the NioDNS client. Use
public static func connectTS(on group: NIOTSEventLoopGroup, config: [SocketAddress]) -> EventLoopFuture<DNSClient> {
guard let address = config.preferred else {
return group.next().makeFailedFuture(MissingNameservers())
}
let dnsDecoder = DNSDecoder(group: group)
return NIOTSDatagramBootstrap(group: group)
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEPORT), value: 1)
.channelInitializer { channel in
return channel.pipeline.addHandlers(dnsDecoder, DNSEncoder())
}
.connect(to: address)
.map { channel -> DNSClient in
let client = DNSClient(
channel: channel,
address: address,
decoder: dnsDecoder
)
dnsDecoder.mainClient = client
return client
}
}
/// Connect to the dns server using TCP using NIOTransportServices. This is only available on iOS 12 and above.
/// The DNS Host is read from /etc/resolv.conf
/// - parameters:
/// - group: EventLoops to use
public static func connectTS(on group: NIOTSEventLoopGroup) -> EventLoopFuture<DNSClient> {
do {
let configString = try String(contentsOfFile: "/etc/resolv.conf")
let config = try ResolvConf(from: configString)
return connectTS(on: group, config: config.nameservers)
} catch {
return group.next().makeFailedFuture(UnableToParseConfig())
}
}
}
When I try to decode a response, I get ProtocolError
.
You can reproduce the issue with this test (the test server is no longer available):
func testComplexSRVRecords() throws {
let expectation = self.expectation(description: "getComplexSRVRecords")
var testComplexClient: DNSClient!
do {
let serverSocketAddress = try SocketAddress(ipAddress: "IP_ADDRESS(removed)", port: 5356)
testComplexClient = try DNSClient.connect(on: group, config: [serverSocketAddress]).wait()
} catch let error {
XCTFail("\(error)")
}
let zone = "c7lu2ylhzmwmmaoa2xgie6nihfxrn25frxauuifp5lxvtjgw4icq====._wireguard._udp.exampleniodns.org."
testComplexClient.getSRVRecords(from: zone)
.whenComplete { (result) in
switch result {
case .failure(let error):
XCTFail("\(error)")
case .success(let answers):
print(answers)
XCTAssertGreaterThanOrEqual(answers.count, 1, "The returned answers should be greater than or equal to 1")
}
expectation.fulfill()
}
self.waitForExpectations(timeout: 5, handler: nil)
}
Running the same query with dig, I get the correct result:
$ dig @IP_ADDRESS(removed) -p 5356 c7lu2ylhzmwmmaoa2xgie6nihfxrn25frxauuifp5lxvtjgw4icq====._wireguard._udp.exampleniodns.org. SRV +noall +answer +additional
; <<>> DiG 9.10.6 <<>> @IP_ADDRESS(removed) -p 5356 c7lu2ylhzmwmmaoa2xgie6nihfxrn25frxauuifp5lxvtjgw4icq====._wireguard._udp.exampleniodns.org. SRV +noall +answer +additional
; (1 server found)
;; global options: +cmd
c7lu2ylhzmwmmaoa2xgie6nihfxrn25frxauuifp5lxvtjgw4icq====._wireguard._udp.exampleniodns.org. 0 IN SRV 0 0 51820 c7lu2ylhzmwmmaoa2xgie6nihfxrn25frxauuifp5lxvtjgw4icq====._wireguard._udp.exampleniodns.org.
c7lu2ylhzmwmmaoa2xgie6nihfxrn25frxauuifp5lxvtjgw4icq====._wireguard._udp.exampleniodns.org. 0 IN A 82.52.204.201
c7lu2ylhzmwmmaoa2xgie6nihfxrn25frxauuifp5lxvtjgw4icq====._wireguard._udp.exampleniodns.org. 0 IN TXT "txtvers=1" "pub=F9dNYWfLLMYBwNXMgnmoOW8W66WNwUogr+rvWaTW4gU=" "allowed=10.101.0.2/32"
The error is thrown at this line:
https://github.com/orlandos-nl/NioDNS/blob/de0b66fdedab85b4b84535df3efbef2e6605f89b/Sources/DNSClient/DNSDecoder.swift#L52-L54
while decoding additionalData
https://github.com/orlandos-nl/NioDNS/blob/de0b66fdedab85b4b84535df3efbef2e6605f89b/Sources/DNSClient/DNSDecoder.swift#L68
UPDATE 29/12/2021
The error is thrown while decoding TXT record
https://github.com/orlandos-nl/NioDNS/blob/de0b66fdedab85b4b84535df3efbef2e6605f89b/Sources/DNSClient/Helpers.swift#L154-L156
It crashes in DNSClient.send
. It looks like multiple threads can access dnsDecoder.messageCache
at the same time. I got this crash with the following code
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let client = try await DNSClient.connectTCP(
on: eventLoopGroup.next(),
host: "1.1.1.1"
).get()
async let result = client.initiateAAAAQuery(host: hostname, port: 0).get()
async let result2 = client.initiateAAAAQuery(host: hostname, port: 0).get()
async let result3 = client.initiateAAAAQuery(host: hostname, port: 0).get()
Can we have this back ported to 1.x?
#2
We are getting a compile failure in our Travis build:
https://travis-ci.org/IBM-Swift/generator-swiftserver/jobs/602887577?utm_medium=notification&utm_source=github_status
The dependency on NioDNS is coming from MongoDB which we rely on:
https://github.com/OpenKitten/MongoKitten/blob/master/5.0/Package.swift#L28
DNS request will failure with this async test as expected.
2019-09-17 15:14:22.499290+0900 xctest[7603:427732] [si_destination_compare] send failed: Invalid argument
2019-09-17 15:14:22.499516+0900 xctest[7603:427732] [si_destination_compare] send failed: Undefined error: 0
NioDNS/Tests/DNSClientTests/DNSClientTests.swift:54: error: -[DNSClientTests.DNSClientTests testSRVRecordsAsync] : Asynchronous wait failed: Exceeded timeout of 5 seconds, with unfulfilled expectations: "getSRVRecords".
No callback is called and it shows send failed
error in console.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.