This commit is contained in:
Yvan
2026-09-04 10:51:45 +08:00
parent d94e2ed225
commit af1e194396
17 changed files with 1015 additions and 61 deletions
+15 -3
View File
@@ -65,10 +65,11 @@ func NewGinEngineWithRuntime(runtime *config.Store, access *systemservice.Access
handleWebSocket := func(c *gin.Context) {
wsLogger := logger
hasToken := middleware.RequestToken(c, true) != ""
if ws == nil || !ws.Enabled() || c.Request.URL.Path != ws.Path() {
expectedPath := websocketRoutePath(prefix, ws)
if ws == nil || !ws.Enabled() || c.Request.URL.Path != expectedPath {
if wsLogger != nil {
wsLogger.WarnContext(c.Request.Context(), "websocket handshake rejected: endpoint unavailable",
"mod", "websocket", "path", c.Request.URL.Path, "expected_path", websocketPath(ws),
"mod", "websocket", "path", c.Request.URL.Path, "expected_path", expectedPath,
"enabled", ws != nil && ws.Enabled(), "has_token", hasToken)
}
c.Status(http.StatusNotFound)
@@ -124,7 +125,7 @@ func NewGinEngineWithRuntime(runtime *config.Store, access *systemservice.Access
}
engine.NoRoute(func(c *gin.Context) {
if ws != nil && ws.Enabled() && c.Request.Method == http.MethodGet && c.Request.URL.Path == ws.Path() {
if ws != nil && ws.Enabled() && c.Request.Method == http.MethodGet && c.Request.URL.Path == websocketRoutePath(prefix, ws) {
handleWebSocket(c)
return
}
@@ -163,6 +164,17 @@ func websocketPath(ws *websocket.Server) string {
return ws.Path()
}
func websocketRoutePath(prefix string, ws *websocket.Server) string {
path := websocketPath(ws)
if path == "" {
return ""
}
if prefix == "" {
return path
}
return strings.TrimSuffix(prefix, "/") + "/" + strings.TrimPrefix(path, "/")
}
func NewGinServer(c *config.Server, engine *gin.Engine) *kratoshttp.Server {
network, address := "tcp", ":8000"
if c != nil && c.HTTP != nil {