From 3682b2cb7dcf69f11622c0f271f5a5171f80a345 Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Tue, 20 Apr 2021 03:06:47 +0530 Subject: [PATCH] Randomize captcha generation Signed-off-by: Rahul Rudragoudar --- src/main/scala/lc/background/taskThread.scala | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/scala/lc/background/taskThread.scala b/src/main/scala/lc/background/taskThread.scala index 33ae997..2b5cd99 100644 --- a/src/main/scala/lc/background/taskThread.scala +++ b/src/main/scala/lc/background/taskThread.scala @@ -2,7 +2,7 @@ package lc.background import lc.database.Statements import java.util.concurrent.{ScheduledThreadPoolExecutor, TimeUnit} -import lc.core.Captcha +import lc.core.{Captcha, Config} import lc.core.{Parameters, Size} class BackgroundTask(throttle: Int, timeLimit: Int) { @@ -22,13 +22,21 @@ class BackgroundTask(throttle: Int, timeLimit: Int) { if (imageNum.next()) throttleIn = (throttleIn - imageNum.getInt("total")) while (0 < throttleIn) { - Captcha.generateChallenge(Parameters("medium", "image/png", "text", Option(Size(0, 0)))) + Captcha.generateChallenge(getRandomParam()) throttleIn -= 1 } } catch { case exception: Exception => println(exception) } } } + private def getRandomParam(): Parameters = { + val level = Config.allowedLevels.toList(Config.getNextRandomInt(Config.allowedLevels.size)) + val media = Config.allowedMedia.toList(Config.getNextRandomInt(Config.allowedMedia.size)) + val inputType = Config.allowedInputType.toList(Config.getNextRandomInt(Config.allowedInputType.size)) + + Parameters(level, media, inputType, Option(Size(0,0))) + } + def beginThread(delay: Int): Unit = { val ex = new ScheduledThreadPoolExecutor(1) ex.scheduleWithFixedDelay(task, 1, delay, TimeUnit.SECONDS)