Skip to content

Commit

Permalink
feat: new cdk verification times limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Aliothmoon committed Jan 10, 2025
1 parent 3ba9f8a commit c0e814a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@


<dependencies>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.moandjiezana.toml</groupId>
<artifactId>toml4j</artifactId>
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/biz/cdkey.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package biz


import config.RDS
import datasource.DB
import exception.ServiceException
import model.PlanParams
Expand All @@ -12,7 +13,9 @@ import org.ktorm.dsl.*
import utils.throwIf
import utils.throwIfNot
import java.nio.ByteBuffer
import java.time.Duration
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.concurrent.ThreadLocalRandom
import java.util.concurrent.atomic.AtomicInteger
import kotlin.math.abs
Expand Down Expand Up @@ -98,6 +101,8 @@ fun validateCDK(params: ValidateParams): Resp {
(eId != specId).throwIf("CDK已被使用")
}

limit(cdk)

// log
DB.insert(OperationLog) {
set(OperationLog.cdk, cdk)
Expand All @@ -108,3 +113,16 @@ fun validateCDK(params: ValidateParams): Resp {

return Resp.success()
}

private fun limit(cdk: String) {
val date = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now())
val key = "limit:${date}:${cdk}"
val cnt = RDS.get().get(key).get()?.toIntOrNull() ?: 0
(cnt > 7).throwIf("您的账号已被限制")

RDS.get().incr(key).get()

if (cnt == 0) {
RDS.get().expire(key, Duration.ofDays(1))
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/config/props.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ object Props {

fun get(key: String): String = TomlStore.config.value.getString("datasource.$key")
}

object Redis {
val url: String by lazy {
TomlStore.config.value.getString("redis.url")
}
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/config/redis.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package config

import io.lettuce.core.RedisClient
import io.lettuce.core.api.StatefulRedisConnection
import io.lettuce.core.api.async.RedisAsyncCommands

object RDS {
private val single = run {
val client: RedisClient = RedisClient.create(Props.Redis.url)
val connection: StatefulRedisConnection<String, String> = client.connect()
connection.async()
}

fun get(): RedisAsyncCommands<String, String> {
return single
}
}

0 comments on commit c0e814a

Please sign in to comment.