Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify config file path #92

Merged
merged 2 commits into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/main/scala/lc/Main.scala
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/lc/background/taskThread.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ 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) }
}
}

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)
Expand Down
14 changes: 7 additions & 7 deletions src/main/scala/lc/core/captcha.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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]()
Expand Down Expand Up @@ -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))
Expand All @@ -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()) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/lc/core/captchaProviders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,15 +24,15 @@ object CaptchaProviders {
}
}

private val config = Config.captchaConfig
private val captchaConfig = config.captchaConfig

def getProviderById(id: String): ChallengeProvider = {
return providers(id)
}

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)
Expand Down
16 changes: 12 additions & 4 deletions src/main/scala/lc/core/config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -94,3 +98,7 @@ object Config {
}

}

object Config{
implicit val formats: DefaultFormats.type = DefaultFormats
}
8 changes: 4 additions & 4 deletions src/main/scala/lc/server/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
)
Expand All @@ -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"))
}
Expand All @@ -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)
}
)
Expand Down