diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 5a38d70..f9cf71f 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -47,6 +47,8 @@ var endReport time.Time var reporting bool var client pb.NezhaServiceClient var ctx = context.Background() +var delayWhenError = time.Second * 10 +var delayWhenReport = time.Second func run(cmd *cobra.Command, args []string) { dao.Conf = &model.Config{ @@ -60,26 +62,36 @@ func run(cmd *cobra.Command, args []string) { var err error var conn *grpc.ClientConn var hc pb.NezhaService_HeartbeatClient - for { + retry := func() { + time.Sleep(delayWhenError) log.Println("Try to reconnect ...") - time.Sleep(time.Second * 5) + } + for { conn, err = grpc.Dial(":5555", grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth)) if err != nil { log.Printf("grpc.Dial err: %v", err) + retry() continue } client = pb.NewNezhaServiceClient(conn) // 第一步注册 - client.Register(ctx, monitor.GetHost().PB()) + _, err = client.Register(ctx, monitor.GetHost().PB()) + if err != nil { + log.Printf("client.Register err: %v", err) + retry() + continue + } hc, err = client.Heartbeat(ctx, &pb.Beat{ Timestamp: fmt.Sprintf("%v", time.Now()), }) if err != nil { - log.Printf("client.Register err: %v", err) + log.Printf("client.Heartbeat err: %v", err) + retry() continue } err = receiveCommand(hc) log.Printf("receiveCommand exit to main: %v", err) + retry() } } @@ -97,7 +109,7 @@ func receiveCommand(hc pb.NezhaService_HeartbeatClient) error { } switch action.GetType() { case model.MTReportState: - endReport = time.Now().Add(time.Second * 10) + endReport = time.Now().Add(time.Minute * 10) default: log.Printf("Unknown action: %v", action) } @@ -110,7 +122,11 @@ func reportState() { for { if endReport.After(time.Now()) { _, err = client.ReportState(ctx, monitor.GetState(0).PB()) + if err != nil { + log.Printf("reportState error %v", err) + time.Sleep(delayWhenError) + } } - time.Sleep(time.Second) + time.Sleep(delayWhenReport) } } diff --git a/cmd/dashboard/controller/common_page.go b/cmd/dashboard/controller/common_page.go index 90ec2a2..20b612b 100644 --- a/cmd/dashboard/controller/common_page.go +++ b/cmd/dashboard/controller/common_page.go @@ -26,10 +26,10 @@ func (cp *commonPage) home(c *gin.Context) { if ok && isLogin.(bool) { admin = dao.Admin } - var servers []model.Server - dao.DB.Find(&servers) + dao.ServerLock.RLock() + defer dao.ServerLock.RUnlock() c.HTML(http.StatusOK, "page/home", mygin.CommonEnvironment(c, gin.H{ "Admin": admin, - "Servers": servers, + "Servers": dao.ServerList, })) } diff --git a/cmd/dashboard/controller/controller.go b/cmd/dashboard/controller/controller.go index 58e4cc2..ff7397d 100644 --- a/cmd/dashboard/controller/controller.go +++ b/cmd/dashboard/controller/controller.go @@ -5,6 +5,7 @@ import ( "html/template" "time" + "code.cloudfoundry.org/bytefmt" "github.com/gin-gonic/gin" "github.com/p14yground/nezha/pkg/mygin" @@ -29,6 +30,9 @@ func ServeWeb() { } return fmt.Sprintf("%d", time.Now().UnixNano()) }, + "bf": func(b uint64) string { + return bytefmt.ByteSize(b) + }, }) r.Static("/static", "resource/static") r.LoadHTMLGlob("resource/template/**/*") diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index ecce8ed..9cc2667 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -7,7 +7,6 @@ import ( "github.com/gin-gonic/gin" "github.com/naiba/com" - "github.com/patrickmn/go-cache" "github.com/p14yground/nezha/model" "github.com/p14yground/nezha/pkg/mygin" @@ -53,7 +52,9 @@ func (ma *memberAPI) addServer(c *gin.Context) { }) return } - dao.Cache.Set(fmt.Sprintf("%s%d%s", model.CtxKeyServer, s.ID, s.Secret), s, cache.NoExpiration) + dao.ServerLock.Lock() + defer dao.ServerLock.Unlock() + dao.ServerList[fmt.Sprintf("%d", s.ID)] = &s c.JSON(http.StatusOK, model.Response{ Code: http.StatusOK, }) diff --git a/cmd/dashboard/controller/member_page.go b/cmd/dashboard/controller/member_page.go index fdf9f00..df8baac 100644 --- a/cmd/dashboard/controller/member_page.go +++ b/cmd/dashboard/controller/member_page.go @@ -4,7 +4,6 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/p14yground/nezha/model" "github.com/p14yground/nezha/pkg/mygin" "github.com/p14yground/nezha/service/dao" ) @@ -26,10 +25,10 @@ func (mp *memberPage) serve() { } func (mp *memberPage) server(c *gin.Context) { - var servers []model.Server - dao.DB.Find(&servers) + dao.ServerLock.RLock() + defer dao.ServerLock.RUnlock() c.HTML(http.StatusOK, "page/server", mygin.CommonEnvironment(c, gin.H{ "Title": "服务器管理", - "Servers": servers, + "Servers": dao.ServerList, })) } diff --git a/cmd/dashboard/main.go b/cmd/dashboard/main.go index 6060d58..976507a 100644 --- a/cmd/dashboard/main.go +++ b/cmd/dashboard/main.go @@ -16,6 +16,7 @@ import ( func init() { var err error + dao.ServerList = make(map[string]*model.Server) dao.Conf, err = model.ReadInConfig("data/config.yaml") if err != nil { panic(err) @@ -31,7 +32,6 @@ func init() { dao.DB = dao.DB.Debug() } dao.Cache = cache.New(5*time.Minute, 10*time.Minute) - initDB() } @@ -41,7 +41,8 @@ func initDB() { var servers []model.Server dao.DB.Find(&servers) for _, s := range servers { - dao.Cache.Set(fmt.Sprintf("%s%d%s", model.CtxKeyServer, s.ID, s.Secret), s, cache.NoExpiration) + innerS := s + dao.ServerList[fmt.Sprintf("%d", innerS.ID)] = &innerS } } diff --git a/cmd/dashboard/rpc/rpc.go b/cmd/dashboard/rpc/rpc.go index 778b4ab..33fdd88 100644 --- a/cmd/dashboard/rpc/rpc.go +++ b/cmd/dashboard/rpc/rpc.go @@ -13,10 +13,7 @@ import ( func ServeRPC() { server := grpc.NewServer() pb.RegisterNezhaServiceServer(server, &rpcService.NezhaHandler{ - Auth: &rpcService.AuthHandler{ - ClientID: "naiba", - ClientSecret: "123456", - }, + Auth: &rpcService.AuthHandler{}, }) listen, err := net.Listen("tcp", ":5555") if err != nil { diff --git a/go.mod b/go.mod index 0708407..2fa5787 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/p14yground/nezha go 1.13 require ( + code.cloudfoundry.org/bytefmt v0.0.0-20190819182555-854d396b647c github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect github.com/fsnotify/fsnotify v1.4.7 github.com/gin-gonic/gin v1.5.0 @@ -10,8 +11,9 @@ require ( github.com/golang/protobuf v1.3.2 github.com/google/go-github/v28 v28.1.1 github.com/jinzhu/gorm v1.9.11 + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.1 // indirect github.com/naiba/com v0.0.0-20191104074000-318339dc72a5 - github.com/naiba/poorsquad v0.0.0-20191204154109-4600d20875f6 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/shirou/gopsutil v2.19.11+incompatible github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect diff --git a/go.sum b/go.sum index fe69b24..d40d8ae 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,10 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.37.4 h1:glPeL3BQJsbF6aIIYfZizMwc5LTYz250bDMjttbBGAU= cloud.google.com/go v0.37.4/go.mod h1:NHPJ89PdicEuT9hdPXMROBD91xc5uRDxsMtSB16k7hw= +code.cloudfoundry.org/bytefmt v0.0.0-20190819182555-854d396b647c h1:2RuXx1+tSNWRjxhY0Bx52kjV2odJQ0a6MTbfTPhGAkg= +code.cloudfoundry.org/bytefmt v0.0.0-20190819182555-854d396b647c/go.mod h1:wN/zk7mhREp/oviagqUXY3EwuHhWyOvAdsn5Y4CzOrc= +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= @@ -27,6 +31,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3 h1:tkum0XDgfR0jcVVXuTsYv/erY2NnEDqwRojbxR1rBYA= github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= @@ -35,14 +40,13 @@ github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1 github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM= github.com/gin-gonic/gin v1.5.0 h1:fi+bqFAx/oLK54somfCtEZs9HeH1LHVoEPUgARpTqyc= github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= @@ -54,6 +58,7 @@ github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotf github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM= github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM= github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY= +github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA= github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -71,8 +76,8 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= github.com/google/go-github/v28 v28.1.1 h1:kORf5ekX5qwXO2mGzXXOjMe/g6ap8ahVe0sBEulhSxo= github.com/google/go-github/v28 v28.1.1/go.mod h1:bsqJWQX05omyWVmc00nEUql9mhQyv38lDZ8kPZcQVoM= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= @@ -81,6 +86,7 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/ github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= @@ -91,33 +97,39 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jinzhu/gorm v1.9.11 h1:gaHGvE+UnWGlbWG4Y3FUwY1EcZ5n6S9WtqBA/uySMLE= github.com/jinzhu/gorm v1.9.11/go.mod h1:bu/pK8szGZ2puuErfU0RwyeNdsf3e6nCX/noXaVxkfw= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M= github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= -github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo= github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8= github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw= +github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4= github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= -github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg= github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q= @@ -127,19 +139,19 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/naiba/avalanche v0.0.0-20191105053910-c34b962c2680 h1:ewXaGaPPlBSW7FJK8Cv28MTZ1aSRg1+y6AqPhvTC8t8= -github.com/naiba/avalanche v0.0.0-20191105053910-c34b962c2680/go.mod h1:JDQv0KccC3nw7xdxtS1ZXVNmXwYcI0q2aPI3jE/gWNs= github.com/naiba/com v0.0.0-20191104074000-318339dc72a5 h1:NYtQRl/K+QXUTLdamg3nHkdI1qGXhN+4B8ZdGktAaVo= github.com/naiba/com v0.0.0-20191104074000-318339dc72a5/go.mod h1:gU4KduAcIb9RnoirznFwMcPjeSyYQhvXYL9NWEWUMYI= -github.com/naiba/poorsquad v0.0.0-20191204154109-4600d20875f6 h1:iinFCCfcFCfUTWtV7kh3k0HPdetASzjh89eZuiaCmxE= -github.com/naiba/poorsquad v0.0.0-20191204154109-4600d20875f6/go.mod h1:fhqvd8+zGZb3ML/O7fPhUhkJwwGhPhWbvmdW9jfTU7s= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= @@ -165,7 +177,6 @@ github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= -github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/shirou/gopsutil v2.19.11+incompatible h1:lJHR0foqAjI4exXqWsU3DbH7bX1xvdhGdnXTIARA9W4= @@ -173,7 +184,9 @@ github.com/shirou/gopsutil v2.19.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMT github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 h1:udFKJ0aHUL60LboW/A+DfgoHVedieIzIXE8uylPue0U= github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -188,7 +201,6 @@ github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb6 github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= -github.com/spf13/viper v1.5.0/go.mod h1:AkYRkVJF8TkSG/xet6PzXX+l39KhhXa2pdqVSxnTcn4= github.com/spf13/viper v1.6.1 h1:VPZzIkznI1YhVMRi6vNFLHSwhnhReBfgTxIPccpfdZk= github.com/spf13/viper v1.6.1/go.mod h1:t3iDnF5Jlj76alVNuyFBk5oUMCvsrkbvZK0WQdfDi5k= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -233,7 +245,6 @@ golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190522155817-f3200d17e092 h1:4QSRKanuywn15aTZvI/mIDEgPQpswuFndXpOj3rKEco= golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -254,7 +265,6 @@ golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 h1:ZBzSG/7F4eNKz2L3GE9o300RX0Az1Bw5HF7PDraD+qU= golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -274,6 +284,7 @@ golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3 golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= @@ -289,15 +300,18 @@ google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= -gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc= gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno= gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/model/common.go b/model/common.go index c683d8f..4602bfb 100644 --- a/model/common.go +++ b/model/common.go @@ -7,8 +7,6 @@ const CtxKeyIsUserLogin = "ckiul" // CtxKeyOauth2State .. const CtxKeyOauth2State = "cko2s" -// CtxKeyServer .. -const CtxKeyServer = "cks" // Common .. type Common struct { diff --git a/model/monitor.go b/model/monitor.go index d71a250..e5d2e08 100644 --- a/model/monitor.go +++ b/model/monitor.go @@ -11,8 +11,8 @@ const ( // State .. type State struct { CPU float64 - MEMTotal uint64 - MEMUsed uint64 + MemTotal uint64 + MemUsed uint64 SwapTotal uint64 SwapUsed uint64 DiskTotal uint64 @@ -25,8 +25,8 @@ type State struct { func (s *State) PB() *pb.State { return &pb.State{ Cpu: s.CPU, - MemTotal: s.MEMTotal, - MemUsed: s.MEMUsed, + MemTotal: s.MemTotal, + MemUsed: s.MemUsed, SwapTotal: s.SwapTotal, SwapUsed: s.SwapUsed, DiskTotal: s.DiskTotal, @@ -36,6 +36,21 @@ func (s *State) PB() *pb.State { } } +// PB2State .. +func PB2State(s *pb.State) State { + return State{ + CPU: s.GetCpu(), + MemTotal: s.GetMemTotal(), + MemUsed: s.GetMemUsed(), + SwapTotal: s.GetSwapTotal(), + SwapUsed: s.GetSwapUsed(), + DiskTotal: s.GetDiskTotal(), + DiskUsed: s.GetDiskUsed(), + NetIn: s.GetNetIn(), + NetOut: s.GetNetOut(), + } +} + // Host .. type Host struct { Platform string @@ -65,3 +80,19 @@ func (h *Host) PB() *pb.Host { Version: h.Version, } } + +// PB2Host ... +func PB2Host(h *pb.Host) Host { + return Host{ + Platform: h.GetPlatform(), + PlatformVersion: h.GetPlatformVersion(), + CPU: h.GetCpu(), + Arch: h.GetArch(), + Virtualization: h.GetVirtualization(), + Uptime: h.GetUptime(), + BootTime: h.GetBootTime(), + IP: h.GetIp(), + CountryCode: h.GetCountryCode(), + Version: h.GetVersion(), + } +} diff --git a/model/server.go b/model/server.go index 685f23d..f4f3a97 100644 --- a/model/server.go +++ b/model/server.go @@ -5,4 +5,7 @@ type Server struct { Common Name string Secret string + + Host Host + State State } diff --git a/resource/static/main.js b/resource/static/main.js index 599f36e..6f2ff03 100644 --- a/resource/static/main.js +++ b/resource/static/main.js @@ -1,12 +1,10 @@ -$('.ui.checkbox').checkbox(); -$('.yellow.info.circle.icon').popup(); - const confirmBtn = $('.mini.confirm.modal .positive.button') + function showConfirm(title, content, callFn, extData) { const modal = $('.mini.confirm.modal') modal.children('.header').text(title) modal.children('.content').text(content) - if (confirmBtn.hasCslass('loading')) { + if (confirmBtn.hasClass('loading')) { return false } modal.modal({ diff --git a/resource/template/page/home.html b/resource/template/page/home.html index ee171f9..c38f0d3 100644 --- a/resource/template/page/home.html +++ b/resource/template/page/home.html @@ -7,15 +7,19 @@ {{range $server := .Servers}}
-
{{.Name}} - +
{{if $server.Host.CountryCode}}{{end}}{{$server.Name}} +
CPU
-
+
@@ -23,7 +27,8 @@
硬盘
-
+
@@ -31,12 +36,18 @@
内存
-
+
+
网络
+
+ {{$server.State.NetIn|bf}} + {{$server.State.NetOut|bf}} +
@@ -46,4 +57,8 @@
{{template "common/footer" .}} + {{end}} \ No newline at end of file diff --git a/service/dao/dao.go b/service/dao/dao.go index 9f93586..7d31020 100644 --- a/service/dao/dao.go +++ b/service/dao/dao.go @@ -1,6 +1,8 @@ package dao import ( + "sync" + "github.com/jinzhu/gorm" "github.com/patrickmn/go-cache" @@ -18,3 +20,9 @@ var DB *gorm.DB // Admin .. var Admin *model.User + +// ServerList .. +var ServerList map[string]*model.Server + +// ServerLock .. +var ServerLock sync.RWMutex diff --git a/service/monitor/monitor.go b/service/monitor/monitor.go index 9dad741..fd89fee 100644 --- a/service/monitor/monitor.go +++ b/service/monitor/monitor.go @@ -82,8 +82,8 @@ func GetState(delay uint64) *model.State { } return &model.State{ CPU: cpuPercent, - MEMTotal: mv.Total, - MEMUsed: mv.Used, + MemTotal: mv.Total, + MemUsed: mv.Used, SwapTotal: ms.Total, SwapUsed: ms.Used, DiskTotal: diskTotal, diff --git a/service/rpc/auth.go b/service/rpc/auth.go index a5fc474..512a545 100644 --- a/service/rpc/auth.go +++ b/service/rpc/auth.go @@ -2,9 +2,7 @@ package rpc import ( "context" - "fmt" - "github.com/p14yground/nezha/model" "github.com/p14yground/nezha/service/dao" "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" @@ -19,7 +17,7 @@ type AuthHandler struct { // GetRequestMetadata .. func (a *AuthHandler) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) { - return map[string]string{"app_key": a.ClientID, "app_secret": a.ClientSecret}, nil + return map[string]string{"client_id": a.ClientID, "client_secret": a.ClientSecret}, nil } // RequireTransportSecurity .. @@ -28,26 +26,27 @@ func (a *AuthHandler) RequireTransportSecurity() bool { } // Check .. -func (a *AuthHandler) Check(ctx context.Context) error { +func (a *AuthHandler) Check(ctx context.Context) (clientID string, err error) { md, ok := metadata.FromIncomingContext(ctx) if !ok { - return status.Errorf(codes.Unauthenticated, "获取 metaData 失败") + err = status.Errorf(codes.Unauthenticated, "获取 metaData 失败") + return } var ( - ClientID string - ClientSecret string + clientSecret string ) - if value, ok := md["app_key"]; ok { - ClientID = value[0] + if value, ok := md["client_id"]; ok { + clientID = value[0] } - if value, ok := md["app_secret"]; ok { - ClientSecret = value[0] + if value, ok := md["client_secret"]; ok { + clientSecret = value[0] } - if _, ok := dao.Cache.Get(fmt.Sprintf("%s%s%s", model.CtxKeyServer, ClientID, ClientSecret)); !ok { - return status.Errorf(codes.Unauthenticated, "客户端认证失败") + dao.ServerLock.RLock() + defer dao.ServerLock.RUnlock() + if server, has := dao.ServerList[clientID]; !has || server.Secret != clientSecret { + err = status.Errorf(codes.Unauthenticated, "客户端认证失败") } - - return nil + return } diff --git a/service/rpc/nezha.go b/service/rpc/nezha.go index 38b4b33..fb753e1 100644 --- a/service/rpc/nezha.go +++ b/service/rpc/nezha.go @@ -2,11 +2,11 @@ package rpc import ( "context" - "fmt" "log" "github.com/p14yground/nezha/model" pb "github.com/p14yground/nezha/proto" + "github.com/p14yground/nezha/service/dao" ) // NezhaHandler .. @@ -16,20 +16,26 @@ type NezhaHandler struct { // ReportState .. func (s *NezhaHandler) ReportState(c context.Context, r *pb.State) (*pb.Receipt, error) { - if err := s.Auth.Check(c); err != nil { + var clientID string + var err error + if clientID, err = s.Auth.Check(c); err != nil { return nil, err } - fmt.Printf("ReportState receive: %s\n", r) + dao.ServerLock.Lock() + defer dao.ServerLock.Unlock() + dao.ServerList[clientID].State = model.PB2State(r) return &pb.Receipt{Proced: true}, nil } // Heartbeat .. func (s *NezhaHandler) Heartbeat(r *pb.Beat, stream pb.NezhaService_HeartbeatServer) error { - defer log.Println("Heartbeat exit") - if err := s.Auth.Check(stream.Context()); err != nil { + var clientID string + var err error + defer log.Printf("Heartbeat exit server:%v err:%v", clientID, err) + if clientID, err = s.Auth.Check(stream.Context()); err != nil { return err } - err := stream.Send(&pb.Command{ + err = stream.Send(&pb.Command{ Type: model.MTReportState, }) if err != nil { @@ -40,9 +46,13 @@ func (s *NezhaHandler) Heartbeat(r *pb.Beat, stream pb.NezhaService_HeartbeatSer // Register .. func (s *NezhaHandler) Register(c context.Context, r *pb.Host) (*pb.Receipt, error) { - if err := s.Auth.Check(c); err != nil { + var clientID string + var err error + if clientID, err = s.Auth.Check(c); err != nil { return nil, err } - fmt.Printf("Register receive: %s\n", r) + dao.ServerLock.Lock() + defer dao.ServerLock.Unlock() + dao.ServerList[clientID].Host = model.PB2Host(r) return &pb.Receipt{Proced: true}, nil }