Skip to content

Commit

Permalink
feat: remove sp_id & add ip record
Browse files Browse the repository at this point in the history
  • Loading branch information
Aliothmoon committed Feb 8, 2025
1 parent ed2d8bb commit f79e76e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 44 deletions.
47 changes: 17 additions & 30 deletions src/main/kotlin/biz/cdkey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import utils.throwIf
import utils.throwIfNot
import utils.throwIfNullOrEmpty
import java.nio.ByteBuffer
import java.security.MessageDigest
import java.time.Duration
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.concurrent.ThreadLocalRandom
Expand Down Expand Up @@ -90,38 +88,38 @@ fun acquireCDK(params: PlanParams): Resp {

}

@OptIn(ExperimentalStdlibApi::class)
private const val VALIDATE = "validate"
private const val tips = "Please confirm that you have entered the correct cdkey"

fun validateCDK(params: ValidateParams): Resp {
with(params) {
cdk.isNullOrBlank().throwIf("cdk cannot be empty")
}
val cdk = params.cdk!!
val tmp = params.specificationId

val cdk = params.cdk
if (cdk.isNullOrBlank()) {
throw ServiceException(tips)
}

val record = C.get(cdk) {


val qr = DB.from(CDK)
.select(CDK.expireTime, CDK.specificationId, CDK.status)
.where {
CDK.key eq cdk
}
.iterator()
if (!qr.hasNext()) {
throw ServiceException("Please confirm that you have entered the correct cdkey")
throw ServiceException(tips)
}
qr.next().run {
ValidTuple(
this[CDK.status]!!,
this[CDK.expireTime]!!,
this[CDK.specificationId]
)
}
}

val expireTime = record.expireTime
val status = record.status
val oldSpecId = record.spId

expireTime.isBefore(LocalDateTime.now()).throwIf("The cdk has expired")

limit(cdk)
Expand All @@ -130,28 +128,17 @@ fun validateCDK(params: ValidateParams): Resp {
val isFirstBinding = status == 0


val specId = when {
tmp != null && oldSpecId == null -> {
MessageDigest.getInstance("SHA-256").digest(tmp.toByteArray()).toHexString()
}
if (isFirstBinding) {

else -> null
}
if (isFirstBinding || specId != null) {
C.invalidate(cdk)
// ignore extreme races for now
val row = DB.update(CDK) {
where {
CDK.key eq cdk
}
if (isFirstBinding) {
set(CDK.status, 1)
}
if (specId != null) {
set(CDK.specificationId, specId)
}
set(CDK.status, 1)
}

C.invalidate(cdk)
(row > 0).throwIfNot("cdk binding update failed")
}

Expand All @@ -160,15 +147,15 @@ fun validateCDK(params: ValidateParams): Resp {
BT.enqueue(
LogRecord(
cdk = cdk,
source = params.source,
spId = specId ?: oldSpecId,
type = "validate",
resource = params.resource,
type = VALIDATE,
ua = params.ua,
ip = params.ip,
time = LocalDateTime.now()
)
)

return Resp.success(isFirstBinding)
return Resp.success()
}

private fun limit(cdk: String) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/cache/cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ import datasource.DB
import model.LogRecord
import model.ValidTuple
import model.entity.OperationLog
import org.ktorm.dsl.QueryRowSet
import org.ktorm.dsl.batchInsert
import java.util.concurrent.TimeUnit


val C: Cache<String, ValidTuple> = Caffeine.newBuilder()
.maximumSize(100)
.expireAfterWrite(10, TimeUnit.MINUTES)
.expireAfterWrite(30, TimeUnit.MINUTES)
.softValues()
.build()

var BT: BufferTrigger<LogRecord> = run {
BufferTrigger.batchBlocking<LogRecord>()
.bufferSize(1000)
.batchSize(500)
.linger(30, TimeUnit.SECONDS)
.linger(5, TimeUnit.SECONDS)
.setConsumerEx {
Thread.startVirtualThread {
val l = ArrayList(it)
DB.batchInsert(OperationLog) {
l.forEach { v ->
item {
set(OperationLog.cdk, v.cdk)
set(OperationLog.source, v.source)
set(OperationLog.resource, v.resource)
set(OperationLog.ua, v.ua)
set(OperationLog.ip, v.ip)
set(OperationLog.type, v.type)
set(OperationLog.createdAt, v.time)
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/model/entity/entities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ object OperationLog : Table<Nothing>("mirrorc_operation_log") {
val id = int("id").primaryKey()
val cdk = varchar("cdk")
val specificationId = varchar("specification_id")
val ip = varchar("ip")
val ua = varchar("ua")
val source = varchar("source")
val resource = varchar("resource")
val type = varchar("type")
val createdAt = datetime("created_at")
}
Expand Down
13 changes: 7 additions & 6 deletions src/main/kotlin/model/param.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,26 @@ class RenewParams {
* @constructor 创建[ValidateParams]
*/
class ValidateParams {
/**
* 硬件ID
* */
var specificationId: String? = null

/**
* CDK
* */
var cdk: String? = null

/**
* 来源
* 资源类型
* */
var source: String? = null
var resource: String? = null

/**
* user-agent
* */
var ua: String? = null

/**
* ip
*/
var ip: String? = ""
}


Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/model/transfer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import java.time.LocalDateTime

data class LogRecord(
val cdk: String,
val source: String?,
val spId: String?,
val resource: String?,
val type: String,
val ua: String?,
val ip: String?,
val time: LocalDateTime,
)

class ValidTuple(
val status: Int,
val expireTime: LocalDateTime,
val spId: String?,
)
3 changes: 3 additions & 0 deletions src/main/kotlin/router/router.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private fun requireJsonParams(ctx: RoutingContext): String? {
* @param [router]
*/
private fun dispatch(router: Router) {

router.post("/acquire").handler { ctx ->
requireJsonParams(ctx)?.let {
val p = JSON.parseObject(it, PlanParams::class.java)
Expand All @@ -76,6 +77,7 @@ private fun dispatch(router: Router) {
}
}
}

router.post("/renew").handler { ctx ->
requireJsonParams(ctx)?.let {
val p = JSON.parseObject(it, RenewParams::class.java)
Expand All @@ -86,6 +88,7 @@ private fun dispatch(router: Router) {
}

}

router.post("/validate").handler { ctx ->
requireJsonParams(ctx)?.let {
val p = JSON.parseObject(it, ValidateParams::class.java)
Expand Down

0 comments on commit f79e76e

Please sign in to comment.