Skip to content

Commit

Permalink
Trust all servers (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
siper committed Jun 7, 2024
1 parent a88a90b commit 47b4200
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
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)
}
}

0 comments on commit 47b4200

Please sign in to comment.