From 01f99a8c2cf0e64b9394a93c769ffd5aa90f7ef1 Mon Sep 17 00:00:00 2001 From: naiba Date: Tue, 2 Mar 2021 23:08:40 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=8E=A5=E5=85=A5=20Gitee=20?= =?UTF-8?q?=E7=99=BB=E5=BD=95=EF=BC=8Cv0.4.11=20=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E7=9A=84=E7=94=A8=E6=88=B7=E9=9C=80=E8=A6=81=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 +-- cmd/dashboard/controller/guest_page.go | 18 +++++- cmd/dashboard/controller/member_api.go | 4 +- cmd/dashboard/controller/oauth2.go | 60 ++++++++++++-------- model/alertrule.go | 12 ++-- model/config.go | 10 +++- resource/template/common/menu.html | 14 ++--- resource/template/dashboard/setting.html | 4 +- script/config.yaml | 5 +- script/install.sh | 70 ++++++------------------ service/dao/dao.go | 2 +- service/rpc/nezha.go | 4 +- 12 files changed, 105 insertions(+), 106 deletions(-) diff --git a/README.md b/README.md index 39f24e7..56f934a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 哪吒监控 -![GitHub Workflow Status](https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=管理面板%20v0.4.11&logo=github&style=for-the-badge) ![Agent release](https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github) ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge) ![shell](https://img.shields.io/badge/安装脚本-v0.4.7-brightgreen?style=for-the-badge&logo=linux) +![GitHub Workflow Status](https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=管理面板%20v0.4.12&logo=github&style=for-the-badge) ![Agent release](https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github) ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge) ![shell](https://img.shields.io/badge/安装脚本-v0.4.7-brightgreen?style=for-the-badge&logo=linux) :trollface: 哪吒监控 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,命令批量执行和计划任务。 @@ -14,14 +14,14 @@ ## 安装脚本 -建议使用 WatchTower 自动更新面板,Windows 终端可以使用 nssm 配置自启动(见尾部教程) +**推荐配置:** 安装前解析 _两个域名_ 到面板服务器,一个作为 _公开访问_ ,可以 **接入CDN**,比如 (status.nai.ba);另外一个作为安装 Agent 时连接 Dashboard 使用,**不能接入CDN** 直接暴露面板主机IP,比如(randomdashboard.nai.ba)。 ```shell curl -L https://raw.githubusercontent.com/naiba/nezha/master/script/install.sh -o nezha.sh && chmod +x nezha.sh ./nezha.sh ``` -_\* 目前安装脚本仅支持 CentOS 7+ / Debian 8+ / Ubuntu 16+,Windows 及 其他 Linux 发行版请在 Release 处下载对应二进制文件。_ +_\* 使用 WatchTower 可以自动更新面板,Windows 终端可以使用 nssm 配置自启动(见尾部教程)_
国内镜像加速:(有缓存,版本更新不及时,能不用尽量不用,非作者维护) @@ -97,7 +97,7 @@ URL 里面也可放置占位符,请求时会进行简单的字符串替换。 - cpu、memory、swap、disk:Min/Max 数值为占用百分比 - net_in_speed(入站网速)、net_out_speed(出站网速)、net_all_speed(双向网速)、transfer_in(入站流量)、transfer_out(出站流量)、transfer_all(双向流量):Min/Max 数值为字节(1kb=1024,1mb = 1024\*1024) - offline:不支持 Min/Max 参数 -- Duration:持续秒数,监控比较简陋,取持续时间内的 70 采样结果 +- Duration:持续秒数,监控比较简陋,取持续时间内的 70% 采样结果 - Ignore: `{"1": true, "2":false}` 忽略此规则的服务器 ID 列表
diff --git a/cmd/dashboard/controller/guest_page.go b/cmd/dashboard/controller/guest_page.go index 87cfa35..668c5fe 100644 --- a/cmd/dashboard/controller/guest_page.go +++ b/cmd/dashboard/controller/guest_page.go @@ -4,6 +4,7 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/naiba/nezha/model" "github.com/naiba/nezha/pkg/mygin" "github.com/naiba/nezha/service/dao" "golang.org/x/oauth2" @@ -26,12 +27,23 @@ func (gp *guestPage) serve() { gr.GET("/login", gp.login) + var endPoint oauth2.Endpoint + + if dao.Conf.Oauth2.Type == model.ConfigTypeGitee { + endPoint = oauth2.Endpoint{ + AuthURL: "https://gitee.com/oauth/authorize", + TokenURL: "https://gitee.com/oauth/token", + } + } else { + endPoint = github.Endpoint + } + oauth := &oauth2controller{ oauth2Config: &oauth2.Config{ - ClientID: dao.Conf.GitHub.ClientID, - ClientSecret: dao.Conf.GitHub.ClientSecret, + ClientID: dao.Conf.Oauth2.ClientID, + ClientSecret: dao.Conf.Oauth2.ClientSecret, Scopes: []string{}, - Endpoint: github.Endpoint, + Endpoint: endPoint, }, r: gr, } diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index 01cd38b..1ef2fca 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -447,6 +447,7 @@ type settingForm struct { CustomCode string ViewPassword string EnableIPChangeNotification string + Oauth2Type string } func (ma *memberAPI) updateSetting(c *gin.Context) { @@ -463,7 +464,8 @@ func (ma *memberAPI) updateSetting(c *gin.Context) { dao.Conf.Site.Theme = sf.Theme dao.Conf.Site.CustomCode = sf.CustomCode dao.Conf.Site.ViewPassword = sf.ViewPassword - dao.Conf.GitHub.Admin = sf.Admin + dao.Conf.Oauth2.Type = sf.Oauth2Type + dao.Conf.Oauth2.Admin = sf.Admin if err := dao.Conf.Save(); err != nil { c.JSON(http.StatusOK, model.Response{ Code: http.StatusBadRequest, diff --git a/cmd/dashboard/controller/oauth2.go b/cmd/dashboard/controller/oauth2.go index e342920..773df9c 100644 --- a/cmd/dashboard/controller/oauth2.go +++ b/cmd/dashboard/controller/oauth2.go @@ -2,11 +2,14 @@ package controller import ( "context" + "errors" "fmt" + "log" "net/http" "strings" "github.com/gin-gonic/gin" + "github.com/google/go-github/github" GitHubAPI "github.com/google/go-github/github" "golang.org/x/oauth2" @@ -26,7 +29,16 @@ func (oa *oauth2controller) serve() { oa.r.GET("/oauth2/callback", oa.callback) } +func (oa *oauth2controller) fillRedirectURL(c *gin.Context) { + schame := "http://" + if strings.HasPrefix(c.Request.Referer(), "https://") { + schame = "https://" + } + oa.oauth2Config.RedirectURL = schame + c.Request.Host + "/oauth2/callback" +} + func (oa *oauth2controller) login(c *gin.Context) { + oa.fillRedirectURL(c) state := utils.RandStringBytesMaskImprSrcUnsafe(6) dao.Cache.Set(fmt.Sprintf("%s%s", model.CacheKeyOauth2State, c.ClientIP()), state, 0) url := oa.oauth2Config.AuthCodeURL(state, oauth2.AccessTypeOnline) @@ -34,30 +46,32 @@ func (oa *oauth2controller) login(c *gin.Context) { } func (oa *oauth2controller) callback(c *gin.Context) { + oa.fillRedirectURL(c) + var err error // 验证登录跳转时的 State state, ok := dao.Cache.Get(fmt.Sprintf("%s%s", model.CacheKeyOauth2State, c.ClientIP())) if !ok || state.(string) != c.Query("state") { - mygin.ShowErrorPage(c, mygin.ErrInfo{ - Code: http.StatusBadRequest, - Title: "登录失败", - Msg: fmt.Sprintf("错误信息:%s", "非法的登录方式"), - }, true) - return + err = errors.New("非法的登录方式") } - // 拉取验证用户信息 ctx := context.Background() - otk, err := oa.oauth2Config.Exchange(ctx, c.Query("code")) - if err != nil { - mygin.ShowErrorPage(c, mygin.ErrInfo{ - Code: http.StatusBadRequest, - Title: "登录失败", - Msg: fmt.Sprintf("错误信息:%s", err), - }, true) - return + var otk *oauth2.Token + if err == nil { + otk, err = oa.oauth2Config.Exchange(ctx, c.Query("code")) } - oc := oa.oauth2Config.Client(ctx, otk) - client := GitHubAPI.NewClient(oc) - gu, _, err := client.Users.Get(ctx, "") + var client *GitHubAPI.Client + if err == nil { + oc := oa.oauth2Config.Client(ctx, otk) + if dao.Conf.Oauth2.Type == "gitee" { + client, err = GitHubAPI.NewEnterpriseClient("https://gitee.com/api/v5/", "https://gitee.com/api/v5/", oc) + } else { + client = GitHubAPI.NewClient(oc) + } + } + var gu *github.User + if err == nil { + gu, _, err = client.Users.Get(ctx, "") + } + log.Printf("%+v", gu) if err != nil { mygin.ShowErrorPage(c, mygin.ErrInfo{ Code: http.StatusBadRequest, @@ -67,12 +81,10 @@ func (oa *oauth2controller) callback(c *gin.Context) { return } var isAdmin bool - if gu.GetID() > 0 { - for _, admin := range strings.Split(dao.Conf.GitHub.Admin, ",") { - if fmt.Sprintf("%d", gu.GetID()) == admin { - isAdmin = true - break - } + for _, admin := range strings.Split(dao.Conf.Oauth2.Admin, ",") { + if admin != "" && gu.GetLogin() == admin { + isAdmin = true + break } } if !isAdmin { diff --git a/model/alertrule.go b/model/alertrule.go index 59d98a3..82bd686 100644 --- a/model/alertrule.go +++ b/model/alertrule.go @@ -17,11 +17,11 @@ const ( type Rule struct { // 指标类型,cpu、memory、swap、disk、net_in_speed、net_out_speed // net_all_speed、transfer_in、transfer_out、transfer_all、offline - Type string - Min uint64 // 最小阈值 (百分比、字节 kb ÷ 1024) - Max uint64 // 最大阈值 (百分比、字节 kb ÷ 1024) - Duration uint64 // 持续时间 (秒) - Ignore map[uint64]bool //忽略此规则的ID列表 + Type string `json:"type,omitempty"` + Min uint64 `json:"min,omitempty"` // 最小阈值 (百分比、字节 kb ÷ 1024) + Max uint64 `json:"max,omitempty"` // 最大阈值 (百分比、字节 kb ÷ 1024) + Duration uint64 `json:"duration,omitempty"` // 持续时间 (秒) + Ignore map[uint64]bool `json:"ignore,omitempty"` //忽略此规则的ID列表 } func percentage(used, total uint64) uint64 { @@ -123,7 +123,7 @@ func (r *AlertRule) Check(points [][]interface{}) (int, string) { fail++ } } - if fail/total > 0.5 { + if fail/total > 0.7 { count++ dist.WriteString(fmt.Sprintf("%+v\n", r.Rules[i])) } diff --git a/model/config.go b/model/config.go index 5d5b7f4..2edfcbd 100644 --- a/model/config.go +++ b/model/config.go @@ -10,6 +10,11 @@ import ( "gopkg.in/yaml.v2" ) +const ( + ConfigTypeGitHub = "github" + ConfigTypeGitee = "gitee" +) + type Config struct { Debug bool Site struct { @@ -19,8 +24,9 @@ type Config struct { CustomCode string ViewPassword string // 前台查看密码 } - GitHub struct { - Admin string // 管理员ID列表 + Oauth2 struct { + Type string + Admin string // 管理员用户名列表 ClientID string ClientSecret string } diff --git a/resource/template/common/menu.html b/resource/template/common/menu.html index 063463d..fd0e469 100644 --- a/resource/template/common/menu.html +++ b/resource/template/common/menu.html @@ -5,16 +5,16 @@ {{if .IsAdminPage}} - 资产 - 服务 - 任务 - 报警 - + 资产 + 服务 + 任务 + 报警 + 设置 {{else}} - 首页 - 服务状态 + 首页 + 服务状态 {{end}}