+54
-2
@@ -62,7 +62,7 @@ func (s *ClientService) Save(tx *gorm.DB, act string, data json.RawMessage, host
|
|||||||
}
|
}
|
||||||
if act == "edit" {
|
if act == "edit" {
|
||||||
// Find changed inbounds
|
// Find changed inbounds
|
||||||
inboundIds, err = s.findInboundsChanges(tx, client)
|
inboundIds, err = s.findInboundsChanges(tx, &client, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -94,6 +94,54 @@ func (s *ClientService) Save(tx *gorm.DB, act string, data json.RawMessage, host
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
case "editbulk":
|
||||||
|
var clients []*model.Client
|
||||||
|
err = json.Unmarshal(data, &clients)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, client := range clients {
|
||||||
|
changedInboundIds, err := s.findInboundsChanges(tx, client, true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(changedInboundIds) > 0 {
|
||||||
|
inboundIds = common.UnionUintArray(inboundIds, changedInboundIds)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(inboundIds) > 0 {
|
||||||
|
err = s.updateLinksWithFixedInbounds(tx, clients, hostname)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = tx.Save(clients).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
case "delbulk":
|
||||||
|
var ids []uint
|
||||||
|
err = json.Unmarshal(data, &ids)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, id := range ids {
|
||||||
|
var client model.Client
|
||||||
|
err = tx.Where("id = ?", id).First(&client).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var clientInbounds []uint
|
||||||
|
err = json.Unmarshal(client.Inbounds, &clientInbounds)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
inboundIds = common.UnionUintArray(inboundIds, clientInbounds)
|
||||||
|
}
|
||||||
|
err = tx.Where("id in ?", ids).Delete(model.Client{}).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
case "del":
|
case "del":
|
||||||
var id uint
|
var id uint
|
||||||
err = json.Unmarshal(data, &id)
|
err = json.Unmarshal(data, &id)
|
||||||
@@ -371,7 +419,7 @@ func (s *ClientService) DepleteClients() ([]uint, error) {
|
|||||||
return inboundIds, nil
|
return inboundIds, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ClientService) findInboundsChanges(tx *gorm.DB, client model.Client) ([]uint, error) {
|
func (s *ClientService) findInboundsChanges(tx *gorm.DB, client *model.Client, fillOmitted bool) ([]uint, error) {
|
||||||
var err error
|
var err error
|
||||||
var oldClient model.Client
|
var oldClient model.Client
|
||||||
var oldInboundIds, newInboundIds []uint
|
var oldInboundIds, newInboundIds []uint
|
||||||
@@ -379,6 +427,10 @@ func (s *ClientService) findInboundsChanges(tx *gorm.DB, client model.Client) ([
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if fillOmitted {
|
||||||
|
client.Links = oldClient.Links
|
||||||
|
client.Config = oldClient.Config
|
||||||
|
}
|
||||||
err = json.Unmarshal(oldClient.Inbounds, &oldInboundIds)
|
err = json.Unmarshal(oldClient.Inbounds, &oldInboundIds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
|
// UnionUintArray returns a new unique slice that contains all elements from both input slices
|
||||||
func UnionUintArray(a []uint, b []uint) []uint {
|
func UnionUintArray(a []uint, b []uint) []uint {
|
||||||
m := make(map[uint]bool)
|
m := make(map[uint]bool)
|
||||||
for _, v := range a {
|
for _, v := range a {
|
||||||
|
|||||||
Reference in New Issue
Block a user