Merge pull request #144 from librecaptcha/simplification

Simplification and minor refactoring
This commit is contained in:
hrj 2022-02-24 20:48:12 +05:30 committed by GitHub
commit 3b6a09649d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 19 deletions

View File

@ -1,6 +1,6 @@
package lc
import lc.core.{CaptchaProviders, Captcha, Config}
import lc.core.{CaptchaProviders, CaptchaManager, Config}
import lc.server.Server
import lc.background.BackgroundTask
import lc.database.Statements
@ -15,13 +15,13 @@ object LCFramework {
val config = new Config(configFilePath)
Statements.maxAttempts = config.maxAttempts
val captchaProviders = new CaptchaProviders(config = config)
val captcha = new Captcha(config = config, captchaProviders = captchaProviders)
val backgroundTask = new BackgroundTask(config = config, captcha = captcha)
val captchaManager = new CaptchaManager(config = config, captchaProviders = captchaProviders)
val backgroundTask = new BackgroundTask(config = config, captchaManager = captchaManager)
backgroundTask.beginThread(delay = config.threadDelay)
val server = new Server(
address = config.address,
port = config.port,
captcha = captcha,
captchaManager = captchaManager,
playgroundEnabled = config.playgroundEnabled,
corsHeader = config.corsHeader
)

View File

@ -2,11 +2,11 @@ package lc.background
import lc.database.Statements
import java.util.concurrent.{ScheduledThreadPoolExecutor, TimeUnit}
import lc.core.{Captcha, Config}
import lc.core.{CaptchaManager, Config}
import lc.core.{Parameters, Size}
import lc.misc.HelperFunctions
class BackgroundTask(config: Config, captcha: Captcha) {
class BackgroundTask(config: Config, captchaManager: CaptchaManager) {
private val task = new Runnable {
def run(): Unit = {
@ -18,13 +18,17 @@ class BackgroundTask(config: Config, captcha: Captcha) {
val challengeGCPstmt = Statements.tlStmts.get.challengeGCPstmt
challengeGCPstmt.executeUpdate()
val imageNum = Statements.tlStmts.get.getCountChallengeTable.executeQuery()
var throttleIn = (config.throttle * 1.1).toInt
if (imageNum.next())
throttleIn = (throttleIn - imageNum.getInt("total"))
while (0 < throttleIn) {
captcha.generateChallenge(getRandomParam())
throttleIn -= 1
val imageNumResult = Statements.tlStmts.get.getCountChallengeTable.executeQuery()
val imageNum = if (imageNumResult.next()) {
imageNumResult.getInt("total")
} else {
0
}
val throttle = (config.throttle * 1.1).toInt - imageNum
for (i <- 0 until throttle) {
captchaManager.generateChallenge(getRandomParam())
}
} catch { case exception: Exception => println(exception) }
}

View File

@ -6,7 +6,7 @@ import java.io.ByteArrayInputStream
import java.sql.{Blob, ResultSet}
import java.util.UUID
class Captcha(config: Config, captchaProviders: CaptchaProviders) {
class CaptchaManager(config: Config, captchaProviders: CaptchaProviders) {
def getCaptcha(id: Id): Either[Error, Image] = {
val blob = getImage(id.id)

View File

@ -2,7 +2,7 @@ package lc.server
import org.json4s.jackson.JsonMethods.parse
import org.json4s.jvalue2extractable
import lc.core.Captcha
import lc.core.CaptchaManager
import lc.core.ErrorMessageEnum
import lc.core.{Answer, ByteConvert, Error, Id, Parameters}
import lc.core.Config.formats
@ -13,7 +13,7 @@ import java.net.InetSocketAddress
import java.util
import scala.jdk.CollectionConverters._
class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Boolean, corsHeader: String) {
class Server(address: String, port: Int, captchaManager: CaptchaManager, playgroundEnabled: Boolean, corsHeader: String) {
var headerMap: util.Map[String, util.List[String]] = _
if (corsHeader.nonEmpty) {
headerMap = Map("Access-Control-Allow-Origin" -> List(corsHeader).asJava).asJava
@ -27,7 +27,7 @@ class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Bo
(request) => {
val json = parse(request.getBodyString())
val param = json.extract[Parameters]
val id = captcha.getChallenge(param)
val id = captchaManager.getChallenge(param)
getResponse(id, headerMap)
}
)
@ -38,7 +38,7 @@ class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Bo
val result = if (params.containsKey("id")) {
val paramId = params.get("id").get(0)
val id = Id(paramId)
captcha.getCaptcha(id)
captchaManager.getCaptcha(id)
} else {
Left(Error(ErrorMessageEnum.INVALID_PARAM.toString + "=> id"))
}
@ -50,7 +50,7 @@ class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Bo
(request) => {
val json = parse(request.getBodyString())
val answer = json.extract[Answer]
val result = captcha.checkAnswer(answer)
val result = captchaManager.checkAnswer(answer)
getResponse(result, headerMap)
}
)