forked from MHSanaei/3x-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show outbound traffic in outbounds table (MHSanaei#1711)
* store outbound traffic in database * show outbound traffic in outbounds table * add refresh button
- Loading branch information
Showing
9 changed files
with
172 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package service | ||
|
||
import ( | ||
"x-ui/database" | ||
"x-ui/database/model" | ||
"x-ui/logger" | ||
"x-ui/xray" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
type OutboundService struct { | ||
xrayApi xray.XrayAPI | ||
} | ||
|
||
func (s *OutboundService) AddTraffic(traffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (error, bool) { | ||
var err error | ||
db := database.GetDB() | ||
tx := db.Begin() | ||
|
||
defer func() { | ||
if err != nil { | ||
tx.Rollback() | ||
} else { | ||
tx.Commit() | ||
} | ||
}() | ||
|
||
err = s.addOutboundTraffic(tx, traffics) | ||
if err != nil { | ||
return err, false | ||
} | ||
|
||
return nil, false | ||
} | ||
|
||
func (s *OutboundService) addOutboundTraffic(tx *gorm.DB, traffics []*xray.Traffic) error { | ||
if len(traffics) == 0 { | ||
return nil | ||
} | ||
|
||
var err error | ||
|
||
for _, traffic := range traffics { | ||
if traffic.IsOutbound { | ||
|
||
var outbound model.OutboundTraffics | ||
|
||
err = tx.Model(&model.OutboundTraffics{}).Where("tag = ?", traffic.Tag). | ||
FirstOrCreate(&outbound).Error | ||
if err != nil { | ||
return err | ||
} | ||
|
||
outbound.Tag = traffic.Tag | ||
outbound.Up = outbound.Up + traffic.Up | ||
outbound.Down = outbound.Down + traffic.Down | ||
outbound.Total = outbound.Up + outbound.Down | ||
|
||
err = tx.Save(&outbound).Error | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (s *OutboundService) GetOutboundsTraffic() ([]*model.OutboundTraffics, error) { | ||
db := database.GetDB() | ||
var traffics []*model.OutboundTraffics | ||
|
||
err := db.Model(model.OutboundTraffics{}).Find(&traffics).Error | ||
if err != nil { | ||
logger.Warning(err) | ||
return nil, err | ||
} | ||
|
||
return traffics, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
package xray | ||
|
||
type Traffic struct { | ||
IsInbound bool | ||
Tag string | ||
Up int64 | ||
Down int64 | ||
IsInbound bool | ||
IsOutbound bool | ||
Tag string | ||
Up int64 | ||
Down int64 | ||
} |