From d3a2c6fa35637d5fa1278b0473296bbb8edc5cbd Mon Sep 17 00:00:00 2001 From: hrj Date: Wed, 14 Apr 2021 09:38:02 +0530 Subject: [PATCH 1/5] Minor: use Map.of() Signed-off-by: hrj --- src/main/java/lc/captchas/FontFunCaptcha.java | 15 +++++++-------- src/main/java/lc/captchas/GifCaptcha.java | 15 +++++++-------- src/main/java/lc/captchas/ShadowTextCaptcha.java | 15 +++++++-------- src/main/scala/lc/captchas/FilterChallenge.scala | 13 +++++-------- src/main/scala/lc/captchas/LabelCaptcha.scala | 13 +++++-------- src/main/scala/lc/captchas/RainDropsCaptcha.scala | 13 +++++-------- 6 files changed, 36 insertions(+), 48 deletions(-) diff --git a/src/main/java/lc/captchas/FontFunCaptcha.java b/src/main/java/lc/captchas/FontFunCaptcha.java index bb1c278..3ad134c 100644 --- a/src/main/java/lc/captchas/FontFunCaptcha.java +++ b/src/main/java/lc/captchas/FontFunCaptcha.java @@ -6,7 +6,7 @@ import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FilenameFilter; -import java.util.HashMap; +import java.util.Map; import java.util.List; import lc.captchas.interfaces.Challenge; import lc.captchas.interfaces.ChallengeProvider; @@ -18,13 +18,12 @@ public class FontFunCaptcha implements ChallengeProvider { return "FontFunCaptcha"; } - public HashMap> supportedParameters() { - HashMap> supportedParams = new HashMap>(); - supportedParams.put("supportedLevels", List.of("medium")); - supportedParams.put("supportedMedia", List.of("image/png")); - supportedParams.put("supportedInputType", List.of("text")); - - return supportedParams; + public Map> supportedParameters() { + return Map.of( + "supportedLevels", List.of("medium"), + "supportedMedia", List.of("image/png"), + "supportedInputType", List.of("text") + ); } public void configure(String config) { diff --git a/src/main/java/lc/captchas/GifCaptcha.java b/src/main/java/lc/captchas/GifCaptcha.java index 13cf0fa..19b161a 100644 --- a/src/main/java/lc/captchas/GifCaptcha.java +++ b/src/main/java/lc/captchas/GifCaptcha.java @@ -6,7 +6,7 @@ import java.awt.RenderingHints; import java.awt.Color; import java.awt.image.BufferedImage; import java.io.IOException; -import java.util.HashMap; +import java.util.Map; import java.util.List; import javax.imageio.stream.ImageOutputStream; @@ -55,13 +55,12 @@ public class GifCaptcha implements ChallengeProvider { // TODO: Add custom config } - public HashMap> supportedParameters() { - HashMap> supportedParams = new HashMap>(); - supportedParams.put("supportedLevels", List.of("hard")); - supportedParams.put("supportedMedia", List.of("image/gif")); - supportedParams.put("supportedInputType", List.of("text")); - - return supportedParams; + public Map> supportedParameters() { + return Map.of( + "supportedLevels", List.of("hard"), + "supportedMedia", List.of("image/gif"), + "supportedInputType", List.of("text") + ); } public Challenge returnChallenge() { diff --git a/src/main/java/lc/captchas/ShadowTextCaptcha.java b/src/main/java/lc/captchas/ShadowTextCaptcha.java index 5b3b873..ba26513 100644 --- a/src/main/java/lc/captchas/ShadowTextCaptcha.java +++ b/src/main/java/lc/captchas/ShadowTextCaptcha.java @@ -10,7 +10,7 @@ import java.awt.image.BufferedImage; import java.awt.image.ConvolveOp; import java.awt.image.Kernel; import java.io.ByteArrayOutputStream; -import java.util.HashMap; +import java.util.Map; import java.util.List; import lc.misc.HelperFunctions; @@ -27,13 +27,12 @@ public class ShadowTextCaptcha implements ChallengeProvider { // TODO: Add custom config } - public HashMap> supportedParameters() { - HashMap> supportedParams = new HashMap>(); - supportedParams.put("supportedLevels", List.of("easy")); - supportedParams.put("supportedMedia", List.of("image/png")); - supportedParams.put("supportedInputType", List.of("text")); - - return supportedParams; + public Map> supportedParameters() { + return Map.of( + "supportedLevels", List.of("easy"), + "supportedMedia", List.of("image/png"), + "supportedInputType", List.of("text") + ); } public boolean checkAnswer(String secret, String answer) { diff --git a/src/main/scala/lc/captchas/FilterChallenge.scala b/src/main/scala/lc/captchas/FilterChallenge.scala index 4fcc89d..8c21e78 100644 --- a/src/main/scala/lc/captchas/FilterChallenge.scala +++ b/src/main/scala/lc/captchas/FilterChallenge.scala @@ -7,7 +7,6 @@ import java.awt.Font import java.awt.Color import lc.captchas.interfaces.ChallengeProvider import lc.captchas.interfaces.Challenge -import scala.jdk.CollectionConverters.MapHasAsJava import java.util.{List => JavaList, Map => JavaMap} class FilterChallenge extends ChallengeProvider { @@ -18,13 +17,11 @@ class FilterChallenge extends ChallengeProvider { } def supportedParameters(): JavaMap[String, JavaList[String]] = { - val supportedParams = Map( - "supportedLevels" -> JavaList.of("medium", "hard"), - "supportedMedia" -> JavaList.of("image/png"), - "supportedInputType" -> JavaList.of("text") - ).asJava - - supportedParams + JavaMap.of( + "supportedLevels",JavaList.of("medium", "hard"), + "supportedMedia", JavaList.of("image/png"), + "supportedInputType", JavaList.of("text") + ) } def returnChallenge(): Challenge = { diff --git a/src/main/scala/lc/captchas/LabelCaptcha.scala b/src/main/scala/lc/captchas/LabelCaptcha.scala index 5233678..cd008e7 100644 --- a/src/main/scala/lc/captchas/LabelCaptcha.scala +++ b/src/main/scala/lc/captchas/LabelCaptcha.scala @@ -9,7 +9,6 @@ import java.awt.image.BufferedImage import java.awt.Color import lc.captchas.interfaces.ChallengeProvider import lc.captchas.interfaces.Challenge -import scala.jdk.CollectionConverters.MapHasAsJava import java.util.{List => JavaList, Map => JavaMap} class LabelCaptcha extends ChallengeProvider { @@ -30,13 +29,11 @@ class LabelCaptcha extends ChallengeProvider { } def supportedParameters(): JavaMap[String, JavaList[String]] = { - val supportedParams = Map( - "supportedLevels" -> JavaList.of("hard"), - "supportedMedia" -> JavaList.of("image/png"), - "supportedInputType" -> JavaList.of("text") - ).asJava - - supportedParams + JavaMap.of( + "supportedLevels", JavaList.of("hard"), + "supportedMedia", JavaList.of("image/png"), + "supportedInputType", JavaList.of("text") + ) } def returnChallenge(): Challenge = diff --git a/src/main/scala/lc/captchas/RainDropsCaptcha.scala b/src/main/scala/lc/captchas/RainDropsCaptcha.scala index 0b08dcd..88581fd 100644 --- a/src/main/scala/lc/captchas/RainDropsCaptcha.scala +++ b/src/main/scala/lc/captchas/RainDropsCaptcha.scala @@ -10,7 +10,6 @@ import javax.imageio.stream.MemoryCacheImageOutputStream; import lc.captchas.interfaces.ChallengeProvider import lc.captchas.interfaces.Challenge import lc.misc.GifSequenceWriter -import scala.jdk.CollectionConverters.MapHasAsJava import java.util.{List => JavaList, Map => JavaMap} class Drop { @@ -38,13 +37,11 @@ class RainDropsCP extends ChallengeProvider { } def supportedParameters(): JavaMap[String, JavaList[String]] = { - val supportedParams = Map( - "supportedLevels" -> JavaList.of("medium", "easy"), - "supportedMedia" -> JavaList.of("image/gif"), - "supportedInputType" -> JavaList.of("text") - ).asJava - - supportedParams + JavaMap.of( + "supportedLevels", JavaList.of("medium", "easy"), + "supportedMedia", JavaList.of("image/gif"), + "supportedInputType", JavaList.of("text") + ) } private def extendDrops(drops: Array[Drop], steps: Int, xOffset: Int) = { From 433621f046129b39f7552057437081860d9b89d8 Mon Sep 17 00:00:00 2001 From: hrj Date: Wed, 14 Apr 2021 09:47:22 +0530 Subject: [PATCH 2/5] Update simpleTest to latest API and solve before answering Signed-off-by: hrj --- scripts/simpleTest.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/scripts/simpleTest.py b/scripts/simpleTest.py index 760c120..4503199 100644 --- a/scripts/simpleTest.py +++ b/scripts/simpleTest.py @@ -1,12 +1,8 @@ import http.client import json +import subprocess conn = http.client.HTTPConnection('localhost', 8888) -conn.request("GET", "/v1/token?email=test") -response = conn.getresponse() -responseStr = response.read() -user = json.loads(responseStr) -token = user["token"] params = """{ "level": "medium", @@ -15,13 +11,25 @@ params = """{ }""" def getCaptcha(): - conn.request("POST", "/v1/captcha", body=params, headers={'access-token': user["token"]}) + conn.request("POST", "/v1/captcha", body=params) response = conn.getresponse() if response: responseStr = response.read() return json.loads(responseStr) +def getAndSolve(idStr): + conn.request("GET", "/v1/media?id=" + idStr) + response = conn.getresponse() + + if response: + responseBytes = response.read() + with open("captcha.png", "wb") as f: + f.write(responseBytes) + ocrResult = subprocess.Popen("gocr captcha.png", shell=True, stdout=subprocess.PIPE) + ocrAnswer = ocrResult.stdout.readlines()[0].strip().decode() + return ocrAnswer + def postAnswer(captchaId, ans): reply = {"answer": ans, "id" : captchaId} conn.request("POST", "/v1/answer", json.dumps(reply)) @@ -33,6 +41,6 @@ def postAnswer(captchaId, ans): for i in range(0, 10000): captcha = getCaptcha() - #print(captcha) captchaId = captcha["id"] - print(i, postAnswer(captchaId, "xyz")) + ans = getAndSolve(captchaId) + print(i, postAnswer(captchaId, ans)) From 352424e8f5169b39ebc9cd70785df59bf4069745 Mon Sep 17 00:00:00 2001 From: hrj Date: Wed, 14 Apr 2021 10:00:36 +0530 Subject: [PATCH 3/5] simpleTest: use XDG_RUNTIME_DIR for storing temp files Signed-off-by: hrj --- scripts/simpleTest.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/scripts/simpleTest.py b/scripts/simpleTest.py index 4503199..04f260b 100644 --- a/scripts/simpleTest.py +++ b/scripts/simpleTest.py @@ -1,7 +1,9 @@ import http.client import json import subprocess +import os +tempDir = os.getenv('XDG_RUNTIME_DIR', '.') conn = http.client.HTTPConnection('localhost', 8888) params = """{ @@ -24,9 +26,10 @@ def getAndSolve(idStr): if response: responseBytes = response.read() - with open("captcha.png", "wb") as f: + fileName = tempDir + "/captcha.png" + with open(fileName, "wb") as f: f.write(responseBytes) - ocrResult = subprocess.Popen("gocr captcha.png", shell=True, stdout=subprocess.PIPE) + ocrResult = subprocess.Popen("gocr " + fileName, shell=True, stdout=subprocess.PIPE) ocrAnswer = ocrResult.stdout.readlines()[0].strip().decode() return ocrAnswer From 8fd294f0cf75c1953200f547e70b217ba5114799 Mon Sep 17 00:00:00 2001 From: hrj Date: Wed, 14 Apr 2021 10:00:58 +0530 Subject: [PATCH 4/5] simpleTest.py : use debug level of difficulty Signed-off-by: hrj --- scripts/simpleTest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/simpleTest.py b/scripts/simpleTest.py index 04f260b..daa3f7f 100644 --- a/scripts/simpleTest.py +++ b/scripts/simpleTest.py @@ -7,7 +7,7 @@ tempDir = os.getenv('XDG_RUNTIME_DIR', '.') conn = http.client.HTTPConnection('localhost', 8888) params = """{ -"level": "medium", +"level": "debug", "media": "image/png", "input_type": "text" }""" From 660447798f30b24322b1fb8088d10da405e32ab9 Mon Sep 17 00:00:00 2001 From: hrj Date: Wed, 14 Apr 2021 10:01:30 +0530 Subject: [PATCH 5/5] Debug Captcha: Print mismatches Signed-off-by: hrj --- src/main/scala/lc/captchas/DebugCaptcha.scala | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/scala/lc/captchas/DebugCaptcha.scala b/src/main/scala/lc/captchas/DebugCaptcha.scala index 084f1e1..787b6cf 100644 --- a/src/main/scala/lc/captchas/DebugCaptcha.scala +++ b/src/main/scala/lc/captchas/DebugCaptcha.scala @@ -33,7 +33,11 @@ class DebugCaptcha extends ChallengeProvider { } def checkAnswer(secret: String, answer: String): Boolean = { - answer.toLowerCase().equals(secret) + val matches = answer.toLowerCase().replaceAll(" ", "").equals(secret) + if (!matches) { + println(s"Didn't match, answer: '$answer' to secret '$secret'") + } + matches } private def simpleText(text: String): Array[Byte] = {