From 9e629d2e562aabd7b63de6e455a29a8dd3b10aad Mon Sep 17 00:00:00 2001 From: lightumcc Date: Sat, 27 Jul 2024 20:45:47 +0800 Subject: [PATCH] feat(recaptcha): Site Verify Endpoint Config --- captcha-google-v2/i18n/en_US.yaml | 5 +++++ captcha-google-v2/i18n/translation.go | 2 ++ captcha-google-v2/i18n/zh_CN.yaml | 5 +++++ captcha-google-v2/recaptcha.go | 18 +++++++++++++++++- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/captcha-google-v2/i18n/en_US.yaml b/captcha-google-v2/i18n/en_US.yaml index 58b41747..f2546329 100644 --- a/captcha-google-v2/i18n/en_US.yaml +++ b/captcha-google-v2/i18n/en_US.yaml @@ -34,6 +34,11 @@ plugin: other: Secret Key description: other: Get it from https://www.google.com/recaptcha/admin + site_verify_endpoint: + title: + other: Site Verify Endpoint + description: + other: If you can't access google.com, you can replace it with https://www.recaptcha.net/recaptcha/api/siteverify frontend: info: name: diff --git a/captcha-google-v2/i18n/translation.go b/captcha-google-v2/i18n/translation.go index b531c43f..7a84a0c9 100644 --- a/captcha-google-v2/i18n/translation.go +++ b/captcha-google-v2/i18n/translation.go @@ -26,4 +26,6 @@ const ( ConfigSiteKeyDescription = "plugin.google_v2_captcha.backend.config.site_key.description" ConfigSecretKeyTitle = "plugin.google_v2_captcha.backend.config.secret_key.title" ConfigSecretKeyDescription = "plugin.google_v2_captcha.backend.config.secret_key.description" + ConfigSiteVerifyEndpointTitle = "plugin.google_v2_captcha.backend.config.site_verify_endpoint.title" + ConfigSiteVerifyEndpointDescription = "plugin.google_v2_captcha.backend.config.site_verify_endpoint.description" ) diff --git a/captcha-google-v2/i18n/zh_CN.yaml b/captcha-google-v2/i18n/zh_CN.yaml index 58a9d530..08a91822 100644 --- a/captcha-google-v2/i18n/zh_CN.yaml +++ b/captcha-google-v2/i18n/zh_CN.yaml @@ -34,6 +34,11 @@ plugin: other: Secret Key description: other: 此密钥用于您的网站和 reCAPTCHA 之间的通信,从 https://www.google.com/recaptcha/admin 获取 + site_verify_endpoint: + title: + other: Site Verify API端点 + description: + other: 如果您无法访问google.com, 可以将其替换为 https://www.recaptcha.net/recaptcha/api/siteverify frontend: title: 验证码 placeholder: 输入上面的文本 diff --git a/captcha-google-v2/recaptcha.go b/captcha-google-v2/recaptcha.go index 127afe01..7083bdc1 100644 --- a/captcha-google-v2/recaptcha.go +++ b/captcha-google-v2/recaptcha.go @@ -44,6 +44,7 @@ type Captcha struct { type CaptchaConfig struct { SiteKey string `json:"site_key"` SecretKey string `json:"secret_key"` + SiteVerifyEndpoint string `json:"site_verify_endpoint"` } type GoogleCaptchaResponse struct { @@ -88,7 +89,11 @@ func (c *Captcha) Verify(captcha, userInput string) (pass bool) { } cli := &http.Client{} cli.Timeout = 10 * time.Second - resp, err := cli.PostForm("https://www.recaptcha.net/recaptcha/api/siteverify", map[string][]string{ + siteVerifyEndpoint := c.Config.SiteVerifyEndpoint + if siteVerifyEndpoint == "" { + siteVerifyEndpoint = "https://www.google.com/recaptcha/api/siteverify" + } + resp, err := cli.PostForm(siteVerifyEndpoint, map[string][]string{ "secret": {c.Config.SecretKey}, "response": {userInput}, }) @@ -135,6 +140,17 @@ func (c *Captcha) ConfigFields() []plugin.ConfigField { }, Value: c.Config.SecretKey, }, + { + Name: "site_verify_endpoint", + Type: plugin.ConfigTypeInput, + Title: plugin.MakeTranslator(i18n.ConfigSiteVerifyEndpointTitle), + Description: plugin.MakeTranslator(i18n.ConfigSiteVerifyEndpointDescription), + Required: false, + UIOptions: plugin.ConfigFieldUIOptions{ + InputType: plugin.InputTypeText, + }, + Value: c.Config.SiteVerifyEndpoint, + }, } }