Merge pull request #92 from rr83019/Config-file-path

Specify config file path
This commit is contained in:
hrj 2021-05-02 20:13:17 +05:30 committed by GitHub
commit c914484b42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 32 deletions

View File

@ -1,25 +1,36 @@
package lc
import lc.core.CaptchaProviders
import lc.core.{CaptchaProviders, Captcha, Config}
import lc.server.Server
import lc.background.BackgroundTask
import lc.core.Config
object LCFramework {
def main(args: scala.Array[String]): Unit = {
val backgroundTask = new BackgroundTask(
throttle = Config.throttle,
timeLimit = Config.captchaExpiryTimeLimit
)
backgroundTask.beginThread(delay = Config.threadDelay)
val server = new Server(port = Config.port)
val configFilePath = if (args.length > 0){
args(0)
} else {
"data/config.json"
}
val config = new Config(configFilePath)
val captchaProviders = new CaptchaProviders(config = config)
val captcha = new Captcha(config = config, captchaProviders = captchaProviders)
val backgroundTask = new BackgroundTask(config = config, captcha = captcha)
backgroundTask.beginThread(delay = config.threadDelay)
val server = new Server(port = config.port, captcha = captcha)
server.start()
}
}
object MakeSamples {
def main(args: scala.Array[String]): Unit = {
val samples = CaptchaProviders.generateChallengeSamples()
val configFilePath = if (args.length > 0){
args(0)
} else {
"data/config.json"
}
val config = new Config(configFilePath)
val captchaProviders = new CaptchaProviders(config = config)
val samples = captchaProviders.generateChallengeSamples()
samples.foreach {
case (key, sample) =>
val extensionMap = Map("image/png" -> "png", "image/gif" -> "gif")

View File

@ -6,24 +6,24 @@ import lc.core.{Captcha, Config}
import lc.core.{Parameters, Size}
import lc.misc.HelperFunctions
class BackgroundTask(throttle: Int, timeLimit: Int) {
class BackgroundTask(config: Config, captcha: Captcha) {
private val task = new Runnable {
def run(): Unit = {
try {
val mapIdGCPstmt = Statements.tlStmts.get.mapIdGCPstmt
mapIdGCPstmt.setInt(1, timeLimit)
mapIdGCPstmt.setInt(1, config.captchaExpiryTimeLimit)
mapIdGCPstmt.executeUpdate()
val challengeGCPstmt = Statements.tlStmts.get.challengeGCPstmt
challengeGCPstmt.executeUpdate()
val imageNum = Statements.tlStmts.get.getCountChallengeTable.executeQuery()
var throttleIn = (throttle * 1.1).toInt
var throttleIn = (config.throttle * 1.1).toInt
if (imageNum.next())
throttleIn = (throttleIn - imageNum.getInt("total"))
while (0 < throttleIn) {
Captcha.generateChallenge(getRandomParam())
captcha.generateChallenge(getRandomParam())
throttleIn -= 1
}
} catch { case exception: Exception => println(exception) }
@ -31,7 +31,7 @@ class BackgroundTask(throttle: Int, timeLimit: Int) {
}
private def getRandomParam(): Parameters = {
val captcha = pickRandom(Config.captchaConfig)
val captcha = pickRandom(config.captchaConfig)
val level = pickRandom(captcha.allowedLevels)
val media = pickRandom(captcha.allowedMedia)
val inputType = pickRandom(captcha.allowedInputType)

View File

@ -9,7 +9,7 @@ import lc.captchas.interfaces.ChallengeProvider
import lc.captchas.interfaces.Challenge
import java.sql.Blob
object Captcha {
class Captcha(config: Config, captchaProviders: CaptchaProviders) {
def getCaptcha(id: Id): Either[Error, Image] = {
val blob = getImage(id.id)
@ -37,7 +37,7 @@ object Captcha {
}
def generateChallenge(param: Parameters): Option[Int] = {
val provider = CaptchaProviders.getProvider(param)
val provider = captchaProviders.getProvider(param)
provider match {
case Some(value) => {
val providerId = value.getId()
@ -75,9 +75,9 @@ object Captcha {
}
}
val allowedInputType = Config.allowedInputType
val allowedLevels = Config.allowedLevels
val allowedMedia = Config.allowedMedia
val allowedInputType = config.allowedInputType
val allowedLevels = config.allowedLevels
val allowedMedia = config.allowedMedia
private def validateParam(param: Parameters): Array[String] = {
var invalid_params = Array[String]()
@ -142,7 +142,7 @@ object Captcha {
case None => Right(Success(ResultEnum.EXPIRED.toString))
case Some(value) => {
val (provider, secret) = value
val check = CaptchaProviders.getProviderById(provider).checkAnswer(secret, answer.answer)
val check = captchaProviders.getProviderById(provider).checkAnswer(secret, answer.answer)
deleteCaptcha(answer.id)
val result = if (check) ResultEnum.TRUE.toString else ResultEnum.FALSE.toString
Right(Success(result))
@ -152,7 +152,7 @@ object Captcha {
private def getSecret(id: String): Option[(String, String)] = {
val selectPstmt = Statements.tlStmts.get.selectPstmt
selectPstmt.setInt(1, Config.captchaExpiryTimeLimit)
selectPstmt.setInt(1, config.captchaExpiryTimeLimit)
selectPstmt.setString(2, id)
val rs: ResultSet = selectPstmt.executeQuery()
if (rs.first()) {

View File

@ -6,7 +6,7 @@ import lc.captchas.interfaces.Challenge
import scala.collection.mutable.Map
import lc.misc.HelperFunctions
object CaptchaProviders {
class CaptchaProviders(config: Config) {
private val providers = Map(
"FilterChallenge" -> new FilterChallenge,
//"FontFunCaptcha" -> new FontFunCaptcha,
@ -24,7 +24,7 @@ object CaptchaProviders {
}
}
private val config = Config.captchaConfig
private val captchaConfig = config.captchaConfig
def getProviderById(id: String): ChallengeProvider = {
return providers(id)
@ -32,7 +32,7 @@ object CaptchaProviders {
private def filterProviderByParam(param: Parameters): Iterable[(String, String)] = {
val configFilter = for {
configValue <- config
configValue <- captchaConfig
if configValue.allowedLevels.contains(param.level)
if configValue.allowedMedia.contains(param.media)
if configValue.allowedInputType.contains(param.input_type)

View File

@ -8,11 +8,10 @@ import java.io.{FileNotFoundException, File, PrintWriter}
import java.{util => ju}
import lc.misc.HelperFunctions
object Config {
class Config(configFilePath: String) {
implicit val formats: DefaultFormats.type = DefaultFormats
import Config.formats
private val configFilePath = "data/config.json"
private val configString =
try {
val configFile = fromFile(configFilePath)
@ -22,7 +21,12 @@ object Config {
} catch {
case _: FileNotFoundException => {
val configFileContent = getDefaultConfig()
val configFile = new PrintWriter(new File(configFilePath))
val file = if(new File(configFilePath).isDirectory){
new File(configFilePath.concat("/config.json"))
} else {
new File(configFilePath)
}
val configFile = new PrintWriter(file)
configFile.write(configFileContent)
configFile.close
configFileContent
@ -94,3 +98,7 @@ object Config {
}
}
object Config{
implicit val formats: DefaultFormats.type = DefaultFormats
}

View File

@ -10,7 +10,7 @@ import org.limium.picoserve.Server.ByteResponse
import scala.io.Source
import org.limium.picoserve.Server.StringResponse
class Server(port: Int) {
class Server(port: Int, captcha: Captcha) {
val server: picoserve.Server = picoserve.Server
.builder()
.port(port)
@ -20,7 +20,7 @@ class Server(port: Int) {
(request) => {
val json = parse(request.getBodyString())
val param = json.extract[Parameters]
val id = Captcha.getChallenge(param)
val id = captcha.getChallenge(param)
getResponse(id)
}
)
@ -31,7 +31,7 @@ class Server(port: Int) {
val result = if (params.containsKey("id")) {
val paramId = params.get("id").get(0)
val id = Id(paramId)
Captcha.getCaptcha(id)
captcha.getCaptcha(id)
} else {
Left(Error(ErrorMessageEnum.INVALID_PARAM.toString + "=> id"))
}
@ -43,7 +43,7 @@ class Server(port: Int) {
(request) => {
val json = parse(request.getBodyString())
val answer = json.extract[Answer]
val result = Captcha.checkAnswer(answer)
val result = captcha.checkAnswer(answer)
getResponse(result)
}
)