mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-01-12 22:43:20 -05:00
Merge pull request #85 from UprootStaging/simplifiedConstructor
Code simplifications and updated test script
This commit is contained in:
commit
3a1b01688a
@ -1,27 +1,38 @@
|
||||
import http.client
|
||||
import json
|
||||
import subprocess
|
||||
import os
|
||||
|
||||
tempDir = os.getenv('XDG_RUNTIME_DIR', '.')
|
||||
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",
|
||||
"level": "debug",
|
||||
"media": "image/png",
|
||||
"input_type": "text"
|
||||
}"""
|
||||
|
||||
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()
|
||||
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):
|
||||
reply = {"answer": ans, "id" : captchaId}
|
||||
conn.request("POST", "/v1/answer", json.dumps(reply))
|
||||
@ -33,6 +44,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))
|
||||
|
@ -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<String, List<String>> supportedParameters() {
|
||||
HashMap<String, List<String>> supportedParams = new HashMap<String, List<String>>();
|
||||
supportedParams.put("supportedLevels", List.of("medium"));
|
||||
supportedParams.put("supportedMedia", List.of("image/png"));
|
||||
supportedParams.put("supportedInputType", List.of("text"));
|
||||
|
||||
return supportedParams;
|
||||
public Map<String, List<String>> supportedParameters() {
|
||||
return Map.of(
|
||||
"supportedLevels", List.of("medium"),
|
||||
"supportedMedia", List.of("image/png"),
|
||||
"supportedInputType", List.of("text")
|
||||
);
|
||||
}
|
||||
|
||||
public void configure(String config) {
|
||||
|
@ -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<String, List<String>> supportedParameters() {
|
||||
HashMap<String, List<String>> supportedParams = new HashMap<String, List<String>>();
|
||||
supportedParams.put("supportedLevels", List.of("hard"));
|
||||
supportedParams.put("supportedMedia", List.of("image/gif"));
|
||||
supportedParams.put("supportedInputType", List.of("text"));
|
||||
|
||||
return supportedParams;
|
||||
public Map<String, List<String>> supportedParameters() {
|
||||
return Map.of(
|
||||
"supportedLevels", List.of("hard"),
|
||||
"supportedMedia", List.of("image/gif"),
|
||||
"supportedInputType", List.of("text")
|
||||
);
|
||||
}
|
||||
|
||||
public Challenge returnChallenge() {
|
||||
|
@ -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<String, List<String>> supportedParameters() {
|
||||
HashMap<String, List<String>> supportedParams = new HashMap<String, List<String>>();
|
||||
supportedParams.put("supportedLevels", List.of("easy"));
|
||||
supportedParams.put("supportedMedia", List.of("image/png"));
|
||||
supportedParams.put("supportedInputType", List.of("text"));
|
||||
|
||||
return supportedParams;
|
||||
public Map<String, List<String>> supportedParameters() {
|
||||
return Map.of(
|
||||
"supportedLevels", List.of("easy"),
|
||||
"supportedMedia", List.of("image/png"),
|
||||
"supportedInputType", List.of("text")
|
||||
);
|
||||
}
|
||||
|
||||
public boolean checkAnswer(String secret, String answer) {
|
||||
|
@ -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] = {
|
||||
|
@ -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 = {
|
||||
|
@ -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 =
|
||||
|
@ -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) = {
|
||||
|
Loading…
Reference in New Issue
Block a user