mirror of
https://github.com/kalarina-bit/Sparkledns.git
synced 2026-11-22 07:36:09 +00:00
Cut allocations on the path of every DNS query
This commit is contained in:
@@ -231,20 +231,28 @@ object Blocklist {
|
||||
}
|
||||
}
|
||||
|
||||
// True when the name or one of its parent domains is in either sorted hash set
|
||||
// True when the name or one of its parent domains is in either sorted hash set. Runs for every
|
||||
// query, so trailing dots are skipped by index rather than trimmed off a copy of the name.
|
||||
internal fun isBlockedIn(name: String, lists: LongArray, custom: LongArray): Boolean {
|
||||
if (lists.isEmpty() && custom.isEmpty()) return false
|
||||
val domain = name.trimEnd('.')
|
||||
val end = nameEnd(name)
|
||||
var start = 0
|
||||
while (true) {
|
||||
val hash = DomainHash.of(domain, start)
|
||||
val hash = DomainHash.of(name, start, end)
|
||||
if (lists.binarySearch(hash) >= 0 || custom.binarySearch(hash) >= 0) return true
|
||||
val dot = domain.indexOf('.', start)
|
||||
if (dot < 0) return false
|
||||
val dot = name.indexOf('.', start)
|
||||
if (dot < 0 || dot >= end) return false
|
||||
start = dot + 1
|
||||
}
|
||||
}
|
||||
|
||||
// Length of the name without trailing dots
|
||||
internal fun nameEnd(name: String): Int {
|
||||
var end = name.length
|
||||
while (end > 0 && name[end - 1] == '.') end--
|
||||
return end
|
||||
}
|
||||
|
||||
// Number of distinct entries in both sorted sets; the custom set is small
|
||||
internal fun unionSize(lists: LongArray, custom: LongArray): Int =
|
||||
lists.size + custom.count { lists.binarySearch(it) < 0 }
|
||||
@@ -287,10 +295,10 @@ internal object DomainHash {
|
||||
private const val FNV_OFFSET = -3750763034362895579L // 0xcbf29ce484222325
|
||||
private const val FNV_PRIME = 1099511628211L
|
||||
|
||||
// FNV-1a over the domain starting at [start], so parent domains are hashed without substrings
|
||||
fun of(s: String, start: Int = 0): Long {
|
||||
// FNV-1a over the characters from [start] to [end], so parent domains are hashed without substrings
|
||||
fun of(s: String, start: Int = 0, end: Int = s.length): Long {
|
||||
var h = FNV_OFFSET
|
||||
for (i in start until s.length) {
|
||||
for (i in start until end) {
|
||||
h = h xor s[i].code.toLong()
|
||||
h *= FNV_PRIME
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package cc.skysparkle.sparkledns
|
||||
|
||||
import java.util.TreeMap
|
||||
|
||||
// The position of each company is saved in StatsStore: add new ones at the end only
|
||||
enum class Company(val label: String) {
|
||||
GOOGLE("Google"),
|
||||
@@ -17,8 +19,16 @@ enum class Company(val label: String) {
|
||||
// Tells which big company a domain belongs to, for the Company dominance card.
|
||||
// Matches the domain and every parent of it against a small built-in table.
|
||||
object Companies {
|
||||
private val bySuffix = HashMap<String, Company>().apply {
|
||||
fun add(company: Company, vararg domains: String) = domains.forEach { put(it, company) }
|
||||
private const val GOOGLE_PREFIX = "google."
|
||||
|
||||
// Hashes of the domains, sorted, with the company of each at the same index: this runs for
|
||||
// every query, and parent domains are looked up by hash without copying the name
|
||||
private val hashes: LongArray
|
||||
private val owners: Array<Company>
|
||||
|
||||
init {
|
||||
val table = TreeMap<Long, Company>()
|
||||
fun add(company: Company, vararg domains: String) = domains.forEach { table[DomainHash.of(it)] = company }
|
||||
|
||||
add(
|
||||
Company.GOOGLE,
|
||||
@@ -62,17 +72,24 @@ object Companies {
|
||||
add(Company.YANDEX, "yandex.ru", "yandex.net", "yandex.com", "ya.ru", "yastatic.net", "yandex.by", "yandex.kz")
|
||||
add(Company.SAMSUNG, "samsung.com", "samsungapps.com", "samsungcloud.com", "samsungqbe.com", "samsungosp.com")
|
||||
add(Company.XIAOMI, "xiaomi.com", "xiaomi.net", "mi.com", "miui.com", "mi-img.com")
|
||||
|
||||
hashes = table.keys.toLongArray()
|
||||
owners = table.values.toTypedArray()
|
||||
}
|
||||
|
||||
fun of(name: String): Company? {
|
||||
var domain = name.lowercase().trimEnd('.')
|
||||
while (domain.isNotEmpty()) {
|
||||
bySuffix[domain]?.let { return it }
|
||||
// Names from the tunnel are lowercase already; anything else is lowercased once
|
||||
val domain = if (name.any { it.isUpperCase() }) name.lowercase() else name
|
||||
val end = nameEnd(domain)
|
||||
var start = 0
|
||||
while (start < end) {
|
||||
val index = hashes.binarySearch(DomainHash.of(domain, start, end))
|
||||
if (index >= 0) return owners[index]
|
||||
// Google's country sites: google.de, google.co.uk, google.com.br
|
||||
if (domain.startsWith("google.") && domain.length - "google.".length <= 6) return Company.GOOGLE
|
||||
val dot = domain.indexOf('.')
|
||||
if (dot < 0) return null
|
||||
domain = domain.substring(dot + 1)
|
||||
if (end - start - GOOGLE_PREFIX.length in 1..6 && domain.startsWith(GOOGLE_PREFIX, start)) return Company.GOOGLE
|
||||
val dot = domain.indexOf('.', start)
|
||||
if (dot < 0 || dot >= end) return null
|
||||
start = dot + 1
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ class DnsCache(
|
||||
|
||||
private class Entry(
|
||||
val response: ByteArray,
|
||||
val ttlOffsets: IntArray,
|
||||
// Offset and value of each TTL field, one pair after another
|
||||
val ttls: IntArray,
|
||||
val storedAt: Long,
|
||||
val expiresAt: Long,
|
||||
@@ -35,7 +35,12 @@ class DnsCache(
|
||||
out[0] = query[0]
|
||||
out[1] = query[1]
|
||||
val elapsed = ((now - entry.storedAt) / 1000).toInt()
|
||||
entry.ttlOffsets.forEachIndexed { i, off -> put32(out, off, maxOf(entry.ttls[i] - elapsed, 0)) }
|
||||
val ttls = entry.ttls
|
||||
var i = 0
|
||||
while (i < ttls.size) {
|
||||
put32(out, ttls[i], maxOf(ttls[i + 1] - elapsed, 0))
|
||||
i += 2
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -45,26 +50,32 @@ class DnsCache(
|
||||
val rcode = response[3].toInt() and 0x0F
|
||||
if (rcode != 0 && rcode != 3) return // only NOERROR and NXDOMAIN
|
||||
|
||||
val records = parseTtls(response) ?: return
|
||||
val minTtl = if (records.first.isEmpty()) NO_RECORDS_TTL else records.second.min()
|
||||
val ttls = parseTtls(response) ?: return
|
||||
var minTtl = if (ttls.isEmpty()) NO_RECORDS_TTL else Int.MAX_VALUE
|
||||
var i = 1
|
||||
while (i < ttls.size) {
|
||||
minTtl = minOf(minTtl, ttls[i])
|
||||
i += 2
|
||||
}
|
||||
val ttl = minOf(minTtl, MAX_TTL)
|
||||
if (ttl <= 0) return
|
||||
|
||||
val now = clock()
|
||||
val entry = Entry(response.copyOf(), records.first, records.second, now, now + ttl * 1000L)
|
||||
val entry = Entry(response.copyOf(), ttls, now, now + ttl * 1000L)
|
||||
synchronized(map) { map[key] = entry }
|
||||
}
|
||||
|
||||
// Offsets and values of every TTL field except the EDNS OPT pseudo-record
|
||||
private fun parseTtls(msg: ByteArray): Pair<IntArray, IntArray>? {
|
||||
// Offset and value of every TTL field except the EDNS OPT pseudo-record, as pairs in one array
|
||||
private fun parseTtls(msg: ByteArray): IntArray? {
|
||||
var pos = 12
|
||||
repeat(u16(msg, 4)) {
|
||||
pos = skipName(msg, pos) ?: return null
|
||||
pos += 4
|
||||
}
|
||||
val total = u16(msg, 6) + u16(msg, 8) + u16(msg, 10)
|
||||
val offsets = ArrayList<Int>(total)
|
||||
val values = ArrayList<Int>(total)
|
||||
// The counts come from the server; the array grows only if a message really has that many
|
||||
var ttls = IntArray(2 * minOf(total, 16))
|
||||
var n = 0
|
||||
repeat(total) {
|
||||
pos = skipName(msg, pos) ?: return null
|
||||
if (pos + 10 > msg.size) return null
|
||||
@@ -72,13 +83,14 @@ class DnsCache(
|
||||
val ttlOffset = pos + 4
|
||||
val rdLength = u16(msg, pos + 8)
|
||||
if (type != TYPE_OPT) {
|
||||
offsets += ttlOffset
|
||||
values += minOf(u32(msg, ttlOffset), Int.MAX_VALUE.toLong()).toInt()
|
||||
if (n == ttls.size) ttls = ttls.copyOf(n * 2)
|
||||
ttls[n++] = ttlOffset
|
||||
ttls[n++] = minOf(u32(msg, ttlOffset), Int.MAX_VALUE.toLong()).toInt()
|
||||
}
|
||||
pos += 10 + rdLength
|
||||
if (pos > msg.size) return null
|
||||
}
|
||||
return offsets.toIntArray() to values.toIntArray()
|
||||
return if (n == ttls.size) ttls else ttls.copyOf(n)
|
||||
}
|
||||
|
||||
private fun keyOf(query: ByteArray): String? {
|
||||
|
||||
@@ -3,9 +3,10 @@ package cc.skysparkle.sparkledns
|
||||
import java.io.ByteArrayOutputStream
|
||||
import kotlin.random.Random
|
||||
|
||||
// IPv4 addresses are kept as plain numbers, so parsing a query allocates only its payload
|
||||
class UdpPacket(
|
||||
val srcIp: ByteArray,
|
||||
val dstIp: ByteArray,
|
||||
val srcIp: Int,
|
||||
val dstIp: Int,
|
||||
val srcPort: Int,
|
||||
val dstPort: Int,
|
||||
val payload: ByteArray,
|
||||
@@ -29,8 +30,8 @@ object Packet {
|
||||
if (end <= ihl + 8) return null
|
||||
|
||||
return UdpPacket(
|
||||
srcIp = buf.copyOfRange(12, 16),
|
||||
dstIp = buf.copyOfRange(16, 20),
|
||||
srcIp = i32(buf, 12),
|
||||
dstIp = i32(buf, 16),
|
||||
srcPort = u16(buf, ihl),
|
||||
dstPort = dstPort,
|
||||
payload = buf.copyOfRange(ihl + 8, end),
|
||||
@@ -47,8 +48,8 @@ object Packet {
|
||||
p[6] = 0x40 // don't fragment
|
||||
p[8] = 64
|
||||
p[9] = 17
|
||||
query.dstIp.copyInto(p, 12)
|
||||
query.srcIp.copyInto(p, 16)
|
||||
put32(p, 12, query.dstIp)
|
||||
put32(p, 16, query.srcIp)
|
||||
put16(p, 10, checksum(p, 0, 20, 0))
|
||||
|
||||
put16(p, 20, query.dstPort)
|
||||
@@ -123,7 +124,9 @@ object DnsMessage {
|
||||
|
||||
fun question(msg: ByteArray): Question? {
|
||||
if (msg.size < 12 || u16(msg, 4) < 1) return null
|
||||
val name = StringBuilder()
|
||||
// Sized up front, so the builder does not grow: a valid name has at most 253 characters,
|
||||
// and never more than the message has bytes after the header
|
||||
val name = StringBuilder(minOf(msg.size - 12, 253))
|
||||
var p = 12
|
||||
while (true) {
|
||||
if (p >= msg.size) return null
|
||||
@@ -180,6 +183,8 @@ internal fun put32(b: ByteArray, off: Int, value: Int) {
|
||||
internal fun u32(b: ByteArray, off: Int): Long =
|
||||
((u16(b, off).toLong()) shl 16) or u16(b, off + 2).toLong()
|
||||
|
||||
internal fun i32(b: ByteArray, off: Int): Int = (u16(b, off) shl 16) or u16(b, off + 2)
|
||||
|
||||
// Returns the offset right after a (possibly compressed) DNS name
|
||||
internal fun skipName(b: ByteArray, start: Int): Int? {
|
||||
var p = start
|
||||
|
||||
@@ -34,15 +34,14 @@ object QueryLog {
|
||||
|
||||
fun add(question: DnsMessage.Question?, status: QueryStatus, latencyMs: Int) {
|
||||
if (question == null) return
|
||||
val time = System.currentTimeMillis()
|
||||
synchronized(this) {
|
||||
if (entries.size >= MAX_ENTRIES) entries.removeFirst()
|
||||
entries.addLast(
|
||||
QueryEntry(nextId++, System.currentTimeMillis(), question.name.ifEmpty { "." }, question.type, status, latencyMs)
|
||||
)
|
||||
entries.addLast(QueryEntry(nextId++, time, question.name.ifEmpty { "." }, question.type, status, latencyMs))
|
||||
version.value++
|
||||
}
|
||||
// Hourly numbers for the charts; outside the lock above, as it takes a lock of its own
|
||||
StatsStore.record(status, latencyMs, Companies.of(question.name))
|
||||
StatsStore.record(status, latencyMs, Companies.of(question.name), time)
|
||||
}
|
||||
|
||||
// Newest first. Builds a real copy: on API 35+ List.reversed() resolves to a live JDK view,
|
||||
|
||||
@@ -24,6 +24,15 @@ class BlocklistMatchTest {
|
||||
assertFalse(isBlockedIn("ads.example.com", LongArray(0), LongArray(0)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun skipsTrailingDots() {
|
||||
val lists = set("example.com")
|
||||
|
||||
assertTrue(isBlockedIn("www.example.com..", lists, LongArray(0)))
|
||||
assertFalse(isBlockedIn("example.org.", lists, LongArray(0)))
|
||||
assertFalse(isBlockedIn("...", lists, LongArray(0)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun mergesSortedListsWithoutDuplicates() {
|
||||
val a = longArrayOf(-5, 1, 4, 9)
|
||||
|
||||
@@ -49,5 +49,6 @@ class BlocklistParserTest {
|
||||
val name = "a.b.example.com"
|
||||
assertEquals(DomainHash.of("example.com"), DomainHash.of(name, name.indexOf("example")))
|
||||
assertEquals(DomainHash.of(name), DomainHash.of(name, 0))
|
||||
assertEquals(DomainHash.of("b.example"), DomainHash.of(name, 2, name.indexOf(".com")))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package cc.skysparkle.sparkledns
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
|
||||
class CompaniesTest {
|
||||
|
||||
@Test
|
||||
fun matchesDomainsAndTheirSubdomains() {
|
||||
assertEquals(Company.GOOGLE, Companies.of("youtube.com"))
|
||||
assertEquals(Company.GOOGLE, Companies.of("r3---sn-abc.googlevideo.com"))
|
||||
assertEquals(Company.META, Companies.of("scontent.cdninstagram.com"))
|
||||
assertEquals(Company.X, Companies.of("t.co"))
|
||||
assertEquals(Company.MICROSOFT, Companies.of("api.github.com"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ignoresCaseAndTrailingDots() {
|
||||
assertEquals(Company.APPLE, Companies.of("WWW.Apple.COM"))
|
||||
assertEquals(Company.AMAZON, Companies.of("s3.amazonaws.com."))
|
||||
assertEquals(Company.AMAZON, Companies.of("s3.amazonaws.com.."))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun knowsGoogleCountrySites() {
|
||||
assertEquals(Company.GOOGLE, Companies.of("google.de"))
|
||||
assertEquals(Company.GOOGLE, Companies.of("www.google.co.uk"))
|
||||
assertEquals(Company.GOOGLE, Companies.of("google.com.br"))
|
||||
assertNull(Companies.of("google.example.org"))
|
||||
assertNull(Companies.of("google."))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun leavesOtherDomainsAlone() {
|
||||
assertNull(Companies.of("example.com"))
|
||||
assertNull(Companies.of("notyoutube.com"))
|
||||
assertNull(Companies.of("youtube.com.example"))
|
||||
assertNull(Companies.of("com"))
|
||||
assertNull(Companies.of(""))
|
||||
assertNull(Companies.of("."))
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,8 @@ class PacketTest {
|
||||
val parsed = requireNotNull(Packet.parseDnsQuery(packet, packet.size))
|
||||
|
||||
assertArrayEquals(query, parsed.payload)
|
||||
assertArrayEquals(client, parsed.srcIp)
|
||||
assertArrayEquals(server, parsed.dstIp)
|
||||
assertEquals(i32(client, 0), parsed.srcIp)
|
||||
assertEquals(i32(server, 0), parsed.dstIp)
|
||||
assertEquals(40000, parsed.srcPort)
|
||||
assertEquals(53, parsed.dstPort)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user