mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-09-19 17:50:12 +00:00
Introduce Personal Access Tokens (nzp_*) as a stateless auth path alongside
JWT, gated per-endpoint by a scope middleware (nezha:{resource}:{verb}) with
fail-closed empty-scope defaults and a server-id whitelist. Self-management
endpoints (profile, api-tokens, oauth2 bind, refresh-token) explicitly reject
PATs to block privilege-escalation chains. A revoke registry tears down active
long-lived connections (terminal, fm, ws, transfer, mcp) the moment a PAT is
deleted, with a tombstone closing the revoke->register race.
Add an MCP endpoint that proxies tool calls (exec, fs read/write/delete,
transfer) to agents over gRPC, guarded by origin/DNS-rebinding checks, a
per-token rate limiter, audit logging, and a kill switch. Serialize all
sends through the IOStream wrapper to honour grpc-go's concurrency contract.
Add CSRF double-submit protection on unsafe cookie-authenticated methods,
exempting authenticated PAT requests by context identity (not a forgeable
Authorization header). Apply visibility/whitelist filtering consistently
across list, get-by-id, and mutate paths to enforce tenant isolation.
Migrate legacy mcp:* scopes: rewrite read/exec to nezha:* equivalents and
drop dangerous write/delete/wildcard grants.
Co-authored-by: cloudcode <cloudcode@users.noreply.github.com>
237 lines
6.8 KiB
Go
237 lines
6.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"embed"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"runtime/debug"
|
|
"strings"
|
|
"time"
|
|
_ "time/tzdata"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/ory/graceful"
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"github.com/nezhahq/nezha/cmd/dashboard/controller"
|
|
"github.com/nezhahq/nezha/cmd/dashboard/controller/waf"
|
|
"github.com/nezhahq/nezha/cmd/dashboard/rpc"
|
|
"github.com/nezhahq/nezha/model"
|
|
"github.com/nezhahq/nezha/pkg/idcodec"
|
|
"github.com/nezhahq/nezha/pkg/utils"
|
|
"github.com/nezhahq/nezha/proto"
|
|
"github.com/nezhahq/nezha/service/singleton"
|
|
)
|
|
|
|
type DashboardCliParam struct {
|
|
Version bool
|
|
ConfigFile string
|
|
DatabaseLocation string
|
|
}
|
|
|
|
var (
|
|
dashboardCliParam DashboardCliParam
|
|
//go:embed *-dist
|
|
frontendDist embed.FS
|
|
)
|
|
|
|
func initSystem(bus chan<- *model.Service) error {
|
|
var usersCount int64
|
|
if err := singleton.DB.Model(&model.User{}).Count(&usersCount).Error; err != nil {
|
|
return err
|
|
}
|
|
if usersCount == 0 {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
admin := model.User{
|
|
Username: "admin",
|
|
Password: string(hash),
|
|
}
|
|
if err := singleton.DB.Create(&admin).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := singleton.LoadSingleton(bus); err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := singleton.CronShared.AddFunc("0 30 3 * * *", singleton.CleanMonitorHistory); err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := singleton.CronShared.AddFunc("0 0 * * * *", func() { singleton.RecordTransferHourlyUsage() }); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := singleton.StartJWTSessionGC(); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func initIDCodec() error {
|
|
return idcodec.Init([]byte(singleton.Conf.JWTSecretKey))
|
|
}
|
|
|
|
// @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
|
|
// @description JWT session token. Browser/UI flow. Format: `Bearer <jwt>` or cookie `nz-jwt`.
|
|
|
|
// @securityDefinitions.apikey APITokenAuth
|
|
// @in header
|
|
// @name Authorization
|
|
// @description Personal Access Token (PAT). Programmatic/CI/LLM flow. Format: `Bearer nzp_<secret>`.
|
|
// @description Each endpoint enforces a specific scope; see the `controller` package godoc for the authoritative scope table.
|
|
|
|
// @externalDocs.description OpenAPI
|
|
// @externalDocs.url https://swagger.io/resources/open-api/
|
|
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数据库文件路径")
|
|
flag.Parse()
|
|
|
|
if dashboardCliParam.Version {
|
|
fmt.Println(singleton.Version)
|
|
os.Exit(0)
|
|
}
|
|
|
|
serviceSentinelDispatchBus := make(chan *model.Service)
|
|
if err := utils.FirstError(singleton.InitFrontendTemplates,
|
|
func() error { return singleton.InitConfigFromPath(dashboardCliParam.ConfigFile) },
|
|
initIDCodec,
|
|
singleton.InitTimezoneAndCache,
|
|
func() error {
|
|
if singleton.Conf.Memory.GoMemLimitMB > 0 {
|
|
debug.SetMemoryLimit(singleton.Conf.Memory.GoMemLimitMB * 1024 * 1024)
|
|
log.Printf("NEZHA>> Go memory limit set to %d MB", singleton.Conf.Memory.GoMemLimitMB)
|
|
}
|
|
return nil
|
|
},
|
|
func() error { return singleton.InitDBFromPath(dashboardCliParam.DatabaseLocation) },
|
|
singleton.InitTSDB,
|
|
func() error { return initSystem(serviceSentinelDispatchBus) }); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", singleton.Conf.ListenHost, singleton.Conf.ListenPort))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
singleton.CleanMonitorHistory()
|
|
rpc.DispatchKeepalive()
|
|
rpc.SetMCPKillSwitchObserver(func() bool {
|
|
return singleton.Conf == nil || !singleton.Conf.MCPEnabled()
|
|
})
|
|
go rpc.DispatchTask(serviceSentinelDispatchBus)
|
|
go singleton.AlertSentinelStart()
|
|
|
|
grpcHandler := rpc.ServeRPC()
|
|
httpHandler := controller.ServeWeb(frontendDist)
|
|
controller.InitUpgrader()
|
|
|
|
muxHandler := newHTTPandGRPCMux(httpHandler, grpcHandler)
|
|
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)
|
|
errHTTPS := errors.New("error from https server")
|
|
|
|
if err := graceful.Graceful(func() error {
|
|
log.Printf("NEZHA>> Dashboard::START ON %s:%d", singleton.Conf.ListenHost, singleton.Conf.ListenPort)
|
|
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
|
|
}, func(c context.Context) error {
|
|
log.Println("NEZHA>> Graceful::START")
|
|
singleton.RecordTransferHourlyUsage()
|
|
singleton.CloseTSDB()
|
|
log.Println("NEZHA>> Graceful::END")
|
|
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))
|
|
}); err != nil {
|
|
log.Printf("NEZHA>> ERROR: %v", err)
|
|
var wrapError *utils.WrapError
|
|
if errors.As(err, &wrapError) {
|
|
log.Printf("NEZHA>> ERROR HTTPS: %v", wrapError.Unwrap())
|
|
}
|
|
}
|
|
|
|
close(errChan)
|
|
}
|
|
|
|
func newHTTPandGRPCMux(httpHandler http.Handler, grpcHandler http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
natConfig := singleton.NATShared.GetNATConfigByDomain(r.Host)
|
|
if natConfig != nil {
|
|
if !natConfig.Enabled {
|
|
c, _ := gin.CreateTestContext(w)
|
|
waf.ShowBlockPage(c, fmt.Errorf("nat host %s is disabled", natConfig.Domain))
|
|
return
|
|
}
|
|
rpc.ServeNAT(w, r, natConfig)
|
|
return
|
|
}
|
|
if r.ProtoMajor == 2 && r.Header.Get("Content-Type") == "application/grpc" &&
|
|
strings.HasPrefix(r.URL.Path, "/"+proto.NezhaService_ServiceDesc.ServiceName) {
|
|
grpcHandler.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
httpHandler.ServeHTTP(w, r)
|
|
})
|
|
}
|