Safe access to url or IP 0

This commit is contained in:
sim 2023-02-26 16:12:27 +01:00
parent 44dd904e4c
commit ae555033d2
3 changed files with 11 additions and 8 deletions

View File

@ -6,8 +6,8 @@ import android.content.Intent
interface AccountFactory {
val apiFactory: Class<*>
val name: String
val url: String
val name: String?
val url: String?
fun initAccount(context: Context)
fun isConnected(context: Context): Boolean
fun connect(activity: Activity)

View File

@ -22,10 +22,10 @@ import org.unifiedpush.distributor.nextpush.utils.TAG
class SSOAccountFactory : AccountFactory {
override val apiFactory: Class<*> = ApiSSOFactory::class.java
override val name: String
get() = ssoAccount!!.name
override val url: String
get() = ssoAccount!!.url
override val name: String?
get() = ssoAccount?.name
override val url: String?
get() = ssoAccount?.url
private var ssoAccount: SingleSignOnAccount? = null

View File

@ -29,6 +29,9 @@ object Api {
private var provider: ApiProviderFactory? = null
private var source: EventSource? = null
private val Context.baseUrl: String
get() = getAccount(this)?.url ?: "http://0.0.0.0/"
private fun Context.withApiProvider(block: (ApiProvider) -> Unit) {
(
provider ?: run {
@ -79,7 +82,7 @@ object Api {
}
override fun onComplete() {
saveUrl(this@apiSync, "${getAccount(this@apiSync)?.url}$mApiEndpoint")
saveUrl(this@apiSync, "$baseUrl$mApiEndpoint")
// Sync once it is registered
deviceId?.let {
@ -97,7 +100,7 @@ object Api {
.readTimeout(0, TimeUnit.SECONDS)
.retryOnConnectionFailure(false)
.build()
val url = "${getAccount(this)?.url}$mApiEndpoint/device/$deviceId"
val url = "$baseUrl$mApiEndpoint/device/$deviceId"
val request = Request.Builder().url(url)
.get()