[refactor] string values to json
This commit is contained in:
+8
-5
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user