init users on create inbound #411
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"s-ui/logger"
|
||||
"s-ui/util"
|
||||
"s-ui/util/common"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -156,6 +157,56 @@ func (s *ClientService) updateLinksWithFixedInbounds(tx *gorm.DB, clients []*mod
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ClientService) UpdateClientsOnInboundAdd(tx *gorm.DB, initIds string, inboundId uint, hostname string) error {
|
||||
clientIds := strings.Split(initIds, ",")
|
||||
var clients []model.Client
|
||||
err := tx.Model(model.Client{}).Where("id in ?", clientIds).Find(&clients).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var inbound model.Inbound
|
||||
err = tx.Model(model.Inbound{}).Preload("Tls").Where("id = ?", inboundId).Find(&inbound).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, client := range clients {
|
||||
// Add inbounds
|
||||
var clientInbounds []uint
|
||||
json.Unmarshal(client.Inbounds, &clientInbounds)
|
||||
clientInbounds = append(clientInbounds, inboundId)
|
||||
client.Inbounds, err = json.MarshalIndent(clientInbounds, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Add links
|
||||
var clientLinks, newClientLinks []map[string]string
|
||||
json.Unmarshal(client.Links, &clientLinks)
|
||||
newLinks := util.LinkGenerator(client.Config, &inbound, hostname)
|
||||
for _, newLink := range newLinks {
|
||||
newClientLinks = append(newClientLinks, map[string]string{
|
||||
"remark": inbound.Tag,
|
||||
"type": "local",
|
||||
"uri": newLink,
|
||||
})
|
||||
}
|
||||
for _, clientLink := range clientLinks {
|
||||
if clientLink["remark"] != inbound.Tag {
|
||||
newClientLinks = append(newClientLinks, clientLink)
|
||||
}
|
||||
}
|
||||
|
||||
client.Links, err = json.MarshalIndent(newClientLinks, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = tx.Save(&client).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ClientService) UpdateClientsOnInboundDelete(tx *gorm.DB, id uint, tag string) error {
|
||||
var clients []model.Client
|
||||
err := tx.Table("clients").
|
||||
|
||||
@@ -116,7 +116,7 @@ func (s *ConfigService) StopCore() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ConfigService) Save(obj string, act string, data json.RawMessage, loginUser string, hostname string) ([]string, error) {
|
||||
func (s *ConfigService) Save(obj string, act string, data json.RawMessage, initUsers string, loginUser string, hostname string) ([]string, error) {
|
||||
var err error
|
||||
var inboundIds []uint
|
||||
var inboundId uint
|
||||
@@ -139,7 +139,7 @@ func (s *ConfigService) Save(obj string, act string, data json.RawMessage, login
|
||||
case "tls":
|
||||
inboundIds, err = s.TlsService.Save(tx, act, data)
|
||||
case "inbounds":
|
||||
inboundId, err = s.InboundService.Save(tx, act, data, hostname)
|
||||
inboundId, err = s.InboundService.Save(tx, act, data, initUsers, hostname)
|
||||
case "outbounds":
|
||||
err = s.OutboundService.Save(tx, act, data)
|
||||
case "endpoints":
|
||||
@@ -173,7 +173,6 @@ func (s *ConfigService) Save(obj string, act string, data json.RawMessage, login
|
||||
// Commit changes so far
|
||||
tx.Commit()
|
||||
LastUpdate = time.Now().Unix()
|
||||
var objs []string = []string{obj}
|
||||
tx = db.Begin()
|
||||
|
||||
// Update side changes
|
||||
@@ -186,8 +185,10 @@ func (s *ConfigService) Save(obj string, act string, data json.RawMessage, login
|
||||
}
|
||||
objs = append(objs, "clients")
|
||||
}
|
||||
if obj == "inbounds" && act != "add" {
|
||||
if obj == "inbounds" {
|
||||
switch act {
|
||||
case "new":
|
||||
err = s.ClientService.UpdateClientsOnInboundAdd(tx, initUsers, inboundId, hostname)
|
||||
case "edit":
|
||||
err = s.ClientService.UpdateLinksByInboundChange(tx, []uint{inboundId}, hostname)
|
||||
case "del":
|
||||
|
||||
+67
-14
@@ -96,7 +96,7 @@ func (s *InboundService) FromIds(ids []uint) ([]*model.Inbound, error) {
|
||||
return inbounds, nil
|
||||
}
|
||||
|
||||
func (s *InboundService) Save(tx *gorm.DB, act string, data json.RawMessage, hostname string) (uint, error) {
|
||||
func (s *InboundService) Save(tx *gorm.DB, act string, data json.RawMessage, initUsers string, hostname string) (uint, error) {
|
||||
var err error
|
||||
var id uint
|
||||
|
||||
@@ -107,7 +107,6 @@ func (s *InboundService) Save(tx *gorm.DB, act string, data json.RawMessage, hos
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id = inbound.Id
|
||||
if inbound.TlsId > 0 {
|
||||
err = tx.Model(model.Tls{}).Where("id = ?", inbound.TlsId).Find(&inbound.Tls).Error
|
||||
if err != nil {
|
||||
@@ -115,6 +114,17 @@ func (s *InboundService) Save(tx *gorm.DB, act string, data json.RawMessage, hos
|
||||
}
|
||||
}
|
||||
|
||||
err = util.FillOutJson(&inbound, hostname)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
err = tx.Save(&inbound).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id = inbound.Id
|
||||
|
||||
if corePtr.IsRunning() {
|
||||
if act == "edit" {
|
||||
var oldTag string
|
||||
@@ -133,7 +143,11 @@ func (s *InboundService) Save(tx *gorm.DB, act string, data json.RawMessage, hos
|
||||
return 0, err
|
||||
}
|
||||
|
||||
inboundConfig, err = s.addUsers(tx, inboundConfig, inbound.Id, inbound.Type)
|
||||
if act == "edit" {
|
||||
inboundConfig, err = s.addUsers(tx, inboundConfig, inbound.Id, inbound.Type)
|
||||
} else {
|
||||
inboundConfig, err = s.initUsers(tx, inboundConfig, initUsers, inbound.Id, inbound.Type)
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -143,16 +157,6 @@ func (s *InboundService) Save(tx *gorm.DB, act string, data json.RawMessage, hos
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
err = util.FillOutJson(&inbound, hostname)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
err = tx.Save(&inbound).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
case "del":
|
||||
var tag string
|
||||
err = json.Unmarshal(data, &tag)
|
||||
@@ -263,7 +267,56 @@ func (s *InboundService) addUsers(db *gorm.DB, inboundJson []byte, inboundId uin
|
||||
inbound["users"] = usersJson
|
||||
}
|
||||
|
||||
inbound["users"] = usersJson
|
||||
return json.Marshal(inbound)
|
||||
}
|
||||
|
||||
func (s *InboundService) initUsers(db *gorm.DB, inboundJson []byte, clientIds string, inboundId uint, inboundType string) ([]byte, error) {
|
||||
ClientIds := strings.Split(clientIds, ",")
|
||||
if len(ClientIds) == 0 {
|
||||
return inboundJson, nil
|
||||
}
|
||||
switch inboundType {
|
||||
case "mixed", "socks", "http", "shadowsocks", "vmess", "trojan", "naive", "hysteria", "shadowtls", "tuic", "hysteria2", "vless":
|
||||
break
|
||||
default:
|
||||
return inboundJson, nil
|
||||
}
|
||||
|
||||
var inbound map[string]interface{}
|
||||
err := json.Unmarshal(inboundJson, &inbound)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if inboundType == "shadowtls" {
|
||||
version, _ := inbound["version"].(float64)
|
||||
if int(version) < 3 {
|
||||
return inboundJson, nil
|
||||
}
|
||||
}
|
||||
if inboundType == "shadowsocks" {
|
||||
method, _ := inbound["method"].(string)
|
||||
if method == "2022-blake3-aes-128-gcm" {
|
||||
inboundType = "shadowsocks16"
|
||||
}
|
||||
}
|
||||
var users []string
|
||||
err = db.Raw(`SELECT json_extract(clients.config, ?)
|
||||
FROM clients
|
||||
WHERE enable = true AND id in ?`,
|
||||
"$."+inboundType, ClientIds).Scan(&users).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var usersJson []json.RawMessage
|
||||
for _, user := range users {
|
||||
usersJson = append(usersJson, json.RawMessage(user))
|
||||
}
|
||||
|
||||
if len(usersJson) > 0 || inboundType != "shadowsocks" {
|
||||
inbound["users"] = usersJson
|
||||
}
|
||||
|
||||
return json.Marshal(inbound)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user