mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-01-27 13:03:12 -05:00
create equal number of captchas for each parameter combination in background task
Signed-off-by: hrj <harshad.rj@gmail.com>
This commit is contained in:
parent
e44a1df22e
commit
8316084ee7
@ -18,22 +18,33 @@ class BackgroundTask(config: Config, captchaManager: CaptchaManager) {
|
|||||||
val challengeGCPstmt = Statements.tlStmts.get.challengeGCPstmt
|
val challengeGCPstmt = Statements.tlStmts.get.challengeGCPstmt
|
||||||
challengeGCPstmt.executeUpdate()
|
challengeGCPstmt.executeUpdate()
|
||||||
|
|
||||||
val imageNumResult = Statements.tlStmts.get.getCountChallengeTable.executeQuery()
|
for (param <- allParameterCombinations()) {
|
||||||
val imageNum = if (imageNumResult.next()) {
|
val imageNum = captchaManager.getCount(param).getOrElse(0)
|
||||||
imageNumResult.getInt("total")
|
val countCreate = (config.throttle * 1.1).toInt - imageNum
|
||||||
} else {
|
if (countCreate > 0) {
|
||||||
0
|
println(s"Creating $countCreate captchas for $param")
|
||||||
}
|
|
||||||
|
|
||||||
val throttle = (config.throttle * 1.1).toInt - imageNum
|
for (i <- 0 until countCreate) {
|
||||||
|
captchaManager.generateChallenge(param)
|
||||||
for (i <- 0 until throttle) {
|
}
|
||||||
captchaManager.generateChallenge(getRandomParam())
|
}
|
||||||
}
|
}
|
||||||
} catch { case exception: Exception => println(exception) }
|
} catch { case exception: Exception => println(exception) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private def allParameterCombinations(): List[Parameters] = {
|
||||||
|
(config.captchaConfig).flatMap {captcha =>
|
||||||
|
(captcha.allowedLevels).flatMap {level =>
|
||||||
|
(captcha.allowedMedia).flatMap {media =>
|
||||||
|
(captcha.allowedInputType).map {inputType =>
|
||||||
|
Parameters(level, media, inputType, Some(Size(0, 0)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private def getRandomParam(): Parameters = {
|
private def getRandomParam(): Parameters = {
|
||||||
val captcha = pickRandom(config.captchaConfig)
|
val captcha = pickRandom(config.captchaConfig)
|
||||||
val level = pickRandom(captcha.allowedLevels)
|
val level = pickRandom(captcha.allowedLevels)
|
||||||
|
@ -105,8 +105,8 @@ class CaptchaManager(config: Config, captchaProviders: CaptchaProviders) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private def getCount(param: Parameters): Option[Int] = {
|
def getCount(param: Parameters): Option[Int] = {
|
||||||
val countPstmt = Statements.tlStmts.get.countPstmt
|
val countPstmt = Statements.tlStmts.get.countForParameterPstmt
|
||||||
countPstmt.setString(1, param.level)
|
countPstmt.setString(1, param.level)
|
||||||
countPstmt.setString(2, param.media)
|
countPstmt.setString(2, param.media)
|
||||||
countPstmt.setString(3, param.input_type)
|
countPstmt.setString(3, param.input_type)
|
||||||
@ -123,16 +123,16 @@ class CaptchaManager(config: Config, captchaProviders: CaptchaProviders) {
|
|||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
val tokenPstmt = Statements.tlStmts.get.tokenPstmt
|
val tokenPstmt = Statements.tlStmts.get.tokenPstmt
|
||||||
tokenPstmt.setString(1, param.level)
|
tokenPstmt.setString(1, param.level)
|
||||||
tokenPstmt.setString(2, param.media)
|
tokenPstmt.setString(2, param.media)
|
||||||
tokenPstmt.setString(3, param.input_type)
|
tokenPstmt.setString(3, param.input_type)
|
||||||
tokenPstmt.setInt(4, count)
|
tokenPstmt.setInt(4, count)
|
||||||
val rs = tokenPstmt.executeQuery()
|
val rs = tokenPstmt.executeQuery()
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
Some(rs.getInt("token"))
|
Some(rs.getInt("token"))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,7 +70,7 @@ class Statements(dbConn: DBConn, maxAttempts: Int) {
|
|||||||
"WHERE token = ?;"
|
"WHERE token = ?;"
|
||||||
)
|
)
|
||||||
|
|
||||||
val countPstmt: PreparedStatement = dbConn.con.prepareStatement(
|
val countForParameterPstmt: PreparedStatement = dbConn.con.prepareStatement(
|
||||||
s"""
|
s"""
|
||||||
SELECT count(*) as count
|
SELECT count(*) as count
|
||||||
FROM challenge
|
FROM challenge
|
||||||
|
Loading…
x
Reference in New Issue
Block a user