mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-02 12:01:50 +08:00
feat: add English interface localization and fix preview crashes
This commit is contained in:
1 parent
a05a886d3d
commit
9ccaa81497
163 files changed
+17100
-6944
No files matched your search
+35
-26
@@ -126,11 +126,11 @@ func (s *Server) requireAuth(h http.Handler) http.Handler {
|
||||
}
|
||||
tok := extractToken(r)
|
||||
if tok == "" {
|
||||
writeErr(w, 401, "未授权")
|
||||
writeProductError(w, 401, "unauthorized", "未授权", nil)
|
||||
return
|
||||
}
|
||||
if !verifyJWT(tok, s.jwtKey) {
|
||||
writeErr(w, 401, "token 无效或已过期")
|
||||
writeProductError(w, 401, "token_expired", "token 无效或已过期", nil)
|
||||
return
|
||||
}
|
||||
h.ServeHTTP(w, r)
|
||||
@@ -148,7 +148,7 @@ func (s *Server) authStatus(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
hash, _, err := pg.GetSetting(authPassKey)
|
||||
if err != nil {
|
||||
writeErr(w, 503, errDataSourceUnavailable)
|
||||
writeProductError(w, 503, "data_unavailable", errDataSourceUnavailable, nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{"initialized": hash != ""})
|
||||
@@ -162,27 +162,28 @@ func (s *Server) authInit(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
existing, _, err := pg.GetSetting(authPassKey)
|
||||
if err != nil {
|
||||
writeErr(w, 503, errDataSourceUnavailable)
|
||||
writeProductError(w, 503, "data_unavailable", errDataSourceUnavailable, nil)
|
||||
return
|
||||
}
|
||||
if existing != "" {
|
||||
writeErr(w, 403, "密码已设置")
|
||||
writeProductError(w, 403, "password_already_set", "密码已设置", nil)
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
if err := decode(r, &req); err != nil || req.Password == "" {
|
||||
writeErr(w, 400, "密码不能为空")
|
||||
writeProductError(w, 400, "password_required", "密码不能为空", nil)
|
||||
return
|
||||
}
|
||||
if msg := validatePassword(req.Password); msg != "" {
|
||||
writeErr(w, 400, msg)
|
||||
code, params := passwordValidationCode(req.Password)
|
||||
writeProductError(w, 400, code, msg, params)
|
||||
return
|
||||
}
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
writeErr(w, 500, "密码加密失败")
|
||||
writeProductError(w, 500, "password_hash_failed", "密码加密失败", nil)
|
||||
return
|
||||
}
|
||||
// 用 INSERT ... ON CONFLICT DO NOTHING 而不是 upsert:上面那次 GetSetting 只是
|
||||
@@ -190,16 +191,16 @@ func (s *Server) authInit(w http.ResponseWriter, r *http.Request) {
|
||||
// 这期间别的请求完全可能先把密码设好,而读检查本身也可能因故障而失效。
|
||||
inserted, err := pg.InsertSettingIfAbsent(authPassKey, string(hash))
|
||||
if err != nil {
|
||||
writeErr(w, 500, "保存失败: "+err.Error())
|
||||
writeProductError(w, 500, "save_failed", "保存失败: "+err.Error(), nil)
|
||||
return
|
||||
}
|
||||
if !inserted {
|
||||
writeErr(w, 403, "密码已设置")
|
||||
writeProductError(w, 403, "password_already_set", "密码已设置", nil)
|
||||
return
|
||||
}
|
||||
tok, err := signJWT(s.jwtKey)
|
||||
if err != nil {
|
||||
writeErr(w, 500, "token 生成失败")
|
||||
writeProductError(w, 500, "token_failed", "token 生成失败", nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{"token": tok})
|
||||
@@ -214,7 +215,7 @@ func (s *Server) authChangePassword(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
if !verifyJWT(extractToken(r), s.jwtKey) {
|
||||
writeErr(w, 401, "未授权")
|
||||
writeProductError(w, 401, "unauthorized", "未授权", nil)
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
@@ -222,37 +223,38 @@ func (s *Server) authChangePassword(w http.ResponseWriter, r *http.Request) {
|
||||
NewPassword string `json:"new_password"`
|
||||
}
|
||||
if err := decode(r, &req); err != nil {
|
||||
writeErr(w, 400, "请求格式错误")
|
||||
writeProductError(w, 400, "invalid_request", "请求格式错误", nil)
|
||||
return
|
||||
}
|
||||
if req.NewPassword == "" {
|
||||
writeErr(w, 400, "新密码不能为空")
|
||||
writeProductError(w, 400, "new_password_required", "新密码不能为空", nil)
|
||||
return
|
||||
}
|
||||
if msg := validatePassword(req.NewPassword); msg != "" {
|
||||
writeErr(w, 400, msg)
|
||||
code, params := passwordValidationCode(req.NewPassword)
|
||||
writeProductError(w, 400, code, msg, params)
|
||||
return
|
||||
}
|
||||
hash, ok, err := pg.GetSetting(authPassKey)
|
||||
if err != nil {
|
||||
writeErr(w, 503, errDataSourceUnavailable)
|
||||
writeProductError(w, 503, "data_unavailable", errDataSourceUnavailable, nil)
|
||||
return
|
||||
}
|
||||
if !ok || hash == "" {
|
||||
writeErr(w, 403, "密码未初始化,请先设置密码")
|
||||
writeProductError(w, 403, "password_not_initialized", "密码未初始化,请先设置密码", nil)
|
||||
return
|
||||
}
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(req.OldPassword)); err != nil {
|
||||
writeErr(w, 401, "当前密码错误")
|
||||
writeProductError(w, 401, "current_password_incorrect", "当前密码错误", nil)
|
||||
return
|
||||
}
|
||||
newHash, err := bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
writeErr(w, 500, "密码加密失败")
|
||||
writeProductError(w, 500, "password_hash_failed", "密码加密失败", nil)
|
||||
return
|
||||
}
|
||||
if err := pg.SetSetting(authPassKey, string(newHash)); err != nil {
|
||||
writeErr(w, 500, "保存失败: "+err.Error())
|
||||
writeProductError(w, 500, "save_failed", "保存失败: "+err.Error(), nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{"ok": true})
|
||||
@@ -269,30 +271,37 @@ func (s *Server) authLogin(w http.ResponseWriter, r *http.Request) {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
if err := decode(r, &req); err != nil {
|
||||
writeErr(w, 400, "请求格式错误")
|
||||
writeProductError(w, 400, "invalid_request", "请求格式错误", nil)
|
||||
return
|
||||
}
|
||||
if req.Username != "ARTEX" {
|
||||
writeErr(w, 401, "用户名或密码错误")
|
||||
writeProductError(w, 401, "invalid_credentials", "用户名或密码错误", nil)
|
||||
return
|
||||
}
|
||||
hash, ok, err := pg.GetSetting(authPassKey)
|
||||
if err != nil {
|
||||
writeErr(w, 503, errDataSourceUnavailable)
|
||||
writeProductError(w, 503, "data_unavailable", errDataSourceUnavailable, nil)
|
||||
return
|
||||
}
|
||||
if !ok || hash == "" {
|
||||
writeErr(w, 403, "密码未初始化,请先设置密码")
|
||||
writeProductError(w, 403, "password_not_initialized", "密码未初始化,请先设置密码", nil)
|
||||
return
|
||||
}
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(req.Password)); err != nil {
|
||||
writeErr(w, 401, "用户名或密码错误")
|
||||
writeProductError(w, 401, "invalid_credentials", "用户名或密码错误", nil)
|
||||
return
|
||||
}
|
||||
tok, err := signJWT(s.jwtKey)
|
||||
if err != nil {
|
||||
writeErr(w, 500, "token 生成失败")
|
||||
writeProductError(w, 500, "token_failed", "token 生成失败", nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{"token": tok})
|
||||
}
|
||||
|
||||
func passwordValidationCode(password string) (string, map[string]any) {
|
||||
if utf8.RuneCountInString(password) < minPasswordRunes {
|
||||
return "password_too_short", map[string]any{"min": minPasswordRunes}
|
||||
}
|
||||
return "password_too_long", map[string]any{"max": maxPasswordBytes}
|
||||
}
|
||||
+11
-10
@@ -140,13 +140,13 @@ func (s *Server) requireBasicAuth(next http.Handler) http.Handler {
|
||||
if !ok || len(password) > maxPasswordBytes || username != cfg.Username ||
|
||||
bcrypt.CompareHashAndPassword([]byte(cfg.PasswordHash), []byte(password)) != nil {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted", charset="UTF-8"`)
|
||||
writeErr(w, http.StatusUnauthorized, "请先完成 HTTP Basic Auth 验证")
|
||||
writeProductError(w, http.StatusUnauthorized, "basic_auth_required", "请先完成 HTTP Basic Auth 验证", nil)
|
||||
return
|
||||
}
|
||||
// Browser API requests already use Authorization: Bearer. A separate
|
||||
// HttpOnly cookie keeps the gate satisfied without replacing that header.
|
||||
if err := s.setBasicAuthCookie(w, r, cfg); err != nil {
|
||||
writeErr(w, 500, "认证会话生成失败")
|
||||
writeProductError(w, 500, "auth_session_failed", "认证会话生成失败", nil)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
@@ -169,7 +169,7 @@ func (s *Server) saveBasicAuthSettings(w http.ResponseWriter, r *http.Request) {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
if err := decode(r, &req); err != nil {
|
||||
writeErr(w, 400, "请求格式错误")
|
||||
writeProductError(w, 400, "invalid_request", "请求格式错误", nil)
|
||||
return
|
||||
}
|
||||
g := &s.basicAuth
|
||||
@@ -179,7 +179,7 @@ func (s *Server) saveBasicAuthSettings(w http.ResponseWriter, r *http.Request) {
|
||||
cfg, store := g.cfg, g.store
|
||||
g.mu.RUnlock()
|
||||
if store == nil {
|
||||
writeErr(w, 503, errDataSourceUnavailable)
|
||||
writeProductError(w, 503, "data_unavailable", errDataSourceUnavailable, nil)
|
||||
return
|
||||
}
|
||||
if req.Enabled != nil {
|
||||
@@ -189,23 +189,24 @@ func (s *Server) saveBasicAuthSettings(w http.ResponseWriter, r *http.Request) {
|
||||
cfg.Username = strings.TrimSpace(*req.Username)
|
||||
}
|
||||
if (cfg.Enabled || cfg.Username != "") && !validBasicUsername(cfg.Username) {
|
||||
writeErr(w, 400, "Basic Auth 用户名不能为空、超过 128 字节或包含冒号及控制字符")
|
||||
writeProductError(w, 400, "basic_auth_username_invalid", "Basic Auth 用户名不能为空、超过 128 字节或包含冒号及控制字符", nil)
|
||||
return
|
||||
}
|
||||
if req.Password != "" {
|
||||
if msg := validatePassword(req.Password); msg != "" {
|
||||
writeErr(w, 400, msg)
|
||||
code, params := passwordValidationCode(req.Password)
|
||||
writeProductError(w, 400, code, msg, params)
|
||||
return
|
||||
}
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
writeErr(w, 500, "密码加密失败")
|
||||
writeProductError(w, 500, "password_hash_failed", "密码加密失败", nil)
|
||||
return
|
||||
}
|
||||
cfg.PasswordHash = string(hash)
|
||||
}
|
||||
if cfg.Enabled && cfg.PasswordHash == "" {
|
||||
writeErr(w, 400, "请先设置 Basic Auth 密码")
|
||||
writeProductError(w, 400, "basic_auth_password_required", "请先设置 Basic Auth 密码", nil)
|
||||
return
|
||||
}
|
||||
// Every save revokes previous gate cookies, including disable/re-enable.
|
||||
@@ -214,7 +215,7 @@ func (s *Server) saveBasicAuthSettings(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), basicAuthSaveTimeout)
|
||||
defer cancel()
|
||||
if err != nil || store.SetSettingContext(ctx, basicAuthKey, string(data)) != nil {
|
||||
writeErr(w, 503, "Basic Auth 配置保存失败,请重试")
|
||||
writeProductError(w, 503, "basic_auth_save_failed", "Basic Auth 配置保存失败,请重试", nil)
|
||||
return
|
||||
}
|
||||
// Hashing, persistence and response output never hold the request mutex.
|
||||
@@ -226,7 +227,7 @@ func (s *Server) saveBasicAuthSettings(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
// Keep the administrator who just saved the settings in their session.
|
||||
if err := s.setBasicAuthCookie(w, r, cfg); err != nil {
|
||||
writeErr(w, 500, "认证会话生成失败")
|
||||
writeProductError(w, 500, "auth_session_failed", "认证会话生成失败", nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, basicAuthPublicSettings(cfg))
|
||||
|
||||
@@ -17,10 +17,12 @@ const maxChatMentions = 10
|
||||
|
||||
// The visible token survives drafts, uploads, retries and conversation history.
|
||||
// Labels are only for display: the server trusts only the type and numeric ID.
|
||||
var chatMentionPattern = regexp.MustCompile(`@\[(漏洞|资产|企业|接口|IP|应用|域名|子域名|服务)#([0-9]+)(?: [^\]\r\n]*)?\]`)
|
||||
var chatMentionPattern = regexp.MustCompile(`@\[(finding|asset|company|endpoint|ip|app|root_domain|subdomain|service|漏洞|资产|企业|接口|IP|应用|域名|子域名|服务)#([0-9]+)(?: [^\]\r\n]*)?\]`)
|
||||
var chatMentionKinds = map[string]string{
|
||||
"漏洞": "finding", "资产": "asset", "企业": "company", "接口": "endpoint",
|
||||
"IP": "ip", "应用": "app", "域名": "root_domain", "子域名": "subdomain", "服务": "service",
|
||||
"finding": "finding", "asset": "asset", "company": "company", "endpoint": "endpoint",
|
||||
"ip": "ip", "app": "app", "root_domain": "root_domain", "subdomain": "subdomain", "service": "service",
|
||||
}
|
||||
|
||||
type chatMentionRef struct {
|
||||
|
||||
@@ -349,3 +349,30 @@ func TestChatMentionConversationReceivesServerDetails(t *testing.T) {
|
||||
t.Fatal("invalid reference persisted a turn")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatMentionStableAndLegacyTokens(t *testing.T) {
|
||||
kinds := []string{"finding", "asset", "company", "endpoint", "ip", "app", "root_domain", "subdomain", "service"}
|
||||
legacy := []string{"漏洞", "资产", "企业", "接口", "IP", "应用", "域名", "子域名", "服务"}
|
||||
for i, kind := range kinds {
|
||||
message := fmt.Sprintf("@[%s#%d original name] @[%s#%d 旧名称]", kind, i+1, legacy[i], i+1)
|
||||
refs, err := parseChatMentions(message)
|
||||
if err != nil || len(refs) != 1 || refs[0].Kind != kind || refs[0].ID != int64(i+1) {
|
||||
t.Fatalf("%s: refs=%+v err=%v", kind, refs, err)
|
||||
}
|
||||
}
|
||||
for _, input := range []string{"@[finding#0]", "@[endpoint#999999999999999999999999]"} {
|
||||
if _, err := parseChatMentions(input); err == nil {
|
||||
t.Fatalf("accepted invalid token %s", input)
|
||||
}
|
||||
}
|
||||
var many strings.Builder
|
||||
for i := 1; i <= 11; i++ {
|
||||
fmt.Fprintf(&many, "@[asset#%d] ", i)
|
||||
}
|
||||
if _, err := parseChatMentions(many.String()); err == nil {
|
||||
t.Fatal("accepted too many stable references")
|
||||
}
|
||||
if refs, err := parseChatMentions("@[unknown#1]"); err != nil || len(refs) != 0 {
|
||||
t.Fatalf("accepted unknown kind: %+v %v", refs, err)
|
||||
}
|
||||
}
|
||||
+30
-13
@@ -167,17 +167,19 @@ type egressCheck struct {
|
||||
Path string `json:"path"` // 实际使用的代理地址(空 = 直连)
|
||||
Skipped bool `json:"skipped,omitempty"`
|
||||
SkipReason string `json:"skip_reason,omitempty"`
|
||||
SkipCode string `json:"skip_code,omitempty"`
|
||||
Result egressProbeResult `json:"result"`
|
||||
}
|
||||
|
||||
// egressReport 是 POST /api/settings/proxy/test 的返回体。
|
||||
type egressReport struct {
|
||||
OK bool `json:"ok"` // 以"② 经全局代理"为准;未配代理时看"① 直连"
|
||||
Capture bool `json:"capture"` // 当前是否开启流量捕获
|
||||
Configured string `json:"configured"` // 表单传上来的 global_proxy
|
||||
AgentProxy string `json:"agent_proxy"` // ProxyAddr() 的实际返回值
|
||||
Checks []egressCheck `json:"checks"`
|
||||
Warnings []string `json:"warnings,omitempty"`
|
||||
OK bool `json:"ok"` // 以"② 经全局代理"为准;未配代理时看"① 直连"
|
||||
Capture bool `json:"capture"` // 当前是否开启流量捕获
|
||||
Configured string `json:"configured"` // 表单传上来的 global_proxy
|
||||
AgentProxy string `json:"agent_proxy"` // ProxyAddr() 的实际返回值
|
||||
Checks []egressCheck `json:"checks"`
|
||||
Warnings []string `json:"warnings,omitempty"`
|
||||
WarningDetails []productMessage `json:"warning_details,omitempty"`
|
||||
}
|
||||
|
||||
// probeEgress 不碰数据库、不碰 Server,所以测试里可以直接调、完全离线。
|
||||
@@ -430,21 +432,25 @@ func (s *Server) testEgressProxy(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
} else {
|
||||
skipReason := "输入框为空,未配置代理"
|
||||
skipCode := "no_proxy"
|
||||
if savedProxy != "" {
|
||||
// 说"未配置代理"会和 W1("Agent 仍使用 xxx")互相否定。
|
||||
skipReason = "输入框为空;已保存的代理仍在生效"
|
||||
skipCode = "saved_proxy_active"
|
||||
}
|
||||
rep.Checks = append(rep.Checks, egressCheck{
|
||||
Key: "global",
|
||||
Label: "② 经全局代理的出口 IP",
|
||||
Skipped: true,
|
||||
SkipReason: skipReason,
|
||||
SkipCode: skipCode,
|
||||
Result: egressProbeResult{Probes: []egressAttempt{}},
|
||||
})
|
||||
}
|
||||
|
||||
rep.OK = judgeEgress(rep)
|
||||
rep.Warnings = egressWarnings(rep, pending, savedProxy)
|
||||
rep.WarningDetails = egressWarningDetails(rep, pending, savedProxy)
|
||||
if req.Resume && rep.OK && !pending {
|
||||
// The navbar uses the same probe endpoint for display-only polling but
|
||||
// omits resume. Only an explicit settings-page test can release the
|
||||
@@ -477,18 +483,29 @@ func judgeEgress(rep egressReport) bool {
|
||||
// egressWarnings 生成"看数字容易忽略"的提醒。
|
||||
func egressWarnings(rep egressReport, pending bool, savedProxy string) []string {
|
||||
var out []string
|
||||
for _, notice := range egressWarningDetails(rep, pending, savedProxy) {
|
||||
out = append(out, notice.Message)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func egressWarningDetails(rep egressReport, pending bool, savedProxy string) []productMessage {
|
||||
var out []productMessage
|
||||
add := func(code string, params map[string]any, message string) {
|
||||
out = append(out, productMessage{Code: code, Params: params, Message: message})
|
||||
}
|
||||
safeSavedProxy := maskSecrets(savedProxy)
|
||||
|
||||
// W1 agent 走的是已保存配置,不说清会被当成"测通了=配好了"。
|
||||
if pending {
|
||||
switch {
|
||||
case rep.Configured == "":
|
||||
out = append(out, "此地址尚未保存,Agent 当前仍使用 "+safeSavedProxy+"。保存后即改为直连。")
|
||||
add("pending_direct", map[string]any{"proxy": safeSavedProxy}, "此地址尚未保存,Agent 当前仍使用 "+safeSavedProxy+"。保存后即改为直连。")
|
||||
case savedProxy == "":
|
||||
out = append(out, "此地址尚未保存,Agent 当前仍为直连。"+
|
||||
add("pending_proxy", nil, "此地址尚未保存,Agent 当前仍为直连。"+
|
||||
"上方结果对应输入框中的值,保存后生效。")
|
||||
default:
|
||||
out = append(out, "此地址尚未保存,Agent 当前仍使用 "+safeSavedProxy+
|
||||
add("pending_change", map[string]any{"proxy": safeSavedProxy}, "此地址尚未保存,Agent 当前仍使用 "+safeSavedProxy+
|
||||
"。上方结果对应输入框中的值,保存后生效。")
|
||||
}
|
||||
}
|
||||
@@ -509,26 +526,26 @@ func egressWarnings(rep egressReport, pending bool, savedProxy string) []string
|
||||
|
||||
// W2 看已保存值;只在 ② 通过时提示——没通的话"这里通不代表"本身就不成立。
|
||||
if !rep.Capture && proxyOK && strings.HasPrefix(strings.ToLower(savedProxy), "socks5") {
|
||||
out = append(out, "当前是「关闭捕获 + socks5」:本测试用的是 Go 客户端(一定遵守代理),"+
|
||||
add("socks_without_capture", nil, "当前是「关闭捕获 + socks5」:本测试用的是 Go 客户端(一定遵守代理),"+
|
||||
"但 agent 的工具不一定——curl 能走,python urllib 会直接报错。"+
|
||||
"所以这里通不代表工具都能走;要稳定生效请开启流量捕获(由 MITM 亲自拨号)。")
|
||||
}
|
||||
|
||||
// W3 两行相同 → 代理可能没生效,或对端只是透明转发。
|
||||
if directIP != "" && proxyIP != "" && directIP == proxyIP {
|
||||
out = append(out, fmt.Sprintf("直连与代理的出口 IP 相同(%s):可能代理未生效、只是透明转发,"+
|
||||
add("same_ip", map[string]any{"ip": directIP}, fmt.Sprintf("直连与代理的出口 IP 相同(%s):可能代理未生效、只是透明转发,"+
|
||||
"也可能跳板机与本机在同一 NAT 后面(出口本来就相同)。"+
|
||||
"要区分:在「自定义 IP 探测服务」里填一台内网回显服务,看它看到的源 IP 是否变成了跳板机。", directIP))
|
||||
}
|
||||
|
||||
// W4 同样看已保存值,否则会和 W1 自相矛盾。
|
||||
if rep.Capture && savedProxy == "" {
|
||||
out = append(out, "已开启流量捕获,但全局代理为空:捕获只负责记录,出口仍是直连。")
|
||||
add("capture_direct", nil, "已开启流量捕获,但全局代理为空:捕获只负责记录,出口仍是直连。")
|
||||
}
|
||||
|
||||
// W5 容器里的 127.0.0.1 指容器自己,连不到宿主机上的代理。
|
||||
if isLoopbackProxy(rep.Configured) {
|
||||
out = append(out, "代理地址是回环地址(127.0.0.1 / localhost)。"+
|
||||
add("loopback_proxy", nil, "代理地址是回环地址(127.0.0.1 / localhost)。"+
|
||||
"如果 ARTEX 跑在容器里,这里的 127.0.0.1 指容器自身,连不到宿主机上的代理;"+
|
||||
"请改用宿主机的可达 IP(如 http://192.168.x.x:端口)。")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package server
|
||||
|
||||
import "net/http"
|
||||
|
||||
// productMessage keeps the original detail alongside a stable translation key.
|
||||
type productMessage struct {
|
||||
Code string `json:"code"`
|
||||
Params map[string]any `json:"params,omitempty"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// Preserve error for existing clients and add stable fields for localized UIs.
|
||||
func writeProductError(w http.ResponseWriter, status int, code, message string, params map[string]any) {
|
||||
payload := map[string]any{"error": message, "code": code}
|
||||
if len(params) > 0 {
|
||||
payload["params"] = params
|
||||
}
|
||||
writeJSON(w, status, payload)
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestProductErrorPreservesLegacyMessage(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
writeProductError(w, 400, "password_too_short", "密码长度至少 8 位", map[string]any{"min": 8})
|
||||
var payload struct {
|
||||
Error string `json:"error"`
|
||||
Code string `json:"code"`
|
||||
Params map[string]int `json:"params"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w.Code != 400 || payload.Error != "密码长度至少 8 位" || payload.Code != "password_too_short" || payload.Params["min"] != 8 {
|
||||
t.Fatalf("unexpected localized error: %d %+v", w.Code, payload)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPasswordValidationCodes(t *testing.T) {
|
||||
for _, item := range []struct {
|
||||
password, code, param string
|
||||
value int
|
||||
}{
|
||||
{"短密码", "password_too_short", "min", 8},
|
||||
{"这是一个超过七十二字节上限的密码这是一个超过七十二字节上限的密码", "password_too_long", "max", 72},
|
||||
} {
|
||||
if validatePassword(item.password) == "" {
|
||||
t.Fatalf("expected invalid password for %s", item.code)
|
||||
}
|
||||
code, params := passwordValidationCode(item.password)
|
||||
if code != item.code || params[item.param] != item.value {
|
||||
t.Fatalf("code=%s params=%v", code, params)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBasicAuthErrorsIncludeTranslationCodes(t *testing.T) {
|
||||
s, _ := basicAuthTestServer(t)
|
||||
for _, tc := range []struct{ body, code string }{
|
||||
{`not-json`, "invalid_request"},
|
||||
{`{"enabled":true}`, "basic_auth_username_invalid"},
|
||||
{`{"username":"bad:name"}`, "basic_auth_username_invalid"},
|
||||
{`{"username":"entry","password":"short"}`, "password_too_short"},
|
||||
{`{"enabled":true,"username":"entry"}`, "basic_auth_password_required"},
|
||||
} {
|
||||
w := saveBasicAuthForTest(s, tc.body)
|
||||
var payload struct {
|
||||
Error string `json:"error"`
|
||||
Code string `json:"code"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w.Code != 400 || payload.Code != tc.code || payload.Error == "" {
|
||||
t.Fatalf("body=%s status=%d payload=%+v", tc.body, w.Code, payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEgressWarningDetailsPreserveLegacyTextAndMaskCredentials(t *testing.T) {
|
||||
rep := egressReport{Configured: "http://new-proxy:8080"}
|
||||
legacy := egressWarnings(rep, true, "http://user:secret@old-proxy:8080")
|
||||
details := egressWarningDetails(rep, true, "http://user:secret@old-proxy:8080")
|
||||
if len(legacy) != len(details) || len(details) != 1 || details[0].Code != "pending_change" {
|
||||
t.Fatalf("legacy=%v details=%+v", legacy, details)
|
||||
}
|
||||
for i, notice := range details {
|
||||
if notice.Message != legacy[i] {
|
||||
t.Fatalf("legacy message changed: %+v", notice)
|
||||
}
|
||||
data, err := json.Marshal(notice)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(data), "secret") {
|
||||
t.Fatal("proxy credentials exposed in localized warning")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user