2022-05-16 23:21:27 -04:00
|
|
|
package singleton
|
|
|
|
|
2022-05-17 08:16:46 -04:00
|
|
|
import (
|
|
|
|
"github.com/naiba/nezha/model"
|
|
|
|
"github.com/naiba/nezha/pkg/utils"
|
|
|
|
)
|
2022-05-16 23:21:27 -04:00
|
|
|
|
2022-05-17 22:10:35 -04:00
|
|
|
var (
|
|
|
|
ApiTokenList = make(map[string]*model.ApiToken)
|
|
|
|
UserIDToApiTokenList = make(map[uint64][]string)
|
|
|
|
)
|
|
|
|
|
2022-05-16 23:21:27 -04:00
|
|
|
type ServerAPI struct {
|
|
|
|
Token string // 传入Token 后期可能会需要用于scope判定
|
|
|
|
IDList []uint64
|
|
|
|
Tag string
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:16:46 -04:00
|
|
|
// CommonResponse 常规返回结构 包含状态码 和 状态信息
|
2022-05-16 23:21:27 -04:00
|
|
|
type CommonResponse struct {
|
|
|
|
Code int `json:"code"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:16:46 -04:00
|
|
|
type CommonServerInfo struct {
|
|
|
|
ID uint64 `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
IPV4 string `json:"ipv4"`
|
|
|
|
IPV6 string `json:"ipv6"`
|
|
|
|
ValidIP string `json:"valid_ip"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatusResponse 服务器状态子结构 包含服务器信息与状态信息
|
2022-05-16 23:21:27 -04:00
|
|
|
type StatusResponse struct {
|
2022-05-17 08:16:46 -04:00
|
|
|
CommonServerInfo
|
2022-05-16 23:21:27 -04:00
|
|
|
Host *model.Host `json:"host"`
|
|
|
|
Status *model.HostState `json:"status"`
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:16:46 -04:00
|
|
|
// ServerStatusResponse 服务器状态返回结构 包含常规返回结构 和 服务器状态子结构
|
2022-05-16 23:21:27 -04:00
|
|
|
type ServerStatusResponse struct {
|
|
|
|
CommonResponse
|
|
|
|
Result []*StatusResponse `json:"result"`
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:16:46 -04:00
|
|
|
// ServerInfoResponse 服务器信息返回结构 包含常规返回结构 和 服务器信息子结构
|
|
|
|
type ServerInfoResponse struct {
|
|
|
|
CommonResponse
|
|
|
|
Result []*CommonServerInfo `json:"result"`
|
|
|
|
}
|
|
|
|
|
2022-05-17 22:10:35 -04:00
|
|
|
func InitAPI() {
|
|
|
|
ApiTokenList = make(map[string]*model.ApiToken)
|
|
|
|
UserIDToApiTokenList = make(map[uint64][]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadAPI() {
|
|
|
|
InitAPI()
|
|
|
|
var tokenList []*model.ApiToken
|
|
|
|
DB.Find(&tokenList)
|
|
|
|
for _, token := range tokenList {
|
|
|
|
ApiTokenList[token.Token] = token
|
|
|
|
UserIDToApiTokenList[token.UserID] = append(UserIDToApiTokenList[token.UserID], token.Token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 23:21:27 -04:00
|
|
|
// GetStatusByIDList 获取传入IDList的服务器状态信息
|
|
|
|
func (s *ServerAPI) GetStatusByIDList() *ServerStatusResponse {
|
2022-05-17 08:16:46 -04:00
|
|
|
res := &ServerStatusResponse{}
|
|
|
|
res.Result = make([]*StatusResponse, 0)
|
2022-05-16 23:21:27 -04:00
|
|
|
|
|
|
|
ServerLock.RLock()
|
|
|
|
defer ServerLock.RUnlock()
|
|
|
|
|
|
|
|
for _, v := range s.IDList {
|
|
|
|
server := ServerList[v]
|
|
|
|
if server == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-05-17 08:16:46 -04:00
|
|
|
ipv4, ipv6, validIP := utils.SplitIPAddr(server.Host.IP)
|
|
|
|
info := CommonServerInfo{
|
|
|
|
ID: server.ID,
|
|
|
|
Name: server.Name,
|
|
|
|
Tag: server.Tag,
|
|
|
|
IPV4: ipv4,
|
|
|
|
IPV6: ipv6,
|
|
|
|
ValidIP: validIP,
|
|
|
|
}
|
|
|
|
res.Result = append(res.Result, &StatusResponse{
|
|
|
|
CommonServerInfo: info,
|
|
|
|
Host: server.Host,
|
|
|
|
Status: server.State,
|
2022-05-16 23:21:27 -04:00
|
|
|
})
|
|
|
|
}
|
2022-05-17 08:16:46 -04:00
|
|
|
res.CommonResponse = CommonResponse{
|
|
|
|
Code: 0,
|
|
|
|
Message: "success",
|
2022-05-16 23:21:27 -04:00
|
|
|
}
|
2022-05-17 08:16:46 -04:00
|
|
|
return res
|
2022-05-16 23:21:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetStatusByTag 获取传入分组的所有服务器状态信息
|
|
|
|
func (s *ServerAPI) GetStatusByTag() *ServerStatusResponse {
|
|
|
|
s.IDList = ServerTagToIDList[s.Tag]
|
|
|
|
return s.GetStatusByIDList()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllStatus 获取所有服务器状态信息
|
|
|
|
func (s *ServerAPI) GetAllStatus() *ServerStatusResponse {
|
2022-05-17 08:16:46 -04:00
|
|
|
res := &ServerStatusResponse{}
|
|
|
|
res.Result = make([]*StatusResponse, 0)
|
2022-05-16 23:21:27 -04:00
|
|
|
ServerLock.RLock()
|
|
|
|
defer ServerLock.RUnlock()
|
|
|
|
for _, v := range ServerList {
|
|
|
|
host := v.Host
|
|
|
|
state := v.State
|
|
|
|
if host == nil || state == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-05-17 08:16:46 -04:00
|
|
|
ipv4, ipv6, validIP := utils.SplitIPAddr(host.IP)
|
|
|
|
info := CommonServerInfo{
|
|
|
|
ID: v.ID,
|
|
|
|
Name: v.Name,
|
|
|
|
Tag: v.Tag,
|
|
|
|
IPV4: ipv4,
|
|
|
|
IPV6: ipv6,
|
|
|
|
ValidIP: validIP,
|
|
|
|
}
|
|
|
|
res.Result = append(res.Result, &StatusResponse{
|
|
|
|
CommonServerInfo: info,
|
|
|
|
Host: v.Host,
|
|
|
|
Status: v.State,
|
2022-05-16 23:21:27 -04:00
|
|
|
})
|
|
|
|
}
|
2022-05-17 08:16:46 -04:00
|
|
|
res.CommonResponse = CommonResponse{
|
|
|
|
Code: 0,
|
|
|
|
Message: "success",
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2022-05-16 23:21:27 -04:00
|
|
|
|
2022-05-17 08:16:46 -04:00
|
|
|
// GetListByTag 获取传入分组的所有服务器信息
|
|
|
|
func (s *ServerAPI) GetListByTag() *ServerInfoResponse {
|
|
|
|
res := &ServerInfoResponse{}
|
|
|
|
res.Result = make([]*CommonServerInfo, 0)
|
|
|
|
|
|
|
|
ServerLock.RLock()
|
|
|
|
defer ServerLock.RUnlock()
|
|
|
|
for _, v := range ServerTagToIDList[s.Tag] {
|
|
|
|
host := ServerList[v].Host
|
|
|
|
if host == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ipv4, ipv6, validIP := utils.SplitIPAddr(host.IP)
|
|
|
|
info := &CommonServerInfo{
|
|
|
|
ID: v,
|
|
|
|
Name: ServerList[v].Name,
|
|
|
|
Tag: ServerList[v].Tag,
|
|
|
|
IPV4: ipv4,
|
|
|
|
IPV6: ipv6,
|
|
|
|
ValidIP: validIP,
|
|
|
|
}
|
|
|
|
res.Result = append(res.Result, info)
|
|
|
|
}
|
|
|
|
res.CommonResponse = CommonResponse{
|
|
|
|
Code: 0,
|
|
|
|
Message: "success",
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllList 获取所有服务器信息
|
|
|
|
func (s *ServerAPI) GetAllList() *ServerInfoResponse {
|
|
|
|
res := &ServerInfoResponse{}
|
|
|
|
res.Result = make([]*CommonServerInfo, 0)
|
|
|
|
|
|
|
|
ServerLock.RLock()
|
|
|
|
defer ServerLock.RUnlock()
|
|
|
|
for _, v := range ServerList {
|
|
|
|
host := v.Host
|
|
|
|
if host == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ipv4, ipv6, validIP := utils.SplitIPAddr(host.IP)
|
|
|
|
info := &CommonServerInfo{
|
|
|
|
ID: v.ID,
|
|
|
|
Name: v.Name,
|
|
|
|
Tag: v.Tag,
|
|
|
|
IPV4: ipv4,
|
|
|
|
IPV6: ipv6,
|
|
|
|
ValidIP: validIP,
|
|
|
|
}
|
|
|
|
res.Result = append(res.Result, info)
|
|
|
|
}
|
|
|
|
res.CommonResponse = CommonResponse{
|
|
|
|
Code: 0,
|
|
|
|
Message: "success",
|
2022-05-16 23:21:27 -04:00
|
|
|
}
|
2022-05-17 08:16:46 -04:00
|
|
|
return res
|
2022-05-16 23:21:27 -04:00
|
|
|
}
|