mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-01-12 22:43:20 -05:00
Merge pull request #102 from JacobPozaic/master
CORS configuration and disable playground
This commit is contained in:
commit
928fc786cf
@ -16,7 +16,7 @@ object LCFramework {
|
|||||||
val captcha = new Captcha(config = config, captchaProviders = captchaProviders)
|
val captcha = new Captcha(config = config, captchaProviders = captchaProviders)
|
||||||
val backgroundTask = new BackgroundTask(config = config, captcha = captcha)
|
val backgroundTask = new BackgroundTask(config = config, captcha = captcha)
|
||||||
backgroundTask.beginThread(delay = config.threadDelay)
|
backgroundTask.beginThread(delay = config.threadDelay)
|
||||||
val server = new Server(address = config.address, port = config.port, captcha = captcha)
|
val server = new Server(address = config.address, port = config.port, captcha = captcha, playgroundEnabled = config.playgroundEnabled, corsHeader = config.corsHeader)
|
||||||
server.start()
|
server.start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,8 @@ object AttributesEnum extends Enumeration {
|
|||||||
val CAPTCHA_EXPIRY_TIME_LIMIT: Value = Value("captchaExpiryTimeLimit")
|
val CAPTCHA_EXPIRY_TIME_LIMIT: Value = Value("captchaExpiryTimeLimit")
|
||||||
val THROTTLE: Value = Value("throttle")
|
val THROTTLE: Value = Value("throttle")
|
||||||
val THREAD_DELAY: Value = Value("threadDelay")
|
val THREAD_DELAY: Value = Value("threadDelay")
|
||||||
|
val PLAYGROUND_ENABLED: Value = Value("playgroundEnabled")
|
||||||
|
val CORS_HEADER: Value = Value("corsHeader")
|
||||||
val CONFIG: Value = Value("config")
|
val CONFIG: Value = Value("config")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +45,8 @@ class Config(configFilePath: String) {
|
|||||||
val seed: Int = (configJson \ AttributesEnum.RANDOM_SEED.toString).extract[Int]
|
val seed: Int = (configJson \ AttributesEnum.RANDOM_SEED.toString).extract[Int]
|
||||||
val captchaExpiryTimeLimit: Int = (configJson \ AttributesEnum.CAPTCHA_EXPIRY_TIME_LIMIT.toString).extract[Int]
|
val captchaExpiryTimeLimit: Int = (configJson \ AttributesEnum.CAPTCHA_EXPIRY_TIME_LIMIT.toString).extract[Int]
|
||||||
val threadDelay: Int = (configJson \ AttributesEnum.THREAD_DELAY.toString).extract[Int]
|
val threadDelay: Int = (configJson \ AttributesEnum.THREAD_DELAY.toString).extract[Int]
|
||||||
|
val playgroundEnabled: Boolean = (configJson \ AttributesEnum.PLAYGROUND_ENABLED.toString).extract[Boolean]
|
||||||
|
val corsHeader: String = (configJson \ AttributesEnum.CORS_HEADER.toString).extract[String]
|
||||||
|
|
||||||
private val captchaConfigJson = (configJson \ "captchas")
|
private val captchaConfigJson = (configJson \ "captchas")
|
||||||
val captchaConfigTransform: JValue = captchaConfigJson transformField {
|
val captchaConfigTransform: JValue = captchaConfigJson transformField {
|
||||||
@ -65,6 +67,8 @@ class Config(configFilePath: String) {
|
|||||||
(AttributesEnum.CAPTCHA_EXPIRY_TIME_LIMIT.toString -> 5) ~
|
(AttributesEnum.CAPTCHA_EXPIRY_TIME_LIMIT.toString -> 5) ~
|
||||||
(AttributesEnum.THROTTLE.toString -> 1000) ~
|
(AttributesEnum.THROTTLE.toString -> 1000) ~
|
||||||
(AttributesEnum.THREAD_DELAY.toString -> 2) ~
|
(AttributesEnum.THREAD_DELAY.toString -> 2) ~
|
||||||
|
(AttributesEnum.PLAYGROUND_ENABLED.toString -> true) ~
|
||||||
|
(AttributesEnum.CORS_HEADER.toString -> "") ~
|
||||||
("captchas" -> List(
|
("captchas" -> List(
|
||||||
(
|
(
|
||||||
(AttributesEnum.NAME.toString -> "FilterChallenge") ~
|
(AttributesEnum.NAME.toString -> "FilterChallenge") ~
|
||||||
|
@ -3,16 +3,21 @@ package lc.server
|
|||||||
import org.json4s.jackson.JsonMethods.parse
|
import org.json4s.jackson.JsonMethods.parse
|
||||||
import lc.core.Captcha
|
import lc.core.Captcha
|
||||||
import lc.core.ErrorMessageEnum
|
import lc.core.ErrorMessageEnum
|
||||||
import lc.core.{Parameters, Id, Answer, Error, ByteConvert}
|
import lc.core.{Answer, ByteConvert, Error, Id, Parameters}
|
||||||
import lc.core.Config.formats
|
import lc.core.Config.formats
|
||||||
import org.limium.picoserve
|
import org.limium.picoserve
|
||||||
import org.limium.picoserve.Server.ByteResponse
|
import org.limium.picoserve.Server.{ByteResponse, ServerBuilder, StringResponse}
|
||||||
import scala.io.Source
|
import scala.io.Source
|
||||||
import org.limium.picoserve.Server.StringResponse
|
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
|
import java.util
|
||||||
|
import scala.jdk.CollectionConverters._
|
||||||
|
|
||||||
class Server(address: String, port: Int, captcha: Captcha) {
|
class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Boolean, corsHeader: String) {
|
||||||
val server: picoserve.Server = picoserve.Server
|
var headerMap: util.Map[String, util.List[String]] = _
|
||||||
|
if( corsHeader.nonEmpty ) {
|
||||||
|
headerMap = Map("Access-Control-Allow-Origin" -> List(corsHeader).asJava).asJava
|
||||||
|
}
|
||||||
|
val serverBuilder: ServerBuilder = picoserve.Server
|
||||||
.builder()
|
.builder()
|
||||||
.address(new InetSocketAddress(address, port))
|
.address(new InetSocketAddress(address, port))
|
||||||
.backlog(32)
|
.backlog(32)
|
||||||
@ -22,7 +27,7 @@ class Server(address: String, port: Int, captcha: Captcha) {
|
|||||||
val json = parse(request.getBodyString())
|
val json = parse(request.getBodyString())
|
||||||
val param = json.extract[Parameters]
|
val param = json.extract[Parameters]
|
||||||
val id = captcha.getChallenge(param)
|
val id = captcha.getChallenge(param)
|
||||||
getResponse(id)
|
getResponse(id, headerMap)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.GET(
|
.GET(
|
||||||
@ -36,7 +41,7 @@ class Server(address: String, port: Int, captcha: Captcha) {
|
|||||||
} else {
|
} else {
|
||||||
Left(Error(ErrorMessageEnum.INVALID_PARAM.toString + "=> id"))
|
Left(Error(ErrorMessageEnum.INVALID_PARAM.toString + "=> id"))
|
||||||
}
|
}
|
||||||
getResponse(result)
|
getResponse(result, headerMap)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.POST(
|
.POST(
|
||||||
@ -45,10 +50,11 @@ class Server(address: String, port: Int, captcha: Captcha) {
|
|||||||
val json = parse(request.getBodyString())
|
val json = parse(request.getBodyString())
|
||||||
val answer = json.extract[Answer]
|
val answer = json.extract[Answer]
|
||||||
val result = captcha.checkAnswer(answer)
|
val result = captcha.checkAnswer(answer)
|
||||||
getResponse(result)
|
getResponse(result, headerMap)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.GET(
|
if( playgroundEnabled ) {
|
||||||
|
serverBuilder.GET(
|
||||||
"/demo/index.html",
|
"/demo/index.html",
|
||||||
(_) => {
|
(_) => {
|
||||||
val resStream = getClass().getResourceAsStream("/index.html")
|
val resStream = getClass().getResourceAsStream("/index.html")
|
||||||
@ -56,15 +62,17 @@ class Server(address: String, port: Int, captcha: Captcha) {
|
|||||||
new StringResponse(200, str)
|
new StringResponse(200, str)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.build()
|
}
|
||||||
|
|
||||||
private def getResponse(response: Either[Error, ByteConvert]): ByteResponse = {
|
val server: picoserve.Server = serverBuilder.build()
|
||||||
|
|
||||||
|
private def getResponse(response: Either[Error, ByteConvert], responseHeaders: util.Map[String, util.List[String]]): ByteResponse = {
|
||||||
response match {
|
response match {
|
||||||
case Right(value) => {
|
case Right(value) => {
|
||||||
new ByteResponse(200, value.toBytes())
|
new ByteResponse(200, value.toBytes(), responseHeaders)
|
||||||
}
|
}
|
||||||
case Left(value) => {
|
case Left(value) => {
|
||||||
new ByteResponse(500, value.toBytes())
|
new ByteResponse(500, value.toBytes(), responseHeaders)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
"captchaExpiryTimeLimit" : 5,
|
"captchaExpiryTimeLimit" : 5,
|
||||||
"throttle" : 10,
|
"throttle" : 10,
|
||||||
"threadDelay" : 2,
|
"threadDelay" : 2,
|
||||||
|
"playgroundEnabled" : false,
|
||||||
|
"corsHeader" : "*",
|
||||||
"captchas" : [ {
|
"captchas" : [ {
|
||||||
"name" : "DebugCaptcha",
|
"name" : "DebugCaptcha",
|
||||||
"allowedLevels" : [ "debug" ],
|
"allowedLevels" : [ "debug" ],
|
||||||
|
Loading…
Reference in New Issue
Block a user