Source added

This commit is contained in:
Fr4nz D13trich 2025-11-20 09:26:33 +01:00
parent b2864b500e
commit ba28ca859e
8352 changed files with 1487182 additions and 1 deletions

View file

@ -0,0 +1,33 @@
plugins {
alias(libs.plugins.jetbrains.kotlin.jvm)
id("java-library")
alias(libs.plugins.ktlint)
}
val signalJavaVersion: JavaVersion by rootProject.extra
val signalKotlinJvmTarget: String by rootProject.extra
java {
sourceCompatibility = signalJavaVersion
targetCompatibility = signalJavaVersion
}
kotlin {
jvmToolchain {
languageVersion = JavaLanguageVersion.of(signalKotlinJvmTarget)
}
}
// NOTE: For now, in order to run ktlint on this project, you have to manually run ./gradlew :build-logic:tools:ktlintFormat
// Gotta figure out how to get it auto-included in the normal ./gradlew ktlintFormat
ktlint {
version.set("1.2.1")
}
dependencies {
implementation(gradleApi())
implementation(libs.dnsjava)
testImplementation(testLibs.junit.junit)
testImplementation(testLibs.mockk)
}

View file

@ -0,0 +1,100 @@
package org.signal.buildtools
import org.xbill.DNS.ARecord
import org.xbill.DNS.Lookup
import org.xbill.DNS.Record
import org.xbill.DNS.SimpleResolver
import org.xbill.DNS.Type
import java.net.UnknownHostException
import kotlin.streams.toList
/**
* A tool to resolve hostname to static IPs.
* Feeds into our custom DNS resolver to provide a static IP fallback for our services.
*/
class StaticIpResolver @JvmOverloads constructor(
private val recordFetcher: RecordFetcher = RealRecordFetcher
) {
/**
* Resolves a hostname to a list of IPs, represented as a Java array declaration. e.g.
*
* ```java
* new String[]{"192.168.1.1", "192.168.1.2"}
* ```
*
* This is intended to be injected as a BuildConfig.
*/
fun resolveToBuildConfig(hostName: String): String {
val ips: List<String> = resolve(hostName)
val builder = StringBuilder()
builder.append("new String[]{")
ips.forEachIndexed { i, ip ->
builder.append("\"").append(ip).append("\"")
if (i < ips.size - 1) {
builder.append(",")
}
}
return builder.append("}").toString()
}
private fun resolve(hostname: String): List<String> {
val ips: MutableSet<String> = mutableSetOf()
// Run several resolves to mitigate DNS round robin
for (i in 1..10) {
ips.addAll(resolveOnce(hostname))
}
return ips.stream().sorted().toList()
}
private fun resolveOnce(hostName: String): List<String> {
try {
val records = recordFetcher.fetchRecords(hostName)
if (records != null) {
return records
.filter { it.type == Type.A }
.map { it as ARecord }
.map { it.address }
.map { it.hostAddress }
.filterNotNull()
} else {
throw IllegalStateException("Failed to resolve host! Lookup did not return any records.. $hostName")
}
} catch (e: UnknownHostException) {
throw IllegalStateException("Failed to resolve host! $hostName", e)
}
}
interface RecordFetcher {
fun fetchRecords(hostName: String): Array<Record>?
}
private object RealRecordFetcher : RecordFetcher {
override fun fetchRecords(hostName: String): Array<Record>? {
val resolver = SimpleResolver("1.1.1.1")
resolver.setTCP(true)
val lookup: Lookup = doLookup(hostName)
lookup.setResolver(resolver)
return lookup.run()
}
@Throws(UnknownHostException::class)
private fun doLookup(hostname: String): Lookup {
try {
return Lookup(hostname)
} catch (e: Throwable) {
throw UnknownHostException()
}
}
}
}

View file

@ -0,0 +1,59 @@
package org.signal.buildtools
import io.mockk.every
import io.mockk.mockk
import org.junit.Assert.assertEquals
import org.junit.Test
import org.xbill.DNS.ARecord
import org.xbill.DNS.DClass
import org.xbill.DNS.Name
import org.xbill.DNS.Record
import java.net.Inet4Address
class StaticIpResolverTest {
companion object {
const val SIGNAL_DOT_ORG = "www.signal.org"
val SIGNAL_IP = byteArrayOf(123, 45, 67, 89)
val STRINGIFIED_IP = SIGNAL_IP.joinToString(".")
}
@Test
fun `Given a hostname with records, when I resolveToBuildConfig, then I expect a matching IP`() {
val staticIpResolver = StaticIpResolver(
FakeRecordFetcher(
mapOf(
SIGNAL_DOT_ORG to arrayOf(
ARecord(
Name.fromString("www."),
DClass.ANY,
0L,
mockk<Inet4Address> {
every { address } returns SIGNAL_IP
every { hostAddress } returns STRINGIFIED_IP
}
)
)
)
)
)
val actual = staticIpResolver.resolveToBuildConfig(SIGNAL_DOT_ORG)
val expected = """
new String[]{"$STRINGIFIED_IP"}
""".trimIndent()
assertEquals(expected, actual)
}
@Test(expected = IllegalStateException::class)
fun `Given a hostname without records, when I resolveToBuildConfig, then I expect`() {
val staticIpResolver = StaticIpResolver(FakeRecordFetcher(emptyMap()))
staticIpResolver.resolveToBuildConfig(SIGNAL_DOT_ORG)
}
private class FakeRecordFetcher(private val recordMap: Map<String, Array<Record>?>) : StaticIpResolver.RecordFetcher {
override fun fetchRecords(hostName: String): Array<Record>? {
return recordMap[hostName]
}
}
}