架構調整-使其支援多筆設定

This commit is contained in:
Raymond Yang 2026-03-12 14:30:26 +08:00
parent 864d181a16
commit 2f4532cc0b
3 changed files with 55 additions and 52 deletions

View File

@ -1,4 +1,10 @@
{ [
{
"token": "n_9L9-W70oYWtMV16GgYTs0yWZ72Vjs17QD4dUZl", "token": "n_9L9-W70oYWtMV16GgYTs0yWZ72Vjs17QD4dUZl",
"domain": "ray650128.com" "domain": "ray650128.com"
} },
{
"token": "n_9L9-W70oYWtMV16GgYTs0yWZ72Vjs17QD4dUZl",
"domain": "sakunadaisuki.club"
}
]

View File

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

View File

@ -1,8 +1,5 @@
package model package model
import com.google.gson.annotations.SerializedName
data class Configure( data class Configure(
val token: String, val token: String,
val domain: String val domain: String