mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-04-19 02:05:17 -04:00
simplified check for user access
1. Make fields private as much as possible 2. single public function to check if user is valid and is within limit. Advantage is that only a single call to synchronisation() is required. 3. Bumped up the rate limit
This commit is contained in:
parent
f08f6041ec
commit
7bfde4eddb
@ -10,14 +10,13 @@ import lc.HTTPServer._
|
|||||||
case class Secret(token: Int)
|
case class Secret(token: Int)
|
||||||
|
|
||||||
class RateLimiter extends DBConn {
|
class RateLimiter extends DBConn {
|
||||||
val userLastActive = collection.mutable.Map[Int, Long]()
|
private val userLastActive = collection.mutable.Map[Int, Long]()
|
||||||
val userAllowance = collection.mutable.Map[Int, Double]()
|
private val userAllowance = collection.mutable.Map[Int, Double]()
|
||||||
val rate = 8.0
|
private val rate = 800000.0
|
||||||
val per = 45.0
|
private val per = 45.0
|
||||||
val allowance = rate
|
private val allowance = rate
|
||||||
|
|
||||||
def validateUser(user: Int) : Boolean = {
|
private def validateUser(user: Int) : Boolean = {
|
||||||
synchronized {
|
|
||||||
val allow = if(userLastActive.contains(user)){
|
val allow = if(userLastActive.contains(user)){
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
@ -35,10 +34,8 @@ class RateLimiter extends DBConn {
|
|||||||
}
|
}
|
||||||
allow
|
allow
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
def checkLimit(user: Int): Boolean = {
|
private def checkLimit(user: Int): Boolean = {
|
||||||
synchronized {
|
|
||||||
val current = System.currentTimeMillis()
|
val current = System.currentTimeMillis()
|
||||||
val time_passed = (current - userLastActive(user)) / 1000
|
val time_passed = (current - userLastActive(user)) / 1000
|
||||||
userLastActive(user) = current
|
userLastActive(user) = current
|
||||||
@ -52,8 +49,16 @@ class RateLimiter extends DBConn {
|
|||||||
}
|
}
|
||||||
allow
|
allow
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
def checkUserAccess(token: Int) : Boolean = {
|
||||||
|
synchronized {
|
||||||
|
if (validateUser(token)) {
|
||||||
|
return checkLimit(token)
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Server(port: Int){
|
class Server(port: Int){
|
||||||
@ -66,7 +71,7 @@ class Server(port: Int){
|
|||||||
|
|
||||||
host.addContext("/v1/captcha",(req, resp) => {
|
host.addContext("/v1/captcha",(req, resp) => {
|
||||||
val accessToken = Option(req.getHeaders().get("access-token")).map(_.toInt)
|
val accessToken = Option(req.getHeaders().get("access-token")).map(_.toInt)
|
||||||
val access = accessToken.map(t => rateLimiter.validateUser(t) && rateLimiter.checkLimit(t)).getOrElse(false)
|
val access = accessToken.map(rateLimiter.checkUserAccess).getOrElse(false)
|
||||||
if(access){
|
if(access){
|
||||||
val body = req.getJson()
|
val body = req.getJson()
|
||||||
val json = parse(body)
|
val json = parse(body)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user