Fix sync via https

This commit is contained in:
Koitharu
2023-05-10 13:09:26 +03:00
parent be666b7854
commit 8bfdc73072
4 changed files with 30 additions and 6 deletions

View File

@@ -20,8 +20,9 @@ class SyncAuthApi @Inject constructor(
val body = JSONObject(
mapOf("email" to email, "password" to password),
).toRequestBody()
val scheme = getScheme(host)
val request = Request.Builder()
.url("http://$host/auth")
.url("$scheme://$host/auth")
.post(body)
.build()
val response = okHttpClient.newCall(request).await()
@@ -33,4 +34,13 @@ class SyncAuthApi @Inject constructor(
throw SyncApiException(message, code)
}
}
private suspend fun getScheme(host: String): String {
val request = Request.Builder()
.url("http://$host/")
.head()
.build()
val response = okHttpClient.newCall(request).await()
return response.request.url.scheme
}
}

View File

@@ -9,6 +9,7 @@ import okhttp3.Request
import okhttp3.Response
import okhttp3.Route
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.network.CommonHeaders
class SyncAuthenticator(
context: Context,
@@ -24,7 +25,7 @@ class SyncAuthenticator(
val newToken = tryRefreshToken() ?: return null
accountManager.setAuthToken(account, tokenType, newToken)
return response.request.newBuilder()
.header("Authorization", "Bearer $newToken")
.header(CommonHeaders.AUTHORIZATION, "Bearer $newToken")
.build()
}

View File

@@ -8,6 +8,7 @@ import okhttp3.Response
import org.koitharu.kotatsu.BuildConfig
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.db.DATABASE_VERSION
import org.koitharu.kotatsu.core.network.CommonHeaders
class SyncInterceptor(
context: Context,
@@ -21,10 +22,10 @@ class SyncInterceptor(
val token = accountManager.peekAuthToken(account, tokenType)
val requestBuilder = chain.request().newBuilder()
if (token != null) {
requestBuilder.header("Authorization", "Bearer $token")
requestBuilder.header(CommonHeaders.AUTHORIZATION, "Bearer $token")
}
requestBuilder.header("X-App-Version", BuildConfig.VERSION_CODE.toString())
requestBuilder.header("X-Db-Version", DATABASE_VERSION.toString())
return chain.proceed(requestBuilder.build())
}
}
}

View File

@@ -54,8 +54,11 @@ class SyncHelper(
.addInterceptor(SyncInterceptor(context, account))
.addInterceptor(GZipInterceptor())
.build()
private val baseUrl: String
get() = "http://${settings.host}"
private val baseUrl: String by lazy {
val host = settings.host
val scheme = getScheme(host)
"$scheme://$host"
}
private val defaultGcPeriod: Long // gc period if sync enabled
get() = TimeUnit.DAYS.toMillis(4)
@@ -260,6 +263,15 @@ class SyncHelper(
return requireNotNull(tag)
}
private fun getScheme(host: String): String {
val request = Request.Builder()
.url("http://$host/")
.head()
.build()
val response = httpClient.newCall(request).execute()
return response.request.url.scheme
}
private fun gcFavourites() {
val deletedAt = System.currentTimeMillis() - defaultGcPeriod
val selection = "deleted_at != 0 AND deleted_at < ?"