mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-01-12 22:43:20 -05:00
Merge pull request #34 from rr83019/master
Add method to check for unsolved captchas
This commit is contained in:
commit
0c47eb6866
@ -21,10 +21,11 @@ import scala.Array
|
|||||||
class Captcha(throttle: Int) {
|
class Captcha(throttle: Int) {
|
||||||
val con: Connection = DriverManager.getConnection("jdbc:h2:./captcha", "sa", "")
|
val con: Connection = DriverManager.getConnection("jdbc:h2:./captcha", "sa", "")
|
||||||
val stmt: Statement = con.createStatement()
|
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 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 selectPstmt: PreparedStatement = con.prepareStatement("SELECT secret, provider FROM challenge WHERE token = ?")
|
||||||
val imagePstmt: PreparedStatement = con.prepareStatement("SELECT image 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,
|
val filters = Map("FilterChallenge" -> new FilterChallenge,
|
||||||
"FontFunCaptcha" -> new FontFunCaptcha,
|
"FontFunCaptcha" -> new FontFunCaptcha,
|
||||||
@ -40,24 +41,27 @@ class Captcha(throttle: Int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
def getCaptcha(id: Id): Array[Byte] = {
|
def getCaptcha(id: Id): Array[Byte] = {
|
||||||
|
var image :Array[Byte] = null
|
||||||
|
var blob: Blob = null
|
||||||
imagePstmt.setString(1, id.id)
|
imagePstmt.setString(1, id.id)
|
||||||
val rs: ResultSet = imagePstmt.executeQuery()
|
val rs: ResultSet = imagePstmt.executeQuery()
|
||||||
rs.next()
|
if(rs.next()){
|
||||||
val blob = rs.getBlob("image")
|
blob = rs.getBlob("image")
|
||||||
var image :Array[Byte] = null
|
updatePstmt.setString(1,id.id)
|
||||||
|
updatePstmt.executeUpdate()
|
||||||
|
}
|
||||||
if(blob != null)
|
if(blob != null)
|
||||||
image = blob.getBytes(1, blob.length().toInt)
|
image = blob.getBytes(1, blob.length().toInt)
|
||||||
image
|
image
|
||||||
}
|
}
|
||||||
|
|
||||||
def getChallenge(param: Parameters): Id = {
|
def generateChallenge(param: Parameters): String = {
|
||||||
//TODO: eval params to choose a provider
|
//TODO: eval params to choose a provider
|
||||||
val providerMap = getProvider()
|
val providerMap = getProvider()
|
||||||
val provider = filters(providerMap)
|
val provider = filters(providerMap)
|
||||||
val challenge = provider.returnChallenge()
|
val challenge = provider.returnChallenge()
|
||||||
val blob = new ByteArrayInputStream(challenge.content)
|
val blob = new ByteArrayInputStream(challenge.content)
|
||||||
val token = scala.util.Random.nextInt(10000).toString
|
val token = scala.util.Random.nextInt(10000).toString
|
||||||
val id = Id(token)
|
|
||||||
insertPstmt.setString(1, token)
|
insertPstmt.setString(1, token)
|
||||||
insertPstmt.setString(2, provider.getId)
|
insertPstmt.setString(2, provider.getId)
|
||||||
insertPstmt.setString(3, challenge.secret)
|
insertPstmt.setString(3, challenge.secret)
|
||||||
@ -65,7 +69,7 @@ class Captcha(throttle: Int) {
|
|||||||
insertPstmt.setString(5, challenge.contentType)
|
insertPstmt.setString(5, challenge.contentType)
|
||||||
insertPstmt.setBlob(6, blob)
|
insertPstmt.setBlob(6, blob)
|
||||||
insertPstmt.executeUpdate()
|
insertPstmt.executeUpdate()
|
||||||
id
|
token
|
||||||
}
|
}
|
||||||
|
|
||||||
val task = new Runnable {
|
val task = new Runnable {
|
||||||
@ -86,7 +90,17 @@ class Captcha(throttle: Int) {
|
|||||||
val thread = ex.scheduleWithFixedDelay(task, 1, delay, TimeUnit.SECONDS)
|
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)
|
selectPstmt.setString(1, answer.id)
|
||||||
val rs: ResultSet = selectPstmt.executeQuery()
|
val rs: ResultSet = selectPstmt.executeQuery()
|
||||||
rs.next()
|
rs.next()
|
||||||
@ -97,13 +111,13 @@ class Captcha(throttle: Int) {
|
|||||||
|
|
||||||
def display(): Unit = {
|
def display(): Unit = {
|
||||||
val rs: ResultSet = stmt.executeQuery("SELECT * FROM challenge")
|
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()) {
|
while(rs.next()) {
|
||||||
val token = rs.getString("token")
|
val token = rs.getString("token")
|
||||||
val id = rs.getString("id")
|
val id = rs.getString("id")
|
||||||
val secret = rs.getString("secret")
|
val secret = rs.getString("secret")
|
||||||
val image = rs.getString("image")
|
val solved = rs.getString("solved")
|
||||||
println(s"${token}\t\t${id}\t\t${secret}\t\t")
|
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 body = req.getJson()
|
||||||
val json = parse(body)
|
val json = parse(body)
|
||||||
val answer = json.extract[Answer]
|
val answer = json.extract[Answer]
|
||||||
val result = captcha.getAnswer(answer)
|
val result = captcha.checkAnswer(answer)
|
||||||
resp.getHeaders().add("Content-Type","application/json")
|
resp.getHeaders().add("Content-Type","application/json")
|
||||||
val responseContent = if(result) """{"result":"True"}""" else """{"result":"False"}"""
|
val responseContent = if(result) """{"result":"True"}""" else """{"result":"False"}"""
|
||||||
resp.send(200,responseContent)
|
resp.send(200,responseContent)
|
||||||
|
Loading…
Reference in New Issue
Block a user