Optimise and test IP2Country (#1684)
* Move ipv4Int to top level * Remove redundant fun calls in ipv4ToCountry * Add null safety to loadFile * Close streams on failure * Simplify cacheCountryForIP * Add IP2CountryTest * Generate binary * Simplify ipv4Int * Fix companion object visibility * Use array instead of Treemap * Synchronize OnionApi#paths * Move csv * Deduplicate locations csv * Move ipToCode to gradle * Use std lib binarySearch --------- Co-authored-by: bemusementpark <bemusementpark>dev
parent
4917548faf
commit
3e17ab2b06
Can't render this file because it is too large.
|
@ -0,0 +1,41 @@
|
|||||||
|
import java.io.File
|
||||||
|
import java.io.DataOutputStream
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
|
||||||
|
task("ipToCode") {
|
||||||
|
val inputFile = File("${projectDir}/geolite2_country_blocks_ipv4.csv")
|
||||||
|
|
||||||
|
val outputDir = "${buildDir}/generated/binary"
|
||||||
|
val outputFile = File(outputDir, "geolite2_country_blocks_ipv4.bin").apply { parentFile.mkdirs() }
|
||||||
|
|
||||||
|
outputs.file(outputFile)
|
||||||
|
|
||||||
|
doLast {
|
||||||
|
|
||||||
|
// Ensure the input file exists
|
||||||
|
if (!inputFile.exists()) {
|
||||||
|
throw IllegalArgumentException("Input file does not exist: ${inputFile.absolutePath}")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a DataOutputStream to write binary data
|
||||||
|
DataOutputStream(FileOutputStream(outputFile)).use { out ->
|
||||||
|
inputFile.useLines { lines ->
|
||||||
|
var prevCode = -1
|
||||||
|
lines.drop(1).forEach { line ->
|
||||||
|
runCatching {
|
||||||
|
val ints = line.split(".", "/", ",")
|
||||||
|
val code = ints[5].toInt().also { if (it == prevCode) return@forEach }
|
||||||
|
val ip = ints.take(4).fold(0) { acc, s -> acc shl 8 or s.toInt() }
|
||||||
|
|
||||||
|
out.writeInt(ip)
|
||||||
|
out.writeInt(code)
|
||||||
|
|
||||||
|
prevCode = code
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Processed data written to: ${outputFile.absolutePath}")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package org.thoughtcrime.securesms.util
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.junit.runners.Parameterized
|
||||||
|
import org.mockito.Mockito.mock
|
||||||
|
|
||||||
|
@RunWith(Parameterized::class)
|
||||||
|
class IP2CountryTest(
|
||||||
|
private val ip: String,
|
||||||
|
private val country: String
|
||||||
|
) {
|
||||||
|
private val context: Context = mock(Context::class.java)
|
||||||
|
private val ip2Country = IP2Country(context, this::class.java.classLoader!!::getResourceAsStream)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun getCountryNamesCache() {
|
||||||
|
assertEquals(country, ip2Country.cacheCountryForIP(ip))
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
@Parameterized.Parameters
|
||||||
|
fun data(): Collection<Array<Any>> = listOf(
|
||||||
|
arrayOf("223.121.64.0", "Hong Kong"),
|
||||||
|
arrayOf("223.121.64.1", "Hong Kong"),
|
||||||
|
arrayOf("223.121.127.0", "Hong Kong"),
|
||||||
|
arrayOf("223.121.128.0", "China"),
|
||||||
|
arrayOf("223.121.129.0", "China"),
|
||||||
|
arrayOf("223.122.0.0", "Hong Kong"),
|
||||||
|
arrayOf("223.123.0.0", "Pakistan"),
|
||||||
|
arrayOf("223.123.128.0", "China"),
|
||||||
|
arrayOf("223.124.0.0", "China"),
|
||||||
|
arrayOf("223.128.0.0", "China"),
|
||||||
|
arrayOf("223.130.0.0", "Singapore")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue