2019-12-05 09:36:58 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-07-14 11:53:37 -04:00
|
|
|
"context"
|
2025-02-28 09:02:54 -05:00
|
|
|
"crypto/tls"
|
2024-11-29 08:31:39 -05:00
|
|
|
"embed"
|
2025-02-28 09:02:54 -05:00
|
|
|
"errors"
|
2024-11-25 09:03:11 -05:00
|
|
|
"flag"
|
2023-11-02 09:06:34 -04:00
|
|
|
"fmt"
|
2022-04-27 11:51:45 -04:00
|
|
|
"log"
|
2024-10-19 23:47:45 -04:00
|
|
|
"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"
|
2024-08-02 07:41:39 -04:00
|
|
|
"time"
|
2024-09-02 10:13:13 -04:00
|
|
|
_ "time/tzdata"
|
2022-04-27 11:51:45 -04:00
|
|
|
|
2025-01-21 09:23:15 -05:00
|
|
|
"github.com/gin-gonic/gin"
|
2024-10-19 23:47:45 -04:00
|
|
|
"github.com/ory/graceful"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
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"
|
2025-03-02 02:37:21 -05:00
|
|
|
"github.com/nezhahq/nezha/pkg/utils"
|
2024-11-28 06:38:54 -05:00
|
|
|
"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 // 配置文件路径
|
2025-01-03 09:39:55 -05:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2025-03-08 05:47:42 -05:00
|
|
|
func initSystem() error {
|
2024-10-19 23:47:45 -04:00
|
|
|
// 初始化管理员账户
|
|
|
|
var usersCount int64
|
|
|
|
if err := singleton.DB.Model(&model.User{}).Count(&usersCount).Error; err != nil {
|
2025-03-08 05:47:42 -05:00
|
|
|
return err
|
2024-10-19 23:47:45 -04:00
|
|
|
}
|
|
|
|
if usersCount == 0 {
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
2025-03-08 05:47:42 -05:00
|
|
|
return err
|
2024-10-19 23:47:45 -04:00
|
|
|
}
|
|
|
|
admin := model.User{
|
|
|
|
Username: "admin",
|
|
|
|
Password: string(hash),
|
|
|
|
}
|
|
|
|
if err := singleton.DB.Create(&admin).Error; err != nil {
|
2025-03-08 05:47:42 -05:00
|
|
|
return err
|
2024-10-19 23:47:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 01:16:33 -04:00
|
|
|
// 启动 singleton 包下的所有服务
|
|
|
|
singleton.LoadSingleton()
|
2021-01-18 20:59:04 -05:00
|
|
|
|
2022-04-11 10:51:02 -04:00
|
|
|
// 每天的3:30 对 监控记录 和 流量记录 进行清理
|
2025-02-21 10:08:12 -05:00
|
|
|
if _, err := singleton.CronShared.AddFunc("0 30 3 * * *", singleton.CleanServiceHistory); err != nil {
|
2025-03-08 05:47:42 -05:00
|
|
|
return err
|
2021-07-18 22:37:12 -04:00
|
|
|
}
|
2021-09-02 11:45:21 -04:00
|
|
|
|
2022-04-11 10:51:02 -04:00
|
|
|
// 每小时对流量记录进行打点
|
2025-02-21 10:08:12 -05:00
|
|
|
if _, err := singleton.CronShared.AddFunc("0 0 * * * *", singleton.RecordTransferHourlyUsage); err != nil {
|
2025-03-08 05:47:42 -05:00
|
|
|
return err
|
2021-07-18 22:37:12 -04:00
|
|
|
}
|
2025-03-08 05:47:42 -05:00
|
|
|
return nil
|
2021-07-14 11:53:37 -04:00
|
|
|
}
|
|
|
|
|
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() {
|
2024-11-25 09:03:11 -05:00
|
|
|
flag.BoolVar(&dashboardCliParam.Version, "v", false, "查看当前版本号")
|
|
|
|
flag.StringVar(&dashboardCliParam.ConfigFile, "c", "data/config.yaml", "配置文件路径")
|
2025-01-03 09:39:55 -05:00
|
|
|
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)
|
2024-10-30 12:15:41 -04:00
|
|
|
os.Exit(0)
|
2023-11-02 09:06:34 -04:00
|
|
|
}
|
|
|
|
|
2024-10-30 12:15:41 -04:00
|
|
|
// 初始化 dao 包
|
2024-12-10 08:57:20 -05:00
|
|
|
singleton.InitFrontendTemplates()
|
2024-10-30 12:15:41 -04:00
|
|
|
singleton.InitConfigFromPath(dashboardCliParam.ConfigFile)
|
|
|
|
singleton.InitTimezoneAndCache()
|
2025-01-03 09:39:55 -05:00
|
|
|
singleton.InitDBFromPath(dashboardCliParam.DatabaseLocation)
|
2025-03-08 05:47:42 -05:00
|
|
|
if err := initSystem(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2024-10-30 12:15:41 -04:00
|
|
|
|
2024-12-05 04:01:21 -05:00
|
|
|
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", singleton.Conf.ListenHost, singleton.Conf.ListenPort))
|
2024-10-19 23:47:45 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-10-24 12:13:45 -04:00
|
|
|
singleton.CleanServiceHistory()
|
2025-02-21 10:08:12 -05:00
|
|
|
serviceSentinelDispatchBus := make(chan *model.Service) // 用于传递服务监控任务信息的channel
|
2024-12-04 11:11:34 -05:00
|
|
|
rpc.DispatchKeepalive()
|
2021-09-02 11:45:21 -04:00
|
|
|
go rpc.DispatchTask(serviceSentinelDispatchBus)
|
2022-01-08 22:54:14 -05:00
|
|
|
go singleton.AlertSentinelStart()
|
2025-02-21 10:08:12 -05:00
|
|
|
singleton.ServiceSentinelShared, err = singleton.NewServiceSentinel(
|
|
|
|
serviceSentinelDispatchBus, singleton.ServerShared, singleton.NotificationShared, singleton.CronShared)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
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)
|
2024-11-18 00:26:41 -05:00
|
|
|
controller.InitUpgrader()
|
2024-10-20 02:05:43 -04:00
|
|
|
|
2024-10-23 00:55:10 -04:00
|
|
|
muxHandler := newHTTPandGRPCMux(httpHandler, grpcHandler)
|
2025-02-28 09:02:54 -05:00
|
|
|
muxServerHTTP := &http.Server{
|
|
|
|
Handler: muxHandler,
|
|
|
|
ReadHeaderTimeout: time.Second * 5,
|
|
|
|
}
|
|
|
|
muxServerHTTP.Protocols = new(http.Protocols)
|
|
|
|
muxServerHTTP.Protocols.SetHTTP1(true)
|
|
|
|
muxServerHTTP.Protocols.SetUnencryptedHTTP2(true)
|
|
|
|
|
|
|
|
var muxServerHTTPS *http.Server
|
|
|
|
if singleton.Conf.HTTPS.ListenPort != 0 {
|
|
|
|
muxServerHTTPS = &http.Server{
|
|
|
|
Addr: fmt.Sprintf("%s:%d", singleton.Conf.ListenHost, singleton.Conf.HTTPS.ListenPort),
|
|
|
|
Handler: muxHandler,
|
|
|
|
ReadHeaderTimeout: time.Second * 5,
|
|
|
|
TLSConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: singleton.Conf.HTTPS.InsecureTLS,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errChan := make(chan error, 2)
|
2025-03-02 02:37:21 -05:00
|
|
|
errHTTPS := errors.New("error from https server")
|
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)
|
2025-02-28 09:02:54 -05:00
|
|
|
if singleton.Conf.HTTPS.ListenPort != 0 {
|
|
|
|
go func() {
|
|
|
|
errChan <- muxServerHTTPS.ListenAndServeTLS(singleton.Conf.HTTPS.TLSCertPath, singleton.Conf.HTTPS.TLSKeyPath)
|
|
|
|
}()
|
|
|
|
log.Printf("NEZHA>> Dashboard::START ON %s:%d", singleton.Conf.ListenHost, singleton.Conf.HTTPS.ListenPort)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
errChan <- muxServerHTTP.Serve(l)
|
|
|
|
}()
|
|
|
|
return <-errChan
|
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")
|
2025-03-02 02:37:21 -05:00
|
|
|
var err error
|
|
|
|
if muxServerHTTPS != nil {
|
|
|
|
err = muxServerHTTPS.Shutdown(c)
|
|
|
|
}
|
|
|
|
return errors.Join(muxServerHTTP.Shutdown(c), utils.IfOr(err != nil, utils.NewWrapError(errHTTPS, err), nil))
|
2024-10-22 11:44:50 -04:00
|
|
|
}); err != nil {
|
|
|
|
log.Printf("NEZHA>> ERROR: %v", err)
|
2025-03-02 02:37:21 -05:00
|
|
|
var wrapError *utils.WrapError
|
|
|
|
if errors.As(err, &wrapError) {
|
|
|
|
log.Printf("NEZHA>> ERROR HTTPS: %v", wrapError.Unwrap())
|
2025-02-28 09:02:54 -05:00
|
|
|
}
|
2024-10-22 11:44:50 -04:00
|
|
|
}
|
2025-02-28 09:02:54 -05:00
|
|
|
|
|
|
|
close(errChan)
|
2019-12-05 09:36:58 -05:00
|
|
|
}
|
2024-08-02 07:41:39 -04: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) {
|
2025-02-21 10:08:12 -05:00
|
|
|
natConfig := singleton.NATShared.GetNATConfigByDomain(r.Host)
|
2024-10-23 08:37:29 -04:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|