Last active
May 25, 2024 10:19
-
-
Save dacr/80d8ac3abdc52c017a9aa65cfa176acd to your computer and use it in GitHub Desktop.
playing with dnsjava api / published by https://github.com/dacr/code-examples-manager #e298e2b1-c0a7-4d21-87dc-db6409929c1e/72dacf9d084e5be036075d1430203885d3dcb553
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : playing with dnsjava api | |
// keywords : scala, dns, dnsjava, resolution, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : e298e2b1-c0a7-4d21-87dc-db6409929c1e | |
// created-on : 2020-07-01T05:11:43Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "org.scalatest::scalatest:3.2.10" | |
//> using dep "dnsjava:dnsjava:3.4.3" | |
//> using dep "org.slf4j:slf4j-nop:1.7.32" | |
// --------------------- | |
import org.scalatest.*, flatspec.*, matchers.* | |
import org.xbill.DNS.* | |
import java.net.{InetAddress, Inet4Address, Inet6Address} | |
import java.time.Duration | |
def getAllByName(hostname:String):List[InetAddress] = { | |
org.xbill.DNS.Address.getAllByName(hostname).toList | |
} | |
def host(hostname:String):List[String] = { | |
getAllByName(hostname).map(_.getHostAddress) | |
} | |
def host4(hostname:String):List[String] = { | |
getAllByName(hostname).collect {case ip:Inet4Address => ip.getHostAddress} | |
} | |
def host6(hostname:String):List[String] = { | |
getAllByName(hostname).collect {case ip:Inet6Address => ip.getHostAddress} | |
} | |
object UnitTests extends AnyFlatSpec with should.Matchers { | |
override def suiteName: String = "UnitTests" | |
"host functions using dnsjava" should "resolve localhost address" in { | |
host("www.mapland.fr") should contain only("176.9.65.81", "2a01:4f8:150:834b:0:0:0:2") | |
} | |
it should "resolve remote host name" in { | |
host("www.mapland.fr") should contain only("176.9.65.81", "2a01:4f8:150:834b:0:0:0:2") | |
host4("www.mapland.fr") should contain only("176.9.65.81") | |
host6("www.mapland.fr") should contain only("2a01:4f8:150:834b:0:0:0:2") | |
} | |
"dnsjava" should "resolve all associated addresses" in { | |
val resolver = new SimpleResolver("8.8.8.8") | |
resolver.setTimeout(Duration.ofSeconds(5)) | |
val lookup = new Lookup("www.mapland.fr", Type.ANY) | |
lookup.setResolver(resolver) | |
val records = Option(lookup.run()).getOrElse(Array.empty[Record]) | |
val results = records.collect { | |
case r:ARecord => r.getAddress.getHostAddress | |
case r:AAAARecord => r.getAddress.getHostAddress | |
} | |
results should contain only("176.9.65.81", "2a01:4f8:150:834b:0:0:0:2") | |
} | |
} | |
UnitTests.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment