From e18354c259532fa55b155945d4c9e8ab21f9bc4c Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Tue, 26 Mar 2019 23:19:01 +0530 Subject: [PATCH] Add LabelCaptcha plugin Signed-off-by: Rahul Rudragoudar --- src/main/scala/lc/LabelCaptcha.scala | 80 ++++++++++++++++++++++++++++ src/main/scala/lc/Main.scala | 4 +- 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/main/scala/lc/LabelCaptcha.scala diff --git a/src/main/scala/lc/LabelCaptcha.scala b/src/main/scala/lc/LabelCaptcha.scala new file mode 100644 index 0000000..664e2d5 --- /dev/null +++ b/src/main/scala/lc/LabelCaptcha.scala @@ -0,0 +1,80 @@ +package lc + +import java.io.File +import java.io.ByteArrayOutputStream +import javax.imageio.ImageIO +import scala.collection.mutable.Map +import java.nio.file.{Files,Path,StandardCopyOption} +import java.awt.image.BufferedImage +import java.awt.{Graphics2D,Color} + +class LabelCaptcha extends ChallengeProvider { + var knownFiles = new File("known").list.toList + var unknownFiles = new File("unknown").list.toList + var tokenImagePair = Map[String, ImagePair]() + var unknownAnswers = Map[String, Map[String, Int]]() + var total = Map[String, Int]() + for(file <- unknownFiles) { + unknownAnswers += file -> Map[String, Int]() + total += file -> 0 + } + def getId = "LabelCaptcha" + def returnChallenge(): Challenge = synchronized { + val r = scala.util.Random.nextInt(knownFiles.length) + val s = scala.util.Random.nextInt(unknownFiles.length) + val knownImageFile = knownFiles(r) + val unknownImageFile = unknownFiles(s) + val ip = new ImagePair(knownImageFile, unknownImageFile) + val token = scala.util.Random.nextInt(10000).toString + tokenImagePair += (token -> ip) + var knownImage = ImageIO.read(new File("known/"+knownImageFile)) + var unknownImage = ImageIO.read(new File("unknown/"+unknownImageFile)) + val width = knownImage.getWidth()+unknownImage.getWidth() + val height = List(knownImage.getHeight(), unknownImage.getHeight()).max + val imageType = knownImage.getType() + val finalImage = new BufferedImage(width, height, imageType) + val g = finalImage.createGraphics() + g.setColor(Color.WHITE) + g.fillRect(0, 0, finalImage.getWidth(), finalImage.getHeight()) + g.drawImage(knownImage, null, 0, 0) + g.drawImage(unknownImage, null, knownImage.getWidth(), 0) + g.dispose() + val baos = new ByteArrayOutputStream() + ImageIO.write(finalImage,"png",baos) + new Challenge(baos.toByteArray(), "image/png", token) + } + def checkAnswer(token: String, input: String): Boolean = synchronized { + val imagePair = tokenImagePair(token) + val expectedAnswer = imagePair.known.split('.')(0) + val userAnswer = input.split(' ') + println(userAnswer) + if(userAnswer(0)==expectedAnswer) { + println("Entered if userAnswer(0)") + val unknownFile = tokenImagePair(token).unknown + if((unknownAnswers(unknownFile)).contains(userAnswer(1))) { + println("Enterd 2 if") + unknownAnswers(unknownFile)(userAnswer(1)) += 1 + total(unknownFile) += 1 + } else { + println("Enter else") + unknownAnswers(unknownFile)+=(userAnswer(1)) -> 1 + total(unknownFile) += 1 + } + if(total(unknownFile)>=3) { + if((unknownAnswers(unknownFile)(userAnswer(1))/total(unknownFile))>=0.9) { + unknownAnswers -= unknownFile + Files.move(new File("unknown/"+unknownFile).toPath, new File("known/"+userAnswer(1)+".png").toPath, StandardCopyOption.REPLACE_EXISTING) + knownFiles = new File("known").list.toList + unknownFiles = new File("unknown").list.toList + } + } + println("ret true") + true + } else { + println("ret false") + false + } + } +} + +class ImagePair(val known: String, val unknown: String) \ No newline at end of file diff --git a/src/main/scala/lc/Main.scala b/src/main/scala/lc/Main.scala index 98dc2dd..e09fad9 100644 --- a/src/main/scala/lc/Main.scala +++ b/src/main/scala/lc/Main.scala @@ -30,7 +30,8 @@ class Captcha { val filters = Map("FilterChallenge" -> new FilterChallenge, "FontFunCaptcha" -> new FontFunCaptcha, "GifCaptcha" -> new GifCaptcha, - "ShadowTextCaptcha" -> new ShadowTextCaptcha) + "ShadowTextCaptcha" -> new ShadowTextCaptcha, + "LabelCaptcha" -> new LabelCaptcha) def getProvider(): String = { val random = new scala.util.Random @@ -58,7 +59,6 @@ class Captcha { val blob = new ByteArrayInputStream(challenge.content) val token = scala.util.Random.nextInt(10000).toString val id = Id(token) - print("Successfull") insertPstmt.setString(1, token) insertPstmt.setString(2, provider.getId) insertPstmt.setString(3, challenge.secret)