mirror of https://github.com/hykilpikonna/AquaDX
[+] Turnstile utility class
parent
9faabba361
commit
3d503971ae
|
@ -0,0 +1,45 @@
|
||||||
|
package icu.samnyan.aqua.net.utils
|
||||||
|
|
||||||
|
import ext.Bool
|
||||||
|
import ext.HTTP
|
||||||
|
import ext.Str
|
||||||
|
import io.ktor.client.call.*
|
||||||
|
import io.ktor.client.request.*
|
||||||
|
import io.ktor.client.request.forms.*
|
||||||
|
import io.ktor.http.*
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties
|
||||||
|
import org.springframework.context.annotation.Configuration
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "turnstile")
|
||||||
|
class TurnstileProperties {
|
||||||
|
var enable: Bool = false
|
||||||
|
|
||||||
|
lateinit var secret: Str
|
||||||
|
}
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class CaptchaService(val props: TurnstileProperties) {
|
||||||
|
@Serializable
|
||||||
|
data class Outcome(val success: Boolean)
|
||||||
|
|
||||||
|
suspend fun validate(captcha: Str?, ip: Str): Boolean {
|
||||||
|
if (!props.enable) return true
|
||||||
|
if (captcha == null) return false
|
||||||
|
|
||||||
|
val outcome: Outcome = HTTP.post("https://challenges.cloudflare.com/turnstile/v0/siteverify") {
|
||||||
|
setBody(
|
||||||
|
FormDataContent(Parameters.build {
|
||||||
|
append("secret", props.secret)
|
||||||
|
append("response", captcha)
|
||||||
|
append("remoteip", ip)
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}.body()
|
||||||
|
|
||||||
|
return outcome.success
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue