Randomize captcha generation

Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com>
This commit is contained in:
Rahul Rudragoudar 2021-04-20 03:06:47 +05:30
parent ef31ee8a57
commit 3682b2cb7d
No known key found for this signature in database
GPG Key ID: 0D2CE231A7287EBC

View File

@ -2,7 +2,7 @@ package lc.background
import lc.database.Statements import lc.database.Statements
import java.util.concurrent.{ScheduledThreadPoolExecutor, TimeUnit} import java.util.concurrent.{ScheduledThreadPoolExecutor, TimeUnit}
import lc.core.Captcha import lc.core.{Captcha, Config}
import lc.core.{Parameters, Size} import lc.core.{Parameters, Size}
class BackgroundTask(throttle: Int, timeLimit: Int) { class BackgroundTask(throttle: Int, timeLimit: Int) {
@ -22,13 +22,21 @@ class BackgroundTask(throttle: Int, timeLimit: Int) {
if (imageNum.next()) if (imageNum.next())
throttleIn = (throttleIn - imageNum.getInt("total")) throttleIn = (throttleIn - imageNum.getInt("total"))
while (0 < throttleIn) { while (0 < throttleIn) {
Captcha.generateChallenge(Parameters("medium", "image/png", "text", Option(Size(0, 0)))) Captcha.generateChallenge(getRandomParam())
throttleIn -= 1 throttleIn -= 1
} }
} catch { case exception: Exception => println(exception) } } 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 = { def beginThread(delay: Int): Unit = {
val ex = new ScheduledThreadPoolExecutor(1) val ex = new ScheduledThreadPoolExecutor(1)
ex.scheduleWithFixedDelay(task, 1, delay, TimeUnit.SECONDS) ex.scheduleWithFixedDelay(task, 1, delay, TimeUnit.SECONDS)