mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-04-19 21:42:24 +00:00
online users backend #1286
This commit is contained in:
parent
8dc23f97b6
commit
f734c821d6
4 changed files with 34 additions and 1 deletions
|
@ -37,6 +37,7 @@ func (a *InboundController) initRouter(g *gin.RouterGroup) {
|
||||||
g.POST("/resetAllTraffics", a.resetAllTraffics)
|
g.POST("/resetAllTraffics", a.resetAllTraffics)
|
||||||
g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)
|
g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)
|
||||||
g.POST("/delDepletedClients/:id", a.delDepletedClients)
|
g.POST("/delDepletedClients/:id", a.delDepletedClients)
|
||||||
|
g.POST("/onlines", a.onlines)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *InboundController) getInbounds(c *gin.Context) {
|
func (a *InboundController) getInbounds(c *gin.Context) {
|
||||||
|
@ -278,3 +279,7 @@ func (a *InboundController) delDepletedClients(c *gin.Context) {
|
||||||
}
|
}
|
||||||
jsonMsg(c, "All delpeted clients are deleted", nil)
|
jsonMsg(c, "All delpeted clients are deleted", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *InboundController) onlines(c *gin.Context) {
|
||||||
|
jsonObj(c, a.inboundService.GetOnlineClinets(), nil)
|
||||||
|
}
|
||||||
|
|
|
@ -716,9 +716,15 @@ func (s *InboundService) addInboundTraffic(tx *gorm.DB, traffics []*xray.Traffic
|
||||||
|
|
||||||
func (s *InboundService) addClientTraffic(tx *gorm.DB, traffics []*xray.ClientTraffic) (err error) {
|
func (s *InboundService) addClientTraffic(tx *gorm.DB, traffics []*xray.ClientTraffic) (err error) {
|
||||||
if len(traffics) == 0 {
|
if len(traffics) == 0 {
|
||||||
|
// Empty onlineUsers
|
||||||
|
if p != nil {
|
||||||
|
p.SetOnlineClients(nil)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var onlineClients []string
|
||||||
|
|
||||||
emails := make([]string, 0, len(traffics))
|
emails := make([]string, 0, len(traffics))
|
||||||
for _, traffic := range traffics {
|
for _, traffic := range traffics {
|
||||||
emails = append(emails, traffic.Email)
|
emails = append(emails, traffic.Email)
|
||||||
|
@ -744,11 +750,19 @@ func (s *InboundService) addClientTraffic(tx *gorm.DB, traffics []*xray.ClientTr
|
||||||
if dbClientTraffics[dbTraffic_index].Email == traffics[traffic_index].Email {
|
if dbClientTraffics[dbTraffic_index].Email == traffics[traffic_index].Email {
|
||||||
dbClientTraffics[dbTraffic_index].Up += traffics[traffic_index].Up
|
dbClientTraffics[dbTraffic_index].Up += traffics[traffic_index].Up
|
||||||
dbClientTraffics[dbTraffic_index].Down += traffics[traffic_index].Down
|
dbClientTraffics[dbTraffic_index].Down += traffics[traffic_index].Down
|
||||||
|
|
||||||
|
// Add user in onlineUsers array on traffic
|
||||||
|
if traffics[traffic_index].Up+traffics[traffic_index].Down > 0 {
|
||||||
|
onlineClients = append(onlineClients, traffics[traffic_index].Email)
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set onlineUsers
|
||||||
|
p.SetOnlineClients(onlineClients)
|
||||||
|
|
||||||
err = tx.Save(dbClientTraffics).Error
|
err = tx.Save(dbClientTraffics).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warning("AddClientTraffic update data ", err)
|
logger.Warning("AddClientTraffic update data ", err)
|
||||||
|
@ -1675,3 +1689,7 @@ func (s *InboundService) MigrateDB() {
|
||||||
s.MigrationRequirements()
|
s.MigrationRequirements()
|
||||||
s.MigrationRemoveOrphanedTraffics()
|
s.MigrationRemoveOrphanedTraffics()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *InboundService) GetOnlineClinets() []string {
|
||||||
|
return p.GetOnlineClients()
|
||||||
|
}
|
||||||
|
|
|
@ -230,7 +230,7 @@ func (s *ServerService) GetStatus(lastStatus *Status) *Status {
|
||||||
|
|
||||||
status.AppStats.Mem = rtm.Sys
|
status.AppStats.Mem = rtm.Sys
|
||||||
status.AppStats.Threads = uint32(runtime.NumGoroutine())
|
status.AppStats.Threads = uint32(runtime.NumGoroutine())
|
||||||
if p.IsRunning() {
|
if p != nil && p.IsRunning() {
|
||||||
status.AppStats.Uptime = p.GetUptime()
|
status.AppStats.Uptime = p.GetUptime()
|
||||||
} else {
|
} else {
|
||||||
status.AppStats.Uptime = 0
|
status.AppStats.Uptime = 0
|
||||||
|
|
|
@ -98,6 +98,8 @@ type process struct {
|
||||||
version string
|
version string
|
||||||
apiPort int
|
apiPort int
|
||||||
|
|
||||||
|
onlineClients []string
|
||||||
|
|
||||||
config *Config
|
config *Config
|
||||||
lines *queue.Queue
|
lines *queue.Queue
|
||||||
exitErr error
|
exitErr error
|
||||||
|
@ -153,6 +155,14 @@ func (p *Process) GetConfig() *Config {
|
||||||
return p.config
|
return p.config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Process) GetOnlineClients() []string {
|
||||||
|
return p.onlineClients
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Process) SetOnlineClients(users []string) {
|
||||||
|
p.onlineClients = users
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Process) GetUptime() uint64 {
|
func (p *Process) GetUptime() uint64 {
|
||||||
return uint64(time.Since(p.startTime).Seconds())
|
return uint64(time.Since(p.startTime).Seconds())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue