Merge pull request #85 from UprootStaging/simplifiedConstructor

Code simplifications and updated test script
This commit is contained in:
hrj 2021-04-14 10:47:18 +05:30 committed by GitHub
commit 3a1b01688a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 61 additions and 58 deletions

View File

@ -1,27 +1,38 @@
import http.client import http.client
import json import json
import subprocess
import os
tempDir = os.getenv('XDG_RUNTIME_DIR', '.')
conn = http.client.HTTPConnection('localhost', 8888) 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 = """{ params = """{
"level": "medium", "level": "debug",
"media": "image/png", "media": "image/png",
"input_type": "text" "input_type": "text"
}""" }"""
def getCaptcha(): def getCaptcha():
conn.request("POST", "/v1/captcha", body=params, headers={'access-token': user["token"]}) conn.request("POST", "/v1/captcha", body=params)
response = conn.getresponse() response = conn.getresponse()
if response: if response:
responseStr = response.read() responseStr = response.read()
return json.loads(responseStr) return json.loads(responseStr)
def getAndSolve(idStr):
conn.request("GET", "/v1/media?id=" + idStr)
response = conn.getresponse()
if response:
responseBytes = response.read()
fileName = tempDir + "/captcha.png"
with open(fileName, "wb") as f:
f.write(responseBytes)
ocrResult = subprocess.Popen("gocr " + fileName, shell=True, stdout=subprocess.PIPE)
ocrAnswer = ocrResult.stdout.readlines()[0].strip().decode()
return ocrAnswer
def postAnswer(captchaId, ans): def postAnswer(captchaId, ans):
reply = {"answer": ans, "id" : captchaId} reply = {"answer": ans, "id" : captchaId}
conn.request("POST", "/v1/answer", json.dumps(reply)) conn.request("POST", "/v1/answer", json.dumps(reply))
@ -33,6 +44,6 @@ def postAnswer(captchaId, ans):
for i in range(0, 10000): for i in range(0, 10000):
captcha = getCaptcha() captcha = getCaptcha()
#print(captcha)
captchaId = captcha["id"] captchaId = captcha["id"]
print(i, postAnswer(captchaId, "xyz")) ans = getAndSolve(captchaId)
print(i, postAnswer(captchaId, ans))

View File

@ -6,7 +6,7 @@ import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.util.HashMap; import java.util.Map;
import java.util.List; import java.util.List;
import lc.captchas.interfaces.Challenge; import lc.captchas.interfaces.Challenge;
import lc.captchas.interfaces.ChallengeProvider; import lc.captchas.interfaces.ChallengeProvider;
@ -18,13 +18,12 @@ public class FontFunCaptcha implements ChallengeProvider {
return "FontFunCaptcha"; return "FontFunCaptcha";
} }
public HashMap<String, List<String>> supportedParameters() { public Map<String, List<String>> supportedParameters() {
HashMap<String, List<String>> supportedParams = new HashMap<String, List<String>>(); return Map.of(
supportedParams.put("supportedLevels", List.of("medium")); "supportedLevels", List.of("medium"),
supportedParams.put("supportedMedia", List.of("image/png")); "supportedMedia", List.of("image/png"),
supportedParams.put("supportedInputType", List.of("text")); "supportedInputType", List.of("text")
);
return supportedParams;
} }
public void configure(String config) { public void configure(String config) {

View File

@ -6,7 +6,7 @@ import java.awt.RenderingHints;
import java.awt.Color; import java.awt.Color;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.Map;
import java.util.List; import java.util.List;
import javax.imageio.stream.ImageOutputStream; import javax.imageio.stream.ImageOutputStream;
@ -55,13 +55,12 @@ public class GifCaptcha implements ChallengeProvider {
// TODO: Add custom config // TODO: Add custom config
} }
public HashMap<String, List<String>> supportedParameters() { public Map<String, List<String>> supportedParameters() {
HashMap<String, List<String>> supportedParams = new HashMap<String, List<String>>(); return Map.of(
supportedParams.put("supportedLevels", List.of("hard")); "supportedLevels", List.of("hard"),
supportedParams.put("supportedMedia", List.of("image/gif")); "supportedMedia", List.of("image/gif"),
supportedParams.put("supportedInputType", List.of("text")); "supportedInputType", List.of("text")
);
return supportedParams;
} }
public Challenge returnChallenge() { public Challenge returnChallenge() {

View File

@ -10,7 +10,7 @@ import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp; import java.awt.image.ConvolveOp;
import java.awt.image.Kernel; import java.awt.image.Kernel;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.HashMap; import java.util.Map;
import java.util.List; import java.util.List;
import lc.misc.HelperFunctions; import lc.misc.HelperFunctions;
@ -27,13 +27,12 @@ public class ShadowTextCaptcha implements ChallengeProvider {
// TODO: Add custom config // TODO: Add custom config
} }
public HashMap<String, List<String>> supportedParameters() { public Map<String, List<String>> supportedParameters() {
HashMap<String, List<String>> supportedParams = new HashMap<String, List<String>>(); return Map.of(
supportedParams.put("supportedLevels", List.of("easy")); "supportedLevels", List.of("easy"),
supportedParams.put("supportedMedia", List.of("image/png")); "supportedMedia", List.of("image/png"),
supportedParams.put("supportedInputType", List.of("text")); "supportedInputType", List.of("text")
);
return supportedParams;
} }
public boolean checkAnswer(String secret, String answer) { public boolean checkAnswer(String secret, String answer) {

View File

@ -33,7 +33,11 @@ class DebugCaptcha extends ChallengeProvider {
} }
def checkAnswer(secret: String, answer: String): Boolean = { 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] = { private def simpleText(text: String): Array[Byte] = {

View File

@ -7,7 +7,6 @@ import java.awt.Font
import java.awt.Color import java.awt.Color
import lc.captchas.interfaces.ChallengeProvider import lc.captchas.interfaces.ChallengeProvider
import lc.captchas.interfaces.Challenge import lc.captchas.interfaces.Challenge
import scala.jdk.CollectionConverters.MapHasAsJava
import java.util.{List => JavaList, Map => JavaMap} import java.util.{List => JavaList, Map => JavaMap}
class FilterChallenge extends ChallengeProvider { class FilterChallenge extends ChallengeProvider {
@ -18,13 +17,11 @@ class FilterChallenge extends ChallengeProvider {
} }
def supportedParameters(): JavaMap[String, JavaList[String]] = { def supportedParameters(): JavaMap[String, JavaList[String]] = {
val supportedParams = Map( JavaMap.of(
"supportedLevels" -> JavaList.of("medium", "hard"), "supportedLevels",JavaList.of("medium", "hard"),
"supportedMedia" -> JavaList.of("image/png"), "supportedMedia", JavaList.of("image/png"),
"supportedInputType" -> JavaList.of("text") "supportedInputType", JavaList.of("text")
).asJava )
supportedParams
} }
def returnChallenge(): Challenge = { def returnChallenge(): Challenge = {

View File

@ -9,7 +9,6 @@ import java.awt.image.BufferedImage
import java.awt.Color import java.awt.Color
import lc.captchas.interfaces.ChallengeProvider import lc.captchas.interfaces.ChallengeProvider
import lc.captchas.interfaces.Challenge import lc.captchas.interfaces.Challenge
import scala.jdk.CollectionConverters.MapHasAsJava
import java.util.{List => JavaList, Map => JavaMap} import java.util.{List => JavaList, Map => JavaMap}
class LabelCaptcha extends ChallengeProvider { class LabelCaptcha extends ChallengeProvider {
@ -30,13 +29,11 @@ class LabelCaptcha extends ChallengeProvider {
} }
def supportedParameters(): JavaMap[String, JavaList[String]] = { def supportedParameters(): JavaMap[String, JavaList[String]] = {
val supportedParams = Map( JavaMap.of(
"supportedLevels" -> JavaList.of("hard"), "supportedLevels", JavaList.of("hard"),
"supportedMedia" -> JavaList.of("image/png"), "supportedMedia", JavaList.of("image/png"),
"supportedInputType" -> JavaList.of("text") "supportedInputType", JavaList.of("text")
).asJava )
supportedParams
} }
def returnChallenge(): Challenge = def returnChallenge(): Challenge =

View File

@ -10,7 +10,6 @@ import javax.imageio.stream.MemoryCacheImageOutputStream;
import lc.captchas.interfaces.ChallengeProvider import lc.captchas.interfaces.ChallengeProvider
import lc.captchas.interfaces.Challenge import lc.captchas.interfaces.Challenge
import lc.misc.GifSequenceWriter import lc.misc.GifSequenceWriter
import scala.jdk.CollectionConverters.MapHasAsJava
import java.util.{List => JavaList, Map => JavaMap} import java.util.{List => JavaList, Map => JavaMap}
class Drop { class Drop {
@ -38,13 +37,11 @@ class RainDropsCP extends ChallengeProvider {
} }
def supportedParameters(): JavaMap[String, JavaList[String]] = { def supportedParameters(): JavaMap[String, JavaList[String]] = {
val supportedParams = Map( JavaMap.of(
"supportedLevels" -> JavaList.of("medium", "easy"), "supportedLevels", JavaList.of("medium", "easy"),
"supportedMedia" -> JavaList.of("image/gif"), "supportedMedia", JavaList.of("image/gif"),
"supportedInputType" -> JavaList.of("text") "supportedInputType", JavaList.of("text")
).asJava )
supportedParams
} }
private def extendDrops(drops: Array[Drop], steps: Int, xOffset: Int) = { private def extendDrops(drops: Array[Drop], steps: Int, xOffset: Int) = {