@@ -2,6 +2,7 @@ package be.mygod.vpnhotspot.client
2
2
3
3
import android.content.Context
4
4
import android.net.MacAddress
5
+ import android.text.Html
5
6
import androidx.annotation.MainThread
6
7
import be.mygod.vpnhotspot.App.Companion.app
7
8
import be.mygod.vpnhotspot.R
@@ -40,54 +41,30 @@ object MacLookup {
40
41
// http://en.wikipedia.org/wiki/ISO_3166-1
41
42
private val countryCodeRegex = " (?:^|[^A-Z])([A-Z]{2})[\\ s\\ d]*$" .toRegex()
42
43
// 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= \" )[^ \" ]*(?= \" )" )
44
45
45
46
private val HttpURLConnection .findErrorStream get() = errorStream ? : inputStream
46
47
47
48
@MainThread
48
49
fun abort (mac : MacAddress ) = macLookupBusy.remove(mac)?.cancel()
49
50
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
- }
70
51
@MainThread
71
52
fun perform (mac : MacAddress , explicit : Boolean = false) {
72
53
abort(mac)
73
54
macLookupBusy[mac] = GlobalScope .launch(Dispatchers .Unconfined , CoroutineStart .UNDISPATCHED ) {
74
55
var response: String? = null
75
56
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 ->
81
58
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" )
83
61
400 , 401 , 402 , 404 , 422 , 429 , 500 -> throw UnexpectedError (mac,
84
62
conn.findErrorStream.bufferedReader().readText())
85
63
else -> throw UnexpectedError (mac, " Unhandled response code $responseCode : " +
86
64
conn.findErrorStream.bufferedReader().readText())
87
65
}
88
66
}
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())
91
68
val result = if (obj.getJSONObject(" blockDetails" ).getBoolean(" blockFound" )) {
92
69
val vendor = obj.getJSONObject(" vendorDetails" )
93
70
val company = vendor.getString(" companyName" )
0 commit comments