From 5e62cf91461c0a84e57252693706f8c97c6df298 Mon Sep 17 00:00:00 2001 From: Aliothmoon Date: Sat, 8 Feb 2025 14:50:12 +0800 Subject: [PATCH] feat: do once billing checkin callback --- pom.xml | 5 ++++ src/main/kotlin/biz/cdkey.kt | 7 ++++-- src/main/kotlin/biz/rpc.kt | 43 +++++++++++++++++++++++++++++++++ src/main/kotlin/config/props.kt | 3 +++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/biz/rpc.kt diff --git a/pom.xml b/pom.xml index 9721f1e..3c5322b 100644 --- a/pom.xml +++ b/pom.xml @@ -120,6 +120,11 @@ + + com.squareup.okhttp3 + okhttp + 4.12.0 + io.vertx vertx-web diff --git a/src/main/kotlin/biz/cdkey.kt b/src/main/kotlin/biz/cdkey.kt index f8e5725..6d57b4c 100644 --- a/src/main/kotlin/biz/cdkey.kt +++ b/src/main/kotlin/biz/cdkey.kt @@ -122,7 +122,7 @@ fun validateCDK(params: ValidateParams): Resp { val status = record.status expireTime.isBefore(LocalDateTime.now()).throwIf("The cdk has expired") - limit(cdk) + doLimit(cdk) val isFirstBinding = status == 0 @@ -142,6 +142,9 @@ fun validateCDK(params: ValidateParams): Resp { (row > 0).throwIfNot("cdk binding update failed") } + Thread.startVirtualThread { + doSendBillingCheckIn(cdk, params.resource ?: "", params.ua ?: "") + } // log BT.enqueue( @@ -158,7 +161,7 @@ fun validateCDK(params: ValidateParams): Resp { return Resp.success() } -private fun limit(cdk: String) { +private fun doLimit(cdk: String) { if (!Props.Extra.limitEnabled) { return } diff --git a/src/main/kotlin/biz/rpc.kt b/src/main/kotlin/biz/rpc.kt new file mode 100644 index 0000000..6e1ba0c --- /dev/null +++ b/src/main/kotlin/biz/rpc.kt @@ -0,0 +1,43 @@ +package biz + +import config.Props +import io.vertx.core.Vertx +import io.vertx.core.http.HttpClient +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody +import org.slf4j.LoggerFactory + +private val log = LoggerFactory.getLogger("RpcKt")!! + +val C: OkHttpClient = OkHttpClient.Builder() + .build() + +private val JSON_MT = "application/json; charset=utf-8".toMediaType() + +private lateinit var client: HttpClient + +fun setupClient(v: Vertx) { + client = v.createHttpClient() +} + +fun doSendBillingCheckIn(cdk: String, resource: String, ua: String) { + val body = """ + { + "cdk": "$cdk", + "application": "$resource", + "user_agent": "$ua", + "module": "", + } + """.trimIndent().toRequestBody(contentType = JSON_MT) + val resp = Request.Builder() + .url(Props.Extra.billingCheckInUrl) + .post(body) + .build().let { + C.newCall(it).execute() + } + if (resp.code != 200) { + log.error("BillingCheckIn response {}", resp.body?.string()) + } +} diff --git a/src/main/kotlin/config/props.kt b/src/main/kotlin/config/props.kt index 5cd752f..7898c99 100644 --- a/src/main/kotlin/config/props.kt +++ b/src/main/kotlin/config/props.kt @@ -61,5 +61,8 @@ object Props { val limitCount: Long by lazy { TomlStore.config.value.getLong("extra.limit.count") ?: 7 } + val billingCheckInUrl: String by lazy { + TomlStore.config.value.getString("extra.billing.checkin_url") + } } }