3x-ui/web/controller/report.go

79 lines
2.1 KiB
Go
Raw Normal View History

# Pull Request: Connection Reporting System & Improvements for Restricted Networks ## Description This PR introduces a comprehensive **Connection Reporting System** designed to improve the reliability and monitoring of connections, specifically tailored for environments with restricted internet access (e.g., active censorship, GFW). ### Key Changes 1. **New Reporting API (`/report`)**: * Added `ReportController` and `ReportService` to handle incoming connection reports. * Endpoint receives data such as `Latency`, `Success` status, `Protocol`, and Client Interface details. * Data is persisted to the database via the new `ConnectionReport` model. 2. **Subscription Link Updates**: * Modified `subService` to append a `reportUrl` parameter to generated subscription links (VLESS, VMess, etc.). * This allows compatible clients to automatically discover the reporting endpoint and send feedback. 3. **Database Integration**: * Added `ConnectionReport` schema to `database/model` and registered it in `database/db.go` for auto-migration. ## Why is this helpful for Restricted Internet Locations? In regions with heavy internet censorship, connection stability is volatile. * **Dynamic Reporting Endpoint**: The `reportUrl` parameter embedded in the subscription link explicitly tells the client *where* to send connection data. * **Bypassing Blocking**: By decoupling the reporting URL from the node address, clients can ensure diagnostic data reaches the panel even if specific node IPs are being interfered with (assuming the panel itself is reachable). * **Real-time Network Intelligence**: This mechanism enables the panel to aggregate "ground truth" data from clients inside the restricted network (e.g., latency, accessibility of specific protocols), allowing admins to react faster to blocking events. * **Protocol Performance Tracking**: Allows comparison of different protocols (Reality vs. VLESS+TLS vs. Trojan) based on real-world latency and success rates from actual users. * **Rapid Troubleshooting**: Administrators can see connection quality trends and rotate IPs/domains proactively when success rates drop, minimizing downtime for users. ## Technical Details * **API Endpoint**: `POST /report` * **Payload Format**: JSON containing `SystemInfo` (Interface), `ConnectionQuality` (Latency, Success), and `ProtocolInfo`. * **Security**: Reports are tied to valid client request contexts (implementation detail: ensure endpoint is rate-limited or authenticated if necessary, though currently designed for open reporting from valid sub links). ## How to Test 1. Update the panel. 2. Generate a subscription link. 3. Observe the `reportUrl` parameter in the link. 4. Simulate a client POST to the report URL and verify the entry in the `ConnectionReports` table.
2026-02-04 10:00:00 +00:00
package controller
import (
"github.com/gin-gonic/gin"
"github.com/mhsanaei/3x-ui/v2/database/model"
"github.com/mhsanaei/3x-ui/v2/logger"
"github.com/mhsanaei/3x-ui/v2/web/service"
)
type ReportController struct {
reportService service.ReportService
}
func NewReportController(g *gin.RouterGroup) *ReportController {
a := &ReportController{
reportService: service.NewReportService(),
}
a.initRouter(g)
return a
}
func (a *ReportController) initRouter(g *gin.RouterGroup) {
g.POST("/report", a.receiveReport)
}
type ReportData struct {
SystemInfo struct {
InterfaceName string `json:"InterfaceName"`
InterfaceDescription string `json:"InterfaceDescription"`
InterfaceType string `json:"InterfaceType"`
Message string `json:"Message"`
} `json:"SystemInfo"`
ConnectionQuality struct {
Latency int `json:"Latency"`
Success bool `json:"Success"`
Message string `json:"Message"`
} `json:"ConnectionQuality"`
ProtocolInfo struct {
Protocol string `json:"Protocol"`
Remarks string `json:"Remarks"`
Address string `json:"Address"`
} `json:"ProtocolInfo"`
}
func (a *ReportController) receiveReport(c *gin.Context) {
var req ReportData
err := c.ShouldBindJSON(&req)
if err != nil {
jsonMsg(c, "Invalid report format", err)
return
}
report := &model.ConnectionReport{
ClientIP: c.ClientIP(),
Protocol: req.ProtocolInfo.Protocol,
Remarks: req.ProtocolInfo.Remarks,
Latency: req.ConnectionQuality.Latency,
Success: req.ConnectionQuality.Success,
InterfaceName: req.SystemInfo.InterfaceName,
InterfaceDescription: req.SystemInfo.InterfaceDescription,
InterfaceType: req.SystemInfo.InterfaceType,
Message: req.SystemInfo.Message,
}
err = a.reportService.SaveReport(report)
if err != nil {
logger.Error("Failed to save report: ", err)
jsonMsg(c, "Failed to save report", err)
return
}
logger.Info("Received and Saved Connection Report: Protocol=%s, UserIP=%s, Latency=%dms",
report.Protocol,
report.ClientIP,
report.Latency)
jsonMsg(c, "Report received and saved successfully", nil)
}