[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
+11 -12
View File
@@ -179,7 +179,7 @@ export default {
const newData = JSON.parse(this.$props.data)
this.client = createClient(newData)
this.title = "edit"
this.clientConfig = JSON.parse(this.client.config)
this.clientConfig = this.client.config
}
else {
this.client = createClient()
@@ -187,10 +187,9 @@ export default {
this.clientConfig = randomConfigs('client')
}
this.clientStats = this.$props.stats
const allLinks = <Link[]>JSON.parse(this.client.links)
this.links = allLinks.filter(l => l.type == 'local')
this.extLinks = allLinks.filter(l => l.type == 'external')
this.subLinks = allLinks.filter(l => l.type == 'sub')
this.links = this.client.links.filter(l => l.type == 'local')
this.extLinks = this.client.links.filter(l => l.type == 'external')
this.subLinks = this.client.links.filter(l => l.type == 'sub')
this.tab = "t1"
},
closeModal() {
@@ -199,11 +198,11 @@ export default {
},
saveChanges() {
this.loading = true
this.client.config = updateConfigs(JSON.stringify(this.clientConfig), this.client.name)
this.client.links = JSON.stringify([
...this.links,
...this.extLinks.filter(l => l.uri != ''),
...this.subLinks.filter(l => l.uri != '')])
this.client.config = updateConfigs(this.clientConfig, this.client.name)
this.client.links = [
...this.links,
...this.extLinks.filter(l => l.uri != ''),
...this.subLinks.filter(l => l.uri != '')]
this.$emit('save', this.client, this.clientStats)
this.loading = false
},
@@ -213,8 +212,8 @@ export default {
},
computed: {
clientInbounds: {
get() { return this.client.inbounds == "" ? [] : this.client.inbounds.split(',').filter(i => this.inboundTags.includes(i)) },
set(newValue:string[]) { this.client.inbounds = newValue.length == 0 ? "" : newValue.join(',') }
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 }
},
expDate: {
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)
// 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.config) this.config = data.config
if (data.clients) this.clients = data.clients
@@ -28,9 +28,9 @@ const Data = defineStore('Data', {
this.onlines = data.onlines
// To avoid ref copy
if (data.config) this.oldData.config = { ...JSON.parse(msg.obj).config }
if (data.clients) this.oldData.clients = [ ...JSON.parse(msg.obj).clients ]
if (data.tls) this.oldData.tlsConfigs = [ ...JSON.parse(msg.obj).tls ]
if (data.config) this.oldData.config = { ...msg.obj }.config
if (data.clients) this.oldData.clients = { ...msg.obj }.clients
if (data.tls) this.oldData.tlsConfigs = { ...msg.obj }.tls
}
},
async pushData() {
+12 -12
View File
@@ -1,12 +1,13 @@
import { Link } from "@/plugins/link"
import RandomUtil from "@/plugins/randomUtil"
export interface Client {
id?: number
enable: boolean
name: string
config: string
inbounds: string
links: string
config: Config
inbounds: string[]
links: Link[]
volume: number
expiry: number
up: number
@@ -17,9 +18,9 @@ export interface Client {
const defaultClient: Client = {
enable: true,
name: "",
config: "[]",
inbounds: "",
links: "[]",
config: {},
inbounds: [],
links: [],
volume: 0,
expiry: 0,
up: 0,
@@ -35,12 +36,11 @@ type Config = {
}
}
export function updateConfigs(configs: string, newUserName: string): string {
const updatedConfigs: Config = JSON.parse(configs)
export function updateConfigs(configs: Config, newUserName: string): Config {
for (const key in updatedConfigs) {
if (updatedConfigs.hasOwnProperty(key)) {
const config = updatedConfigs[key]
for (const key in configs) {
if (configs.hasOwnProperty(key)) {
const config = configs[key]
if (config.hasOwnProperty("name")) {
config.name = newUserName
} 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 {
+14 -16
View File
@@ -38,7 +38,7 @@
<v-col cols="auto">
<v-switch color="primary"
v-model="clients[index].enable"
@update:model-value="buildInboundsUsers(item.inbounds.split(','))"
@update:model-value="buildInboundsUsers(item.inbounds)"
hideDetails density="compact" />
</v-col>
</v-row>
@@ -53,9 +53,9 @@
<v-col>{{ $t('pages.inbounds') }}</v-col>
<v-col dir="ltr">
<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>
{{ item.inbounds != '' ? item.inbounds.split(',').length : 0 }}
{{ item.inbounds.length }}
</v-col>
</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)
return
}
const inboundTags: string[] = data.inbounds.split(',')?? []
if(modal.value.index == -1) {
clients.value.push(data)
} else {
const oldData = createClient(clients.value[modal.value.index])
oldData.inbounds.split(',').forEach((i:string) => {
if (!inboundTags.includes(i)) inboundTags.push(i)
oldData.inbounds.forEach((i:string) => {
if (!data.inbounds.includes(i)) data.inbounds.push(i)
})
clients.value[modal.value.index] = data
}
// Rebuild affected inbounds
buildInboundsUsers(inboundTags)
buildInboundsUsers(data.inbounds)
// Rebuild links
data.links = updateLinks(data)
@@ -229,15 +228,14 @@ const buildInboundsUsers = (inboundTags:string[]) => {
if (inbound_index != -1){
const users = <any>[]
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 => {
const clientConfig = JSON.parse(c.config)
// Remove flow in non tls VLESS
if (newInbound.type == InTypes.VLESS) {
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
@@ -257,8 +255,8 @@ const buildInboundsUsers = (inboundTags:string[]) => {
}
})
}
const updateLinks = (c:Client):string => {
const clientInbounds = <Inbound[]>inbounds.value.filter(i => c.inbounds.split(',').includes(i.tag))
const updateLinks = (c:Client):Link[] => {
const clientInbounds = <Inbound[]>inbounds.value.filter(i => c.inbounds.includes(i.tag))
const newLinks = <Link[]>[]
clientInbounds.forEach(i =>{
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 })
}
})
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')]
return JSON.stringify(links)
return links
}
const delClient = (clientIndex: number) => {
const id = clients.value[clientIndex].id
@@ -284,7 +282,7 @@ const delClient = (clientIndex: number) => {
}
clients.value.splice(clientIndex,1)
buildInboundsUsers(oldData.inbounds.split(','))
buildInboundsUsers(oldData.inbounds)
if (id>0) Data().delClient(id)
delOverlay.value[clientIndex] = false
}
+11 -13
View File
@@ -222,7 +222,7 @@ const updateLinks = (i: InboundWithUser) => {
i.users.forEach((u:any) => {
const client = clients.value.find(c => u.username? c.name == u.username : c.name == u.name)
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[]>[]
clientInbounds.forEach(i =>{
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 })
}
})
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')]
client.links = JSON.stringify(links)
client.links = links
}
})
}
@@ -250,8 +250,8 @@ const delInbound = (index: number) => {
inbU.users.forEach((u:any) => {
const c_index = clients.value.findIndex(c => u.username? u.username == c.name : u.user == c.name)
if (c_index != -1) {
const clientInbounds = clients.value[c_index].inbounds.split(',').filter((x:string) => x!=tag)
clients.value[c_index].inbounds = clientInbounds.join(',')
const clientInbounds = clients.value[c_index].inbounds.filter((x:string) => x!=tag)
clients.value[c_index].inbounds = clientInbounds
}
})
}
@@ -278,15 +278,14 @@ const delInbound = (index: number) => {
}
const buildInboundsUsers = (inbound:InboundWithUser):Inbound => {
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 => {
const clientConfig = JSON.parse(c.config)
// Remove flow in non tls VLESS
if (inbound.type == InTypes.VLESS) {
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
@@ -306,11 +305,10 @@ const buildInboundsUsers = (inbound:InboundWithUser):Inbound => {
}
const changeClientInboundsTag = (oldtag: string, newTag:string) => {
clients.value.forEach((c, c_index) => {
const inboundsArray = c.inbounds.split(',')
const inbound_index = inboundsArray.findIndex(i => i == oldtag)
const inbound_index = c.inbounds.findIndex(i => i == oldtag)
if (inbound_index != -1) {
inboundsArray[inbound_index] = newTag
clients.value[c_index].inbounds = inboundsArray.join(',')
c.inbounds[inbound_index] = newTag
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) => {
const client = clients.value.find(c => u.username? c.name == u.username : c.name == u.name)
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[]>[]
clientInbounds.forEach(i =>{
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 })
}
})
let links = client.links && client.links.length>0? <Link[]>JSON.parse(client.links) : <Link[]>[]
links = [...newLinks, ...links.filter(l => l.type != 'local')]
let links = client.links && client.links.length>0? client.links : <Link[]>[]
links = [...newLinks, ...links.filter((l:Link) => l.type != 'local')]
client.links = JSON.stringify(links)
client.links = links
}
})
}