2019-12-05 22:36:58 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-07-14 23:53:37 +08:00
|
|
|
"context"
|
2024-11-29 21:31:39 +08:00
|
|
|
"embed"
|
2024-11-25 22:03:11 +08:00
|
|
|
"flag"
|
2023-11-02 21:06:34 +08:00
|
|
|
"fmt"
|
2022-04-27 23:51:45 +08:00
|
|
|
"log"
|
2024-10-20 11:47:45 +08:00
|
|
|
"net"
|
2024-10-22 23:44:50 +08:00
|
|
|
"net/http"
|
2024-10-31 03:34:25 +08:00
|
|
|
"os"
|
2024-10-22 23:44:50 +08:00
|
|
|
"strings"
|
2024-08-02 19:41:39 +08:00
|
|
|
"time"
|
2024-09-02 22:13:13 +08:00
|
|
|
_ "time/tzdata"
|
2022-04-27 23:51:45 +08:00
|
|
|
|
2024-10-20 11:47:45 +08:00
|
|
|
"github.com/ory/graceful"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
2024-10-22 23:44:50 +08:00
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"golang.org/x/net/http2/h2c"
|
2024-10-20 11:47:45 +08:00
|
|
|
|
2024-11-28 19:38:54 +08:00
|
|
|
"github.com/nezhahq/nezha/cmd/dashboard/controller"
|
|
|
|
"github.com/nezhahq/nezha/cmd/dashboard/rpc"
|
|
|
|
"github.com/nezhahq/nezha/model"
|
|
|
|
"github.com/nezhahq/nezha/proto"
|
|
|
|
"github.com/nezhahq/nezha/service/singleton"
|
2023-11-02 21:06:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type DashboardCliParam struct {
|
|
|
|
Version bool // 当前版本号
|
|
|
|
ConfigFile string // 配置文件路径
|
|
|
|
DatebaseLocation string // Sqlite3 数据库文件路径
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
dashboardCliParam DashboardCliParam
|
2024-12-07 01:18:34 +08:00
|
|
|
//go:embed *-dist
|
|
|
|
frontendDist embed.FS
|
2019-12-05 22:36:58 +08:00
|
|
|
)
|
|
|
|
|
2021-01-19 09:59:04 +08:00
|
|
|
func initSystem() {
|
2024-10-20 11:47:45 +08:00
|
|
|
// 初始化管理员账户
|
|
|
|
var usersCount int64
|
|
|
|
if err := singleton.DB.Model(&model.User{}).Count(&usersCount).Error; err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if usersCount == 0 {
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
admin := model.User{
|
|
|
|
Username: "admin",
|
|
|
|
Password: string(hash),
|
|
|
|
}
|
|
|
|
if err := singleton.DB.Create(&admin).Error; err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:16:33 +08:00
|
|
|
// 启动 singleton 包下的所有服务
|
|
|
|
singleton.LoadSingleton()
|
2021-01-19 09:59:04 +08:00
|
|
|
|
2022-04-11 22:51:02 +08:00
|
|
|
// 每天的3:30 对 监控记录 和 流量记录 进行清理
|
2024-10-25 00:13:45 +08:00
|
|
|
if _, err := singleton.Cron.AddFunc("0 30 3 * * *", singleton.CleanServiceHistory); err != nil {
|
2021-07-19 10:37:12 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
2021-09-02 23:45:21 +08:00
|
|
|
|
2022-04-11 22:51:02 +08:00
|
|
|
// 每小时对流量记录进行打点
|
2022-04-12 13:16:33 +08:00
|
|
|
if _, err := singleton.Cron.AddFunc("0 0 * * * *", singleton.RecordTransferHourlyUsage); err != nil {
|
2021-07-19 10:37:12 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
2021-07-14 23:53:37 +08:00
|
|
|
}
|
|
|
|
|
2024-10-20 14:05:43 +08:00
|
|
|
// @title Nezha Monitoring API
|
|
|
|
// @version 1.0
|
|
|
|
// @description Nezha Monitoring API
|
|
|
|
// @termsOfService http://nezhahq.github.io
|
|
|
|
|
|
|
|
// @contact.name API Support
|
|
|
|
// @contact.url http://nezhahq.github.io
|
|
|
|
// @contact.email hi@nai.ba
|
|
|
|
|
|
|
|
// @license.name Apache 2.0
|
|
|
|
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
|
|
|
|
|
|
|
// @host localhost:8008
|
|
|
|
// @BasePath /api/v1
|
|
|
|
|
|
|
|
// @securityDefinitions.apikey BearerAuth
|
|
|
|
// @in header
|
|
|
|
// @name Authorization
|
|
|
|
|
|
|
|
// @externalDocs.description OpenAPI
|
|
|
|
// @externalDocs.url https://swagger.io/resources/open-api/
|
2019-12-08 16:59:58 +08:00
|
|
|
func main() {
|
2024-11-25 22:03:11 +08:00
|
|
|
flag.BoolVar(&dashboardCliParam.Version, "v", false, "查看当前版本号")
|
|
|
|
flag.StringVar(&dashboardCliParam.ConfigFile, "c", "data/config.yaml", "配置文件路径")
|
2024-10-31 03:34:25 +08:00
|
|
|
flag.StringVar(&dashboardCliParam.DatebaseLocation, "db", "data/sqlite.db", "Sqlite3数据库文件路径")
|
|
|
|
flag.Parse()
|
|
|
|
|
2023-11-02 21:06:34 +08:00
|
|
|
if dashboardCliParam.Version {
|
|
|
|
fmt.Println(singleton.Version)
|
2024-10-31 00:15:41 +08:00
|
|
|
os.Exit(0)
|
2023-11-02 21:06:34 +08:00
|
|
|
}
|
|
|
|
|
2024-10-31 00:15:41 +08:00
|
|
|
// 初始化 dao 包
|
|
|
|
singleton.InitConfigFromPath(dashboardCliParam.ConfigFile)
|
|
|
|
singleton.InitTimezoneAndCache()
|
|
|
|
singleton.InitDBFromPath(dashboardCliParam.DatebaseLocation)
|
|
|
|
initSystem()
|
|
|
|
|
2024-12-05 17:01:21 +08:00
|
|
|
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", singleton.Conf.ListenHost, singleton.Conf.ListenPort))
|
2024-10-20 11:47:45 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-10-25 00:13:45 +08:00
|
|
|
singleton.CleanServiceHistory()
|
|
|
|
serviceSentinelDispatchBus := make(chan model.Service) // 用于传递服务监控任务信息的channel
|
2024-12-05 00:11:34 +08:00
|
|
|
rpc.DispatchKeepalive()
|
2021-09-02 23:45:21 +08:00
|
|
|
go rpc.DispatchTask(serviceSentinelDispatchBus)
|
2022-01-09 11:54:14 +08:00
|
|
|
go singleton.AlertSentinelStart()
|
|
|
|
singleton.NewServiceSentinel(serviceSentinelDispatchBus)
|
2024-10-19 23:14:53 +08:00
|
|
|
|
2024-10-22 23:44:50 +08:00
|
|
|
grpcHandler := rpc.ServeRPC()
|
2024-12-07 01:18:34 +08:00
|
|
|
httpHandler := controller.ServeWeb(frontendDist)
|
2024-11-18 13:26:41 +08:00
|
|
|
controller.InitUpgrader()
|
2024-10-20 14:05:43 +08:00
|
|
|
|
2024-10-23 12:55:10 +08:00
|
|
|
muxHandler := newHTTPandGRPCMux(httpHandler, grpcHandler)
|
2024-10-22 23:44:50 +08:00
|
|
|
http2Server := &http2.Server{}
|
2024-10-23 12:55:10 +08:00
|
|
|
muxServer := &http.Server{Handler: h2c.NewHandler(muxHandler, http2Server), ReadHeaderTimeout: time.Second * 5}
|
2024-10-22 23:44:50 +08:00
|
|
|
|
|
|
|
if err := graceful.Graceful(func() error {
|
2024-12-05 17:01:21 +08:00
|
|
|
log.Printf("NEZHA>> Dashboard::START ON %s:%d", singleton.Conf.ListenHost, singleton.Conf.ListenPort)
|
2024-10-23 12:55:10 +08:00
|
|
|
return muxServer.Serve(l)
|
2024-10-22 23:44:50 +08:00
|
|
|
}, func(c context.Context) error {
|
|
|
|
log.Println("NEZHA>> Graceful::START")
|
|
|
|
singleton.RecordTransferHourlyUsage()
|
|
|
|
log.Println("NEZHA>> Graceful::END")
|
2024-10-23 12:55:10 +08:00
|
|
|
return muxServer.Shutdown(c)
|
2024-10-22 23:44:50 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Printf("NEZHA>> ERROR: %v", err)
|
|
|
|
}
|
2019-12-05 22:36:58 +08:00
|
|
|
}
|
2024-08-02 19:41:39 +08:00
|
|
|
|
2024-10-22 23:44:50 +08:00
|
|
|
func newHTTPandGRPCMux(httpHandler http.Handler, grpcHandler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-10-23 20:37:29 +08:00
|
|
|
natConfig := singleton.GetNATConfigByDomain(r.Host)
|
|
|
|
if natConfig != nil {
|
|
|
|
rpc.ServeNAT(w, r, natConfig)
|
|
|
|
return
|
|
|
|
}
|
2024-10-23 12:55:10 +08:00
|
|
|
if r.ProtoMajor == 2 && r.Header.Get("Content-Type") == "application/grpc" &&
|
|
|
|
strings.HasPrefix(r.URL.Path, "/"+proto.NezhaService_ServiceDesc.ServiceName) {
|
2024-10-22 23:44:50 +08:00
|
|
|
grpcHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
httpHandler.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|