refactor: don't inherit classes from DBConn

This commit is contained in:
hrj 2020-07-16 17:53:26 +05:30
parent 7bfde4eddb
commit 9af66adf9f
3 changed files with 32 additions and 29 deletions

View File

@ -5,14 +5,6 @@ import java.sql._
class DBConn(){
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 = {
con.createStatement()
}

View File

@ -14,13 +14,7 @@ case class Answer(answer: String, id: String)
case class ProviderSecret(provider: String, secret: String)
class Captcha(throttle: Int) extends DBConn {
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)")
object CaptchaProviders {
val providers = Map(
"FilterChallenge" -> new FilterChallenge,
"FontFunCaptcha" -> new FontFunCaptcha,
@ -30,6 +24,28 @@ class Captcha(throttle: Int) extends DBConn {
"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 = {
val random = new scala.util.Random
val keys = providers.keys
@ -57,12 +73,6 @@ class Captcha(throttle: Int) extends DBConn {
imageOpt
}
def generateChallengeSamples() = {
providers.map {case (key, provider) =>
(key, provider.returnChallenge())
}
}
private val uniqueIntCount = new AtomicInteger()
def generateChallenge(param: Parameters): String = {
@ -167,8 +177,9 @@ class Captcha(throttle: Int) extends DBConn {
object LCFramework{
def main(args: scala.Array[String]) {
val captcha = new Captcha(2)
val server = new Server(8888)
val dbConn = new DBConn()
val captcha = new Captcha(2, dbConn)
val server = new Server(8888, captcha, dbConn)
captcha.beginThread(2)
server.start()
}
@ -176,8 +187,7 @@ object LCFramework{
object MakeSamples {
def main(args: scala.Array[String]) {
val captcha = new Captcha(2)
val samples = captcha.generateChallengeSamples()
val samples = CaptchaProviders.generateChallengeSamples()
samples.foreach {case (key, sample) =>
val extensionMap = Map("image/png" -> "png", "image/gif" -> "gif")
println(key + ": " + sample)

View File

@ -9,13 +9,15 @@ import lc.HTTPServer._
case class Secret(token: Int)
class RateLimiter extends DBConn {
class RateLimiter(dbConn: DBConn) {
private val userLastActive = collection.mutable.Map[Int, Long]()
private val userAllowance = collection.mutable.Map[Int, Double]()
private val rate = 800000.0
private val per = 45.0
private val allowance = rate
private val validatePstmt = dbConn.con.prepareStatement("SELECT hash FROM users WHERE hash = ? LIMIT 1")
private def validateUser(user: Int) : Boolean = {
val allow = if(userLastActive.contains(user)){
true
@ -61,9 +63,8 @@ class RateLimiter extends DBConn {
}
}
class Server(port: Int){
val captcha = new Captcha(0)
val rateLimiter = new RateLimiter()
class Server(port: Int, captcha: Captcha, dbConn: DBConn){
val rateLimiter = new RateLimiter(dbConn)
val server = new HTTPServer(port)
val host = server.getVirtualHost(null)