fix(core): resolve startup crash on existing devices, add cleartext traffic permission, and implement safe Gson deserializers (v1.3.1)
Build & Release Android APK / Build & Publish Release APK (push) Successful in 1m57s
Build & Release Android APK / Build & Publish Release APK (push) Successful in 1m57s
This commit is contained in:
@@ -117,14 +117,20 @@ class MainActivity : FragmentActivity() {
|
||||
}
|
||||
|
||||
// Auto-upgrade existing default device with SysPulse defaults if missing
|
||||
repository.getDevices().forEach { dev ->
|
||||
if (dev.node.equals("debian", ignoreCase = true) && dev.syspulseUrl.isBlank()) {
|
||||
val upgraded = dev.copy(
|
||||
syspulseUrl = "http://100.85.63.44:8088",
|
||||
syspulseSecretKey = if (dev.syspulseSecretKey.isBlank()) "default_pulse_secret_key_change_me" else dev.syspulseSecretKey
|
||||
)
|
||||
repository.updateDevice(upgraded)
|
||||
try {
|
||||
repository.getDevices().forEach { dev ->
|
||||
val sysUrl = dev.syspulseUrl
|
||||
val nodeName = dev.node
|
||||
if (nodeName.equals("debian", ignoreCase = true) && sysUrl.isBlank()) {
|
||||
val upgraded = dev.copy(
|
||||
syspulseUrl = "http://100.85.63.44:8088",
|
||||
syspulseSecretKey = if (dev.syspulseSecretKey.isBlank()) "default_pulse_secret_key_change_me" else dev.syspulseSecretKey
|
||||
)
|
||||
repository.updateDevice(upgraded)
|
||||
}
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
// Guard against any startup migration issues
|
||||
}
|
||||
triggerClient = TriggerClient()
|
||||
appSettings = AppSettings(applicationContext)
|
||||
|
||||
@@ -7,7 +7,7 @@ data class DeviceConfig(
|
||||
val name: String,
|
||||
val node: String,
|
||||
val secretKey: String,
|
||||
val r2Url: String,
|
||||
val r2Url: String = "",
|
||||
val powerOnUrl: String,
|
||||
val powerOffUrl: String,
|
||||
val rebootUrl: String,
|
||||
|
||||
@@ -4,7 +4,14 @@ import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import com.expapp.trigger.data.model.DeviceConfig
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.JsonDeserializationContext
|
||||
import com.google.gson.JsonDeserializer
|
||||
import com.google.gson.JsonElement
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import java.lang.reflect.Type
|
||||
import java.util.UUID
|
||||
|
||||
interface KeyValueStorage {
|
||||
fun getString(key: String, defaultValue: String?): String?
|
||||
@@ -44,8 +51,29 @@ class SharedPrefsStorage(
|
||||
}
|
||||
}
|
||||
|
||||
class DeviceConfigDeserializer : JsonDeserializer<DeviceConfig> {
|
||||
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): DeviceConfig {
|
||||
val obj = if (json.isJsonObject) json.asJsonObject else JsonObject()
|
||||
return DeviceConfig(
|
||||
id = obj.get("id")?.asString?.ifBlank { UUID.randomUUID().toString() } ?: UUID.randomUUID().toString(),
|
||||
name = obj.get("name")?.asString ?: "Server",
|
||||
node = obj.get("node")?.asString ?: "debian",
|
||||
secretKey = obj.get("secretKey")?.asString ?: "",
|
||||
r2Url = obj.get("r2Url")?.asString ?: "",
|
||||
powerOnUrl = obj.get("powerOnUrl")?.asString ?: "",
|
||||
powerOffUrl = obj.get("powerOffUrl")?.asString ?: "",
|
||||
rebootUrl = obj.get("rebootUrl")?.asString ?: "",
|
||||
statusUrl = obj.get("statusUrl")?.asString ?: "",
|
||||
syspulseUrl = obj.get("syspulseUrl")?.asString ?: "",
|
||||
syspulseSecretKey = obj.get("syspulseSecretKey")?.asString ?: ""
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
class DeviceRepository(private val storage: KeyValueStorage) {
|
||||
private val gson = Gson()
|
||||
private val gson: Gson = GsonBuilder()
|
||||
.registerTypeAdapter(DeviceConfig::class.java, DeviceConfigDeserializer())
|
||||
.create()
|
||||
private val storageKey = "saved_devices_json"
|
||||
|
||||
fun getDevices(): List<DeviceConfig> {
|
||||
|
||||
@@ -2,14 +2,23 @@ package com.expapp.trigger.network
|
||||
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import com.expapp.trigger.data.model.CpuMetrics
|
||||
import com.expapp.trigger.data.model.DeviceConfig
|
||||
import com.expapp.trigger.data.model.DiskMetrics
|
||||
import com.expapp.trigger.data.model.RamMetrics
|
||||
import com.expapp.trigger.data.model.ScheduledTask
|
||||
import com.expapp.trigger.data.model.ScheduledTasksResponse
|
||||
import com.expapp.trigger.data.model.ServiceOperation
|
||||
import com.expapp.trigger.data.model.SysPulseStatus
|
||||
import com.expapp.trigger.data.model.SysServiceInfo
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.JsonDeserializationContext
|
||||
import com.google.gson.JsonDeserializer
|
||||
import com.google.gson.JsonElement
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonParser
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import okhttp3.Call
|
||||
import okhttp3.Callback
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
@@ -18,8 +27,41 @@ import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.Response
|
||||
import java.io.IOException
|
||||
import java.lang.reflect.Type
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class SysPulseStatusDeserializer : JsonDeserializer<SysPulseStatus> {
|
||||
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): SysPulseStatus {
|
||||
if (!json.isJsonObject) return SysPulseStatus()
|
||||
val obj = json.asJsonObject
|
||||
val cpu = if (obj.has("cpu") && obj.get("cpu").isJsonObject) {
|
||||
context.deserialize<CpuMetrics>(obj.get("cpu"), CpuMetrics::class.java) ?: CpuMetrics()
|
||||
} else CpuMetrics()
|
||||
val ram = if (obj.has("ram") && obj.get("ram").isJsonObject) {
|
||||
context.deserialize<RamMetrics>(obj.get("ram"), RamMetrics::class.java) ?: RamMetrics()
|
||||
} else RamMetrics()
|
||||
val disk = if (obj.has("disk") && obj.get("disk").isJsonArray) {
|
||||
val listType = object : TypeToken<List<DiskMetrics>>() {}.type
|
||||
context.deserialize<List<DiskMetrics>>(obj.get("disk"), listType) ?: emptyList()
|
||||
} else emptyList()
|
||||
val services = if (obj.has("services") && obj.get("services").isJsonArray) {
|
||||
val listType = object : TypeToken<List<SysServiceInfo>>() {}.type
|
||||
context.deserialize<List<SysServiceInfo>>(obj.get("services"), listType) ?: emptyList()
|
||||
} else emptyList()
|
||||
val uptime = obj.get("uptime")?.asString ?: ""
|
||||
val success = obj.get("success")?.asBoolean ?: true
|
||||
|
||||
return SysPulseStatus(
|
||||
success = success,
|
||||
uptime = uptime,
|
||||
cpu = cpu,
|
||||
ram = ram,
|
||||
disk = disk,
|
||||
services = services
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
class SysPulseClient(
|
||||
private val okHttpClient: OkHttpClient = OkHttpClient.Builder()
|
||||
.followRedirects(true)
|
||||
@@ -27,7 +69,9 @@ class SysPulseClient(
|
||||
.connectTimeout(8, TimeUnit.SECONDS)
|
||||
.readTimeout(12, TimeUnit.SECONDS)
|
||||
.build(),
|
||||
private val gson: Gson = Gson()
|
||||
private val gson: Gson = GsonBuilder()
|
||||
.registerTypeAdapter(SysPulseStatus::class.java, SysPulseStatusDeserializer())
|
||||
.create()
|
||||
) {
|
||||
companion object {
|
||||
const val DEFAULT_FALLBACK_SECRET = "default_pulse_secret_key_change_me"
|
||||
|
||||
Reference in New Issue
Block a user