mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-01-12 22:43:20 -05:00
Add config support
Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com>
This commit is contained in:
parent
de50d8123e
commit
600c7c5e8f
@ -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()
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
80
src/main/scala/lc/core/config.scala
Normal file
80
src/main/scala/lc/core/config.scala
Normal file
@ -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
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user