mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-01-14 15:25:00 -05:00
refactor: don't inherit classes from DBConn
This commit is contained in:
parent
7bfde4eddb
commit
9af66adf9f
@ -5,14 +5,6 @@ import java.sql._
|
|||||||
class DBConn(){
|
class DBConn(){
|
||||||
val con: Connection = DriverManager.getConnection("jdbc:h2:./captcha", "sa", "")
|
val con: Connection = DriverManager.getConnection("jdbc:h2:./captcha", "sa", "")
|
||||||
|
|
||||||
lazy val insertPstmt: PreparedStatement = con.prepareStatement("INSERT INTO challenge(token, id, secret, provider, contentType, image) VALUES (?, ?, ?, ?, ?, ?)")
|
|
||||||
lazy val mapPstmt: PreparedStatement = con.prepareStatement("INSERT INTO mapId(uuid, token) VALUES (?, ?)")
|
|
||||||
lazy val selectPstmt: PreparedStatement = con.prepareStatement("SELECT secret, provider FROM challenge WHERE token = (SELECT m.token FROM mapId m, challenge c WHERE m.token=c.token AND m.uuid = ?)")
|
|
||||||
lazy val imagePstmt: PreparedStatement = con.prepareStatement("SELECT image FROM challenge c, mapId m WHERE c.token=m.token AND m.uuid = ?")
|
|
||||||
lazy val updatePstmt: PreparedStatement = con.prepareStatement("UPDATE challenge SET solved = True WHERE token = (SELECT m.token FROM mapId m, challenge c WHERE m.token=c.token AND m.uuid = ?)")
|
|
||||||
lazy val userPstmt: PreparedStatement = con.prepareStatement("INSERT INTO users(email, hash) VALUES (?,?)")
|
|
||||||
lazy val validatePstmt: PreparedStatement = con.prepareStatement("SELECT hash FROM users WHERE hash = ? LIMIT 1")
|
|
||||||
|
|
||||||
def getStatement(): Statement = {
|
def getStatement(): Statement = {
|
||||||
con.createStatement()
|
con.createStatement()
|
||||||
}
|
}
|
||||||
|
@ -14,13 +14,7 @@ case class Answer(answer: String, id: String)
|
|||||||
|
|
||||||
case class ProviderSecret(provider: String, secret: String)
|
case class ProviderSecret(provider: String, secret: String)
|
||||||
|
|
||||||
class Captcha(throttle: Int) extends DBConn {
|
object CaptchaProviders {
|
||||||
|
|
||||||
val stmt = getStatement()
|
|
||||||
stmt.execute("CREATE TABLE IF NOT EXISTS challenge(token varchar, id varchar, secret varchar, provider varchar, contentType varchar, image blob, solved boolean default False, PRIMARY KEY(token))")
|
|
||||||
stmt.execute("CREATE TABLE IF NOT EXISTS mapId(uuid varchar, token varchar, PRIMARY KEY(uuid), FOREIGN KEY(token) REFERENCES challenge(token))")
|
|
||||||
stmt.execute("CREATE TABLE IF NOT EXISTS users(email varchar, hash int)")
|
|
||||||
|
|
||||||
val providers = Map(
|
val providers = Map(
|
||||||
"FilterChallenge" -> new FilterChallenge,
|
"FilterChallenge" -> new FilterChallenge,
|
||||||
"FontFunCaptcha" -> new FontFunCaptcha,
|
"FontFunCaptcha" -> new FontFunCaptcha,
|
||||||
@ -30,6 +24,28 @@ class Captcha(throttle: Int) extends DBConn {
|
|||||||
"LabelCaptcha" -> new LabelCaptcha
|
"LabelCaptcha" -> new LabelCaptcha
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def generateChallengeSamples() = {
|
||||||
|
providers.map {case (key, provider) =>
|
||||||
|
(key, provider.returnChallenge())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Captcha(throttle: Int, dbConn: DBConn) {
|
||||||
|
import CaptchaProviders._
|
||||||
|
|
||||||
|
private val stmt = dbConn.getStatement()
|
||||||
|
stmt.execute("CREATE TABLE IF NOT EXISTS challenge(token varchar, id varchar, secret varchar, provider varchar, contentType varchar, image blob, solved boolean default False, PRIMARY KEY(token))")
|
||||||
|
stmt.execute("CREATE TABLE IF NOT EXISTS mapId(uuid varchar, token varchar, PRIMARY KEY(uuid), FOREIGN KEY(token) REFERENCES challenge(token))")
|
||||||
|
stmt.execute("CREATE TABLE IF NOT EXISTS users(email varchar, hash int)")
|
||||||
|
|
||||||
|
private val insertPstmt = dbConn.con.prepareStatement("INSERT INTO challenge(token, id, secret, provider, contentType, image) VALUES (?, ?, ?, ?, ?, ?)")
|
||||||
|
private val mapPstmt = dbConn.con.prepareStatement("INSERT INTO mapId(uuid, token) VALUES (?, ?)")
|
||||||
|
private val selectPstmt = dbConn.con.prepareStatement("SELECT secret, provider FROM challenge WHERE token = (SELECT m.token FROM mapId m, challenge c WHERE m.token=c.token AND m.uuid = ?)")
|
||||||
|
private val imagePstmt = dbConn.con.prepareStatement("SELECT image FROM challenge c, mapId m WHERE c.token=m.token AND m.uuid = ?")
|
||||||
|
private val updatePstmt = dbConn.con.prepareStatement("UPDATE challenge SET solved = True WHERE token = (SELECT m.token FROM mapId m, challenge c WHERE m.token=c.token AND m.uuid = ?)")
|
||||||
|
private val userPstmt = dbConn.con.prepareStatement("INSERT INTO users(email, hash) VALUES (?,?)")
|
||||||
|
|
||||||
def getProvider(): String = {
|
def getProvider(): String = {
|
||||||
val random = new scala.util.Random
|
val random = new scala.util.Random
|
||||||
val keys = providers.keys
|
val keys = providers.keys
|
||||||
@ -57,12 +73,6 @@ class Captcha(throttle: Int) extends DBConn {
|
|||||||
imageOpt
|
imageOpt
|
||||||
}
|
}
|
||||||
|
|
||||||
def generateChallengeSamples() = {
|
|
||||||
providers.map {case (key, provider) =>
|
|
||||||
(key, provider.returnChallenge())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private val uniqueIntCount = new AtomicInteger()
|
private val uniqueIntCount = new AtomicInteger()
|
||||||
|
|
||||||
def generateChallenge(param: Parameters): String = {
|
def generateChallenge(param: Parameters): String = {
|
||||||
@ -167,8 +177,9 @@ class Captcha(throttle: Int) extends DBConn {
|
|||||||
|
|
||||||
object LCFramework{
|
object LCFramework{
|
||||||
def main(args: scala.Array[String]) {
|
def main(args: scala.Array[String]) {
|
||||||
val captcha = new Captcha(2)
|
val dbConn = new DBConn()
|
||||||
val server = new Server(8888)
|
val captcha = new Captcha(2, dbConn)
|
||||||
|
val server = new Server(8888, captcha, dbConn)
|
||||||
captcha.beginThread(2)
|
captcha.beginThread(2)
|
||||||
server.start()
|
server.start()
|
||||||
}
|
}
|
||||||
@ -176,8 +187,7 @@ object LCFramework{
|
|||||||
|
|
||||||
object MakeSamples {
|
object MakeSamples {
|
||||||
def main(args: scala.Array[String]) {
|
def main(args: scala.Array[String]) {
|
||||||
val captcha = new Captcha(2)
|
val samples = CaptchaProviders.generateChallengeSamples()
|
||||||
val samples = captcha.generateChallengeSamples()
|
|
||||||
samples.foreach {case (key, sample) =>
|
samples.foreach {case (key, sample) =>
|
||||||
val extensionMap = Map("image/png" -> "png", "image/gif" -> "gif")
|
val extensionMap = Map("image/png" -> "png", "image/gif" -> "gif")
|
||||||
println(key + ": " + sample)
|
println(key + ": " + sample)
|
||||||
|
@ -9,13 +9,15 @@ import lc.HTTPServer._
|
|||||||
|
|
||||||
case class Secret(token: Int)
|
case class Secret(token: Int)
|
||||||
|
|
||||||
class RateLimiter extends DBConn {
|
class RateLimiter(dbConn: DBConn) {
|
||||||
private val userLastActive = collection.mutable.Map[Int, Long]()
|
private val userLastActive = collection.mutable.Map[Int, Long]()
|
||||||
private val userAllowance = collection.mutable.Map[Int, Double]()
|
private val userAllowance = collection.mutable.Map[Int, Double]()
|
||||||
private val rate = 800000.0
|
private val rate = 800000.0
|
||||||
private val per = 45.0
|
private val per = 45.0
|
||||||
private val allowance = rate
|
private val allowance = rate
|
||||||
|
|
||||||
|
private val validatePstmt = dbConn.con.prepareStatement("SELECT hash FROM users WHERE hash = ? LIMIT 1")
|
||||||
|
|
||||||
private def validateUser(user: Int) : Boolean = {
|
private def validateUser(user: Int) : Boolean = {
|
||||||
val allow = if(userLastActive.contains(user)){
|
val allow = if(userLastActive.contains(user)){
|
||||||
true
|
true
|
||||||
@ -61,9 +63,8 @@ class RateLimiter extends DBConn {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Server(port: Int){
|
class Server(port: Int, captcha: Captcha, dbConn: DBConn){
|
||||||
val captcha = new Captcha(0)
|
val rateLimiter = new RateLimiter(dbConn)
|
||||||
val rateLimiter = new RateLimiter()
|
|
||||||
val server = new HTTPServer(port)
|
val server = new HTTPServer(port)
|
||||||
val host = server.getVirtualHost(null)
|
val host = server.getVirtualHost(null)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user