commit 7e008377371c632059fa7a967bd58ca5aa98939a Author: Fijxu Date: Thu Sep 5 21:36:44 2024 -0400 xd diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..86b560e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Fijxu + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d60ba5a --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# mcaptcha-backend-poc + +Just a little Proof of concept that I did to learn how to use mCaptcha diff --git a/config.example.yml b/config.example.yml new file mode 100644 index 0000000..6d3e36a --- /dev/null +++ b/config.example.yml @@ -0,0 +1,5 @@ +# https://mcaptcha.nadeko.net/sitekeys +mcaptchaSitekey: "" +# https://mcaptcha.nadeko.net/settings +mcaptchaAccountSecret: "" +mcaptchaInstance: "mcaptcha.nadeko.net" \ No newline at end of file diff --git a/shard.yml b/shard.yml new file mode 100644 index 0000000..ae8e984 --- /dev/null +++ b/shard.yml @@ -0,0 +1,17 @@ +name: mcaptcha-backend +version: 0.1.0 + +authors: + - Fijxu + +targets: + mcaptcha-backend: + main: src/mcaptcha-backend.cr + +dependencies: + kemal: + github: kemalcr/kemal + +crystal: '>= 1.13.1' + +license: MIT diff --git a/spec/mcaptcha-backend_spec.cr b/spec/mcaptcha-backend_spec.cr new file mode 100644 index 0000000..cbe4fc4 --- /dev/null +++ b/spec/mcaptcha-backend_spec.cr @@ -0,0 +1,9 @@ +require "./spec_helper" + +describe Mcaptcha::Backend do + # TODO: Write tests + + it "works" do + false.should eq(true) + end +end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr new file mode 100644 index 0000000..4e49b32 --- /dev/null +++ b/spec/spec_helper.cr @@ -0,0 +1,2 @@ +require "spec" +require "../src/mcaptcha-backend" diff --git a/src/config.cr b/src/config.cr new file mode 100644 index 0000000..29251c5 --- /dev/null +++ b/src/config.cr @@ -0,0 +1,16 @@ +require "yaml" + +class Config + include YAML::Serializable + + property mcaptchaSitekey : String + property mcaptchaAccountSecret : String + property mcaptchaInstance : String + + def self.load + config_file = "./config.yml" + config_yaml = File.read(config_file) + config = Config.from_yaml(config_yaml) + config + end +end diff --git a/src/mcaptcha-backend.cr b/src/mcaptcha-backend.cr new file mode 100644 index 0000000..8d87913 --- /dev/null +++ b/src/mcaptcha-backend.cr @@ -0,0 +1,48 @@ +require "http/client" +require "kemal" +require "json" +require "./config" + +CONFIG = Config.load + +allowed_ips = Array(String).new + +def ip_address(env) : String + begin + return env.request.headers.try &.["X-Forwarded-For"] + rescue + return env.request.remote_address.to_s.split(":").first + end +end + +get "/" do |env| + if !allowed_ips.includes?(ip_address(env)) + render "src/views/captcha.ecr" + else + render "src/views/main.html" + end +end + +post "/captcha" do |env| + # token = env.params.body["mcaptcha__token"] + token = env.params.json["token"] + data = JSON.build do |j| + j.object do + j.field "token", token + j.field "key", CONFIG.mcaptchaSitekey + j.field "secret", CONFIG.mcaptchaAccountSecret + end + end + req = HTTP::Client.post("https://#{CONFIG.mcaptchaInstance}/api/v1/pow/siteverify", + headers: HTTP::Headers{"Content-Type" => "application/json"}, + body: data) + + if req.success? + res = JSON.parse(req.body) + end + + allowed_ips << ip_address(env) + res +end + +Kemal.run diff --git a/src/views/captcha.ecr b/src/views/captcha.ecr new file mode 100644 index 0000000..c200c2d --- /dev/null +++ b/src/views/captcha.ecr @@ -0,0 +1,97 @@ + + + + + + + +
+ + + + + \ No newline at end of file diff --git a/src/views/main.html b/src/views/main.html new file mode 100644 index 0000000..5453e12 --- /dev/null +++ b/src/views/main.html @@ -0,0 +1 @@ +passed \ No newline at end of file