Merge pull request #4 from rr83019/master

Added http end points to the framework
This commit is contained in:
hrj 2019-02-26 23:02:33 +05:30 committed by GitHub
commit aa3aaa4ce7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 3231 additions and 26 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.class
*.log
*.png
/bin/

View File

@ -13,8 +13,14 @@ lazy val root = (project in file(".")).
libraryDependencies += "com.sksamuel.scrimage" %% "scrimage-io-extra" % "2.1.8",
libraryDependencies += "com.sksamuel.scrimage" %% "scrimage-filters" % "2.1.8"
libraryDependencies += "com.sksamuel.scrimage" %% "scrimage-filters" % "2.1.8",
libraryDependencies += "org.json4s" %% "json4s-jackson" % "3.6.1"
)
unmanagedResourceDirectories in Compile += {baseDirectory.value / "lib"}
javacOptions += "-g:none"
compileOrder := CompileOrder.JavaThenScala
fork in run := true

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,7 @@ import java.awt.image.BufferedImage
import java.awt.Font
import java.awt.Color
class FilterChallenge extends ChallengeProvider {
class FilterChallenge extends ChallengeProvider{
val id = "filter"
def returnChallenge(): (Image, String) = {
val filterTypes = List(new FilterType1, new FilterType2)

View File

@ -3,6 +3,17 @@ package lc
import com.sksamuel.scrimage._
import java.sql._
import java.io._
import httpserver._
import javax.imageio._
import java.awt.image._
import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.JsonDSL._
import java.util.Base64
import org.json4s.jackson.Serialization
import org.json4s.jackson.Serialization.{read, write}
import java.util.concurrent._
import scala.Array
trait ChallengeProvider {
val id: String
@ -14,37 +25,70 @@ trait ChallengeProvider {
class Captcha {
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, image blob)")
val insertPstmt: PreparedStatement = con.prepareStatement("INSERT INTO challenge(token, id, secret) VALUES (?, ?, ?)")
val selectPstmt: PreparedStatement = con.prepareStatement("SELECT secret FROM challenge WHERE token = ?")
def getCaptcha(): Boolean = {
val provider = new FilterChallenge
val (token, image) = this.getChallenge(provider)
image.output(new File("Captcha.png"))
println(s"Token: ${token}")
println("Enter your answer: ")
val answer = scala.io.StdIn.readLine()
this.getAnswer(token, answer, provider)
stmt.execute("CREATE TABLE IF NOT EXISTS challenge(token varchar, id varchar, secret varchar, provider varchar, image blob)")
val insertPstmt: PreparedStatement = con.prepareStatement("INSERT INTO challenge(token, id, secret, provider, 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 filters = Map("FilterChallenge" -> new FilterChallenge)
def getCaptcha(id: Id): Array[Byte] = {
imagePstmt.setString(1, id.id)
val rs: ResultSet = imagePstmt.executeQuery()
rs.next()
val blob = rs.getBlob("image")
var image :Array[Byte] = null
if(blob != null)
image = blob.getBytes(1, blob.length().toInt)
image
}
def getChallenge(provider: ChallengeProvider): (String, Image) = {
def getChallenge(param: Parameters): Id = {
//TODO: eval params to choose a provider
val providerMap = "FilterChallenge"
val provider = filters(providerMap)
val (image, secret) = provider.returnChallenge()
val blob = new ByteArrayInputStream(image.bytes)
val token = scala.util.Random.nextInt(10000).toString
val id = Id(token)
insertPstmt.setString(1, token)
insertPstmt.setString(2, provider.id)
insertPstmt.setString(3, secret)
//TODO: insert image into database
insertPstmt.setString(4, providerMap)
insertPstmt.setBlob(5, blob)
insertPstmt.executeUpdate()
(token, image)
id
}
def getAnswer(token: String, answer: String, provider: ChallengeProvider): Boolean = {
selectPstmt.setString(1, token)
val task = new Runnable {
val providerMap = "FilterChallenge"
val provider = filters(providerMap)
def run(): Unit = {
val (image, secret) = provider.returnChallenge()
val blob = new ByteArrayInputStream(image.bytes)
val token = scala.util.Random.nextInt(10000).toString
val id = Id(token)
insertPstmt.setString(1, token)
insertPstmt.setString(2, provider.id)
insertPstmt.setString(3, secret)
insertPstmt.setString(4, providerMap)
insertPstmt.setBlob(5, blob)
insertPstmt.executeUpdate()
}
}
def beginThread(delay: Int) : Unit = {
val ex = new ScheduledThreadPoolExecutor(1)
val thread = ex.scheduleAtFixedRate(task, 1, delay, TimeUnit.SECONDS)
}
def getAnswer(answer: Answer): Boolean = {
selectPstmt.setString(1, answer.id)
val rs: ResultSet = selectPstmt.executeQuery()
rs.next()
val secret = rs.getString("secret")
provider.checkAnswer(secret, answer)
val provider = rs.getString("provider")
filters(provider).checkAnswer(secret, answer.answer)
}
def display(): Unit = {
@ -55,7 +99,7 @@ class Captcha {
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${image}")
println(s"${token}\t\t${id}\t\t${secret}\t\t")
}
}
@ -64,13 +108,62 @@ class Captcha {
}
}
case class Size(height: Int, width: Int)
case class Parameters(level: String, media: String, input_type: String, size: Option[Size])
case class Id(id: String)
case class Answer(answer: String, id: String)
class Server(port: Int){
val captcha = new Captcha()
val server = new HTTPServer(port)
val host = server.getVirtualHost(null)
implicit val formats = DefaultFormats
host.addContext("/v1/captcha",(req, resp) => {
val body = req.getJson()
val json = parse(body)
val param = json.extract[Parameters]
val id = captcha.getChallenge(param)
resp.getHeaders().add("Content-Type","application/json")
resp.send(200, write(id))
0
},"POST")
host.addContext("/v1/media",(req, resp) => {
val body = req.getJson()
val json = parse(body)
val id = json.extract[Id]
val image = captcha.getCaptcha(id)
resp.getHeaders().add("Content-Type","image/png")
resp.send(200, image)
0
},"POST")
host.addContext("/v1/answer",(req, resp) => {
val body = req.getJson()
val json = parse(body)
val answer = json.extract[Answer]
val result = captcha.getAnswer(answer)
resp.getHeaders().add("Content-Type","application/json")
val responseContent = if(result) """{"result":"True"}""" else """{"result":"False"}"""
resp.send(200,responseContent)
0
},"POST")
def start(): Unit = {
server.start()
}
}
object LCFramework{
def main(args: scala.Array[String]) {
val captcha = new Captcha
val result = captcha.getCaptcha()
println(result)
captcha.display()
captcha.closeConnection()
val captcha = new Captcha()
val server = new Server(8888)
server.start()
//captcha.beginThread(2)
}
}