update ws & settings api (#547)

This commit is contained in:
UUBulb 2024-12-04 20:00:18 +08:00 committed by GitHub
parent 6295371cac
commit 872002d938
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 38 additions and 9 deletions

View File

@ -14,19 +14,25 @@ import (
// @Security BearerAuth // @Security BearerAuth
// @Tags common // @Tags common
// @Produce json // @Produce json
// @Success 200 {object} model.CommonResponse[model.Config] // @Success 200 {object} model.CommonResponse[model.SettingResponse]
// @Router /setting [get] // @Router /setting [get]
func listConfig(c *gin.Context) (model.Config, error) { func listConfig(c *gin.Context) (model.SettingResponse, error) {
_, isMember := c.Get(model.CtxKeyAuthorizedUser) _, isMember := c.Get(model.CtxKeyAuthorizedUser)
authorized := isMember // TODO || isViewPasswordVerfied authorized := isMember // TODO || isViewPasswordVerfied
conf := *singleton.Conf conf := model.SettingResponse{
Config: *singleton.Conf,
Version: singleton.Version,
}
if !authorized { if !authorized {
conf = model.Config{ conf = model.SettingResponse{
SiteName: conf.SiteName, Config: model.Config{
Language: conf.Language, SiteName: conf.SiteName,
CustomCode: conf.CustomCode, Language: conf.Language,
CustomCodeDashboard: conf.CustomCodeDashboard, CustomCode: conf.CustomCode,
CustomCodeDashboard: conf.CustomCodeDashboard,
},
} }
} }
@ -62,6 +68,7 @@ func updateConfig(c *gin.Context) (any, error) {
singleton.Conf.CustomCode = sf.CustomCode singleton.Conf.CustomCode = sf.CustomCode
singleton.Conf.CustomCodeDashboard = sf.CustomCodeDashboard singleton.Conf.CustomCodeDashboard = sf.CustomCodeDashboard
singleton.Conf.RealIPHeader = sf.RealIPHeader singleton.Conf.RealIPHeader = sf.RealIPHeader
singleton.Conf.TLS = sf.TLS
if err := singleton.Conf.Save(); err != nil { if err := singleton.Conf.Save(); err != nil {
return nil, newGormError("%v", err) return nil, newGormError("%v", err)

View File

@ -155,7 +155,7 @@ func getServerStat(c *gin.Context, withPublicNote bool) ([]byte, error) {
Name: server.Name, Name: server.Name,
PublicNote: utils.IfOr(withPublicNote, server.PublicNote, ""), PublicNote: utils.IfOr(withPublicNote, server.PublicNote, ""),
DisplayIndex: server.DisplayIndex, DisplayIndex: server.DisplayIndex,
Host: server.Host, Host: utils.IfOr(authorized, server.Host, server.Host.Filter()),
State: server.State, State: server.State,
CountryCode: countryCode, CountryCode: countryCode,
LastActive: server.LastActive, LastActive: server.LastActive,

View File

@ -127,6 +127,21 @@ func (h *Host) PB() *pb.Host {
} }
} }
// Filter returns a new instance of Host with some fields redacted.
func (h *Host) Filter() *Host {
return &Host{
Platform: h.Platform,
CPU: h.CPU,
MemTotal: h.MemTotal,
DiskTotal: h.DiskTotal,
SwapTotal: h.SwapTotal,
Arch: h.Arch,
Virtualization: h.Virtualization,
BootTime: h.BootTime,
GPU: h.GPU,
}
}
func PB2Host(h *pb.Host) Host { func PB2Host(h *pb.Host) Host {
return Host{ return Host{
Platform: h.GetPlatform(), Platform: h.GetPlatform(),

View File

@ -12,6 +12,13 @@ type SettingForm struct {
CustomCodeDashboard string `json:"custom_code_dashboard,omitempty" validate:"optional"` CustomCodeDashboard string `json:"custom_code_dashboard,omitempty" validate:"optional"`
RealIPHeader string `json:"real_ip_header,omitempty" validate:"optional"` // 真实IP RealIPHeader string `json:"real_ip_header,omitempty" validate:"optional"` // 真实IP
TLS bool `json:"tls,omitempty" validate:"optional"`
EnableIPChangeNotification bool `json:"enable_ip_change_notification,omitempty" validate:"optional"` EnableIPChangeNotification bool `json:"enable_ip_change_notification,omitempty" validate:"optional"`
EnablePlainIPInNotification bool `json:"enable_plain_ip_in_notification,omitempty" validate:"optional"` EnablePlainIPInNotification bool `json:"enable_plain_ip_in_notification,omitempty" validate:"optional"`
} }
type SettingResponse struct {
Config
Version string `json:"version,omitempty"`
}