mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-04-19 10:07:21 -04:00
Remove access methods
Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com>
This commit is contained in:
parent
6c4a3d0152
commit
070b862f25
@ -8,13 +8,13 @@ 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(port = Config.getPort, captcha = captcha)
|
val server = new Server(port = Config.port, captcha = captcha)
|
||||||
val backgroundTask = new BackgroundTask(
|
val backgroundTask = new BackgroundTask(
|
||||||
captcha = captcha,
|
captcha = captcha,
|
||||||
throttle = Config.getThrottle,
|
throttle = Config.throttle,
|
||||||
timeLimit = Config.getCaptchaExpiryTimeLimit
|
timeLimit = Config.captchaExpiryTimeLimit
|
||||||
)
|
)
|
||||||
backgroundTask.beginThread(delay = Config.getThreadDelay)
|
backgroundTask.beginThread(delay = Config.threadDelay)
|
||||||
server.start()
|
server.start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,9 +53,9 @@ class Captcha {
|
|||||||
token.asInstanceOf[Int]
|
token.asInstanceOf[Int]
|
||||||
}
|
}
|
||||||
|
|
||||||
val supportedinputType = Config.getSupportedinputType
|
val supportedinputType = Config.supportedinputType
|
||||||
val supportedLevels = Config.getSupportedLevels
|
val supportedLevels = Config.supportedLevels
|
||||||
val supportedMedia = Config.getSupportedMedia
|
val supportedMedia = Config.supportedMedia
|
||||||
|
|
||||||
private def validateParam(param: Parameters): Boolean = {
|
private def validateParam(param: Parameters): Boolean = {
|
||||||
if (
|
if (
|
||||||
@ -115,7 +115,7 @@ class Captcha {
|
|||||||
|
|
||||||
def checkAnswer(answer: Answer): Result = {
|
def checkAnswer(answer: Answer): Result = {
|
||||||
val selectPstmt = Statements.tlStmts.get.selectPstmt
|
val selectPstmt = Statements.tlStmts.get.selectPstmt
|
||||||
selectPstmt.setInt(1, Config.getCaptchaExpiryTimeLimit)
|
selectPstmt.setInt(1, Config.captchaExpiryTimeLimit)
|
||||||
selectPstmt.setString(2, answer.id)
|
selectPstmt.setString(2, answer.id)
|
||||||
val rs: ResultSet = selectPstmt.executeQuery()
|
val rs: ResultSet = selectPstmt.executeQuery()
|
||||||
val psOpt = if (rs.first()) {
|
val psOpt = if (rs.first()) {
|
||||||
|
@ -3,8 +3,7 @@ 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 scala.collection.mutable.Map
|
||||||
import org.json4s.jackson.Serialization.write
|
|
||||||
|
|
||||||
object CaptchaProviders {
|
object CaptchaProviders {
|
||||||
private val providers = Map(
|
private val providers = Map(
|
||||||
@ -23,10 +22,9 @@ object CaptchaProviders {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
implicit val formats: DefaultFormats.type = DefaultFormats
|
private val seed = Config.seed
|
||||||
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 val config = Config.captchaConfigMap
|
||||||
|
|
||||||
private def getNextRandomInt(max: Int): Int =
|
private def getNextRandomInt(max: Int): Int =
|
||||||
random.synchronized {
|
random.synchronized {
|
||||||
@ -37,26 +35,21 @@ object CaptchaProviders {
|
|||||||
return providers(id)
|
return providers(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
private def filterProviderByParam(param: Parameters): List[(String, String)] = {
|
private def filterProviderByParam(param: Parameters): Iterable[(List[String], List[String])] = {
|
||||||
for {
|
for {
|
||||||
JObject(child) <- config
|
configValue <- config.values
|
||||||
JField("name", JString(name)) <- child
|
if configValue("supportedLevels").contains(param.level)
|
||||||
JField("supportedLevels", JArray(supportedLevels)) <- child
|
if configValue("supportedMedia").contains(param.media)
|
||||||
JField("supportedMedia", JArray(supportedMedia)) <- child
|
if configValue("supportedinputType").contains(param.input_type)
|
||||||
JField("supportedinputType", JArray(supportedinputType)) <- child
|
} yield (configValue("name"), configValue("config"))
|
||||||
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 = {
|
def getProvider(param: Parameters): ChallengeProvider = {
|
||||||
val providerConfig = filterProviderByParam(param)
|
val providerConfig = filterProviderByParam(param).toList
|
||||||
val randomIndex = getNextRandomInt(providerConfig.length)
|
val randomIndex = getNextRandomInt(providerConfig.length)
|
||||||
val providerIndex = providerConfig(randomIndex)._1
|
val providerIndex = providerConfig(randomIndex)._1
|
||||||
val selectedProvider = providers(providerIndex)
|
val selectedProvider = providers(providerIndex(0))
|
||||||
selectedProvider.configure(providerConfig(randomIndex)._2)
|
selectedProvider.configure(providerConfig(randomIndex)._2(0))
|
||||||
selectedProvider
|
selectedProvider
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package lc.core
|
|||||||
import scala.io.Source.fromFile
|
import scala.io.Source.fromFile
|
||||||
import org.json4s.{DefaultFormats, JValue, JObject, JField}
|
import org.json4s.{DefaultFormats, JValue, JObject, JField}
|
||||||
import org.json4s.jackson.JsonMethods.parse
|
import org.json4s.jackson.JsonMethods.parse
|
||||||
|
import scala.collection.immutable.HashMap
|
||||||
|
|
||||||
object Config {
|
object Config {
|
||||||
|
|
||||||
@ -14,16 +15,17 @@ object Config {
|
|||||||
finally configFile.close
|
finally configFile.close
|
||||||
private val configJson = parse(configString)
|
private val configJson = parse(configString)
|
||||||
|
|
||||||
private val port = (configJson \ "port").extract[Int]
|
val port: Int = (configJson \ "port").extract[Int]
|
||||||
private val throttle = (configJson \ "throttle").extract[Int]
|
val throttle: Int = (configJson \ "throttle").extract[Int]
|
||||||
private val seed = (configJson \ "randomSeed").extract[Int]
|
val seed: Int = (configJson \ "randomSeed").extract[Int]
|
||||||
private val captchaExpiryTimeLimit = (configJson \ "captchaExpiryTimeLimit").extract[Int]
|
val captchaExpiryTimeLimit: Int = (configJson \ "captchaExpiryTimeLimit").extract[Int]
|
||||||
private val threadDelay = (configJson \ "threadDelay").extract[Int]
|
val threadDelay: Int = (configJson \ "threadDelay").extract[Int]
|
||||||
private val capthcaConfig = (configJson \ "captchas")
|
private val captchaConfig = (configJson \ "captchas")
|
||||||
|
val captchaConfigMap: Map[String,HashMap[String,List[String]]] = captchaConfig.values.asInstanceOf[Map[String, HashMap[String, List[String]]]]
|
||||||
|
|
||||||
private val supportedLevels = getAllValues(configJson, "supportedLevels")
|
val supportedLevels: Set[String] = getAllValues(configJson, "supportedLevels")
|
||||||
private val supportedMedia = getAllValues(configJson, "supportedMedia")
|
val supportedMedia: Set[String] = getAllValues(configJson, "supportedMedia")
|
||||||
private val supportedinputType = getAllValues(configJson, "supportedinputType")
|
val supportedinputType: Set[String] = getAllValues(configJson, "supportedinputType")
|
||||||
|
|
||||||
private def getAllValues(config: JValue, param: String): Set[String] = {
|
private def getAllValues(config: JValue, param: String): Set[String] = {
|
||||||
val configValues = (config \\ param)
|
val configValues = (config \\ param)
|
||||||
@ -41,40 +43,4 @@ object Config {
|
|||||||
valueSet
|
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