Browse Source

feat: replace reCaptcha with Turnstile

Cat 3 years ago
parent
commit
2000219492

+ 8 - 8
config/settings.json

@@ -392,11 +392,11 @@
     {
         "id": null,
         "item": "captcha_provider",
-        "value": "recaptcha",
+        "value": "turnstile",
         "class": "captcha",
         "is_public": 1,
         "type": "string",
-        "default": "recaptcha",
+        "default": "turnstile",
         "mark": "验证码提供商"
     },
     {
@@ -431,23 +431,23 @@
     },
     {
         "id": null,
-        "item": "recaptcha_sitekey",
+        "item": "turnstile_sitekey",
         "value": "",
-        "class": "recaptcha",
+        "class": "turnstile",
         "is_public": 0,
         "type": "string",
         "default": "",
-        "mark": "reCaptcha网站密钥"
+        "mark": "Turnstile网站密钥"
     },
     {
         "id": null,
-        "item": "recaptcha_secret",
+        "item": "turnstile_secret",
         "value": "",
-        "class": "recaptcha",
+        "class": "turnstile",
         "is_public": 0,
         "type": "string",
         "default": "",
-        "mark": "reCaptcha密钥"
+        "mark": "Turnstile密钥"
     },
     {
         "id": null,

+ 15 - 15
resources/views/material/admin/setting.tpl

@@ -572,7 +572,7 @@
                                                 <a data-toggle="tab" href="#verification_code_public_settings">&nbsp;设置</a>
                                             </li>
                                             <li>
-                                                <a data-toggle="tab" href="#recaptcha">&nbsp;reCAPTCHA</a>
+                                                <a data-toggle="tab" href="#turnstile">&nbsp;Turnstile</a>
                                             </li>
                                             <li>
                                                 <a data-toggle="tab" href="#geetest">&nbsp;Geetest</a>
@@ -585,7 +585,7 @@
                                         <div class="form-group form-group-label">
                                             <label class="floating-label">验证码提供商</label>
                                             <select id="captcha_provider" class="form-control maxwidth-edit">
-                                                <option value="recaptcha" {if $settings['captcha_provider'] == "recaptcha"}selected{/if}>reCaptcha</option>
+                                                <option value="turnstile" {if $settings['captcha_provider'] == "turnstile"}selected{/if}>Turnstile</option>
                                                 <option value="geetest" {if $settings['captcha_provider'] == "geetest"}selected{/if}>Geetest</option>
                                             </select>
                                         </div>
@@ -616,20 +616,20 @@
 
                                         <button id="submit_verify_code" type="submit" class="btn btn-block btn-brand">提交</button>
                                     </div>
-                                    <div class="tab-pane fade" id="recaptcha">
-                                        <p class="form-control-guide"><i class="mdi mdi-information"></i>在 <a href="https://www.google.com/recaptcha/admin/create" target="view_window">https://www.google.com/recaptcha/admin/create</a> 申请,选择【reCAPTCHA 第 2 版】的子选项【进行人机身份验证复选框】</p>
-                                        <!-- recaptcha_sitekey -->
+                                    <div class="tab-pane fade" id="turnstile">
+                                        <p class="form-control-guide"><i class="mdi mdi-information"></i>在 <a href="https://developers.cloudflare.com/turnstile/get-started/#sitekey-and-secret-key" target="view_window">https://developers.cloudflare.com/turnstile/get-started/#sitekey-and-secret-key</a> 申请</p>
+                                        <!-- turnstile_sitekey -->
                                         <div class="form-group form-group-label">
-                                            <label class="floating-label">reCaptcha Site Key</label>
-                                            <input class="form-control maxwidth-edit" id="recaptcha_sitekey" value="{$settings['recaptcha_sitekey']}">
+                                            <label class="floating-label">Turnstile Site Key</label>
+                                            <input class="form-control maxwidth-edit" id="turnstile_sitekey" value="{$settings['turnstile_sitekey']}">
                                         </div>
-                                        <!-- recaptcha_secret -->
+                                        <!-- turnstile_secret -->
                                         <div class="form-group form-group-label">
-                                            <label class="floating-label">reCaptcha Secret</label>
-                                            <input class="form-control maxwidth-edit" id="recaptcha_secret" value="{$settings['recaptcha_secret']}">
+                                            <label class="floating-label">Turnstile Secret</label>
+                                            <input class="form-control maxwidth-edit" id="turnstile_secret" value="{$settings['turnstile_secret']}">
                                         </div>
 
-                                        <button id="submit_recaptcha" type="submit" class="btn btn-block btn-brand">提交</button>
+                                        <button id="submit_turnstile" type="submit" class="btn btn-block btn-brand">提交</button>
                                     </div>
                                     <div class="tab-pane fade" id="geetest">
                                         <p class="form-control-guide"><i class="mdi mdi-information"></i>在 <a href="https://gtaccount.geetest.com/sensebot/overview" target="view_window">https://gtaccount.geetest.com/sensebot/overview</a> 申请</p>
@@ -1421,15 +1421,15 @@
 
 <script>
     window.addEventListener('load', () => {
-        $$.getElementById('submit_recaptcha').addEventListener('click', () => {
+        $$.getElementById('submit_turnstile').addEventListener('click', () => {
             $.ajax({
                 type: "POST",
                 url: "/admin/setting",
                 dataType: "json",
                 data: {
-                    class: 'verify_code_recaptcha',
-                    recaptcha_sitekey: $$getValue('recaptcha_sitekey'),
-                    recaptcha_secret: $$getValue('recaptcha_secret')
+                    class: 'verify_code_turnstile',
+                    turnstile_sitekey: $$getValue('turnstile_sitekey'),
+                    turnstile_secret: $$getValue('turnstile_secret')
                 },
                 success: data => {
                     $("#result").modal();

+ 6 - 6
resources/views/material/auth/login.tpl

@@ -39,10 +39,10 @@
                         <div id="embed-captcha"></div>
                     </div>
                 {/if}
-                {if $config['enable_login_captcha'] == true && $config['captcha_provider'] == 'recaptcha'}
+                {if $config['enable_login_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
                     <div class="form-group-label auth-row">
                         <div class="row">
-                            <div align="center" class="g-recaptcha" data-sitekey="{$recaptcha_sitekey}"></div>
+                            <div align="center" class="cf-turnstile" data-sitekey="{$turnstile_sitekey}"></div>
                         </div>
                     </div>
                 {/if}
@@ -119,8 +119,8 @@
                 url: location.pathname,
                 dataType: "json",
                 data: {
-                    {if $config['enable_login_captcha'] == true && $config['captcha_provider'] == 'recaptcha'}
-                    recaptcha: grecaptcha.getResponse(),
+                    {if $config['enable_login_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+                    turnstile: turnstile.getResponse(),
                     {/if}
                     {if $geetest_html != null}
                     geetest_challenge: validate.geetest_challenge,
@@ -203,6 +203,6 @@
     </script>
 {/if}
 
-{if $config['enable_login_captcha'] == true && $config['captcha_provider'] == 'recaptcha'}
-    <script src="https://recaptcha.net/recaptcha/api.js" async defer></script>
+{if $config['enable_login_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+    <script src="https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha" async defer></script>
 {/if}

+ 6 - 6
resources/views/material/auth/register.tpl

@@ -118,10 +118,10 @@
                             </div>
                         </div>
                     {/if}
-                    {if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'recaptcha'}
+                    {if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
                         <div class="form-group form-group-label">
                             <div class="row">
-                                <div align="center" class="g-recaptcha" data-sitekey="{$recaptcha_sitekey}"></div>
+                                <div align="center" class="cf-turnstile" data-sitekey="{$turnstile_sitekey}"></div>
                             </div>
                         </div>
                     {/if}
@@ -260,8 +260,8 @@ document.getElementById('passwd').addEventListener('input', checkStrong);
                     url: location.pathname,
                     dataType: "json",
                     data: {
-                        {if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'recaptcha'}
-                        recaptcha: grecaptcha.getResponse(),
+                        {if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+                            turnstile: turnstile.getResponse(),
                         {/if}
                         {if $geetest_html != null}
                         geetest_challenge: validate.geetest_challenge,
@@ -426,8 +426,8 @@ document.getElementById('passwd').addEventListener('input', checkStrong);
     </script>
 {/if}
 
-{if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'recaptcha'}
-    <script src="https://recaptcha.net/recaptcha/api.js" async defer></script>
+{if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+    <script src="https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha" async defer></script>
 {/if}
 
 {*dumplin:aff链*}

+ 6 - 6
resources/views/material/user/index.tpl

@@ -215,8 +215,8 @@
                                 {if $geetest_html != null}
                                     <div id="popup-captcha"></div>
                                 {/if}
-                                {if $config['enable_checkin_captcha'] == true && $config['captcha_provider'] == 'recaptcha' && $user->isAbleToCheckin()}
-                                    <div class="g-recaptcha" data-sitekey="{$recaptcha_sitekey}"></div>
+                                {if $config['enable_checkin_captcha'] == true && $config['captcha_provider'] == 'turnstile' && $user->isAbleToCheckin()}
+                                    <div class="cf-turnstile" data-sitekey="{$turnstile_sitekey}"></div>
                                 {/if}
                                 <div class="card-action">
                                     <div class="usercheck pull-left">
@@ -734,9 +734,9 @@
                 type: "POST",
                 url: "/user/checkin",
                 dataType: "json",
-                {if $config['enable_checkin_captcha'] == true && $config['captcha_provider'] == 'recaptcha'}
+                {if $config['enable_checkin_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
                 data: {
-                    recaptcha: grecaptcha.getResponse()
+                    turnstile: turnstile.getResponse()
                 },
                 {/if}
                 success: (data) => {
@@ -813,6 +813,6 @@
     {/if}
 </script>
 
-{if $config['enable_checkin_captcha'] == true && $config['captcha_provider'] == 'recaptcha'}
-    <script src="https://recaptcha.net/recaptcha/api.js" async defer></script>
+{if $config['enable_checkin_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+    <script src="https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha" async defer></script>
 {/if}

+ 15 - 15
resources/views/tabler/admin/setting.tpl

@@ -572,7 +572,7 @@
                                                 <a data-toggle="tab" href="#verification_code_public_settings">&nbsp;设置</a>
                                             </li>
                                             <li>
-                                                <a data-toggle="tab" href="#recaptcha">&nbsp;reCAPTCHA</a>
+                                                <a data-toggle="tab" href="#turnstile">&nbsp;Turnstile</a>
                                             </li>
                                             <li>
                                                 <a data-toggle="tab" href="#geetest">&nbsp;Geetest</a>
@@ -585,7 +585,7 @@
                                         <div class="form-group form-group-label">
                                             <label class="floating-label">验证码提供商</label>
                                             <select id="captcha_provider" class="form-control maxwidth-edit">
-                                                <option value="recaptcha" {if $settings['captcha_provider'] == "recaptcha"}selected{/if}>reCaptcha</option>
+                                                <option value="turnstile" {if $settings['captcha_provider'] == "turnstile"}selected{/if}>Turnstile</option>
                                                 <option value="geetest" {if $settings['captcha_provider'] == "geetest"}selected{/if}>Geetest</option>
                                             </select>
                                         </div>
@@ -616,20 +616,20 @@
 
                                         <button id="submit_verify_code" type="submit" class="btn btn-block btn-brand">提交</button>
                                     </div>
-                                    <div class="tab-pane fade" id="recaptcha">
-                                        <p class="form-control-guide"><i class="mdi mdi-information"></i>在 <a href="https://www.google.com/recaptcha/admin/create" target="view_window">https://www.google.com/recaptcha/admin/create</a> 申请,选择【reCAPTCHA 第 2 版】的子选项【进行人机身份验证复选框】</p>
-                                        <!-- recaptcha_sitekey -->
+                                    <div class="tab-pane fade" id="turnstile">
+                                        <p class="form-control-guide"><i class="mdi mdi-information"></i>在 <a href="https://developers.cloudflare.com/turnstile/get-started/#sitekey-and-secret-key" target="view_window">https://developers.cloudflare.com/turnstile/get-started/#sitekey-and-secret-key</a> 申请</p>
+                                        <!-- turnstile_sitekey -->
                                         <div class="form-group form-group-label">
-                                            <label class="floating-label">reCaptcha Site Key</label>
-                                            <input class="form-control maxwidth-edit" id="recaptcha_sitekey" value="{$settings['recaptcha_sitekey']}">
+                                            <label class="floating-label">Turnstile Site Key</label>
+                                            <input class="form-control maxwidth-edit" id="turnstile_sitekey" value="{$settings['turnstile_sitekey']}">
                                         </div>
-                                        <!-- recaptcha_secret -->
+                                        <!-- turnstile_secret -->
                                         <div class="form-group form-group-label">
-                                            <label class="floating-label">reCaptcha Secret</label>
-                                            <input class="form-control maxwidth-edit" id="recaptcha_secret" value="{$settings['recaptcha_secret']}">
+                                            <label class="floating-label">Turnstile Secret</label>
+                                            <input class="form-control maxwidth-edit" id="turnstile_secret" value="{$settings['turnstile_secret']}">
                                         </div>
 
-                                        <button id="submit_recaptcha" type="submit" class="btn btn-block btn-brand">提交</button>
+                                        <button id="submit_turnstile" type="submit" class="btn btn-block btn-brand">提交</button>
                                     </div>
                                     <div class="tab-pane fade" id="geetest">
                                         <p class="form-control-guide"><i class="mdi mdi-information"></i>在 <a href="https://gtaccount.geetest.com/sensebot/overview" target="view_window">https://gtaccount.geetest.com/sensebot/overview</a> 申请</p>
@@ -1421,15 +1421,15 @@
 
 <script>
     window.addEventListener('load', () => {
-        $$.getElementById('submit_recaptcha').addEventListener('click', () => {
+        $$.getElementById('submit_turnstile').addEventListener('click', () => {
             $.ajax({
                 type: "POST",
                 url: "/admin/setting",
                 dataType: "json",
                 data: {
-                    class: 'verify_code_recaptcha',
-                    recaptcha_sitekey: $$getValue('recaptcha_sitekey'),
-                    recaptcha_secret: $$getValue('recaptcha_secret')
+                    class: 'verify_code_turnstile',
+                    turnstile_sitekey: $$getValue('turnstile_sitekey'),
+                    turnstile_secret: $$getValue('turnstile_secret')
                 },
                 success: data => {
                     $("#result").modal();

+ 6 - 6
resources/views/tabler/auth/login.tpl

@@ -39,10 +39,10 @@
                         <div id="embed-captcha"></div>
                     </div>
                 {/if}
-                {if $config['enable_login_captcha'] == true && $config['captcha_provider'] == 'recaptcha'}
+                {if $config['enable_login_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
                     <div class="form-group-label auth-row">
                         <div class="row">
-                            <div align="center" class="g-recaptcha" data-sitekey="{$recaptcha_sitekey}"></div>
+                            <div align="center" class="cf-turnstile" data-sitekey="{$turnstile_sitekey}"></div>
                         </div>
                     </div>
                 {/if}
@@ -119,8 +119,8 @@
                 url: location.pathname,
                 dataType: "json",
                 data: {
-                    {if $config['enable_login_captcha'] == true && $config['captcha_provider'] == 'recaptcha'}
-                    recaptcha: grecaptcha.getResponse(),
+                    {if $config['enable_login_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+                    turnstile: turnstile.getResponse(),
                     {/if}
                     {if $geetest_html != null}
                     geetest_challenge: validate.geetest_challenge,
@@ -203,6 +203,6 @@
     </script>
 {/if}
 
-{if $config['enable_login_captcha'] == true && $config['captcha_provider'] == 'recaptcha'}
-    <script src="https://recaptcha.net/recaptcha/api.js" async defer></script>
+{if $config['enable_login_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+    <script src="https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha" async defer></script>
 {/if}

+ 6 - 6
resources/views/tabler/auth/register.tpl

@@ -118,10 +118,10 @@
                             </div>
                         </div>
                     {/if}
-                    {if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'recaptcha'}
+                    {if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
                         <div class="form-group form-group-label">
                             <div class="row">
-                                <div align="center" class="g-recaptcha" data-sitekey="{$recaptcha_sitekey}"></div>
+                                <div align="center" class="cf-turnstile" data-sitekey="{$turnstile_sitekey}"></div>
                             </div>
                         </div>
                     {/if}
@@ -260,8 +260,8 @@ document.getElementById('passwd').addEventListener('input', checkStrong);
                     url: location.pathname,
                     dataType: "json",
                     data: {
-                        {if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'recaptcha'}
-                        recaptcha: grecaptcha.getResponse(),
+                        {if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+                        turnstile: turnstile.getResponse(),
                         {/if}
                         {if $geetest_html != null}
                         geetest_challenge: validate.geetest_challenge,
@@ -426,8 +426,8 @@ document.getElementById('passwd').addEventListener('input', checkStrong);
     </script>
 {/if}
 
-{if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'recaptcha'}
-    <script src="https://recaptcha.net/recaptcha/api.js" async defer></script>
+{if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+    <script src="https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha" async defer></script>
 {/if}
 
 {*dumplin:aff链*}

+ 9 - 2
resources/views/tabler/user/index.tpl

@@ -381,10 +381,13 @@
                             </div>
                             <div class="card-footer">
                                 <div class="d-flex">
+                                    {if $config['enable_checkin_captcha'] == true && $config['captcha_provider'] == 'turnstile' && $user->isAbleToCheckin()}
+                                    <div class="cf-turnstile" data-sitekey="{$turnstile_sitekey}"></div>
+                                    {/if}
                                     {if !$user->isAbleToCheckin()}
-                                        <button id="check-in" class="btn btn-primary ms-auto" disabled>已签到</button>
+                                    <button id="check-in" class="btn btn-primary ms-auto" disabled>已签到</button>
                                     {else}
-                                        <button id="check-in" class="btn btn-primary ms-auto">签到</button>
+                                    <button id="check-in" class="btn btn-primary ms-auto">签到</button>
                                     {/if}
                                 </div>
                             </div>
@@ -420,4 +423,8 @@
             })
         });
     </script>
+
+    {if $config['enable_checkin_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+    <script src="https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha" async defer></script>
+    {/if}
 {include file='user/tabler_footer.tpl'}

+ 2 - 2
src/Controllers/Admin/SettingController.php

@@ -81,8 +81,8 @@ final class SettingController extends BaseController
             case 'verify_code':
                 $list = ['captcha_provider', 'enable_reg_captcha', 'enable_login_captcha', 'enable_checkin_captcha'];
                 break;
-            case 'verify_code_recaptcha':
-                $list = ['recaptcha_sitekey', 'recaptcha_secret'];
+            case 'verify_code_turnstile':
+                $list = ['turnstile_sitekey', 'turnstile_secret'];
                 break;
             case 'verify_code_geetest':
                 $list = ['geetest_id', 'geetest_key'];

+ 3 - 3
src/Controllers/AuthController.php

@@ -57,7 +57,7 @@ final class AuthController extends BaseController
             ->assign('login_number', $login_number)
             ->assign('base_url', $_ENV['baseUrl'])
             ->assign('telegram_bot', $_ENV['telegram_bot'])
-            ->assign('recaptcha_sitekey', $captcha['recaptcha'])
+            ->assign('turnstile_sitekey', $captcha['turnstile'])
             ->display('auth/login.tpl');
     }
 
@@ -68,7 +68,7 @@ final class AuthController extends BaseController
     {
         $captcha = Captcha::generate();
         return $response->withJson([
-            'recaptchaKey' => $captcha['recaptcha'],
+            'turnstileKey' => $captcha['turnstile'],
             'GtSdk' => $captcha['geetest'],
             'respon' => 1,
         ]);
@@ -195,7 +195,7 @@ final class AuthController extends BaseController
             ->assign('login_number', $login_number)
             ->assign('geetest_html', $geetest_html)
             ->assign('telegram_bot', $_ENV['telegram_bot'])
-            ->assign('recaptcha_sitekey', $captcha['recaptcha'])
+            ->assign('turnstile_sitekey', $captcha['turnstile'])
             ->assign('enable_email_verify', Setting::obtain('reg_email_verify'))
             ->display('auth/register.tpl');
     }

+ 1 - 1
src/Controllers/UserController.php

@@ -94,7 +94,7 @@ final class UserController extends BaseController
                 ->assign('mergeSub', $_ENV['mergeSub'])
                 ->assign('subUrl', $_ENV['subUrl'] . '/link/')
                 ->registerClass('URL', URL::class)
-                ->assign('recaptcha_sitekey', $captcha['recaptcha'])
+                ->assign('turnstile_sitekey', $captcha['turnstile'])
                 ->assign('subInfo', LinkController::getSubinfo($this->user, 0))
                 ->assign('getUniversalSub', SubController::getUniversalSub($this->user))
                 ->assign('getClient', $token)

+ 8 - 8
src/Services/Captcha.php

@@ -12,11 +12,11 @@ final class Captcha
     public static function generate(): array
     {
         $geetest = null;
-        $recaptcha = null;
+        $turnstile = null;
 
         switch (Setting::obtain('captcha_provider')) {
-            case 'recaptcha':
-                $recaptcha = Setting::obtain('recaptcha_sitekey');
+            case 'turnstile':
+                $turnstile = Setting::obtain('turnstile_sitekey');
                 break;
             case 'geetest':
                 $geetest = Geetest::get(\time() . random_int(1, 10000));
@@ -25,7 +25,7 @@ final class Captcha
 
         return [
             'geetest' => $geetest,
-            'recaptcha' => $recaptcha,
+            'turnstile' => $turnstile,
         ];
     }
 
@@ -37,10 +37,10 @@ final class Captcha
         $result = false;
 
         switch (Setting::obtain('captcha_provider')) {
-            case 'recaptcha':
-                if (isset($param['recaptcha'])) {
-                    if ($param['recaptcha'] !== '') {
-                        $json = file_get_contents('https://recaptcha.net/recaptcha/api/siteverify?secret=' . Setting::obtain('recaptcha_secret') . '&response=' . $param['recaptcha']);
+            case 'turnstile':
+                if (isset($param['turnstile'])) {
+                    if ($param['turnstile'] !== '') {
+                        $json = file_get_contents('https://challenges.cloudflare.com/turnstile/v0/siteverify?secret=' . Setting::obtain('turnstile_secret') . '&response=' . $param['turnstile']);
                         $result = \json_decode($json)->success;
                     }
                 }