5

【笔记】CloudflareTurnstile 学习笔记

 4 months ago
source link: https://loli.fj.cn/2024/04/29/CloudflareTurnstile%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【笔记】CloudflareTurnstile 学习笔记

2024-04-29 2024-04-30

2

CloudflareTurnstile 学习笔记

01.webp
  • 定义站点名称和域名 -> 创建
02.webp
  • 复制站点密钥密钥
03.webp
  • 发送请求给 Cloudflare 获取 token
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit"></script>
<div id="example-container">
<script>
turnstile.ready(function () {
turnstile.render('#example-container', {
sitekey: '站点密钥',
callback: function(token) {
console.log(`Challenge Success ${token}`);
},
});
});
</script>
  • 把从 Cloudflare 获取 token 发送请求给后端
POST http://localhost:8080/api
Content-Type: application/json

{username: "", password: "", token: "从Cloudflare获取的token"}
  • 发送请求给 Cloudflare 验证从前端获取的 token
POST https://challenges.cloudflare.com/turnstile/v0/siteverify
Content-Type: application/json

{secret: "密钥", response: "从前端获取的token"}
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)

type CloudflareTurnstileVerificationResponseEntity struct {
Success bool `json:"success"`
ErrorCodes []string `json:"error-codes"`
ChallengeTS string `json:"challenge_ts"`
Hostname string `json:"hostname"`
Action string `json:"action"`
Cdata string `json:"cdata"`
Messages []string `json:"messages"`
}

func CloudflareTurnstileVerification(token string) (err error, success bool) {

var url = "https://challenges.cloudflare.com/turnstile/v0/siteverify"
var secret = "密钥"

// 请求体参数
var requestPayloadMap map[string]string = map[string]string{
"secret": secret,
"response": token,
}
var requestPayloadString string
if res, err := json.Marshal(requestPayloadMap); err != nil {
return err, success
} else {
requestPayloadString = string(res)
}
// 发起请求获取响应
var responseEntity *http.Response
if res, err := http.Post(url, "application/json", strings.NewReader(requestPayloadString)); err != nil {
return err, success
} else {
responseEntity = res
}
defer responseEntity.Body.Close()
// 将响应体转换为文本
var responseText []byte
if res, err := ioutil.ReadAll(responseEntity.Body); err != nil {
return err, success
} else {
responseText = res
}
// 将JSON格式的响应体文本转换为响应体结构体
var responseObject CloudflareTurnstileVerificationResponseEntity
if err := json.Unmarshal(responseText, &responseObject); err != nil {
return err, success
}
if responseObject.Success == false {
var errorCodeListString strings.Builder
errorCodeListString.WriteString("\nErrorCodes:\n")
for i := 0; i < len(responseObject.ErrorCodes); i++ {
errorCodeListString.WriteString(fmt.Sprintf("%d:%s\n", i, responseObject.ErrorCodes[i]))
}
errorCodeListString.WriteString("\nMessages:\n")
for i := 0; i < len(responseObject.Messages); i++ {
errorCodeListString.WriteString(fmt.Sprintf("%d:%s\n", i, responseObject.Messages[i]))
}
return err, false
}

return err, true
}

CloudflareDocs
CloudflareDocs


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK