mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-04-16 00:48:08 -04: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.core.{Captcha, CaptchaProviders}
|
||||||
import lc.server.Server
|
import lc.server.Server
|
||||||
import lc.background.BackgroundTask
|
import lc.background.BackgroundTask
|
||||||
|
import lc.core.Config
|
||||||
|
|
||||||
object LCFramework {
|
object LCFramework {
|
||||||
def main(args: scala.Array[String]): Unit = {
|
def main(args: scala.Array[String]): Unit = {
|
||||||
val captcha = new Captcha()
|
val captcha = new Captcha()
|
||||||
val server = new Server(8888, captcha)
|
val server = new Server(port = Config.getPort, captcha = captcha)
|
||||||
val backgroudTask = new BackgroundTask(captcha, 10)
|
val backgroundTask = new BackgroundTask(
|
||||||
backgroudTask.beginThread(2)
|
captcha = captcha,
|
||||||
|
throttle = Config.getThrottle,
|
||||||
|
timeLimit = Config.getCaptchaExpiryTimeLimit
|
||||||
|
)
|
||||||
|
backgroundTask.beginThread(delay = Config.getThreadDelay)
|
||||||
server.start()
|
server.start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@ package lc.core
|
|||||||
import lc.captchas._
|
import lc.captchas._
|
||||||
import lc.captchas.interfaces.ChallengeProvider
|
import lc.captchas.interfaces.ChallengeProvider
|
||||||
import lc.captchas.interfaces.Challenge
|
import lc.captchas.interfaces.Challenge
|
||||||
|
import org.json4s.{DefaultFormats, JObject, JField, JArray, JString}
|
||||||
|
import org.json4s.jackson.Serialization.write
|
||||||
|
|
||||||
object CaptchaProviders {
|
object CaptchaProviders {
|
||||||
private val providers = Map(
|
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 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.synchronized {
|
||||||
random.nextInt(max)
|
random.nextInt(max)
|
||||||
}
|
}
|
||||||
@ -33,9 +37,26 @@ object CaptchaProviders {
|
|||||||
return providers(id)
|
return providers(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
def getProvider(): ChallengeProvider = {
|
private def filterProviderByParam(param: Parameters): List[(String, String)] = {
|
||||||
val keys = providers.keys
|
for {
|
||||||
val providerIndex = keys.toVector(getNextRandomInt(keys.size))
|
JObject(child) <- config
|
||||||
providers(providerIndex)
|
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…
x
Reference in New Issue
Block a user