From 600c7c5e8f756f772f5bc7ccec09dc3c96316a39 Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Tue, 2 Mar 2021 21:31:07 +0530 Subject: [PATCH] Add config support Signed-off-by: Rahul Rudragoudar --- src/main/scala/lc/Main.scala | 11 ++- src/main/scala/lc/core/captchaProviders.scala | 33 ++++++-- src/main/scala/lc/core/config.scala | 80 +++++++++++++++++++ 3 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 src/main/scala/lc/core/config.scala diff --git a/src/main/scala/lc/Main.scala b/src/main/scala/lc/Main.scala index 739bc35..145f939 100644 --- a/src/main/scala/lc/Main.scala +++ b/src/main/scala/lc/Main.scala @@ -3,13 +3,18 @@ package lc import lc.core.{Captcha, CaptchaProviders} import lc.server.Server import lc.background.BackgroundTask +import lc.core.Config object LCFramework { def main(args: scala.Array[String]): Unit = { val captcha = new Captcha() - val server = new Server(8888, captcha) - val backgroudTask = new BackgroundTask(captcha, 10) - backgroudTask.beginThread(2) + val server = new Server(port = Config.getPort, captcha = captcha) + val backgroundTask = new BackgroundTask( + captcha = captcha, + throttle = Config.getThrottle, + timeLimit = Config.getCaptchaExpiryTimeLimit + ) + backgroundTask.beginThread(delay = Config.getThreadDelay) server.start() } } diff --git a/src/main/scala/lc/core/captchaProviders.scala b/src/main/scala/lc/core/captchaProviders.scala index 0277015..1567cd4 100644 --- a/src/main/scala/lc/core/captchaProviders.scala +++ b/src/main/scala/lc/core/captchaProviders.scala @@ -3,6 +3,8 @@ package lc.core import lc.captchas._ import lc.captchas.interfaces.ChallengeProvider import lc.captchas.interfaces.Challenge +import org.json4s.{DefaultFormats, JObject, JField, JArray, JString} +import org.json4s.jackson.Serialization.write object CaptchaProviders { private val providers = Map( @@ -21,10 +23,12 @@ object CaptchaProviders { } } - private val seed = System.currentTimeMillis.toString.substring(2, 6).toInt + implicit val formats: DefaultFormats.type = DefaultFormats + private val seed = Config.getSeed private val random = new scala.util.Random(seed) + private val config = Config.getCaptchaConfig - private def getNextRandomInt(max: Int) = + private def getNextRandomInt(max: Int): Int = random.synchronized { random.nextInt(max) } @@ -33,9 +37,26 @@ object CaptchaProviders { return providers(id) } - def getProvider(): ChallengeProvider = { - val keys = providers.keys - val providerIndex = keys.toVector(getNextRandomInt(keys.size)) - providers(providerIndex) + private def filterProviderByParam(param: Parameters): List[(String, String)] = { + for { + JObject(child) <- config + JField("name", JString(name)) <- child + JField("supportedLevels", JArray(supportedLevels)) <- child + JField("supportedMedia", JArray(supportedMedia)) <- child + JField("supportedinputType", JArray(supportedinputType)) <- child + JField("config", config) <- child + if supportedLevels.contains(JString(param.level)) + if supportedMedia.contains(JString(param.media)) + if supportedinputType.contains(JString(param.input_type)) + } yield (name, write(config)) + } + + def getProvider(param: Parameters): ChallengeProvider = { + val providerConfig = filterProviderByParam(param) + val randomIndex = getNextRandomInt(providerConfig.length) + val providerIndex = providerConfig(randomIndex)._1 + val selectedProvider = providers(providerIndex) + selectedProvider.configure(providerConfig(randomIndex)._2) + selectedProvider } } diff --git a/src/main/scala/lc/core/config.scala b/src/main/scala/lc/core/config.scala new file mode 100644 index 0000000..0321e96 --- /dev/null +++ b/src/main/scala/lc/core/config.scala @@ -0,0 +1,80 @@ +package lc.core + +import scala.io.Source.fromFile +import org.json4s.{DefaultFormats, JValue, JObject, JField} +import org.json4s.jackson.JsonMethods.parse + +object Config { + + implicit val formats: DefaultFormats.type = DefaultFormats + + private val configFile = fromFile("config.json") + private val configString = + try configFile.mkString + finally configFile.close + private val configJson = parse(configString) + + private val port = (configJson \ "port").extract[Int] + private val throttle = (configJson \ "throttle").extract[Int] + private val seed = (configJson \ "randomSeed").extract[Int] + private val captchaExpiryTimeLimit = (configJson \ "captchaExpiryTimeLimit").extract[Int] + private val threadDelay = (configJson \ "threadDelay").extract[Int] + private val capthcaConfig = (configJson \ "captchas") + + private val supportedLevels = getAllValues(configJson, "supportedLevels") + private val supportedMedia = getAllValues(configJson, "supportedMedia") + private val supportedinputType = getAllValues(configJson, "supportedinputType") + + private def getAllValues(config: JValue, param: String): Set[String] = { + val configValues = (config \\ param) + val result = for { + JObject(child) <- configValues + JField(param) <- child + } yield (param) + + var valueSet = Set[String]() + for (valueList <- result) { + for (value <- valueList._2.children) { + valueSet += value.values.toString + } + } + valueSet + } + + def getPort(): Int = { + port + } + + def getThrottle(): Int = { + throttle + } + + def getSeed(): Int = { + seed + } + + def getCaptchaExpiryTimeLimit(): Int = { + captchaExpiryTimeLimit + } + + def getThreadDelay(): Int = { + threadDelay + } + + def getCaptchaConfig(): JValue = { + capthcaConfig + } + + def getSupportedLevels(): Set[String] = { + supportedLevels + } + + def getSupportedMedia(): Set[String] = { + supportedMedia + } + + def getSupportedinputType(): Set[String] = { + supportedinputType + } + +}