From cde17ce482aa1e6eedff0305c8841e1bc901af97 Mon Sep 17 00:00:00 2001 From: samwaf Date: Fri, 11 Jul 2025 13:43:01 +0800 Subject: [PATCH] feat:capjs custom content #399 --- exedata/capjs/capversion | 1 + exedata/capjs/index.html | 642 ++++++++++++++-------------- exedata/capjs/version | 2 +- model/hosts.go | 16 + wafenginecore/wafcaptcha/service.go | 46 +- 5 files changed, 394 insertions(+), 313 deletions(-) create mode 100644 exedata/capjs/capversion diff --git a/exedata/capjs/capversion b/exedata/capjs/capversion new file mode 100644 index 0000000..99d85ec --- /dev/null +++ b/exedata/capjs/capversion @@ -0,0 +1 @@ +0.0.6 \ No newline at end of file diff --git a/exedata/capjs/index.html b/exedata/capjs/index.html index 9f5b9ff..a1f5ad8 100644 --- a/exedata/capjs/index.html +++ b/exedata/capjs/index.html @@ -1,335 +1,357 @@ - - - - - - - 进行验证 + + + + + + + 进行验证 - + + + + +
+ +
+

安全验证

+

为了确保您的访问安全,请完成以下验证

+
+ + +
+ + + - - - - - + } catch (error) { + console.error('Validation error:', error); + // 显示错误toast + showToast(langManager.getText('validationError') + ': ' + error.message, 'error'); + } + } + + \ No newline at end of file diff --git a/exedata/capjs/version b/exedata/capjs/version index 99d85ec..7f20734 100644 --- a/exedata/capjs/version +++ b/exedata/capjs/version @@ -1 +1 @@ -0.0.6 \ No newline at end of file +1.0.1 \ No newline at end of file diff --git a/model/hosts.go b/model/hosts.go index 2113971..d0626b7 100644 --- a/model/hosts.go +++ b/model/hosts.go @@ -77,6 +77,14 @@ type CaptchaConfig struct { ChallengeSize int `json:"challengeSize,omitempty"` // Size of each challenge in bytes (default: 32) ChallengeDifficulty int `json:"challengeDifficulty,omitempty"` // Difficulty level (default: 4) ExpiresMs int `json:"expiresMs,omitempty"` // Expiration time in milliseconds (default: 600000) + InfoTitle struct { + En string `json:"en,omitempty"` // English title + Zh string `json:"zh,omitempty"` // Chinese title + } `json:"infoTitle,omitempty"` // Multi-language info title + InfoText struct { + En string `json:"en,omitempty"` // English text + Zh string `json:"zh,omitempty"` // Chinese text + } `json:"infoText,omitempty"` // Multi-language info text } `json:"cap_js_config"` } @@ -97,6 +105,14 @@ func ParseCaptchaConfig(captchaJSON string) CaptchaConfig { config.CapJsConfig.ChallengeDifficulty = 4 // 默认难度级别4 config.CapJsConfig.ExpiresMs = 600000 // 默认过期时间600秒(10分钟) + // 初始化InfoTitle默认值 + config.CapJsConfig.InfoTitle.Zh = "安全验证" + config.CapJsConfig.InfoTitle.En = "Security Verification" + + // 初始化InfoText默认值 + config.CapJsConfig.InfoText.Zh = "为了确保您的访问安全,请完成以下验证" + config.CapJsConfig.InfoText.En = "To ensure the security of your access, please complete the following verification" + // 如果JSON不为空,则解析覆盖默认值 if captchaJSON != "" { err := json.Unmarshal([]byte(captchaJSON), &config) diff --git a/wafenginecore/wafcaptcha/service.go b/wafenginecore/wafcaptcha/service.go index d23e614..f2d4166 100644 --- a/wafenginecore/wafcaptcha/service.go +++ b/wafenginecore/wafcaptcha/service.go @@ -19,6 +19,7 @@ import ( "github.com/wenlng/go-captcha/v2/base/option" "github.com/wenlng/go-captcha/v2/click" "go.uber.org/zap" + "io/ioutil" "log" "net/http" "os" @@ -512,8 +513,49 @@ func (s *CaptchaService) ShowCaptchaHomePage(w http.ResponseWriter, r *http.Requ // 从指定目录加载index.html http.ServeFile(w, r, utils.GetCurrentDir()+"/data/captcha/index.html") } else if configStruct.EngineType == "capJs" { - // 从指定目录加载index.html - http.ServeFile(w, r, utils.GetCurrentDir()+"/data/capjs/index.html") + // 读取HTML模板文件 + htmlPath := utils.GetCurrentDir() + "/data/capjs/index.html" + htmlContent, err := ioutil.ReadFile(htmlPath) + if err != nil { + http.Error(w, "Failed to load page", http.StatusInternalServerError) + return + } + + // 准备替换的数据 + htmlStr := string(htmlContent) + + // 替换中文提示信息 + zhInfoTitle := configStruct.CapJsConfig.InfoTitle.Zh + zhInfoText := configStruct.CapJsConfig.InfoText.Zh + if zhInfoTitle == "" { + zhInfoTitle = "安全验证" + } + if zhInfoText == "" { + zhInfoText = "为了确保您的访问安全,请完成以下验证" + } + + // 替换英文提示信息 + enInfoTitle := configStruct.CapJsConfig.InfoTitle.En + enInfoText := configStruct.CapJsConfig.InfoText.En + if enInfoTitle == "" { + enInfoTitle = "Security Verification" + } + if enInfoText == "" { + enInfoText = "To ensure the security of your access, please complete the following verification" + } + + // 使用strings.Replace替换HTML中的静态文本 + htmlStr = strings.Replace(htmlStr, "infoTitle: '安全验证',", fmt.Sprintf("infoTitle: '%s',", zhInfoTitle), 1) + htmlStr = strings.Replace(htmlStr, "infoText: '为了确保您的访问安全,请完成以下验证',", fmt.Sprintf("infoText: '%s',", zhInfoText), 1) + htmlStr = strings.Replace(htmlStr, "infoTitle: 'Security Verification',", fmt.Sprintf("infoTitle: '%s',", enInfoTitle), 1) + htmlStr = strings.Replace(htmlStr, "infoText: 'To ensure the security of your access, please complete the following verification',", fmt.Sprintf("infoText: '%s',", enInfoText), 1) + + // 同时替换HTML中的默认显示文本 + htmlStr = strings.Replace(htmlStr, "

安全验证

", fmt.Sprintf("

%s

", zhInfoTitle), 1) + htmlStr = strings.Replace(htmlStr, "

为了确保您的访问安全,请完成以下验证

", fmt.Sprintf("

%s

", zhInfoText), 1) + + // 输出修改后的HTML + w.Write([]byte(htmlStr)) } }