From fe9138dbaf0ca04ea34b3c38c539cd45ba7b8291 Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Sun, 7 Apr 2019 10:49:34 +0530 Subject: [PATCH 1/2] 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 --- src/main/scala/lc/Main.scala | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/scala/lc/Main.scala b/src/main/scala/lc/Main.scala index 006a529..ae913a1 100644 --- a/src/main/scala/lc/Main.scala +++ b/src/main/scala/lc/Main.scala @@ -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}") } } From f3f8900fd457eb5c62398ed301af5531c5865b17 Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Sun, 7 Apr 2019 20:50:56 +0530 Subject: [PATCH 2/2] Minor fixes Signed-off-by: Rahul Rudragoudar --- src/main/scala/lc/Main.scala | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/scala/lc/Main.scala b/src/main/scala/lc/Main.scala index ae913a1..a92c510 100644 --- a/src/main/scala/lc/Main.scala +++ b/src/main/scala/lc/Main.scala @@ -92,16 +92,15 @@ class Captcha(throttle: Int) { 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") + val id = if(rs.next()){ + rs.getString("token") } else { - id = generateChallenge(param) + generateChallenge(param) } Id(id) } - def getAnswer(answer: Answer): Boolean = { + def checkAnswer(answer: Answer): Boolean = { selectPstmt.setString(1, answer.id) val rs: ResultSet = selectPstmt.executeQuery() rs.next() @@ -169,7 +168,7 @@ class Server(port: Int){ val body = req.getJson() val json = parse(body) val answer = json.extract[Answer] - val result = captcha.getAnswer(answer) + val result = captcha.checkAnswer(answer) resp.getHeaders().add("Content-Type","application/json") val responseContent = if(result) """{"result":"True"}""" else """{"result":"False"}""" resp.send(200,responseContent)