Merge pull request #33 from rr83019/master

Add Captcha thread throttle
This commit is contained in:
hrj 2019-04-07 07:45:13 +05:30 committed by GitHub
commit 4d79018799
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 21 deletions

View File

@ -18,7 +18,7 @@ import org.json4s.jackson.Serialization.{read, write}
import java.util.concurrent._ import java.util.concurrent._
import scala.Array import scala.Array
class Captcha { 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)")
@ -26,7 +26,6 @@ class Captcha {
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 filters = Map("FilterChallenge" -> new FilterChallenge, val filters = Map("FilterChallenge" -> new FilterChallenge,
"FontFunCaptcha" -> new FontFunCaptcha, "FontFunCaptcha" -> new FontFunCaptcha,
"GifCaptcha" -> new GifCaptcha, "GifCaptcha" -> new GifCaptcha,
@ -70,26 +69,21 @@ class Captcha {
} }
val task = new Runnable { val task = new Runnable {
val providerMap = getProvider()
val provider = filters(providerMap)
def run(): Unit = { def run(): Unit = {
val challenge = provider.returnChallenge() val imageNum = stmt.executeQuery("SELECT COUNT(*) AS total FROM challenge")
val blob = new ByteArrayInputStream(challenge.content) var throttleIn = (throttle*1.1).toInt
val token = scala.util.Random.nextInt(10000).toString if(imageNum.next())
val id = Id(token) throttleIn = (throttleIn-imageNum.getInt("total"))
insertPstmt.setString(1, token) while(0 < throttleIn){
insertPstmt.setString(2, provider.getId) getChallenge(Parameters("","","",Option(Size(0,0))))
insertPstmt.setString(3, challenge.secret) throttleIn -= 1
insertPstmt.setString(4, providerMap) }
insertPstmt.setString(5, challenge.contentType)
insertPstmt.setBlob(6, blob)
insertPstmt.executeUpdate()
} }
} }
def beginThread(delay: Int) : Unit = { def beginThread(delay: Int) : Unit = {
val ex = new ScheduledThreadPoolExecutor(1) val ex = new ScheduledThreadPoolExecutor(1)
val thread = ex.scheduleAtFixedRate(task, 1, delay, TimeUnit.SECONDS) val thread = ex.scheduleWithFixedDelay(task, 1, delay, TimeUnit.SECONDS)
} }
def getAnswer(answer: Answer): Boolean = { def getAnswer(answer: Answer): Boolean = {
@ -124,7 +118,7 @@ case class Id(id: String)
case class Answer(answer: String, id: String) case class Answer(answer: String, id: String)
class Server(port: Int){ class Server(port: Int){
val captcha = new Captcha() val captcha = new Captcha(0)
val server = new HTTPServer(port) val server = new HTTPServer(port)
val host = server.getVirtualHost(null) val host = server.getVirtualHost(null)
@ -175,11 +169,10 @@ class Server(port: Int){
object LCFramework{ object LCFramework{
def main(args: scala.Array[String]) { def main(args: scala.Array[String]) {
val captcha = new Captcha() val captcha = new Captcha(50)
val server = new Server(8888) val server = new Server(8888)
captcha.beginThread(2)
server.start() server.start()
//captcha.beginThread(2)
} }
} }