Migrate to another MAC API with history
This commit is contained in:
@@ -31,7 +31,6 @@ object MacLookup {
|
|||||||
override fun getLocalizedMessage() = formatMessage(app)
|
override fun getLocalizedMessage() = formatMessage(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
const val HOST = "app.maclookup.app"
|
|
||||||
// private val sha1 = MessageDigest.getInstance("SHA-1")
|
// private val sha1 = MessageDigest.getInstance("SHA-1")
|
||||||
private val macLookupBusy = mutableMapOf<MacAddress, Job>()
|
private val macLookupBusy = mutableMapOf<MacAddress, Job>()
|
||||||
// http://en.wikipedia.org/wiki/ISO_3166-1
|
// http://en.wikipedia.org/wiki/ISO_3166-1
|
||||||
@@ -46,25 +45,32 @@ object MacLookup {
|
|||||||
macLookupBusy[mac] = GlobalScope.launch(Dispatchers.Unconfined, CoroutineStart.UNDISPATCHED) {
|
macLookupBusy[mac] = GlobalScope.launch(Dispatchers.Unconfined, CoroutineStart.UNDISPATCHED) {
|
||||||
var response: String? = null
|
var response: String? = null
|
||||||
try {
|
try {
|
||||||
response = connectCancellable("https://$HOST/v2/app/search/macs?q=$mac") { conn ->
|
response = connectCancellable("https://mac-address.alldatafeeds.com/api/mac-address/lookup") { conn ->
|
||||||
// conn.setRequestProperty("X-App-Id", "net.mobizme.macaddress")
|
conn.requestMethod = "POST"
|
||||||
// conn.setRequestProperty("X-App-Version", "2.0.11")
|
conn.setRequestProperty("Content-Type", "application/json")
|
||||||
// conn.setRequestProperty("X-App-Version-Code", "111")
|
conn.outputStream.writer().use { it.write("{\"mac-address\":\"$mac\"}") }
|
||||||
// val epoch = System.currentTimeMillis()
|
|
||||||
// conn.setRequestProperty("X-App-Epoch", epoch.toString())
|
|
||||||
// conn.setRequestProperty("X-App-Sign", "%032x".format(BigInteger(1,
|
|
||||||
// sha1.digest("aBA6AEkfg8cbHlWrBDYX_${mac}_$epoch".toByteArray()))))
|
|
||||||
when (val responseCode = conn.responseCode) {
|
when (val responseCode = conn.responseCode) {
|
||||||
200 -> conn.inputStream.bufferedReader().readText()
|
200 -> conn.inputStream.bufferedReader().readText()
|
||||||
// 400, 401, 429 -> throw UnexpectedError(mac, conn.inputStream.bufferedReader().readText())
|
400, 401, 402, 404, 422, 429, 500 -> throw UnexpectedError(mac,
|
||||||
|
conn.inputStream.bufferedReader().readText())
|
||||||
else -> throw UnexpectedError(mac, "Unhandled response code $responseCode: " +
|
else -> throw UnexpectedError(mac, "Unhandled response code $responseCode: " +
|
||||||
conn.inputStream.bufferedReader().readText())
|
conn.inputStream.bufferedReader().readText())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val obj = JSONObject(response)
|
val reportId = JSONObject(response).getString("report_id")
|
||||||
val result = if (obj.getBoolean("found")) {
|
response = connectCancellable("https://mac-address.alldatafeeds.com/_next/data/aIw2msYEY99JzKeaEXrix" +
|
||||||
val company = obj.getString("vendor")
|
"/mac-address-lookup/$reportId.json") { conn ->
|
||||||
val match = extractCountry(mac, response, obj)
|
when (val responseCode = conn.responseCode) {
|
||||||
|
200 -> conn.inputStream.bufferedReader().readText()
|
||||||
|
else -> throw UnexpectedError(mac, "Unhandled response code $responseCode: " +
|
||||||
|
conn.inputStream.bufferedReader().readText())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val obj = JSONObject(response).getJSONObject("pageProps").getJSONObject("lookupResults")
|
||||||
|
val result = if (obj.getJSONObject("blockDetails").getBoolean("blockFound")) {
|
||||||
|
val vendor = obj.getJSONObject("vendorDetails")
|
||||||
|
val company = vendor.getString("companyName")
|
||||||
|
val match = extractCountry(mac, response, vendor, obj)
|
||||||
if (match != null) {
|
if (match != null) {
|
||||||
String(match.groupValues[1].flatMap { listOf('\uD83C', it + 0xDDA5) }.toCharArray()) + ' ' +
|
String(match.groupValues[1].flatMap { listOf('\uD83C', it + 0xDDA5) }.toCharArray()) + ' ' +
|
||||||
company
|
company
|
||||||
@@ -87,10 +93,14 @@ object MacLookup {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun extractCountry(mac: MacAddress, response: String, obj: JSONObject): MatchResult? {
|
private fun extractCountry(mac: MacAddress, response: String, vendor: JSONObject, obj: JSONObject): MatchResult? {
|
||||||
val address = obj.optString("address")
|
for (candidate in sequence {
|
||||||
if (address.isBlank()) return null
|
yield(vendor)
|
||||||
countryCodeRegex.find(address)?.also { return it }
|
val history = obj.optJSONArray("blockHistory") ?: return@sequence
|
||||||
|
for (i in history.length() - 1 downTo 0) {
|
||||||
|
yield(history.getJSONObject(i))
|
||||||
|
}
|
||||||
|
}) countryCodeRegex.matchEntire(candidate.getString("countryCode"))?.also { return it }
|
||||||
Timber.w(UnexpectedError(mac, response))
|
Timber.w(UnexpectedError(mac, response))
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -265,7 +265,6 @@ private val engine by lazy @TargetApi(34) {
|
|||||||
setPathDegradationMigration(ConnectionMigrationOptions.MIGRATION_OPTION_ENABLED)
|
setPathDegradationMigration(ConnectionMigrationOptions.MIGRATION_OPTION_ENABLED)
|
||||||
}.build())
|
}.build())
|
||||||
setEnableBrotli(true)
|
setEnableBrotli(true)
|
||||||
addQuicHint(MacLookup.HOST, 443, 443)
|
|
||||||
}.build()
|
}.build()
|
||||||
}
|
}
|
||||||
suspend fun <T> connectCancellable(url: String, block: suspend (HttpURLConnection) -> T): T {
|
suspend fun <T> connectCancellable(url: String, block: suspend (HttpURLConnection) -> T): T {
|
||||||
|
|||||||
Reference in New Issue
Block a user