Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions server/internal/http/legacy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"

"github.com/m1k1o/neko/server/internal/api"
Expand Down Expand Up @@ -44,6 +45,7 @@ type LegacyHandler struct {
bannedIPs map[string]struct{}
sessionIPs map[string]string
wsDialer *websocket.Dialer
mu sync.Mutex
}

func New(serverAddr string) *LegacyHandler {
Expand Down Expand Up @@ -393,6 +395,8 @@ func (h *LegacyHandler) Route(r types.Router) {

func (h *LegacyHandler) ban(sessionId string) error {
// find session by id
h.mu.Lock()
defer h.mu.Unlock()
ip, ok := h.sessionIPs[sessionId]
if !ok {
return fmt.Errorf("session not found")
Expand All @@ -404,6 +408,9 @@ func (h *LegacyHandler) ban(sessionId string) error {

func (h *LegacyHandler) isBanned(r *http.Request) bool {
ip := getIp(r)
h.mu.Lock()
defer h.mu.Unlock()

_, ok := h.bannedIPs[ip]
return ok
}
Expand Down
4 changes: 4 additions & 0 deletions server/internal/http/legacy/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ func (s *session) create(username, password string) error {
}

s.id, s.ip = data.ID, getIp(s.r)
s.h.mu.Lock()
defer s.h.mu.Unlock()
s.h.sessionIPs[s.id] = s.ip // save session ip by id
s.token = data.Token
s.name = data.Profile.Name
Expand All @@ -204,5 +206,7 @@ func (s *session) destroy() {
}

// remove session id from ip map
s.h.mu.Lock()
defer s.h.mu.Unlock()
delete(s.h.sessionIPs, s.id)
}
Loading