1
1
package me.teble.xposed.autodaily.utils
2
2
3
+ import kotlinx.serialization.json.JsonArray
4
+ import kotlinx.serialization.json.JsonElement
5
+ import kotlinx.serialization.json.JsonObject
3
6
import okhttp3.Request
4
7
5
8
enum class FileEnum (val path : String ) {
@@ -24,14 +27,23 @@ object RepoFileLoader {
24
27
25
28
private var repoUrl = originRepoUrl
26
29
27
- private fun loadRepoFile (path : String ): String? {
30
+ private fun loadRepoFile (path : String , verifyJson : Boolean ): String? {
28
31
LogUtil .d(" curr repo url: $repoUrl " )
29
32
val url = " $repoUrl /$path "
30
33
val req = Request .Builder ().url(url).build()
31
34
try {
32
35
val response = client.newCall(req).execute()
33
36
if (response.isSuccessful) {
34
- return response.body!! .string()
37
+ val content = response.body!! .string()
38
+ if (verifyJson) {
39
+ val cls = content.parse<JsonElement >()::class .java
40
+ LogUtil .d(" verify json content: $cls " )
41
+ if (cls != JsonObject ::class .java
42
+ && cls != JsonArray ::class .java) {
43
+ throw IllegalArgumentException (" invalid json: $content " )
44
+ }
45
+ }
46
+ return content
35
47
}
36
48
LogUtil .w(" load repo file failed: $url , code: ${response.code} , response: ${response.body} " )
37
49
return null
@@ -40,15 +52,15 @@ object RepoFileLoader {
40
52
val index = repoUrls.indexOf(repoUrl)
41
53
if (index < repoUrls.size - 1 ) {
42
54
repoUrl = repoUrls[index + 1 ]
43
- return loadRepoFile(path)
55
+ return loadRepoFile(path, verifyJson )
44
56
}
45
57
throw e
46
58
}
47
59
}
48
60
49
61
fun load (fileEnum : FileEnum ): String? {
50
62
return runCatching {
51
- loadRepoFile(fileEnum.path)
63
+ loadRepoFile(fileEnum.path, fileEnum.path.endsWith( " .json " ) )
52
64
}.getOrNull()
53
65
}
54
66
}
0 commit comments