Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ba717734 authored by Jonathan Klee's avatar Jonathan Klee
Browse files

Implement http redirection

parent 9259c6f2
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -9,5 +9,6 @@
        <domain includeSubdomains="true">portal.mav.hu</domain>
        <domain includeSubdomains="true">cdwifi.cz</domain>
        <domain includeSubdomains="true">hsp.hotsplots.net</domain>
        <domain includeSubdomains="true">192.168.1.65</domain>
    </domain-config>
</network-security-config>
 No newline at end of file
+2 −1
Original line number Diff line number Diff line
@@ -9,5 +9,6 @@ data class GeolocateResponse(
    val location: ResponseLocation? = null,
    val accuracy: Double? = null,
    val fallback: String? = null,
    val error: ResponseError? = null
    val error: ResponseError? = null,
    val redirectionUrl: String? = null
)
 No newline at end of file
+59 −13
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ import android.location.Location
import android.net.Uri
import android.os.Bundle
import android.util.Log
import com.android.volley.Request.Method
import com.android.volley.NetworkResponse
import com.android.volley.VolleyError
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
@@ -22,6 +22,7 @@ import org.microg.gms.location.network.wifi.WifiDetails
import org.microg.gms.location.network.wifi.isMoving
import org.microg.gms.location.provider.BuildConfig
import org.microg.gms.utils.singleInstanceOf
import java.net.HttpURLConnection
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
@@ -58,16 +59,38 @@ class IchnaeaServiceClient(private val context: Context) {
        val response = rawGeoLocate(request)
        Log.d(TAG, "$request -> $response")
        if (response.location != null) {
            return buildLocation(response)
        } else if (response.error?.code == HttpURLConnection.HTTP_MOVED_PERM) {
            val redirection = rawGeoLocate(request, response.redirectionUrl)
            if (redirection.location != null) {
                return buildLocation(redirection)
            }
            throw ServiceException(redirection.error!!)
        } else {
            throw RuntimeException("Invalid response JSON")
        }
    }

    private fun buildLocation(response: GeolocateResponse) : Location {
        return Location("ichnaea").apply {
            if (response.location == null) {
                throw RuntimeException("location is empty")
            }

            latitude = response.location.lat
            longitude = response.location.lng
                if (response.accuracy != null) accuracy = response.accuracy.toFloat()
                if (response.fallback != null) extras = Bundle().apply { putString(LOCATION_EXTRA_FALLBACK, response.fallback) }
            if (response.accuracy != null) {
                accuracy = response.accuracy.toFloat()
            }

            if (response.fallback != null) {
                extras = Bundle().apply {
                    putString(
                        LOCATION_EXTRA_FALLBACK,
                        response.fallback
                    )
                }
            }
        } else if (response.error != null) {
            throw ServiceException(response.error)
        } else {
            throw RuntimeException("Invalid response JSON")
        }
    }

@@ -91,15 +114,38 @@ class IchnaeaServiceClient(private val context: Context) {
            }
        } catch (_: Exception) {
        }
        if (error.networkResponse != null) {
            continuation.resume(GeolocateResponse(error = ResponseError(error.networkResponse.statusCode, error.message)))

        if (error.networkResponse == null) {
            continuation.resumeWithException(error)
            return
        }
        continuation.resumeWithException(error)

        val redirectionUrl = if (error.networkResponse.statusCode == HttpURLConnection.HTTP_MOVED_PERM) {
            readRedirectionUrl(error.networkResponse)
        } else {
            ""
        }

        continuation.resume(
            GeolocateResponse(
                redirectionUrl = redirectionUrl,
                error = ResponseError(
                    error.networkResponse.statusCode,
                    error.message
                )
            )
        )
    }

    private fun readRedirectionUrl(response: NetworkResponse): String? {
        return response.allHeaders?.find { header ->
            header.name.equals("Location", ignoreCase = true)
        }?.value
    }

    private suspend fun rawGeoLocate(request: GeolocateRequest): GeolocateResponse = suspendCoroutine { continuation ->
        val url = Uri.parse(settings.ichneaeEndpoint).buildUpon().appendPath("v1").appendPath("geolocate").build().toString()
    private suspend fun rawGeoLocate(request: GeolocateRequest, address: String? = null): GeolocateResponse = suspendCoroutine { continuation ->
        val endpoint = address ?: settings.ichneaeEndpoint
        val url = Uri.parse(endpoint).buildUpon().appendPath("v1").appendPath("geolocate").build().toString()
        queue.add(object : JsonObjectRequest(Method.POST, url, request.toJson(), {
            continuation.resume(it.toGeolocateResponse())
        }, {