nezha/cmd/dashboard/main.go

168 lines
4.7 KiB
Go
Raw Permalink Normal View History

2019-12-05 09:36:58 -05:00
package main
import (
"context"
2024-11-29 08:31:39 -05:00
"embed"
"flag"
2023-11-02 09:06:34 -04:00
"fmt"
2022-04-27 11:51:45 -04:00
"log"
"net"
2024-10-22 11:44:50 -04:00
"net/http"
2024-10-30 15:34:25 -04:00
"os"
2024-10-22 11:44:50 -04:00
"strings"
"time"
_ "time/tzdata"
2022-04-27 11:51:45 -04:00
2025-01-21 09:23:15 -05:00
"github.com/gin-gonic/gin"
"github.com/ory/graceful"
"golang.org/x/crypto/bcrypt"
2024-10-22 11:44:50 -04:00
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
2024-11-28 06:38:54 -05:00
"github.com/nezhahq/nezha/cmd/dashboard/controller"
2025-01-21 09:23:15 -05:00
"github.com/nezhahq/nezha/cmd/dashboard/controller/waf"
2024-11-28 06:38:54 -05:00
"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 09:06:34 -04:00
)
type DashboardCliParam struct {
Version bool // 当前版本号
ConfigFile string // 配置文件路径
DatabaseLocation string // Sqlite3 数据库文件路径
2023-11-02 09:06:34 -04:00
}
var (
dashboardCliParam DashboardCliParam
2024-12-06 12:18:34 -05:00
//go:embed *-dist
frontendDist embed.FS
2019-12-05 09:36:58 -05:00
)
func initSystem() {
// 初始化管理员账户
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 01:16:33 -04:00
// 启动 singleton 包下的所有服务
singleton.LoadSingleton()
2022-04-11 10:51:02 -04:00
// 每天的3:30 对 监控记录 和 流量记录 进行清理
2024-10-24 12:13:45 -04:00
if _, err := singleton.Cron.AddFunc("0 30 3 * * *", singleton.CleanServiceHistory); err != nil {
2021-07-18 22:37:12 -04:00
panic(err)
}
2022-04-11 10:51:02 -04:00
// 每小时对流量记录进行打点
2022-04-12 01:16:33 -04:00
if _, err := singleton.Cron.AddFunc("0 0 * * * *", singleton.RecordTransferHourlyUsage); err != nil {
2021-07-18 22:37:12 -04:00
panic(err)
}
}
2024-10-20 02:05:43 -04: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 03:59:58 -05:00
func main() {
flag.BoolVar(&dashboardCliParam.Version, "v", false, "查看当前版本号")
flag.StringVar(&dashboardCliParam.ConfigFile, "c", "data/config.yaml", "配置文件路径")
flag.StringVar(&dashboardCliParam.DatabaseLocation, "db", "data/sqlite.db", "Sqlite3数据库文件路径")
2024-10-30 15:34:25 -04:00
flag.Parse()
2023-11-02 09:06:34 -04:00
if dashboardCliParam.Version {
fmt.Println(singleton.Version)
os.Exit(0)
2023-11-02 09:06:34 -04:00
}
// 初始化 dao 包
singleton.InitFrontendTemplates()
singleton.InitConfigFromPath(dashboardCliParam.ConfigFile)
singleton.InitTimezoneAndCache()
singleton.InitDBFromPath(dashboardCliParam.DatabaseLocation)
initSystem()
2024-12-05 04:01:21 -05:00
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", singleton.Conf.ListenHost, singleton.Conf.ListenPort))
if err != nil {
log.Fatal(err)
}
2024-10-24 12:13:45 -04:00
singleton.CleanServiceHistory()
serviceSentinelDispatchBus := make(chan model.Service) // 用于传递服务监控任务信息的channel
2024-12-04 11:11:34 -05:00
rpc.DispatchKeepalive()
go rpc.DispatchTask(serviceSentinelDispatchBus)
2022-01-08 22:54:14 -05:00
go singleton.AlertSentinelStart()
singleton.NewServiceSentinel(serviceSentinelDispatchBus)
2024-10-19 11:14:53 -04:00
2024-10-22 11:44:50 -04:00
grpcHandler := rpc.ServeRPC()
2024-12-06 12:18:34 -05:00
httpHandler := controller.ServeWeb(frontendDist)
controller.InitUpgrader()
2024-10-20 02:05:43 -04:00
2024-10-23 00:55:10 -04:00
muxHandler := newHTTPandGRPCMux(httpHandler, grpcHandler)
2024-10-22 11:44:50 -04:00
http2Server := &http2.Server{}
2024-10-23 00:55:10 -04:00
muxServer := &http.Server{Handler: h2c.NewHandler(muxHandler, http2Server), ReadHeaderTimeout: time.Second * 5}
2024-10-22 11:44:50 -04:00
if err := graceful.Graceful(func() error {
2024-12-05 04:01:21 -05:00
log.Printf("NEZHA>> Dashboard::START ON %s:%d", singleton.Conf.ListenHost, singleton.Conf.ListenPort)
2024-10-23 00:55:10 -04:00
return muxServer.Serve(l)
2024-10-22 11:44:50 -04:00
}, func(c context.Context) error {
log.Println("NEZHA>> Graceful::START")
singleton.RecordTransferHourlyUsage()
log.Println("NEZHA>> Graceful::END")
2024-10-23 00:55:10 -04:00
return muxServer.Shutdown(c)
2024-10-22 11:44:50 -04:00
}); err != nil {
log.Printf("NEZHA>> ERROR: %v", err)
}
2019-12-05 09:36:58 -05:00
}
2024-10-22 11:44:50 -04:00
func newHTTPandGRPCMux(httpHandler http.Handler, grpcHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2024-10-23 08:37:29 -04:00
natConfig := singleton.GetNATConfigByDomain(r.Host)
if natConfig != nil {
2025-01-21 09:23:15 -05:00
if !natConfig.Enabled {
c, _ := gin.CreateTestContext(w)
waf.ShowBlockPage(c, fmt.Errorf("nat host %s is disabled", natConfig.Domain))
return
}
2024-10-23 08:37:29 -04:00
rpc.ServeNAT(w, r, natConfig)
return
}
2024-10-23 00:55:10 -04:00
if r.ProtoMajor == 2 && r.Header.Get("Content-Type") == "application/grpc" &&
strings.HasPrefix(r.URL.Path, "/"+proto.NezhaService_ServiceDesc.ServiceName) {
2024-10-22 11:44:50 -04:00
grpcHandler.ServeHTTP(w, r)
return
}
httpHandler.ServeHTTP(w, r)
})
}