Merge pull request #116 from rr83019/max-attempts-config

Max attempts config
This commit is contained in:
hrj 2021-12-27 19:50:03 +05:30 committed by GitHub
commit 4fc2d4ebb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 14 additions and 11 deletions

View File

@ -3,6 +3,7 @@ package lc
import lc.core.{CaptchaProviders, Captcha, Config}
import lc.server.Server
import lc.background.BackgroundTask
import lc.database.Statements
object LCFramework {
def main(args: scala.Array[String]): Unit = {
@ -12,6 +13,7 @@ object LCFramework {
"data/config.json"
}
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)

View File

@ -1,13 +1,10 @@
package lc.core
import java.sql.ResultSet
import java.util.UUID
import java.io.ByteArrayInputStream
import lc.captchas.interfaces.{Challenge, ChallengeProvider}
import lc.database.Statements
import lc.core.CaptchaProviders
import lc.captchas.interfaces.ChallengeProvider
import lc.captchas.interfaces.Challenge
import java.sql.Blob
import java.io.ByteArrayInputStream
import java.sql.{Blob, ResultSet}
import java.util.UUID
class Captcha(config: Config, captchaProviders: CaptchaProviders) {

View File

@ -26,6 +26,7 @@ object AttributesEnum extends Enumeration {
val PLAYGROUND_ENABLED: Value = Value("playgroundEnabled")
val CORS_HEADER: Value = Value("corsHeader")
val CONFIG: Value = Value("config")
val MAX_ATTEMPTS: Value = Value("maxAttempts")
}
object ResultEnum extends Enumeration {

View File

@ -1,6 +1,6 @@
package lc.core
import lc.captchas._
import lc.captchas.*
import lc.captchas.interfaces.ChallengeProvider
import lc.captchas.interfaces.Challenge
import scala.collection.mutable.Map
@ -50,7 +50,7 @@ class CaptchaProviders(config: Config) {
def getProvider(param: Parameters): Option[ChallengeProvider] = {
val providerConfig = filterProviderByParam(param).toList
if (providerConfig.length > 0) {
if (providerConfig.nonEmpty) {
val randomIndex = HelperFunctions.randomNumber(providerConfig.length)
val providerIndex = providerConfig(randomIndex)._1
val selectedProvider = providers(providerIndex)

View File

@ -47,6 +47,7 @@ class Config(configFilePath: String) {
val threadDelay: Int = (configJson \ AttributesEnum.THREAD_DELAY.toString).extract[Int]
val playgroundEnabled: Boolean = (configJson \ AttributesEnum.PLAYGROUND_ENABLED.toString).extract[Boolean]
val corsHeader: String = (configJson \ AttributesEnum.CORS_HEADER.toString).extract[String]
val maxAttempts: Int = (configJson \ AttributesEnum.MAX_ATTEMPTS.toString).extract[Int]
private val captchaConfigJson = (configJson \ "captchas")
val captchaConfigTransform: JValue = captchaConfigJson transformField { case JField("config", JObject(config)) =>
@ -69,6 +70,7 @@ class Config(configFilePath: String) {
(AttributesEnum.THREAD_DELAY.toString -> 2) ~
(AttributesEnum.PLAYGROUND_ENABLED.toString -> true) ~
(AttributesEnum.CORS_HEADER.toString -> "") ~
(AttributesEnum.MAX_ATTEMPTS.toString -> 10) ~
("captchas" -> List(
(
(AttributesEnum.NAME.toString -> "FilterChallenge") ~

View File

@ -1,6 +1,6 @@
package lc.database
import java.sql._
import java.sql.{Connection, DriverManager, Statement}
class DBConn() {
val con: Connection = DriverManager.getConnection("jdbc:h2:./data/H2/captcha2", "sa", "")

View File

@ -118,6 +118,6 @@ object Statements {
```
*/
private val dbConn: DBConn = new DBConn()
private val maxAttempts = 10
var maxAttempts: Int = 10
val tlStmts: ThreadLocal[Statements] = ThreadLocal.withInitial(() => new Statements(dbConn, maxAttempts))
}

View File

@ -7,6 +7,7 @@
"threadDelay" : 2,
"playgroundEnabled" : false,
"corsHeader" : "*",
"maxAttempts" : 20,
"captchas" : [ {
"name" : "DebugCaptcha",
"allowedLevels" : [ "debug" ],