Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6c84ecf

Browse files
committedMar 23, 2024·
Fix MacLookup
1 parent a2ea079 commit 6c84ecf

File tree

2 files changed

+7
-29
lines changed

2 files changed

+7
-29
lines changed
 

‎mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt

+6-29
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package be.mygod.vpnhotspot.client
22

33
import android.content.Context
44
import android.net.MacAddress
5+
import android.text.Html
56
import androidx.annotation.MainThread
67
import be.mygod.vpnhotspot.App.Companion.app
78
import be.mygod.vpnhotspot.R
@@ -40,54 +41,30 @@ object MacLookup {
4041
// http://en.wikipedia.org/wiki/ISO_3166-1
4142
private val countryCodeRegex = "(?:^|[^A-Z])([A-Z]{2})[\\s\\d]*$".toRegex()
4243
// nanoid matcher with preceding pattern
43-
private val buildIdPattern by lazy { Pattern.compile("(?<=_next/static/|\"buildId\":\")[A-Za-z0-9_-]{21}") }
44+
private val dataPattern = Pattern.compile("(?<=data=\")[^\"]*(?=\")")
4445

4546
private val HttpURLConnection.findErrorStream get() = errorStream ?: inputStream
4647

4748
@MainThread
4849
fun abort(mac: MacAddress) = macLookupBusy.remove(mac)?.cancel()
4950

50-
private var buildId = "GE0JVrT_SuaGTRX5y1FL3"
51-
private suspend fun readResponse(mac: MacAddress, reportId: String): String {
52-
repeat(5) {
53-
connectCancellable(
54-
"https://mac-address.alldatafeeds.com/_next/data/$buildId/mac-address-lookup/$reportId.json") { conn ->
55-
when (val responseCode = conn.responseCode) {
56-
200 -> conn.inputStream.bufferedReader().readText()
57-
404, 500 -> {
58-
buildId = conn.errorStream.use { Scanner(it).findWithinHorizon(buildIdPattern, 0) }
59-
?: throw UnexpectedError(mac, "failed to locate buildId in 404")
60-
Timber.d("Obtained new buildId: $buildId")
61-
null
62-
}
63-
else -> throw UnexpectedError(mac, "$responseCode-" +
64-
conn.findErrorStream.bufferedReader().readText())
65-
}
66-
}?.let { return it }
67-
}
68-
throw UnexpectedError(mac, "Repeated 404")
69-
}
7051
@MainThread
7152
fun perform(mac: MacAddress, explicit: Boolean = false) {
7253
abort(mac)
7354
macLookupBusy[mac] = GlobalScope.launch(Dispatchers.Unconfined, CoroutineStart.UNDISPATCHED) {
7455
var response: String? = null
7556
try {
76-
response = connectCancellable("https://mac-address.alldatafeeds.com/api/mac-address/lookup") { conn ->
77-
conn.doOutput = true
78-
conn.requestMethod = "POST"
79-
conn.setRequestProperty("Content-Type", "application/json")
80-
conn.outputStream.writer().use { it.write("{\"mac-address\":\"$mac\"}") }
57+
response = connectCancellable("https://macaddress.io/macaddress/$mac") { conn ->
8158
when (val responseCode = conn.responseCode) {
82-
200 -> conn.inputStream.bufferedReader().readText()
59+
200 -> conn.inputStream.use { Scanner(it).findWithinHorizon(dataPattern, 0) }
60+
?: throw UnexpectedError(mac, "failed to locate data")
8361
400, 401, 402, 404, 422, 429, 500 -> throw UnexpectedError(mac,
8462
conn.findErrorStream.bufferedReader().readText())
8563
else -> throw UnexpectedError(mac, "Unhandled response code $responseCode: " +
8664
conn.findErrorStream.bufferedReader().readText())
8765
}
8866
}
89-
response = readResponse(mac, JSONObject(response).getString("report_id"))
90-
val obj = JSONObject(response).getJSONObject("pageProps").getJSONObject("lookupResults")
67+
val obj = JSONObject(Html.fromHtml(response, 0).toString())
9168
val result = if (obj.getJSONObject("blockDetails").getBoolean("blockFound")) {
9269
val vendor = obj.getJSONObject("vendorDetails")
9370
val company = vendor.getString("companyName")

‎mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt

+1
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ private val engine by lazy @RequiresExtension(Build.VERSION_CODES.S, 7) {
283283
setPathDegradationMigration(ConnectionMigrationOptions.MIGRATION_OPTION_ENABLED)
284284
}.build())
285285
setEnableBrotli(true)
286+
addQuicHint("macaddress.io", 443, 443)
286287
}.build()
287288
}
288289
suspend fun <T> connectCancellable(url: String, block: suspend (HttpURLConnection) -> T): T {

0 commit comments

Comments
 (0)
Please sign in to comment.