架構調整-使其支援多筆設定
This commit is contained in:
parent
864d181a16
commit
2f4532cc0b
10
config.json
10
config.json
@ -1,4 +1,10 @@
|
||||
{
|
||||
[
|
||||
{
|
||||
"token": "n_9L9-W70oYWtMV16GgYTs0yWZ72Vjs17QD4dUZl",
|
||||
"domain": "ray650128.com"
|
||||
}
|
||||
},
|
||||
{
|
||||
"token": "n_9L9-W70oYWtMV16GgYTs0yWZ72Vjs17QD4dUZl",
|
||||
"domain": "sakunadaisuki.club"
|
||||
}
|
||||
]
|
||||
@ -1,5 +1,6 @@
|
||||
import api.CloudflareApiService
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withContext
|
||||
@ -14,10 +15,7 @@ import java.io.IOException
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
|
||||
private lateinit var config: Configure
|
||||
|
||||
private lateinit var token: String
|
||||
private lateinit var domain: String
|
||||
private var configs: List<Configure> = emptyList()
|
||||
|
||||
fun main() {
|
||||
println("Cloudflare DDNS 更新程式已啟動")
|
||||
@ -36,7 +34,7 @@ fun main() {
|
||||
return
|
||||
}
|
||||
|
||||
config = loadConfigFile(configRawText)
|
||||
configs = loadConfigFile(configRawText)
|
||||
|
||||
val retrofit = Retrofit.Builder()
|
||||
.baseUrl("https://api.cloudflare.com")
|
||||
@ -51,13 +49,14 @@ fun main() {
|
||||
if (ipAddress != null) {
|
||||
println("您目前的 WAN IP 位址為: $ipAddress")
|
||||
try {
|
||||
println("驗證 CF 權杖是否有效...")
|
||||
configs.forEach { (token, domain) ->
|
||||
println("[$domain]驗證 CF 權杖是否有效...")
|
||||
val verifyTokenResult = apiService.getVerifyToken("Bearer $token")
|
||||
if (verifyTokenResult.success) {
|
||||
println("CF 權杖有效,載入 Zone 資料中...")
|
||||
println("[$domain]CF 權杖有效,載入 Zone 資料中...")
|
||||
val zonesResult = apiService.getZones("Bearer $token")
|
||||
if (zonesResult.success) {
|
||||
println("Zone 資料載入成功,取得域名資料中...")
|
||||
println("[$domain]Zone 資料載入成功,取得域名資料中...")
|
||||
val zoneData = zonesResult.result.firstOrNull { it.name == domain } ?: run {
|
||||
println("無法找到指定的域名 [ $domain ]...")
|
||||
return@runBlocking
|
||||
@ -65,7 +64,7 @@ fun main() {
|
||||
val zoneId = zoneData.id
|
||||
val zoneDnsRecords = apiService.getZoneDnsRecords("Bearer $token", zoneId)
|
||||
if (zoneDnsRecords.success) {
|
||||
println("域名資料載入成功,處理中...")
|
||||
println("[$domain]域名資料載入成功,處理中...")
|
||||
val record = zoneDnsRecords.result.firstOrNull { it.name == domain } ?: run {
|
||||
println("無法找到指定的域名 [ $domain ]...")
|
||||
return@runBlocking
|
||||
@ -80,24 +79,25 @@ fun main() {
|
||||
)
|
||||
val updateDnsResult = apiService.updateZoneDnsRecords("Bearer $token", zoneId, recordId, updateDnsBody)
|
||||
if (updateDnsResult.success) {
|
||||
println("更新 DDNS 成功")
|
||||
println("[$domain]更新 DDNS 成功")
|
||||
exitProcess(0)
|
||||
} else {
|
||||
println("更新 DDNS 失敗")
|
||||
println("[$domain]更新 DDNS 失敗")
|
||||
exitProcess(0)
|
||||
}
|
||||
} else {
|
||||
println("無法取得域名資料資料")
|
||||
println("[$domain]無法取得域名資料")
|
||||
exitProcess(0)
|
||||
}
|
||||
} else {
|
||||
println("無法取得 Zone 資料")
|
||||
println("[$domain]無法取得 Zone 資料")
|
||||
exitProcess(0)
|
||||
}
|
||||
} else {
|
||||
println("CF 權杖無效")
|
||||
println("[$domain]CF 權杖無效")
|
||||
exitProcess(0)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
println("Error: ${e.message}")
|
||||
@ -110,14 +110,14 @@ fun main() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadConfigFile(configRawText: String): Configure {
|
||||
val config = Gson().fromJson(configRawText, Configure::class.java)
|
||||
token = config.token
|
||||
domain = config.domain
|
||||
private fun loadConfigFile(configRawText: String): List<Configure> {
|
||||
val listType = object : TypeToken<List<Configure>>() {}.type
|
||||
val config = Gson().fromJson<List<Configure>>(configRawText, listType)
|
||||
|
||||
println("-------- [ 設定檔已載入 ] --------")
|
||||
println("CF 權杖: $token")
|
||||
println("欲更新的域名: $domain")
|
||||
println("設定檔筆數: ${config.size}")
|
||||
println("欲更新的域名:")
|
||||
config.map { it.domain }.forEach(::println)
|
||||
return config
|
||||
}
|
||||
|
||||
|
||||
@ -1,8 +1,5 @@
|
||||
package model
|
||||
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class Configure(
|
||||
val token: String,
|
||||
val domain: String
|
||||
|
||||
Loading…
Reference in New Issue
Block a user