[refactor] string values to json

This commit is contained in:
Alireza Ahmadi
2024-06-06 22:07:26 +02:00
parent c994f4b24a
commit 2cabf0aefb
12 changed files with 121 additions and 138 deletions
+8 -5
View File
@@ -1,7 +1,6 @@
package api package api
import ( import (
"fmt"
"s-ui/logger" "s-ui/logger"
"s-ui/service" "s-ui/service"
"strconv" "strconv"
@@ -157,8 +156,8 @@ func (a *APIHandler) getHandler(c *gin.Context) {
} }
} }
func (a *APIHandler) loadData(c *gin.Context) (string, error) { func (a *APIHandler) loadData(c *gin.Context) (interface{}, error) {
var data string data := make(map[string]interface{}, 0)
lu := c.Query("lu") lu := c.Query("lu")
isUpdated, err := a.ConfigService.CheckChanges(lu) isUpdated, err := a.ConfigService.CheckChanges(lu)
if err != nil { if err != nil {
@@ -185,9 +184,13 @@ func (a *APIHandler) loadData(c *gin.Context) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
data = fmt.Sprintf(`{"config": %s, "clients": %s, "tls": %s, "subURI": "%s", "onlines": %s}`, string(*config), clients, tlsConfigs, subURI, onlines) data["config"] = *config
data["clients"] = clients
data["tls"] = tlsConfigs
data["subURI"] = subURI
data["onlines"] = onlines
} else { } else {
data = fmt.Sprintf(`{"onlines": %s}`, onlines) data["onlines"] = onlines
} }
return data, nil return data, nil
+3 -3
View File
@@ -27,9 +27,9 @@ type Client struct {
Id uint `json:"id" form:"id" gorm:"primaryKey;autoIncrement"` Id uint `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
Enable bool `json:"enable" form:"enable"` Enable bool `json:"enable" form:"enable"`
Name string `json:"name" form:"name"` Name string `json:"name" form:"name"`
Config string `json:"config" form:"config"` Config json.RawMessage `json:"config" form:"config"`
Inbounds string `json:"inbounds" form:"inbounds"` Inbounds json.RawMessage `json:"inbounds" form:"inbounds"`
Links string `json:"links" form:"links"` Links json.RawMessage `json:"links" form:"links"`
Volume int64 `json:"volume" form:"volume"` Volume int64 `json:"volume" form:"volume"`
Expiry int64 `json:"expiry" form:"expiry"` Expiry int64 `json:"expiry" form:"expiry"`
Down int64 `json:"down" form:"down"` Down int64 `json:"down" form:"down"`
+8 -10
View File
@@ -5,7 +5,6 @@ import (
"s-ui/database" "s-ui/database"
"s-ui/database/model" "s-ui/database/model"
"s-ui/logger" "s-ui/logger"
"strings"
"time" "time"
"gorm.io/gorm" "gorm.io/gorm"
@@ -14,18 +13,14 @@ import (
type ClientService struct { type ClientService struct {
} }
func (s *ClientService) GetAll() (string, error) { func (s *ClientService) GetAll() ([]model.Client, error) {
db := database.GetDB() db := database.GetDB()
clients := []model.Client{} clients := []model.Client{}
err := db.Model(model.Client{}).Scan(&clients).Error err := db.Model(model.Client{}).Scan(&clients).Error
if err != nil { if err != nil {
return "", err return nil, err
} }
data, err := json.Marshal(clients) return clients, nil
if err != nil {
return "", err
}
return string(data), nil
} }
func (s *ClientService) Save(tx *gorm.DB, changes []model.Changes) error { func (s *ClientService) Save(tx *gorm.DB, changes []model.Changes) error {
@@ -62,14 +57,16 @@ func (s *ClientService) DepleteClients() ([]string, []string, error) {
return nil, nil, err return nil, nil, err
} }
dt := time.Now().Unix()
var users, inbounds []string var users, inbounds []string
for _, client := range clients { for _, client := range clients {
logger.Debug("Client ", client.Name, " is going to be disabled") logger.Debug("Client ", client.Name, " is going to be disabled")
users = append(users, client.Name) users = append(users, client.Name)
userInbounds := strings.Split(client.Inbounds, ",") var userInbounds []string
json.Unmarshal(client.Inbounds, &userInbounds)
inbounds = append(inbounds, userInbounds...) inbounds = append(inbounds, userInbounds...)
changes = append(changes, model.Changes{ changes = append(changes, model.Changes{
DateTime: time.Now().Unix(), DateTime: dt,
Actor: "DepleteJob", Actor: "DepleteJob",
Key: "clients", Key: "clients",
Action: "disable", Action: "disable",
@@ -87,6 +84,7 @@ func (s *ClientService) DepleteClients() ([]string, []string, error) {
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
LastUpdate = dt
} }
return users, inbounds, nil return users, inbounds, nil
+32 -37
View File
@@ -54,16 +54,27 @@ func (s *ConfigService) InitConfig() error {
return err return err
} }
} }
return s.RefreshApiAddr(&data) var singboxConfig SingBoxConfig
err = json.Unmarshal(data, &singboxConfig)
if err != nil {
return err
} }
func (s *ConfigService) GetConfig() (*[]byte, error) { return s.RefreshApiAddr(&singboxConfig)
}
func (s *ConfigService) GetConfig() (*SingBoxConfig, error) {
configPath := config.GetBinFolderPath() configPath := config.GetBinFolderPath()
data, err := os.ReadFile(configPath + "/config.json") data, err := os.ReadFile(configPath + "/config.json")
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &data, nil singboxConfig := SingBoxConfig{}
err = json.Unmarshal(data, &singboxConfig)
if err != nil {
return nil, err
}
return &singboxConfig, nil
} }
func (s *ConfigService) SaveChanges(changes map[string]string, loginUser string) error { func (s *ConfigService) SaveChanges(changes map[string]string, loginUser string) error {
@@ -127,11 +138,7 @@ func (s *ConfigService) SaveChanges(changes map[string]string, loginUser string)
if err != nil { if err != nil {
return err return err
} }
newConfig := SingBoxConfig{} newConfig := *singboxConfig
err = json.Unmarshal(*singboxConfig, &newConfig)
if err != nil {
return err
}
for _, change := range configChanges { for _, change := range configChanges {
rawObject := change.Obj rawObject := change.Obj
switch change.Key { switch change.Key {
@@ -169,12 +176,7 @@ func (s *ConfigService) SaveChanges(changes map[string]string, loginUser string)
} }
} }
// Save to config.json err = s.Save(&newConfig)
data, err := json.MarshalIndent(newConfig, "", " ")
if err != nil {
return err
}
err = s.Save(&data)
if err != nil { if err != nil {
return err return err
} }
@@ -215,7 +217,7 @@ func (s *ConfigService) CheckChanges(lu string) (bool, error) {
} }
} }
func (s *ConfigService) Save(data *[]byte) error { func (s *ConfigService) Save(singboxConfig *SingBoxConfig) error {
configPath := config.GetBinFolderPath() configPath := config.GetBinFolderPath()
_, err := os.Stat(configPath + "/config.json") _, err := os.Stat(configPath + "/config.json")
if os.IsNotExist(err) { if os.IsNotExist(err) {
@@ -227,35 +229,36 @@ func (s *ConfigService) Save(data *[]byte) error {
return err return err
} }
err = os.WriteFile(configPath+"/config.json", *data, 0764) data, err := json.MarshalIndent(singboxConfig, " ", " ")
if err != nil { if err != nil {
return err return err
} }
s.RefreshApiAddr(data) err = os.WriteFile(configPath+"/config.json", data, 0764)
if err != nil {
return err
}
s.RefreshApiAddr(singboxConfig)
s.Controller.Restart() s.Controller.Restart()
return nil return nil
} }
func (s *ConfigService) RefreshApiAddr(data *[]byte) error { func (s *ConfigService) RefreshApiAddr(singboxConfig *SingBoxConfig) error {
Env_API := config.GetEnvApi() Env_API := config.GetEnvApi()
if len(Env_API) > 0 { if len(Env_API) > 0 {
ApiAddr = Env_API ApiAddr = Env_API
} else { } else {
var err error var err error
if data == nil { if singboxConfig == nil {
data, err = s.GetConfig() singboxConfig, err = s.GetConfig()
if err != nil { if err != nil {
return err return err
} }
}
singboxConfig := SingBoxConfig{}
err = json.Unmarshal(*data, &singboxConfig)
if err != nil {
return err
} }
var experimental struct { var experimental struct {
V2rayApi struct { V2rayApi struct {
Listen string `json:"listen"` Listen string `json:"listen"`
@@ -282,12 +285,7 @@ func (s *ConfigService) DepleteClients() error {
if err != nil { if err != nil {
return err return err
} }
newConfig := SingBoxConfig{} for inbound_index, inbound := range singboxConfig.Inbounds {
err = json.Unmarshal(*singboxConfig, &newConfig)
if err != nil {
return err
}
for inbound_index, inbound := range newConfig.Inbounds {
var inboundJson map[string]interface{} var inboundJson map[string]interface{}
json.Unmarshal(inbound, &inboundJson) json.Unmarshal(inbound, &inboundJson)
if s.contains(inbounds, inboundJson["tag"].(string)) { if s.contains(inbounds, inboundJson["tag"].(string)) {
@@ -326,13 +324,10 @@ func (s *ConfigService) DepleteClients() error {
if err != nil { if err != nil {
return err return err
} }
newConfig.Inbounds[inbound_index] = modifiedInbound singboxConfig.Inbounds[inbound_index] = modifiedInbound
} }
modifiedConfig, err := json.MarshalIndent(newConfig, "", " ")
if err != nil { err = s.Save(singboxConfig)
return err
}
err = s.Save(&modifiedConfig)
if err != nil { if err != nil {
return err return err
} }
+2 -7
View File
@@ -1,7 +1,6 @@
package service package service
import ( import (
"encoding/json"
"s-ui/database" "s-ui/database"
"s-ui/database/model" "s-ui/database/model"
"time" "time"
@@ -86,12 +85,8 @@ func (s *StatsService) GetStats(resorce string, tag string, limit int) ([]model.
return result, nil return result, nil
} }
func (s *StatsService) GetOnlines() (string, error) { func (s *StatsService) GetOnlines() (onlines, error) {
onlines, err := json.Marshal(onlineResources) return *onlineResources, nil
if err != nil {
return "", err
}
return string(onlines), nil
} }
func (s *StatsService) DelOldStats(days int) error { func (s *StatsService) DelOldStats(days int) error {
oldTime := time.Now().AddDate(0, 0, -(days)).Unix() oldTime := time.Now().AddDate(0, 0, -(days)).Unix()
+4 -7
View File
@@ -11,18 +11,15 @@ import (
type TlsService struct { type TlsService struct {
} }
func (s *TlsService) GetAll() (string, error) { func (s *TlsService) GetAll() ([]model.Tls, error) {
db := database.GetDB() db := database.GetDB()
tlsConfig := []model.Tls{} tlsConfig := []model.Tls{}
err := db.Model(model.Tls{}).Scan(&tlsConfig).Error err := db.Model(model.Tls{}).Scan(&tlsConfig).Error
if err != nil { if err != nil {
return "", err return nil, err
} }
data, err := json.Marshal(tlsConfig)
if err != nil { return tlsConfig, nil
return "", err
}
return string(data), nil
} }
func (s *TlsService) Save(tx *gorm.DB, changes []model.Changes) error { func (s *TlsService) Save(tx *gorm.DB, changes []model.Changes) error {
+9 -10
View File
@@ -179,7 +179,7 @@ export default {
const newData = JSON.parse(this.$props.data) const newData = JSON.parse(this.$props.data)
this.client = createClient(newData) this.client = createClient(newData)
this.title = "edit" this.title = "edit"
this.clientConfig = JSON.parse(this.client.config) this.clientConfig = this.client.config
} }
else { else {
this.client = createClient() this.client = createClient()
@@ -187,10 +187,9 @@ export default {
this.clientConfig = randomConfigs('client') this.clientConfig = randomConfigs('client')
} }
this.clientStats = this.$props.stats this.clientStats = this.$props.stats
const allLinks = <Link[]>JSON.parse(this.client.links) this.links = this.client.links.filter(l => l.type == 'local')
this.links = allLinks.filter(l => l.type == 'local') this.extLinks = this.client.links.filter(l => l.type == 'external')
this.extLinks = allLinks.filter(l => l.type == 'external') this.subLinks = this.client.links.filter(l => l.type == 'sub')
this.subLinks = allLinks.filter(l => l.type == 'sub')
this.tab = "t1" this.tab = "t1"
}, },
closeModal() { closeModal() {
@@ -199,11 +198,11 @@ export default {
}, },
saveChanges() { saveChanges() {
this.loading = true this.loading = true
this.client.config = updateConfigs(JSON.stringify(this.clientConfig), this.client.name) this.client.config = updateConfigs(this.clientConfig, this.client.name)
this.client.links = JSON.stringify([ this.client.links = [
...this.links, ...this.links,
...this.extLinks.filter(l => l.uri != ''), ...this.extLinks.filter(l => l.uri != ''),
...this.subLinks.filter(l => l.uri != '')]) ...this.subLinks.filter(l => l.uri != '')]
this.$emit('save', this.client, this.clientStats) this.$emit('save', this.client, this.clientStats)
this.loading = false this.loading = false
}, },
@@ -213,8 +212,8 @@ export default {
}, },
computed: { computed: {
clientInbounds: { clientInbounds: {
get() { return this.client.inbounds == "" ? [] : this.client.inbounds.split(',').filter(i => this.inboundTags.includes(i)) }, get() { return this.client.inbounds.length>0 ? this.client.inbounds.filter(i => this.inboundTags.includes(i)) : [] },
set(newValue:string[]) { this.client.inbounds = newValue.length == 0 ? "" : newValue.join(',') } set(newValue:string[]) { this.client.inbounds = newValue.length == 0 ? [] : newValue }
}, },
expDate: { expDate: {
get() { return this.client.expiry}, get() { return this.client.expiry},
+4 -4
View File
@@ -20,7 +20,7 @@ const Data = defineStore('Data', {
this.lastLoad = Math.floor((new Date()).getTime()/1000) this.lastLoad = Math.floor((new Date()).getTime()/1000)
// Set new data // Set new data
const data = JSON.parse(msg.obj) const data = JSON.parse(JSON.stringify(msg.obj))
if (data.subURI) this.subURI = data.subURI if (data.subURI) this.subURI = data.subURI
if (data.config) this.config = data.config if (data.config) this.config = data.config
if (data.clients) this.clients = data.clients if (data.clients) this.clients = data.clients
@@ -28,9 +28,9 @@ const Data = defineStore('Data', {
this.onlines = data.onlines this.onlines = data.onlines
// To avoid ref copy // To avoid ref copy
if (data.config) this.oldData.config = { ...JSON.parse(msg.obj).config } if (data.config) this.oldData.config = { ...msg.obj }.config
if (data.clients) this.oldData.clients = [ ...JSON.parse(msg.obj).clients ] if (data.clients) this.oldData.clients = { ...msg.obj }.clients
if (data.tls) this.oldData.tlsConfigs = [ ...JSON.parse(msg.obj).tls ] if (data.tls) this.oldData.tlsConfigs = { ...msg.obj }.tls
} }
}, },
async pushData() { async pushData() {
+12 -12
View File
@@ -1,12 +1,13 @@
import { Link } from "@/plugins/link"
import RandomUtil from "@/plugins/randomUtil" import RandomUtil from "@/plugins/randomUtil"
export interface Client { export interface Client {
id?: number id?: number
enable: boolean enable: boolean
name: string name: string
config: string config: Config
inbounds: string inbounds: string[]
links: string links: Link[]
volume: number volume: number
expiry: number expiry: number
up: number up: number
@@ -17,9 +18,9 @@ export interface Client {
const defaultClient: Client = { const defaultClient: Client = {
enable: true, enable: true,
name: "", name: "",
config: "[]", config: {},
inbounds: "", inbounds: [],
links: "[]", links: [],
volume: 0, volume: 0,
expiry: 0, expiry: 0,
up: 0, up: 0,
@@ -35,12 +36,11 @@ type Config = {
} }
} }
export function updateConfigs(configs: string, newUserName: string): string { export function updateConfigs(configs: Config, newUserName: string): Config {
const updatedConfigs: Config = JSON.parse(configs)
for (const key in updatedConfigs) { for (const key in configs) {
if (updatedConfigs.hasOwnProperty(key)) { if (configs.hasOwnProperty(key)) {
const config = updatedConfigs[key] const config = configs[key]
if (config.hasOwnProperty("name")) { if (config.hasOwnProperty("name")) {
config.name = newUserName config.name = newUserName
} else if (config.hasOwnProperty("username")) { } else if (config.hasOwnProperty("username")) {
@@ -49,7 +49,7 @@ export function updateConfigs(configs: string, newUserName: string): string {
} }
} }
return JSON.stringify(updatedConfigs) return configs
} }
export function randomConfigs(user: string): Config { export function randomConfigs(user: string): Config {
+14 -16
View File
@@ -38,7 +38,7 @@
<v-col cols="auto"> <v-col cols="auto">
<v-switch color="primary" <v-switch color="primary"
v-model="clients[index].enable" v-model="clients[index].enable"
@update:model-value="buildInboundsUsers(item.inbounds.split(','))" @update:model-value="buildInboundsUsers(item.inbounds)"
hideDetails density="compact" /> hideDetails density="compact" />
</v-col> </v-col>
</v-row> </v-row>
@@ -53,9 +53,9 @@
<v-col>{{ $t('pages.inbounds') }}</v-col> <v-col>{{ $t('pages.inbounds') }}</v-col>
<v-col dir="ltr"> <v-col dir="ltr">
<v-tooltip activator="parent" dir="ltr" location="bottom" v-if="item.inbounds != ''"> <v-tooltip activator="parent" dir="ltr" location="bottom" v-if="item.inbounds != ''">
<span v-for="i in item.inbounds.split(',')">{{ i }}<br /></span> <span v-for="i in item.inbounds">{{ i }}<br /></span>
</v-tooltip> </v-tooltip>
{{ item.inbounds != '' ? item.inbounds.split(',').length : 0 }} {{ item.inbounds.length }}
</v-col> </v-col>
</v-row> </v-row>
<v-row> <v-row>
@@ -189,19 +189,18 @@ const saveModal = (data:any, stats:boolean) => {
sb.showMessage(i18n.global.t('error.dplData') + ': ' + i18n.global.t('client.name') ,'error', 5000) sb.showMessage(i18n.global.t('error.dplData') + ': ' + i18n.global.t('client.name') ,'error', 5000)
return return
} }
const inboundTags: string[] = data.inbounds.split(',')?? []
if(modal.value.index == -1) { if(modal.value.index == -1) {
clients.value.push(data) clients.value.push(data)
} else { } else {
const oldData = createClient(clients.value[modal.value.index]) const oldData = createClient(clients.value[modal.value.index])
oldData.inbounds.split(',').forEach((i:string) => { oldData.inbounds.forEach((i:string) => {
if (!inboundTags.includes(i)) inboundTags.push(i) if (!data.inbounds.includes(i)) data.inbounds.push(i)
}) })
clients.value[modal.value.index] = data clients.value[modal.value.index] = data
} }
// Rebuild affected inbounds // Rebuild affected inbounds
buildInboundsUsers(inboundTags) buildInboundsUsers(data.inbounds)
// Rebuild links // Rebuild links
data.links = updateLinks(data) data.links = updateLinks(data)
@@ -229,15 +228,14 @@ const buildInboundsUsers = (inboundTags:string[]) => {
if (inbound_index != -1){ if (inbound_index != -1){
const users = <any>[] const users = <any>[]
const newInbound = <InboundWithUser>inbounds.value[inbound_index] const newInbound = <InboundWithUser>inbounds.value[inbound_index]
const inboundClients = clients.value.filter(c => c.enable && c.inbounds.split(',').includes(tag)) const inboundClients = clients.value.filter(c => c.enable && c.inbounds.includes(tag))
inboundClients.forEach(c => { inboundClients.forEach(c => {
const clientConfig = JSON.parse(c.config)
// Remove flow in non tls VLESS // Remove flow in non tls VLESS
if (newInbound.type == InTypes.VLESS) { if (newInbound.type == InTypes.VLESS) {
const vlessInbound = <VLESS>newInbound const vlessInbound = <VLESS>newInbound
if (!vlessInbound.tls?.enabled || vlessInbound.transport?.type) delete(clientConfig["vless"].flow) if (!vlessInbound.tls?.enabled || vlessInbound.transport?.type) delete(c.config?.vless?.flow)
} }
users.push(clientConfig[newInbound.type]) users.push(c.config[newInbound.type])
}) })
newInbound.users = users newInbound.users = users
@@ -257,8 +255,8 @@ const buildInboundsUsers = (inboundTags:string[]) => {
} }
}) })
} }
const updateLinks = (c:Client):string => { const updateLinks = (c:Client):Link[] => {
const clientInbounds = <Inbound[]>inbounds.value.filter(i => c.inbounds.split(',').includes(i.tag)) const clientInbounds = <Inbound[]>inbounds.value.filter(i => c.inbounds.includes(i.tag))
const newLinks = <Link[]>[] const newLinks = <Link[]>[]
clientInbounds.forEach(i =>{ clientInbounds.forEach(i =>{
const tlsConfig = <any>Data().tlsConfigs?.findLast((t:any) => t.inbounds.includes(i.tag)) const tlsConfig = <any>Data().tlsConfigs?.findLast((t:any) => t.inbounds.includes(i.tag))
@@ -267,10 +265,10 @@ const updateLinks = (c:Client):string => {
newLinks.push(<Link>{ type: 'local', remark: i.tag, uri: uri }) newLinks.push(<Link>{ type: 'local', remark: i.tag, uri: uri })
} }
}) })
let links = c.links && c.links.length>0? <Link[]>JSON.parse(c.links) : <Link[]>[] let links = c.links && c.links.length>0? c.links : <Link[]>[]
links = [...newLinks, ...links.filter(l => l.type != 'local')] links = [...newLinks, ...links.filter(l => l.type != 'local')]
return JSON.stringify(links) return links
} }
const delClient = (clientIndex: number) => { const delClient = (clientIndex: number) => {
const id = clients.value[clientIndex].id const id = clients.value[clientIndex].id
@@ -284,7 +282,7 @@ const delClient = (clientIndex: number) => {
} }
clients.value.splice(clientIndex,1) clients.value.splice(clientIndex,1)
buildInboundsUsers(oldData.inbounds.split(',')) buildInboundsUsers(oldData.inbounds)
if (id>0) Data().delClient(id) if (id>0) Data().delClient(id)
delOverlay.value[clientIndex] = false delOverlay.value[clientIndex] = false
} }
+11 -13
View File
@@ -222,7 +222,7 @@ const updateLinks = (i: InboundWithUser) => {
i.users.forEach((u:any) => { i.users.forEach((u:any) => {
const client = clients.value.find(c => u.username? c.name == u.username : c.name == u.name) const client = clients.value.find(c => u.username? c.name == u.username : c.name == u.name)
if (client){ if (client){
const clientInbounds = <Inbound[]>inbounds.value.filter(inb => client?.inbounds.split(',').includes(inb.tag)) const clientInbounds = <Inbound[]>inbounds.value.filter(inb => client?.inbounds.includes(inb.tag))
const newLinks = <Link[]>[] const newLinks = <Link[]>[]
clientInbounds.forEach(i =>{ clientInbounds.forEach(i =>{
const tlsClient = tlsConfigs?.value.findLast((t:any) => t.inbounds.includes(i.tag))?.client?? null const tlsClient = tlsConfigs?.value.findLast((t:any) => t.inbounds.includes(i.tag))?.client?? null
@@ -231,10 +231,10 @@ const updateLinks = (i: InboundWithUser) => {
newLinks.push(<Link>{ type: 'local', remark: i.tag, uri: uri }) newLinks.push(<Link>{ type: 'local', remark: i.tag, uri: uri })
} }
}) })
let links = client.links && client.links.length>0? <Link[]>JSON.parse(client.links) : <Link[]>[] let links = client.links && client.links.length>0? client.links : <Link[]>[]
links = [...newLinks, ...links.filter(l => l.type != 'local')] links = [...newLinks, ...links.filter(l => l.type != 'local')]
client.links = JSON.stringify(links) client.links = links
} }
}) })
} }
@@ -250,8 +250,8 @@ const delInbound = (index: number) => {
inbU.users.forEach((u:any) => { inbU.users.forEach((u:any) => {
const c_index = clients.value.findIndex(c => u.username? u.username == c.name : u.user == c.name) const c_index = clients.value.findIndex(c => u.username? u.username == c.name : u.user == c.name)
if (c_index != -1) { if (c_index != -1) {
const clientInbounds = clients.value[c_index].inbounds.split(',').filter((x:string) => x!=tag) const clientInbounds = clients.value[c_index].inbounds.filter((x:string) => x!=tag)
clients.value[c_index].inbounds = clientInbounds.join(',') clients.value[c_index].inbounds = clientInbounds
} }
}) })
} }
@@ -278,15 +278,14 @@ const delInbound = (index: number) => {
} }
const buildInboundsUsers = (inbound:InboundWithUser):Inbound => { const buildInboundsUsers = (inbound:InboundWithUser):Inbound => {
const users = <any>[] const users = <any>[]
const inboundClients = clients.value.filter(c => c.enable && c.inbounds.split(',').includes(inbound.tag)) const inboundClients = clients.value.filter(c => c.enable && c.inbounds.includes(inbound.tag))
inboundClients.forEach(c => { inboundClients.forEach(c => {
const clientConfig = JSON.parse(c.config)
// Remove flow in non tls VLESS // Remove flow in non tls VLESS
if (inbound.type == InTypes.VLESS) { if (inbound.type == InTypes.VLESS) {
const vlessInbound = <VLESS>inbound const vlessInbound = <VLESS>inbound
if (!vlessInbound.tls?.enabled || vlessInbound.transport?.type) delete(clientConfig["vless"].flow) if (!vlessInbound.tls?.enabled || vlessInbound.transport?.type) delete(c.config?.vless?.flow)
} }
users.push(clientConfig[inbound.type]) users.push(c.config[inbound.type])
}) })
inbound.users = users inbound.users = users
@@ -306,11 +305,10 @@ const buildInboundsUsers = (inbound:InboundWithUser):Inbound => {
} }
const changeClientInboundsTag = (oldtag: string, newTag:string) => { const changeClientInboundsTag = (oldtag: string, newTag:string) => {
clients.value.forEach((c, c_index) => { clients.value.forEach((c, c_index) => {
const inboundsArray = c.inbounds.split(',') const inbound_index = c.inbounds.findIndex(i => i == oldtag)
const inbound_index = inboundsArray.findIndex(i => i == oldtag)
if (inbound_index != -1) { if (inbound_index != -1) {
inboundsArray[inbound_index] = newTag c.inbounds[inbound_index] = newTag
clients.value[c_index].inbounds = inboundsArray.join(',') clients.value[c_index].inbounds = c.inbounds
} }
}) })
} }
+4 -4
View File
@@ -141,7 +141,7 @@ const updateLinks = (i:any,tlsClient:any) => {
i.users.forEach((u:any) => { i.users.forEach((u:any) => {
const client = clients.value.find(c => u.username? c.name == u.username : c.name == u.name) const client = clients.value.find(c => u.username? c.name == u.username : c.name == u.name)
if (client){ if (client){
const clientInbounds = <Inbound[]>inbounds.value.filter(inb => client?.inbounds.split(',').includes(inb.tag)) const clientInbounds = <Inbound[]>inbounds.value.filter(inb => client?.inbounds.includes(inb.tag))
const newLinks = <Link[]>[] const newLinks = <Link[]>[]
clientInbounds.forEach(i =>{ clientInbounds.forEach(i =>{
const uri = LinkUtil.linkGenerator(client.name,i,tlsClient) const uri = LinkUtil.linkGenerator(client.name,i,tlsClient)
@@ -149,10 +149,10 @@ const updateLinks = (i:any,tlsClient:any) => {
newLinks.push(<Link>{ type: 'local', remark: i.tag, uri: uri }) newLinks.push(<Link>{ type: 'local', remark: i.tag, uri: uri })
} }
}) })
let links = client.links && client.links.length>0? <Link[]>JSON.parse(client.links) : <Link[]>[] let links = client.links && client.links.length>0? client.links : <Link[]>[]
links = [...newLinks, ...links.filter(l => l.type != 'local')] links = [...newLinks, ...links.filter((l:Link) => l.type != 'local')]
client.links = JSON.stringify(links) client.links = links
} }
}) })
} }