diff --git a/server/internal/config/server.go b/server/internal/config/server.go index 99be15f4..ac76e87b 100644 --- a/server/internal/config/server.go +++ b/server/internal/config/server.go @@ -2,12 +2,11 @@ package config import ( "path" + "slices" "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" - - "github.com/m1k1o/neko/server/pkg/utils" ) type Server struct { @@ -121,8 +120,7 @@ func (s *Server) Set() { s.Metrics = viper.GetBool("server.metrics") s.CORS = viper.GetStringSlice("server.cors") - in, _ := utils.ArrayIn("*", s.CORS) - if len(s.CORS) == 0 || in { + if len(s.CORS) == 0 || slices.Contains(s.CORS, "*") { s.CORS = []string{"*"} } } @@ -162,8 +160,7 @@ func (s *Server) SetV2() { } if viper.IsSet("cors") { s.CORS = viper.GetStringSlice("cors") - in, _ := utils.ArrayIn("*", s.CORS) - if len(s.CORS) == 0 || in { + if len(s.CORS) == 0 || slices.Contains(s.CORS, "*") { s.CORS = []string{"*"} } log.Warn().Msg("you are using v2 configuration 'NEKO_CORS' which is deprecated, please use 'NEKO_SERVER_CORS' instead") @@ -182,12 +179,12 @@ func (s *Server) HasCors() bool { } func (s *Server) AllowOrigin(origin string) bool { - // if CORS is disabled, allow all origins - if len(s.CORS) == 0 { + // if CORS is not configured, or a wildcard is present, + // allow WebSocket connections from any origin + if len(s.CORS) == 0 || s.CORS[0] == "*" { return true } - // if CORS is enabled, allow only origins in the list - in, _ := utils.ArrayIn(origin, s.CORS) - return in || s.CORS[0] == "*" + // allow only origins in the list + return slices.Contains(s.CORS, origin) } diff --git a/server/internal/http/batch.go b/server/internal/http/batch.go index 6e008f5f..b7d813dd 100644 --- a/server/internal/http/batch.go +++ b/server/internal/http/batch.go @@ -5,6 +5,7 @@ import ( "encoding/json" "io" "net/http" + "slices" "strings" "github.com/m1k1o/neko/server/pkg/types" @@ -55,7 +56,7 @@ func (b *batchHandler) Handle(w http.ResponseWriter, r *http.Request) error { continue } - if exists, _ := utils.ArrayIn(request.Path, b.Excluded); exists { + if slices.Contains(b.Excluded, request.Path) { res.Error(utils.HttpBadRequest("this path is excluded from batch requests")) responses[i] = res continue diff --git a/server/internal/session/manager.go b/server/internal/session/manager.go index a638595b..67cf14bb 100644 --- a/server/internal/session/manager.go +++ b/server/internal/session/manager.go @@ -2,6 +2,7 @@ package session import ( "errors" + "slices" "sync" "sync/atomic" "time" @@ -303,7 +304,7 @@ func (manager *SessionManagerCtx) Broadcast(event string, payload any, exclude . } if len(exclude) > 0 { - if in, _ := utils.ArrayIn(session.ID(), exclude); in { + if slices.Contains(exclude, session.ID()) { continue } } @@ -319,7 +320,7 @@ func (manager *SessionManagerCtx) AdminBroadcast(event string, payload any, excl } if len(exclude) > 0 { - if in, _ := utils.ArrayIn(session.ID(), exclude); in { + if slices.Contains(exclude, session.ID()) { continue } } @@ -335,7 +336,7 @@ func (manager *SessionManagerCtx) InactiveCursorsBroadcast(event string, payload } if len(exclude) > 0 { - if in, _ := utils.ArrayIn(session.ID(), exclude); in { + if slices.Contains(exclude, session.ID()) { continue } } diff --git a/server/internal/websocket/manager.go b/server/internal/websocket/manager.go index 9fd67fab..e62bba33 100644 --- a/server/internal/websocket/manager.go +++ b/server/internal/websocket/manager.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "net/http" + "slices" "sync" "time" @@ -333,7 +334,7 @@ func (manager *WebSocketManagerCtx) handle(connection *websocket.Conn, peer type } // log events if not ignored - if ok, _ := utils.ArrayIn(data.Event, nologEvents); !ok { + if !slices.Contains(nologEvents, data.Event) { payload := data.Payload if len(payload) > maxPayloadLogLength { payload = []byte("") diff --git a/server/internal/websocket/peer.go b/server/internal/websocket/peer.go index 0c2fd553..f14e54d4 100644 --- a/server/internal/websocket/peer.go +++ b/server/internal/websocket/peer.go @@ -3,6 +3,7 @@ package websocket import ( "encoding/json" "errors" + "slices" "sync" "github.com/gorilla/websocket" @@ -11,7 +12,6 @@ import ( "github.com/m1k1o/neko/server/pkg/types" "github.com/m1k1o/neko/server/pkg/types/event" "github.com/m1k1o/neko/server/pkg/types/message" - "github.com/m1k1o/neko/server/pkg/utils" ) type WebSocketPeerCtx struct { @@ -51,7 +51,7 @@ func (peer *WebSocketPeerCtx) Send(event string, payload any) { } // log events if not ignored - if ok, _ := utils.ArrayIn(event, nologEvents); !ok { + if !slices.Contains(nologEvents, event) { if len(raw) > maxPayloadLogLength { raw = []byte("") } diff --git a/server/pkg/utils/array.go b/server/pkg/utils/array.go deleted file mode 100644 index a84853be..00000000 --- a/server/pkg/utils/array.go +++ /dev/null @@ -1,14 +0,0 @@ -package utils - -func ArrayIn[T comparable](val T, array []T) (exists bool, index int) { - exists, index = false, -1 - - for i, a := range array { - if a == val { - exists, index = true, i - return - } - } - - return -}