Fix sync via https

pull/367/head
Koitharu 3 years ago
parent be666b7854
commit 8bfdc73072
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -20,8 +20,9 @@ class SyncAuthApi @Inject constructor(
val body = JSONObject( val body = JSONObject(
mapOf("email" to email, "password" to password), mapOf("email" to email, "password" to password),
).toRequestBody() ).toRequestBody()
val scheme = getScheme(host)
val request = Request.Builder() val request = Request.Builder()
.url("http://$host/auth") .url("$scheme://$host/auth")
.post(body) .post(body)
.build() .build()
val response = okHttpClient.newCall(request).await() val response = okHttpClient.newCall(request).await()
@ -33,4 +34,13 @@ class SyncAuthApi @Inject constructor(
throw SyncApiException(message, code) 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
}
} }

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

@ -8,6 +8,7 @@ import okhttp3.Response
import org.koitharu.kotatsu.BuildConfig import org.koitharu.kotatsu.BuildConfig
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.db.DATABASE_VERSION import org.koitharu.kotatsu.core.db.DATABASE_VERSION
import org.koitharu.kotatsu.core.network.CommonHeaders
class SyncInterceptor( class SyncInterceptor(
context: Context, context: Context,
@ -21,7 +22,7 @@ class SyncInterceptor(
val token = accountManager.peekAuthToken(account, tokenType) val token = accountManager.peekAuthToken(account, tokenType)
val requestBuilder = chain.request().newBuilder() val requestBuilder = chain.request().newBuilder()
if (token != null) { 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-App-Version", BuildConfig.VERSION_CODE.toString())
requestBuilder.header("X-Db-Version", DATABASE_VERSION.toString()) requestBuilder.header("X-Db-Version", DATABASE_VERSION.toString())

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

Loading…
Cancel
Save