nezha/model/server.go

78 lines
2.5 KiB
Go
Raw Permalink Normal View History

2019-12-08 10:18:29 -05:00
package model
2019-12-10 04:57:57 -05:00
import (
2020-12-08 21:27:00 -05:00
"fmt"
"html/template"
"log"
2024-08-11 22:06:55 -04:00
"sync"
2020-10-24 09:29:05 -04:00
"time"
"github.com/naiba/nezha/pkg/utils"
2020-11-10 21:07:45 -05:00
pb "github.com/naiba/nezha/proto"
"gorm.io/gorm"
2019-12-10 04:57:57 -05:00
)
2019-12-08 10:18:29 -05:00
type Server struct {
Common
2021-01-08 08:04:50 -05:00
Name string
Tag string // 分组名
Secret string `gorm:"uniqueIndex" json:"-"`
Note string `json:"-"` // 管理员可见备注
PublicNote string `json:"PublicNote,omitempty"` // 公开备注
DisplayIndex int // 展示排序,越大越靠前
HideForGuest bool // 对游客隐藏
EnableDDNS bool // 启用DDNS
DDNSProfiles []uint64 `gorm:"-" json:"-"` // DDNS配置
DDNSProfilesRaw string `gorm:"default:'[]';column:ddns_profiles_raw" json:"-"`
2021-01-17 09:05:59 -05:00
Host *Host `gorm:"-"`
State *HostState `gorm:"-"`
2021-01-18 00:45:06 -05:00
LastActive time.Time `gorm:"-"`
2019-12-10 04:57:57 -05:00
2024-08-11 22:06:55 -04:00
TaskClose chan error `gorm:"-" json:"-"`
TaskCloseLock *sync.Mutex `gorm:"-" json:"-"`
TaskStream pb.NezhaService_RequestTaskServer `gorm:"-" json:"-"`
PrevTransferInSnapshot int64 `gorm:"-" json:"-"` // 上次数据点时的入站使用量
PrevTransferOutSnapshot int64 `gorm:"-" json:"-"` // 上次数据点时的出站使用量
2019-12-08 10:18:29 -05:00
}
2020-12-08 21:27:00 -05:00
func (s *Server) CopyFromRunningServer(old *Server) {
s.Host = old.Host
s.State = old.State
s.LastActive = old.LastActive
s.TaskClose = old.TaskClose
2024-08-11 22:06:55 -04:00
s.TaskCloseLock = old.TaskCloseLock
s.TaskStream = old.TaskStream
s.PrevTransferInSnapshot = old.PrevTransferInSnapshot
s.PrevTransferOutSnapshot = old.PrevTransferOutSnapshot
}
func (s *Server) AfterFind(tx *gorm.DB) error {
if s.DDNSProfilesRaw != "" {
if err := utils.Json.Unmarshal([]byte(s.DDNSProfilesRaw), &s.DDNSProfiles); err != nil {
log.Println("NEZHA>> Server.AfterFind:", err)
return nil
}
}
return nil
}
func boolToString(b bool) string {
if b {
return "true"
}
return "false"
}
func (s Server) MarshalForDashboard() template.JS {
name, _ := utils.Json.Marshal(s.Name)
tag, _ := utils.Json.Marshal(s.Tag)
note, _ := utils.Json.Marshal(s.Note)
secret, _ := utils.Json.Marshal(s.Secret)
ddnsProfilesRaw, _ := utils.Json.Marshal(s.DDNSProfilesRaw)
publicNote, _ := utils.Json.Marshal(s.PublicNote)
return template.JS(fmt.Sprintf(`{"ID":%d,"Name":%s,"Secret":%s,"DisplayIndex":%d,"Tag":%s,"Note":%s,"HideForGuest": %s,"EnableDDNS": %s,"DDNSProfilesRaw": %s,"PublicNote": %s}`, s.ID, name, secret, s.DisplayIndex, tag, note, boolToString(s.HideForGuest), boolToString(s.EnableDDNS), ddnsProfilesRaw, publicNote))
2020-12-08 21:27:00 -05:00
}