🐛 修复修改服务器导致 Agent 离线

This commit is contained in:
naiba 2021-07-25 23:50:08 +08:00
parent bcb95ba718
commit e24121bdf7
6 changed files with 28 additions and 12 deletions

View File

@ -1,7 +1,7 @@
<div align="center">
<img width="500" style="max-width:100%" src="resource/static/brand.png" title="哪吒监控">
<br><br>
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.9.10&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github">&nbsp;<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/badge/Installer-v0.6.4-brightgreen?style=for-the-badge&logo=linux">
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.9.11&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github">&nbsp;<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/badge/Installer-v0.6.4-brightgreen?style=for-the-badge&logo=linux">
<br>
<br>
<p>:trollface: <b>哪吒监控</b> 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,命令批量执行和计划任务。</p>

View File

@ -154,7 +154,7 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) {
s.ID = sf.ID
s.Tag = sf.Tag
s.Note = sf.Note
if sf.ID == 0 {
if s.ID == 0 {
s.Secret = utils.MD5(fmt.Sprintf("%s%s%d", time.Now(), sf.Name, admin.ID))
s.Secret = s.Secret[:18]
err = dao.DB.Create(&s).Error
@ -171,13 +171,17 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) {
return
}
if isEdit {
dao.ServerLock.RLock()
s.Host = dao.ServerList[s.ID].Host
s.State = dao.ServerList[s.ID].State
dao.SecretToID[s.Secret] = s.ID
delete(dao.SecretToID, dao.ServerList[s.ID].Secret)
dao.ServerLock.Lock()
s.CopyFromRunningServer(dao.ServerList[s.ID])
// 如果修改了 Secret
if s.Secret != dao.ServerList[s.ID].Secret {
// 删除旧 Secret-ID 绑定关系
dao.SecretToID[s.Secret] = s.ID
// 设置新的 Secret-ID 绑定关系
delete(dao.SecretToID, dao.ServerList[s.ID].Secret)
}
dao.ServerList[s.ID] = &s
dao.ServerLock.RUnlock()
dao.ServerLock.Unlock()
} else {
s.Host = &model.Host{}
s.State = &model.HostState{}

View File

@ -8,7 +8,6 @@ import (
"strings"
"github.com/gin-gonic/gin"
"github.com/google/go-github/github"
GitHubAPI "github.com/google/go-github/github"
"golang.org/x/oauth2"
GitHubOauth2 "golang.org/x/oauth2/github"
@ -87,7 +86,7 @@ func (oa *oauth2controller) callback(c *gin.Context) {
client = GitHubAPI.NewClient(oc)
}
}
var gu *github.User
var gu *GitHubAPI.User
if err == nil {
gu, _, err = client.Users.Get(ctx, "")
}

View File

@ -28,6 +28,16 @@ type Server struct {
PrevHourlyTransferOut int64 `gorm:"-" json:"-"` // 上次数据点时的出站使用量
}
func (s *Server) CopyFromRunningServer(old *Server) {
s.Host = old.Host
s.State = old.State
s.LastActive = old.LastActive
s.TaskClose = old.TaskClose
s.TaskStream = old.TaskStream
s.PrevHourlyTransferIn = old.PrevHourlyTransferIn
s.PrevHourlyTransferOut = old.PrevHourlyTransferOut
}
func (s Server) Marshal() template.JS {
name, _ := json.Marshal(s.Name)
tag, _ := json.Marshal(s.Tag)

View File

@ -13,7 +13,7 @@ import (
pb "github.com/naiba/nezha/proto"
)
var Version = "v0.9.10" // !!记得修改 README 中的 badge 版本!!
var Version = "v0.9.11" // !!记得修改 README 中的 badge 版本!!
var (
Conf *model.Config

View File

@ -35,8 +35,11 @@ func (a *AuthHandler) Check(ctx context.Context) (uint64, error) {
dao.ServerLock.RLock()
defer dao.ServerLock.RUnlock()
clientID, hasID := dao.SecretToID[clientSecret]
if !hasID {
return 0, status.Errorf(codes.Unauthenticated, "客户端认证失败")
}
_, hasServer := dao.ServerList[clientID]
if !hasID || !hasServer {
if !hasServer {
return 0, status.Errorf(codes.Unauthenticated, "客户端认证失败")
}
return clientID, nil