From 617d4d5cd2c104019e8211e5189a43678c355a7e Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 25 May 2023 19:09:10 -0400 Subject: [PATCH] Update buildId automagically --- .../be/mygod/vpnhotspot/client/MacLookup.kt | 34 ++++++++++++++----- .../java/be/mygod/vpnhotspot/util/Utils.kt | 1 - 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt b/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt index aaee7f64..42b4103a 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt @@ -19,6 +19,8 @@ import org.json.JSONObject import timber.log.Timber import java.io.IOException import java.net.HttpURLConnection +import java.util.Scanner +import java.util.regex.Pattern /** * This class generates a default nickname for new clients. @@ -36,12 +38,34 @@ object MacLookup { private val macLookupBusy = mutableMapOf() // http://en.wikipedia.org/wiki/ISO_3166-1 private val countryCodeRegex = "(?:^|[^A-Z])([A-Z]{2})[\\s\\d]*$".toRegex() + // nanoid matcher with preceding pattern + private val buildIdPattern by lazy { Pattern.compile("(?<=_next/static/|\"buildId\":\")[A-Za-z0-9_-]{21}") } private val HttpURLConnection.findErrorStream get() = errorStream ?: inputStream @MainThread fun abort(mac: MacAddress) = macLookupBusy.remove(mac)?.cancel() + private var buildId = "Tf9pzEFow9scQ87pOfWw3" + private suspend fun readResponse(mac: MacAddress, reportId: String): String { + repeat(5) { + connectCancellable( + "https://mac-address.alldatafeeds.com/_next/data/$buildId/mac-address-lookup/$reportId.json") { conn -> + when (val responseCode = conn.responseCode) { + 200 -> conn.inputStream.bufferedReader().readText() + 404 -> { + buildId = conn.errorStream.use { Scanner(it).findWithinHorizon(buildIdPattern, 0) } + ?: throw UnexpectedError(mac, "failed to locate buildId in 404") + Timber.d("Obtained new buildId: $buildId") + null + } + else -> throw UnexpectedError(mac, "Unhandled response code $responseCode: " + + conn.findErrorStream.bufferedReader().readText()) + } + }?.let { return it } + } + throw UnexpectedError(mac, "Repeated 404") + } @MainThread fun perform(mac: MacAddress, explicit: Boolean = false) { abort(mac) @@ -60,15 +84,7 @@ object MacLookup { conn.findErrorStream.bufferedReader().readText()) } } - val reportId = JSONObject(response).getString("report_id") - response = connectCancellable("https://mac-address.alldatafeeds.com/_next/data/Tf9pzEFow9scQ87pOfWw3" + - "/mac-address-lookup/$reportId.json") { conn -> - when (val responseCode = conn.responseCode) { - 200 -> conn.inputStream.bufferedReader().readText() - else -> throw UnexpectedError(mac, "Unhandled response code $responseCode: " + - conn.findErrorStream.bufferedReader().readText()) - } - } + response = readResponse(mac, JSONObject(response).getString("report_id")) val obj = JSONObject(response).getJSONObject("pageProps").getJSONObject("lookupResults") val result = if (obj.getJSONObject("blockDetails").getBoolean("blockFound")) { val vendor = obj.getJSONObject("vendorDetails") diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt index f61ec618..564a0569 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt @@ -22,7 +22,6 @@ import androidx.databinding.BindingAdapter import androidx.fragment.app.DialogFragment import androidx.fragment.app.FragmentManager import be.mygod.vpnhotspot.App.Companion.app -import be.mygod.vpnhotspot.client.MacLookup import be.mygod.vpnhotspot.net.MacAddressCompat import be.mygod.vpnhotspot.widget.SmartSnackbar import kotlinx.coroutines.CancellationException