docs(gateway): gateway 剩余文件注释改中文(逐行 1:1,代码零字节改动)
主人 2026-10-03 拍板「注释一律中文」;gateway 至此整包注释中文化完成(本会话共 3 批: 已推库 16 个文件 + 本批剩余文件)。 校验:机械校验(非注释改动行)= 空;剥注释后代码文本逐行比对全等(不一致文件数 0); 全树 go test ./... 58 包 ok / 0 FAIL;交叉编译绿。
This commit is contained in:
1 parent
91e8d7a029
commit
074ffc4ebf
73 files changed
+2529
-2529
No files matched your search
@@ -5,14 +5,14 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// originChecker allows same-origin, same-host, and whitelisted WebSocket origins,
|
||||
// mirroring the CORS allow list. A "*" allows all.
|
||||
// originChecker 允许同源、同 host 以及白名单内的 WebSocket 来源,
|
||||
// 镜像 CORS 允许列表。"*" 允许全部。
|
||||
//
|
||||
// Same-host relaxation: when cyrene serves its own embedded frontend (production
|
||||
// mode), the page origin is exactly the gateway's Host (e.g. http://localhost:10942),
|
||||
// which is not in the dev-phase ALLOWED_ORIGINS list. We must let that through so a
|
||||
// user who changes PORT does not need to edit .env for the WS to connect. Only truly
|
||||
// cross-origin clients (external IM / dashboards on another origin) hit the allow-list.
|
||||
// 同 host 放宽:cyrene 提供自己的内嵌前端时(生产
|
||||
// 模式),页面来源恰好是网关的 Host(例如 http://localhost:10942),
|
||||
// 它不在开发期的 ALLOWED_ORIGINS 列表中。我们必须放行它,使
|
||||
// 改过 PORT 的用户不必为了 WS 能连上而编辑 .env。只有真正的
|
||||
// 跨源客户端(外部 IM / 另一来源上的看板)才会走允许列表。
|
||||
func originChecker(allowed map[string]bool, allowAll bool) func(*http.Request) bool {
|
||||
return func(r *http.Request) bool {
|
||||
origin := r.Header.Get("Origin")
|
||||
@@ -25,16 +25,16 @@ func originChecker(allowed map[string]bool, allowAll bool) func(*http.Request) b
|
||||
if allowed[origin] {
|
||||
return true
|
||||
}
|
||||
// Same-host: the Origin matches this request's Host (scheme://host:port == host[:port]).
|
||||
// 同 host:Origin 与该请求的 Host 匹配(scheme://host:port == host[:port])。
|
||||
return sameOriginAsHost(origin, r)
|
||||
}
|
||||
}
|
||||
|
||||
// sameOriginAsHost reports whether origin (a full URL origin string such as
|
||||
// "http://localhost:10942") corresponds to this request's Host header. It compares
|
||||
// the host[:port] portion with loopback equivalence: "localhost" and "127.0.0.1"
|
||||
// (and the IPv6 form) are treated as the same host, so serving the embedded frontend
|
||||
// on 127.0.0.1 while a client connects via localhost (or vice versa) still passes.
|
||||
// sameOriginAsHost 报告 origin(完整的 URL 来源串,例如
|
||||
// "http://localhost:10942")是否对应本请求的 Host 头。它比较
|
||||
// host[:port] 部分并做回环等价:"localhost" 与 "127.0.0.1"
|
||||
// (以及 IPv6 形式)被视为同一 host,因此在 127.0.0.1 上提供内嵌
|
||||
// 前端而客户端经 localhost 连接(或反之)仍然通过。
|
||||
func sameOriginAsHost(origin string, r *http.Request) bool {
|
||||
if origin == "" || r == nil {
|
||||
return false
|
||||
@@ -49,21 +49,21 @@ func sameOriginAsHost(origin string, r *http.Request) bool {
|
||||
return hostEquivalent(after, r.Host)
|
||||
}
|
||||
|
||||
// hostEquivalent compares two host[:port] strings, treating empty-port variants,
|
||||
// case, and loopback aliases as equal. It handles "localhost" vs "127.0.0.1" and
|
||||
// bracketed IPv6 loopback "[::1]".
|
||||
// hostEquivalent 比较两个 host[:port] 串,把空端口变体、
|
||||
// 大小写与回环别名都视为相等。它处理 "localhost" 与 "127.0.0.1"
|
||||
// 以及带括号的 IPv6 回环 "[::1]"。
|
||||
func hostEquivalent(a, b string) bool {
|
||||
na := normalizeHost(a)
|
||||
nb := normalizeHost(b)
|
||||
if na == nb {
|
||||
return true
|
||||
}
|
||||
// Loopback aliases: localhost, 127.0.0.1, [::1]
|
||||
// 回环别名:localhost、127.0.0.1、[::1]
|
||||
loop := map[string]bool{"localhost": true, "127.0.0.1": true, "[::1]": true, "::1": true, "127.0.0.2": true}
|
||||
return loop[na] && loop[nb]
|
||||
}
|
||||
|
||||
// normalizeHost strips the port (if present), lowercases, and trims brackets.
|
||||
// normalizeHost 剥掉端口(若有)、转小写并去掉括号。
|
||||
func normalizeHost(h string) string {
|
||||
h = strings.ToLower(strings.TrimSpace(h))
|
||||
if i := strings.LastIndex(h, ":"); i >= 0 && !strings.Contains(h[i:], "]") {
|
||||
|
||||
Reference in new issue
Block a user