Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trust all servers #141

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package ru.stersh.youamp.core.api

import android.net.Uri
import com.squareup.moshi.Moshi
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory


class SubsonicApi(
val url: String,
val username: String,
Expand All @@ -28,7 +28,7 @@ class SubsonicApi(
useLegacyAuth
)
private val errorInterceptor = ErrorInterceptor(moshi)
private val client = OkHttpClient.Builder()
private val client = getUnsafeOkHttpClient()
.addInterceptor(loggingInterceptor)
.addInterceptor(responseInterceptor)
.addInterceptor(errorInterceptor)
Expand Down Expand Up @@ -109,7 +109,7 @@ class SubsonicApi(
position: Long? = null
) = api.savePlayQueue(id, current, position)

suspend fun starSong(vararg songId: String, ) = api.star(id = songId.asList())
suspend fun starSong(vararg songId: String) = api.star(id = songId.asList())

suspend fun unstarSong(vararg id: String) = api.unstar(id = id.asList())

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ru.stersh.youamp.core.api

import android.annotation.SuppressLint
import okhttp3.OkHttpClient
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager

@SuppressLint("CustomX509TrustManager,TrustAllX509TrustManager")
internal fun getUnsafeOkHttpClient(): OkHttpClient.Builder {
try {
val trustAllCerts = arrayOf<TrustManager>(
object : X509TrustManager {
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {
}

override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {
}

override fun getAcceptedIssuers(): Array<X509Certificate> {
return arrayOf()
}
}
)

val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustAllCerts, SecureRandom())

val sslSocketFactory: SSLSocketFactory = sslContext.socketFactory

val builder: OkHttpClient.Builder = OkHttpClient.Builder()
builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
builder.hostnameVerifier { _, _ -> true }
return builder
} catch (e: Exception) {
throw RuntimeException(e)
}
}
Loading