mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-04-14 19:45:47 +00:00
## Critical Fixes
### 1. DATA LOSS: 5 functions discard all other clients when updating one
Functions affected:
- SetClientTelegramUserID
- ToggleClientEnableByEmail
- ResetClientIpLimitByEmail
- ResetClientExpiryTimeByEmail
- ResetClientTrafficLimitByEmail
All five built a `newClients` slice by only appending the client
matching the target email, then replaced the entire client list.
Every other client in the inbound was silently deleted.
Fix: update client in-place with break instead of building new slice.
### 2. DATA LOSS: ResetSettings never deletes user credentials
ResetSettings() called `.Where("1 = 1").Error` instead of
`.Delete(model.User{}).Error`. The reset command did nothing to users.
### 3. SECURITY: WebSocket CheckOrigin allows cross-origin hijacking
The fallback `(originHost == "" || requestHost == "")` accepted
any origin with a missing host component. Removed the fallback and
added proper host normalization for IPv6/ports.
### 4. GRACEFUL SHUTDOWN: Server.Stop() uses cancelled context
s.cancel() was called before s.httpServer.Shutdown(s.ctx), making
the context already-done. Shutdown returned immediately (forced kill)
instead of waiting 10 seconds. Moved s.cancel() to end and used
context.WithTimeout(10s) for shutdown. Same fix applied to sub.go.
## Medium Fixes
### 5. Wrong success messages on error paths (~11 endpoints)
When validation failed, endpoints returned messages like
"inboundUpdateSuccess" alongside the error. Fixed to use
"somethingWentWrong" for all error paths.
### 6. resetAllTraffics/resetAllClientTraffics trigger restart on error
SetToNeedRestart() was called in else branch that ran even on failure.
Restructured to only call after confirming success.
### 7. disableInvalidClients has duplicate unreachable error check
Same "User %s not found" string check was nested twice.
Removed the inner duplicate.
### 8. DelInbound logs uninitialized tag variable
The else branch logged empty tag variable instead of actual inbound id.
### 9. check_cpu_usage.go index-out-of-range panic
cpu.Percent() can return empty slice. Added len(percent) > 0 guard.
### 10. Dead code: cron.Remove(entry) on never-added entry
var entry cron.EntryID defaults to 0; cron.Remove(0) is a no-op.
### 11. checkEmailExistForInbound duplicates checkEmailsExistForClients
Refactored to delegate to existing function instead of reimplementing.
188 lines
5.2 KiB
Go
188 lines
5.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/mhsanaei/3x-ui/v2/logger"
|
|
"github.com/mhsanaei/3x-ui/v2/util/common"
|
|
"github.com/mhsanaei/3x-ui/v2/web/session"
|
|
"github.com/mhsanaei/3x-ui/v2/web/websocket"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
ws "github.com/gorilla/websocket"
|
|
)
|
|
|
|
const (
|
|
// Time allowed to write a message to the peer
|
|
writeWait = 10 * time.Second
|
|
|
|
// Time allowed to read the next pong message from the peer
|
|
pongWait = 60 * time.Second
|
|
|
|
// Send pings to peer with this period (must be less than pongWait)
|
|
pingPeriod = (pongWait * 9) / 10
|
|
|
|
// Maximum message size allowed from peer
|
|
maxMessageSize = 512
|
|
)
|
|
|
|
var upgrader = ws.Upgrader{
|
|
ReadBufferSize: 4096, // Increased from 1024 for better performance
|
|
WriteBufferSize: 4096, // Increased from 1024 for better performance
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
origin := r.Header.Get("Origin")
|
|
if origin == "" {
|
|
// Allow connections without Origin header (same-origin requests)
|
|
return true
|
|
}
|
|
|
|
// Extract host from origin
|
|
originHost := origin
|
|
if strings.HasPrefix(originHost, "http://") || strings.HasPrefix(originHost, "https://") {
|
|
originHost = strings.TrimPrefix(strings.TrimPrefix(originHost, "http://"), "https://")
|
|
if idx := strings.Index(originHost, "/"); idx != -1 {
|
|
originHost = originHost[:idx]
|
|
}
|
|
}
|
|
|
|
// Normalize host for comparison (strip ports and IPv6 brackets)
|
|
normalizeHost := func(h string) string {
|
|
h = strings.TrimPrefix(h, "[")
|
|
if idx := strings.LastIndex(h, "]:"); idx != -1 {
|
|
h = h[:idx+1]
|
|
}
|
|
if idx := strings.LastIndex(h, ":"); idx != -1 && !strings.Contains(h[:idx], "]") {
|
|
h = h[:idx]
|
|
}
|
|
return h
|
|
}
|
|
|
|
return normalizeHost(originHost) == normalizeHost(r.Host)
|
|
},
|
|
}
|
|
|
|
// WebSocketController handles WebSocket connections for real-time updates
|
|
type WebSocketController struct {
|
|
BaseController
|
|
hub *websocket.Hub
|
|
}
|
|
|
|
// NewWebSocketController creates a new WebSocket controller
|
|
func NewWebSocketController(hub *websocket.Hub) *WebSocketController {
|
|
return &WebSocketController{
|
|
hub: hub,
|
|
}
|
|
}
|
|
|
|
// HandleWebSocket handles WebSocket connections
|
|
func (w *WebSocketController) HandleWebSocket(c *gin.Context) {
|
|
// Check authentication
|
|
if !session.IsLogin(c) {
|
|
logger.Warningf("Unauthorized WebSocket connection attempt from %s", getRemoteIp(c))
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
// Upgrade connection to WebSocket
|
|
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
|
if err != nil {
|
|
logger.Error("Failed to upgrade WebSocket connection:", err)
|
|
return
|
|
}
|
|
|
|
// Create client
|
|
clientID := uuid.New().String()
|
|
client := &websocket.Client{
|
|
ID: clientID,
|
|
Hub: w.hub,
|
|
Send: make(chan []byte, 512), // Increased from 256 to 512 to prevent overflow
|
|
Topics: make(map[websocket.MessageType]bool),
|
|
}
|
|
|
|
// Register client
|
|
w.hub.Register(client)
|
|
logger.Debugf("WebSocket client %s registered from %s", clientID, getRemoteIp(c))
|
|
|
|
// Start goroutines for reading and writing
|
|
go w.writePump(client, conn)
|
|
go w.readPump(client, conn)
|
|
}
|
|
|
|
// readPump pumps messages from the WebSocket connection to the hub
|
|
func (w *WebSocketController) readPump(client *websocket.Client, conn *ws.Conn) {
|
|
defer func() {
|
|
if r := common.Recover("WebSocket readPump panic"); r != nil {
|
|
logger.Error("WebSocket readPump panic recovered:", r)
|
|
}
|
|
w.hub.Unregister(client)
|
|
conn.Close()
|
|
}()
|
|
|
|
conn.SetReadDeadline(time.Now().Add(pongWait))
|
|
conn.SetPongHandler(func(string) error {
|
|
conn.SetReadDeadline(time.Now().Add(pongWait))
|
|
return nil
|
|
})
|
|
conn.SetReadLimit(maxMessageSize)
|
|
|
|
for {
|
|
_, message, err := conn.ReadMessage()
|
|
if err != nil {
|
|
if ws.IsUnexpectedCloseError(err, ws.CloseGoingAway, ws.CloseAbnormalClosure) {
|
|
logger.Debugf("WebSocket read error for client %s: %v", client.ID, err)
|
|
}
|
|
break
|
|
}
|
|
|
|
// Validate message size
|
|
if len(message) > maxMessageSize {
|
|
logger.Warningf("WebSocket message from client %s exceeds max size: %d bytes", client.ID, len(message))
|
|
continue
|
|
}
|
|
|
|
// Handle incoming messages (e.g., subscription requests)
|
|
// For now, we'll just log them
|
|
logger.Debugf("Received WebSocket message from client %s: %s", client.ID, string(message))
|
|
}
|
|
}
|
|
|
|
// writePump pumps messages from the hub to the WebSocket connection
|
|
func (w *WebSocketController) writePump(client *websocket.Client, conn *ws.Conn) {
|
|
ticker := time.NewTicker(pingPeriod)
|
|
defer func() {
|
|
if r := common.Recover("WebSocket writePump panic"); r != nil {
|
|
logger.Error("WebSocket writePump panic recovered:", r)
|
|
}
|
|
ticker.Stop()
|
|
conn.Close()
|
|
}()
|
|
|
|
for {
|
|
select {
|
|
case message, ok := <-client.Send:
|
|
conn.SetWriteDeadline(time.Now().Add(writeWait))
|
|
if !ok {
|
|
// Hub closed the channel
|
|
conn.WriteMessage(ws.CloseMessage, []byte{})
|
|
return
|
|
}
|
|
|
|
// Send each message individually (no batching)
|
|
// This ensures each JSON message is sent separately and can be parsed correctly
|
|
if err := conn.WriteMessage(ws.TextMessage, message); err != nil {
|
|
logger.Debugf("WebSocket write error for client %s: %v", client.ID, err)
|
|
return
|
|
}
|
|
|
|
case <-ticker.C:
|
|
conn.SetWriteDeadline(time.Now().Add(writeWait))
|
|
if err := conn.WriteMessage(ws.PingMessage, nil); err != nil {
|
|
logger.Debugf("WebSocket ping error for client %s: %v", client.ID, err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|