diff --git a/config.json b/config.json index b91be9c..5734549 100644 --- a/config.json +++ b/config.json @@ -1,4 +1,10 @@ -{ - "token": "n_9L9-W70oYWtMV16GgYTs0yWZ72Vjs17QD4dUZl", - "domain": "ray650128.com" -} \ No newline at end of file +[ + { + "token": "n_9L9-W70oYWtMV16GgYTs0yWZ72Vjs17QD4dUZl", + "domain": "ray650128.com" + }, + { + "token": "n_9L9-W70oYWtMV16GgYTs0yWZ72Vjs17QD4dUZl", + "domain": "sakunadaisuki.club" + } +] \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index be56191..9ac04af 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -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 = 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,52 +49,54 @@ fun main() { if (ipAddress != null) { println("您目前的 WAN IP 位址為: $ipAddress") try { - println("驗證 CF 權杖是否有效...") - val verifyTokenResult = apiService.getVerifyToken("Bearer $token") - if (verifyTokenResult.success) { - println("CF 權杖有效,載入 Zone 資料中...") - val zonesResult = apiService.getZones("Bearer $token") - if (zonesResult.success) { - println("Zone 資料載入成功,取得域名資料中...") - val zoneData = zonesResult.result.firstOrNull { it.name == domain } ?: run { - println("無法找到指定的域名 [ $domain ]...") - return@runBlocking - } - val zoneId = zoneData.id - val zoneDnsRecords = apiService.getZoneDnsRecords("Bearer $token", zoneId) - if (zoneDnsRecords.success) { - println("域名資料載入成功,處理中...") - val record = zoneDnsRecords.result.firstOrNull { it.name == domain } ?: run { + configs.forEach { (token, domain) -> + println("[$domain]驗證 CF 權杖是否有效...") + val verifyTokenResult = apiService.getVerifyToken("Bearer $token") + if (verifyTokenResult.success) { + println("[$domain]CF 權杖有效,載入 Zone 資料中...") + val zonesResult = apiService.getZones("Bearer $token") + if (zonesResult.success) { + println("[$domain]Zone 資料載入成功,取得域名資料中...") + val zoneData = zonesResult.result.firstOrNull { it.name == domain } ?: run { println("無法找到指定的域名 [ $domain ]...") return@runBlocking } - val recordId = record.id - val updateDnsBody = UpdateDnsBody( - type = "A", - name = record.name, - content = ipAddress, - ttl = 120, - proxied = false - ) - val updateDnsResult = apiService.updateZoneDnsRecords("Bearer $token", zoneId, recordId, updateDnsBody) - if (updateDnsResult.success) { - println("更新 DDNS 成功") - exitProcess(0) + val zoneId = zoneData.id + val zoneDnsRecords = apiService.getZoneDnsRecords("Bearer $token", zoneId) + if (zoneDnsRecords.success) { + println("[$domain]域名資料載入成功,處理中...") + val record = zoneDnsRecords.result.firstOrNull { it.name == domain } ?: run { + println("無法找到指定的域名 [ $domain ]...") + return@runBlocking + } + val recordId = record.id + val updateDnsBody = UpdateDnsBody( + type = "A", + name = record.name, + content = ipAddress, + ttl = 120, + proxied = false + ) + val updateDnsResult = apiService.updateZoneDnsRecords("Bearer $token", zoneId, recordId, updateDnsBody) + if (updateDnsResult.success) { + println("[$domain]更新 DDNS 成功") + exitProcess(0) + } else { + println("[$domain]更新 DDNS 失敗") + exitProcess(0) + } } else { - println("更新 DDNS 失敗") + println("[$domain]無法取得域名資料") exitProcess(0) } } else { - println("無法取得域名資料資料") + println("[$domain]無法取得 Zone 資料") exitProcess(0) } } else { - println("無法取得 Zone 資料") + println("[$domain]CF 權杖無效") exitProcess(0) } - } else { - println("CF 權杖無效") - exitProcess(0) } } catch (e: Exception) { e.printStackTrace() @@ -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 { + val listType = object : TypeToken>() {}.type + val config = Gson().fromJson>(configRawText, listType) println("-------- [ 設定檔已載入 ] --------") - println("CF 權杖: $token") - println("欲更新的域名: $domain") + println("設定檔筆數: ${config.size}") + println("欲更新的域名:") + config.map { it.domain }.forEach(::println) return config } diff --git a/src/main/kotlin/model/Configure.kt b/src/main/kotlin/model/Configure.kt index fb46c28..015c30f 100644 --- a/src/main/kotlin/model/Configure.kt +++ b/src/main/kotlin/model/Configure.kt @@ -1,8 +1,5 @@ package model - -import com.google.gson.annotations.SerializedName - data class Configure( val token: String, val domain: String