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
+2 -1
View File
@@ -209,7 +209,8 @@ func formatConsoleEntry(entry zapcore.Entry, module string, fields []zapcore.Fie
caller := ""
if entry.Caller.Defined {
caller = filepath.Base(entry.Caller.File) + fmt.Sprintf(":%d", entry.Caller.Line)
callerPath := strings.ReplaceAll(entry.Caller.File, "\\", "/")
caller = filepath.Base(callerPath) + fmt.Sprintf(":%d", entry.Caller.Line)
}
if module == "" {
module = "app"
+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 {
+33
View File
@@ -71,4 +71,37 @@ func TestWebSocketRouteRequiresAndAcceptsLoginToken(t *testing.T) {
}
}
func TestWebSocketRouteSupportsRouterPrefix(t *testing.T) {
store := runtimeconfig.NewStore()
raw, err := json.Marshal(map[string]any{"path": "/ws"})
if err != nil {
t.Fatal(err)
}
store.Set(runtimeconfig.Config{Kind: "websocket", Provider: websocketintegration.ProviderMelody, Enabled: true, Values: raw})
ws, cleanup, err := websocketintegration.New(store)
if err != nil {
t.Fatal(err)
}
defer cleanup()
auth := &websocketAuthStub{claims: &system.AuthClaims{ID: 7}}
runtime := config.NewStore(&config.Config{Admin: &config.Admin{RouterPrefix: "/admin"}})
engine := NewGinEngineWithRuntime(runtime, nil, auth, nil, nil, nil, "test", nil, ws)
httpServer := httptest.NewServer(engine)
defer httpServer.Close()
wsURL := "ws" + strings.TrimPrefix(httpServer.URL, "http") + "/admin/ws?token=login-token"
connection, response, err := gorillawebsocket.DefaultDialer.Dial(wsURL, nil)
if response != nil && response.Body != nil {
_ = response.Body.Close()
}
if err != nil {
t.Fatalf("prefixed websocket handshake failed: %v", err)
}
if connection == nil {
t.Fatal("prefixed websocket handshake returned nil connection")
}
_ = connection.Close()
}
var _ platformws.Hub = (*websocketintegration.Server)(nil)