Add method to check for unsolved captchas

The framework checks for existing unsolved captchas before generating new captcha
The captcha is set to solved after the media is requested

Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com>
This commit is contained in:
Rahul Rudragoudar 2019-04-07 10:49:34 +05:30
parent 89a1039c87
commit fe9138dbaf
No known key found for this signature in database
GPG Key ID: 5825856C50762765

View File

@ -21,10 +21,11 @@ import scala.Array
class Captcha(throttle: Int) {
val con: Connection = DriverManager.getConnection("jdbc:h2:./captcha", "sa", "")
val stmt: Statement = con.createStatement()
stmt.execute("CREATE TABLE IF NOT EXISTS challenge(token varchar, id varchar, secret varchar, provider varchar, contentType varchar, image blob)")
stmt.execute("CREATE TABLE IF NOT EXISTS challenge(token varchar, id varchar, secret varchar, provider varchar, contentType varchar, image blob, solved boolean default False)")
val insertPstmt: PreparedStatement = con.prepareStatement("INSERT INTO challenge(token, id, secret, provider, contentType, image) VALUES (?, ?, ?, ?, ?, ?)")
val selectPstmt: PreparedStatement = con.prepareStatement("SELECT secret, provider FROM challenge WHERE token = ?")
val imagePstmt: PreparedStatement = con.prepareStatement("SELECT image FROM challenge WHERE token = ?")
val updatePstmt: PreparedStatement = con.prepareStatement("UPDATE challenge SET solved = True WHERE token = ?")
val filters = Map("FilterChallenge" -> new FilterChallenge,
"FontFunCaptcha" -> new FontFunCaptcha,
@ -40,24 +41,27 @@ class Captcha(throttle: Int) {
}
def getCaptcha(id: Id): Array[Byte] = {
var image :Array[Byte] = null
var blob: Blob = null
imagePstmt.setString(1, id.id)
val rs: ResultSet = imagePstmt.executeQuery()
rs.next()
val blob = rs.getBlob("image")
var image :Array[Byte] = null
if(rs.next()){
blob = rs.getBlob("image")
updatePstmt.setString(1,id.id)
updatePstmt.executeUpdate()
}
if(blob != null)
image = blob.getBytes(1, blob.length().toInt)
image
}
def getChallenge(param: Parameters): Id = {
def generateChallenge(param: Parameters): String = {
//TODO: eval params to choose a provider
val providerMap = getProvider()
val provider = filters(providerMap)
val challenge = provider.returnChallenge()
val blob = new ByteArrayInputStream(challenge.content)
val token = scala.util.Random.nextInt(10000).toString
val id = Id(token)
insertPstmt.setString(1, token)
insertPstmt.setString(2, provider.getId)
insertPstmt.setString(3, challenge.secret)
@ -65,7 +69,7 @@ class Captcha(throttle: Int) {
insertPstmt.setString(5, challenge.contentType)
insertPstmt.setBlob(6, blob)
insertPstmt.executeUpdate()
id
token
}
val task = new Runnable {
@ -86,6 +90,17 @@ class Captcha(throttle: Int) {
val thread = ex.scheduleWithFixedDelay(task, 1, delay, TimeUnit.SECONDS)
}
def getChallenge(param: Parameters): Id = {
val rs = stmt.executeQuery("SELECT token FROM challenge WHERE solved=FALSE LIMIT 1")
var id: String = null
if(rs.next()){
id = rs.getString("token")
} else {
id = generateChallenge(param)
}
Id(id)
}
def getAnswer(answer: Answer): Boolean = {
selectPstmt.setString(1, answer.id)
val rs: ResultSet = selectPstmt.executeQuery()
@ -97,13 +112,13 @@ class Captcha(throttle: Int) {
def display(): Unit = {
val rs: ResultSet = stmt.executeQuery("SELECT * FROM challenge")
println("token\t\tid\t\tsecret\t\timage")
println("token\t\tid\t\tsecret\t\tsolved")
while(rs.next()) {
val token = rs.getString("token")
val id = rs.getString("id")
val secret = rs.getString("secret")
val image = rs.getString("image")
println(s"${token}\t\t${id}\t\t${secret}\t\t")
val solved = rs.getString("solved")
println(s"${token}\t\t${id}\t\t${secret}\t\t${solved}")
}
}