Skip to content

Commit b1ab156

Browse files
authored
Endpoint for updating client traffic by email (#3259)
* Update api.go * Update inbound.go * Update inbound.go
1 parent fa45bf8 commit b1ab156

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

web/controller/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func (a *APIController) initRouter(g *gin.RouterGroup) {
4747
{"POST", "/resetAllClientTraffics/:id", a.inboundController.resetAllClientTraffics},
4848
{"POST", "/delDepletedClients/:id", a.inboundController.delDepletedClients},
4949
{"POST", "/onlines", a.inboundController.onlines},
50+
{"POST", "/updateClientTraffic/:email", a.inboundController.updateClientTraffic},
5051
}
5152

5253
for _, route := range inboundRoutes {

web/controller/inbound.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,28 @@ func (a *InboundController) delDepletedClients(c *gin.Context) {
339339
func (a *InboundController) onlines(c *gin.Context) {
340340
jsonObj(c, a.inboundService.GetOnlineClients(), nil)
341341
}
342+
343+
func (a *InboundController) updateClientTraffic(c *gin.Context) {
344+
email := c.Param("email")
345+
346+
// Define the request structure for traffic update
347+
type TrafficUpdateRequest struct {
348+
Upload int64 `json:"upload"`
349+
Download int64 `json:"download"`
350+
}
351+
352+
var request TrafficUpdateRequest
353+
err := c.ShouldBindJSON(&request)
354+
if err != nil {
355+
jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err)
356+
return
357+
}
358+
359+
err = a.inboundService.UpdateClientTrafficByEmail(email, request.Upload, request.Download)
360+
if err != nil {
361+
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
362+
return
363+
}
364+
365+
jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundClientUpdateSuccess"), nil)
366+
}

web/service/inbound.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,21 @@ func (s *InboundService) GetClientTrafficByEmail(email string) (traffic *xray.Cl
17851785
return nil, nil
17861786
}
17871787

1788+
func (s *InboundService) UpdateClientTrafficByEmail(email string, upload int64, download int64) error {
1789+
db := database.GetDB()
1790+
1791+
result := db.Model(xray.ClientTraffic{}).
1792+
Where("email = ?", email).
1793+
Updates(map[string]any{"up": upload, "down": download})
1794+
1795+
err := result.Error
1796+
if err != nil {
1797+
logger.Warningf("Error updating ClientTraffic with email %s: %v", email, err)
1798+
return err
1799+
}
1800+
return nil
1801+
}
1802+
17881803
func (s *InboundService) GetClientTrafficByID(id string) ([]xray.ClientTraffic, error) {
17891804
db := database.GetDB()
17901805
var traffics []xray.ClientTraffic

0 commit comments

Comments
 (0)