Plus some fixes
This commit is contained in:
Alireza Ahmadi
2023-07-16 17:20:19 +02:00
parent 8051a70ef2
commit d2e50d0493
4 changed files with 90 additions and 58 deletions

View File

@@ -1 +1 @@
1.5.0
1.5.1

View File

@@ -96,7 +96,6 @@ func (a *InboundController) addInbound(c *gin.Context) {
}
user := session.GetLoginUser(c)
inbound.UserId = user.Id
inbound.Enable = true
inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
needRestart := false

View File

@@ -25,11 +25,13 @@ func (j *CheckInboundJob) Run() {
}
}
count, err = j.inboundService.DisableInvalidInbounds()
needRestart, count, err = j.inboundService.DisableInvalidInbounds()
if err != nil {
logger.Warning("Error in disabling invalid inbounds:", err)
} else if count > 0 {
logger.Debugf("%v inbounds disabled", count)
j.xrayService.SetToNeedRestart()
if needRestart {
j.xrayService.SetToNeedRestart()
}
}
}

View File

@@ -174,20 +174,22 @@ func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound, boo
}
needRestart := false
s.xrayApi.Init(p.GetAPIPort())
inboundJson, err1 := json.MarshalIndent(inbound.GenXrayInboundConfig(), "", " ")
if err1 != nil {
logger.Debug("Unable to marshal inbound config:", err1)
}
if inbound.Enable {
s.xrayApi.Init(p.GetAPIPort())
inboundJson, err1 := json.MarshalIndent(inbound.GenXrayInboundConfig(), "", " ")
if err1 != nil {
logger.Debug("Unable to marshal inbound config:", err1)
}
err1 = s.xrayApi.AddInbound(inboundJson)
if err1 == nil {
logger.Debug("New inbound added by api:", inbound.Tag)
} else {
logger.Debug("Unable to add inbound by api:", err1)
needRestart = true
err1 = s.xrayApi.AddInbound(inboundJson)
if err1 == nil {
logger.Debug("New inbound added by api:", inbound.Tag)
} else {
logger.Debug("Unable to add inbound by api:", err1)
needRestart = true
}
s.xrayApi.Close()
}
s.xrayApi.Close()
return inbound, needRestart, err
}
@@ -226,28 +228,30 @@ func (s *InboundService) AddInbounds(inbounds []*model.Inbound) error {
func (s *InboundService) DelInbound(id int) (bool, error) {
db := database.GetDB()
var tag string
needRestart := false
result := db.Model(model.Inbound{}).Select("tag").Where("id = ? and enable = ?", id, true).First(&tag)
if result.Error == nil {
s.xrayApi.Init(p.GetAPIPort())
err1 := s.xrayApi.DelInbound(tag)
if err1 == nil {
logger.Debug("Inbound deleted by api:", tag)
} else {
logger.Debug("Unable to delete inbound by api:", err1)
needRestart = true
}
s.xrayApi.Close()
} else {
logger.Debug("No enabled inbound founded to removing by api", tag)
}
// Delete client traffics of inbounds
err := db.Where("inbound_id = ?", id).Delete(xray.ClientTraffic{}).Error
if err != nil {
return false, err
}
var tag string
result := db.Model(model.Inbound{}).Select("tag").Where("id = ?", id).First(&tag)
if result.Error != nil {
return false, err
}
needRestart := false
s.xrayApi.Init(p.GetAPIPort())
err1 := s.xrayApi.DelInbound(tag)
if err1 == nil {
logger.Debug("Inbound deleted by api:", tag)
} else {
logger.Debug("Unable to delete inbound by api:", err1)
needRestart = true
}
s.xrayApi.Close()
return needRestart, db.Delete(model.Inbound{}, id).Error
}
@@ -305,17 +309,19 @@ func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound,
} else {
logger.Debug("Old inbound deleted by api:", tag)
inboundJson, err2 := json.MarshalIndent(oldInbound.GenXrayInboundConfig(), "", " ")
if err2 != nil {
logger.Debug("Unable to marshal updated inbound config:", err2)
}
if inbound.Enable {
inboundJson, err2 := json.MarshalIndent(oldInbound.GenXrayInboundConfig(), "", " ")
if err2 != nil {
logger.Debug("Unable to marshal updated inbound config:", err2)
}
err2 = s.xrayApi.AddInbound(inboundJson)
if err1 == nil {
logger.Debug("Updated inbound added by api:", oldInbound.Tag)
} else {
logger.Debug("Unable to update inbound by api:", err2)
needRestart = true
err2 = s.xrayApi.AddInbound(inboundJson)
if err1 == nil {
logger.Debug("Updated inbound added by api:", oldInbound.Tag)
} else {
logger.Debug("Unable to update inbound by api:", err2)
needRestart = true
}
}
}
s.xrayApi.Close()
@@ -440,16 +446,18 @@ func (s *InboundService) AddInboundClient(data *model.Inbound) (bool, error) {
for _, client := range clients {
if len(client.Email) > 0 {
s.AddClientStat(tx, data.Id, &client)
err1 := s.xrayApi.AddUser(string(oldInbound.Protocol), oldInbound.Tag, map[string]interface{}{
"email": client.Email,
"id": client.ID,
"flow": client.Flow,
"password": client.Password,
})
if err1 == nil {
logger.Debug("Client added by api:", client.Email)
} else {
needRestart = true
if client.Enable {
err1 := s.xrayApi.AddUser(string(oldInbound.Protocol), oldInbound.Tag, map[string]interface{}{
"email": client.Email,
"id": client.ID,
"flow": client.Flow,
"password": client.Password,
})
if err1 == nil {
logger.Debug("Client added by api:", client.Email)
} else {
needRestart = true
}
}
} else {
needRestart = true
@@ -507,7 +515,7 @@ func (s *InboundService) DelInboundClient(inboundId int, clientId string) (bool,
logger.Error("Delete stats Data Error")
return false, err
}
needRestart := true
needRestart := false
s.xrayApi.Init(p.GetAPIPort())
if len(email) > 0 {
err = s.xrayApi.RemoveUser(oldInbound.Tag, email)
@@ -613,7 +621,7 @@ func (s *InboundService) UpdateInboundClient(data *model.Inbound, clientId strin
return false, err
}
}
needRestart := true
needRestart := false
s.xrayApi.Init(p.GetAPIPort())
if len(oldEmail) > 0 {
s.xrayApi.RemoveUser(oldInbound.Tag, oldEmail)
@@ -762,15 +770,38 @@ func (s *InboundService) adjustTraffics(tx *gorm.DB, dbClientTraffics []*xray.Cl
return dbClientTraffics, nil
}
func (s *InboundService) DisableInvalidInbounds() (int64, error) {
func (s *InboundService) DisableInvalidInbounds() (bool, int64, error) {
db := database.GetDB()
now := time.Now().Unix() * 1000
needRestart := false
if p != nil {
var tags []string
err := db.Table("inbounds").
Select("inbounds.tag").
Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ?", now, true).
Scan(&tags).Error
if err != nil {
return false, 0, err
}
s.xrayApi.Init(p.GetAPIPort())
for _, tag := range tags {
err = s.xrayApi.DelInbound(tag)
if err == nil {
logger.Debug("Inbound disabled by api:", tag)
} else {
needRestart = true
}
}
s.xrayApi.Close()
}
result := db.Model(model.Inbound{}).
Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ?", now, true).
Update("enable", false)
err := result.Error
count := result.RowsAffected
return count, err
return needRestart, count, err
}
func (s *InboundService) DisableInvalidClients() (bool, int64, error) {
@@ -796,7 +827,7 @@ func (s *InboundService) DisableInvalidClients() (bool, int64, error) {
for _, result := range results {
err = s.xrayApi.RemoveUser(result.Tag, result.Email)
if err == nil {
logger.Debug("Client deleted by api:", result.Email)
logger.Debug("Client disabled by api:", result.Email)
} else {
needRestart = true
}