[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
import (
"fmt"
"s-ui/logger"
"s-ui/service"
"strconv"
@@ -157,8 +156,8 @@ func (a *APIHandler) getHandler(c *gin.Context) {
}
}
func (a *APIHandler) loadData(c *gin.Context) (string, error) {
var data string
func (a *APIHandler) loadData(c *gin.Context) (interface{}, error) {
data := make(map[string]interface{}, 0)
lu := c.Query("lu")
isUpdated, err := a.ConfigService.CheckChanges(lu)
if err != nil {
@@ -185,9 +184,13 @@ func (a *APIHandler) loadData(c *gin.Context) (string, error) {
if err != nil {
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 {
data = fmt.Sprintf(`{"onlines": %s}`, onlines)
data["onlines"] = onlines
}
return data, nil
+11 -11
View File
@@ -24,17 +24,17 @@ type User struct {
}
type Client struct {
Id uint `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
Enable bool `json:"enable" form:"enable"`
Name string `json:"name" form:"name"`
Config string `json:"config" form:"config"`
Inbounds string `json:"inbounds" form:"inbounds"`
Links string `json:"links" form:"links"`
Volume int64 `json:"volume" form:"volume"`
Expiry int64 `json:"expiry" form:"expiry"`
Down int64 `json:"down" form:"down"`
Up int64 `json:"up" form:"up"`
Desc string `json:"desc" from:"desc"`
Id uint `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
Enable bool `json:"enable" form:"enable"`
Name string `json:"name" form:"name"`
Config json.RawMessage `json:"config" form:"config"`
Inbounds json.RawMessage `json:"inbounds" form:"inbounds"`
Links json.RawMessage `json:"links" form:"links"`
Volume int64 `json:"volume" form:"volume"`
Expiry int64 `json:"expiry" form:"expiry"`
Down int64 `json:"down" form:"down"`
Up int64 `json:"up" form:"up"`
Desc string `json:"desc" from:"desc"`
}
type Stats struct {
+8 -10
View File
@@ -5,7 +5,6 @@ import (
"s-ui/database"
"s-ui/database/model"
"s-ui/logger"
"strings"
"time"
"gorm.io/gorm"
@@ -14,18 +13,14 @@ import (
type ClientService struct {
}
func (s *ClientService) GetAll() (string, error) {
func (s *ClientService) GetAll() ([]model.Client, error) {
db := database.GetDB()
clients := []model.Client{}
err := db.Model(model.Client{}).Scan(&clients).Error
if err != nil {
return "", err
return nil, err
}
data, err := json.Marshal(clients)
if err != nil {
return "", err
}
return string(data), nil
return clients, nil
}
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
}
dt := time.Now().Unix()
var users, inbounds []string
for _, client := range clients {
logger.Debug("Client ", client.Name, " is going to be disabled")
users = append(users, client.Name)
userInbounds := strings.Split(client.Inbounds, ",")
var userInbounds []string
json.Unmarshal(client.Inbounds, &userInbounds)
inbounds = append(inbounds, userInbounds...)
changes = append(changes, model.Changes{
DateTime: time.Now().Unix(),
DateTime: dt,
Actor: "DepleteJob",
Key: "clients",
Action: "disable",
@@ -87,6 +84,7 @@ func (s *ClientService) DepleteClients() ([]string, []string, error) {
if err != nil {
return nil, nil, err
}
LastUpdate = dt
}
return users, inbounds, nil
+32 -37
View File
@@ -54,16 +54,27 @@ func (s *ConfigService) InitConfig() error {
return err
}
}
return s.RefreshApiAddr(&data)
var singboxConfig SingBoxConfig
err = json.Unmarshal(data, &singboxConfig)
if err != nil {
return err
}
return s.RefreshApiAddr(&singboxConfig)
}
func (s *ConfigService) GetConfig() (*[]byte, error) {
func (s *ConfigService) GetConfig() (*SingBoxConfig, error) {
configPath := config.GetBinFolderPath()
data, err := os.ReadFile(configPath + "/config.json")
if err != nil {
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 {
@@ -127,11 +138,7 @@ func (s *ConfigService) SaveChanges(changes map[string]string, loginUser string)
if err != nil {
return err
}
newConfig := SingBoxConfig{}
err = json.Unmarshal(*singboxConfig, &newConfig)
if err != nil {
return err
}
newConfig := *singboxConfig
for _, change := range configChanges {
rawObject := change.Obj
switch change.Key {
@@ -169,12 +176,7 @@ func (s *ConfigService) SaveChanges(changes map[string]string, loginUser string)
}
}
// Save to config.json
data, err := json.MarshalIndent(newConfig, "", " ")
if err != nil {
return err
}
err = s.Save(&data)
err = s.Save(&newConfig)
if err != nil {
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()
_, err := os.Stat(configPath + "/config.json")
if os.IsNotExist(err) {
@@ -227,35 +229,36 @@ func (s *ConfigService) Save(data *[]byte) error {
return err
}
err = os.WriteFile(configPath+"/config.json", *data, 0764)
data, err := json.MarshalIndent(singboxConfig, " ", " ")
if err != nil {
return err
}
s.RefreshApiAddr(data)
err = os.WriteFile(configPath+"/config.json", data, 0764)
if err != nil {
return err
}
s.RefreshApiAddr(singboxConfig)
s.Controller.Restart()
return nil
}
func (s *ConfigService) RefreshApiAddr(data *[]byte) error {
func (s *ConfigService) RefreshApiAddr(singboxConfig *SingBoxConfig) error {
Env_API := config.GetEnvApi()
if len(Env_API) > 0 {
ApiAddr = Env_API
} else {
var err error
if data == nil {
data, err = s.GetConfig()
if singboxConfig == nil {
singboxConfig, err = s.GetConfig()
if err != nil {
return err
}
}
singboxConfig := SingBoxConfig{}
err = json.Unmarshal(*data, &singboxConfig)
if err != nil {
return err
}
var experimental struct {
V2rayApi struct {
Listen string `json:"listen"`
@@ -282,12 +285,7 @@ func (s *ConfigService) DepleteClients() error {
if err != nil {
return err
}
newConfig := SingBoxConfig{}
err = json.Unmarshal(*singboxConfig, &newConfig)
if err != nil {
return err
}
for inbound_index, inbound := range newConfig.Inbounds {
for inbound_index, inbound := range singboxConfig.Inbounds {
var inboundJson map[string]interface{}
json.Unmarshal(inbound, &inboundJson)
if s.contains(inbounds, inboundJson["tag"].(string)) {
@@ -326,13 +324,10 @@ func (s *ConfigService) DepleteClients() error {
if err != nil {
return err
}
newConfig.Inbounds[inbound_index] = modifiedInbound
singboxConfig.Inbounds[inbound_index] = modifiedInbound
}
modifiedConfig, err := json.MarshalIndent(newConfig, "", " ")
if err != nil {
return err
}
err = s.Save(&modifiedConfig)
err = s.Save(singboxConfig)
if err != nil {
return err
}
+2 -7
View File
@@ -1,7 +1,6 @@
package service
import (
"encoding/json"
"s-ui/database"
"s-ui/database/model"
"time"
@@ -86,12 +85,8 @@ func (s *StatsService) GetStats(resorce string, tag string, limit int) ([]model.
return result, nil
}
func (s *StatsService) GetOnlines() (string, error) {
onlines, err := json.Marshal(onlineResources)
if err != nil {
return "", err
}
return string(onlines), nil
func (s *StatsService) GetOnlines() (onlines, error) {
return *onlineResources, nil
}
func (s *StatsService) DelOldStats(days int) error {
oldTime := time.Now().AddDate(0, 0, -(days)).Unix()
+4 -7
View File
@@ -11,18 +11,15 @@ import (
type TlsService struct {
}
func (s *TlsService) GetAll() (string, error) {
func (s *TlsService) GetAll() ([]model.Tls, error) {
db := database.GetDB()
tlsConfig := []model.Tls{}
err := db.Model(model.Tls{}).Scan(&tlsConfig).Error
if err != nil {
return "", err
return nil, err
}
data, err := json.Marshal(tlsConfig)
if err != nil {
return "", err
}
return string(data), nil
return tlsConfig, nil
}
func (s *TlsService) Save(tx *gorm.DB, changes []model.Changes) error {
+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
}
})
}