Merge pull request #34 from rr83019/master

Add method to check for unsolved captchas
This commit is contained in:
hrj 2019-04-07 22:54:06 +05:30 committed by GitHub
commit 0c47eb6866
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,7 +90,17 @@ class Captcha(throttle: Int) {
val thread = ex.scheduleWithFixedDelay(task, 1, delay, TimeUnit.SECONDS)
}
def getAnswer(answer: Answer): Boolean = {
def getChallenge(param: Parameters): Id = {
val rs = stmt.executeQuery("SELECT token FROM challenge WHERE solved=FALSE LIMIT 1")
val id = if(rs.next()){
rs.getString("token")
} else {
generateChallenge(param)
}
Id(id)
}
def checkAnswer(answer: Answer): Boolean = {
selectPstmt.setString(1, answer.id)
val rs: ResultSet = selectPstmt.executeQuery()
rs.next()
@ -97,13 +111,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}")
}
}
@ -154,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)