nezha/cmd/dashboard/controller/controller.go

167 lines
3.9 KiB
Go
Raw Normal View History

2019-12-08 03:59:58 -05:00
package controller
import (
"fmt"
2023-11-28 20:42:51 -05:00
"log"
"net/http"
2019-12-08 03:59:58 -05:00
"time"
2024-10-19 12:09:16 -04:00
jwt "github.com/appleboy/gin-jwt/v2"
2021-05-10 06:04:38 -04:00
"github.com/gin-contrib/pprof"
2019-12-08 03:59:58 -05:00
"github.com/gin-gonic/gin"
2024-07-14 07:41:50 -04:00
"github.com/hashicorp/go-uuid"
2024-10-19 11:14:53 -04:00
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
2019-12-08 03:59:58 -05:00
2024-10-19 11:14:53 -04:00
docs "github.com/naiba/nezha/cmd/dashboard/docs"
2024-07-14 07:41:50 -04:00
"github.com/naiba/nezha/model"
2020-11-10 21:07:45 -05:00
"github.com/naiba/nezha/pkg/mygin"
2024-07-14 07:41:50 -04:00
"github.com/naiba/nezha/pkg/utils"
"github.com/naiba/nezha/proto"
"github.com/naiba/nezha/service/rpc"
2022-01-08 22:54:14 -05:00
"github.com/naiba/nezha/service/singleton"
2019-12-08 03:59:58 -05:00
)
2024-10-19 12:09:16 -04:00
// @title Swagger Example API
// @version 1.0
// @description This is a sample server celler server.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /api/v1
// @securityDefinitions.basic BasicAuth
// @externalDocs.description OpenAPI
// @externalDocs.url https://swagger.io/resources/open-api/
func ServeWeb(port uint) *http.Server {
2019-12-08 10:18:29 -05:00
gin.SetMode(gin.ReleaseMode)
2021-05-10 06:04:38 -04:00
r := gin.Default()
2024-10-19 11:14:53 -04:00
docs.SwaggerInfo.BasePath = "/api/v1"
2024-07-14 07:41:50 -04:00
if singleton.Conf.Debug {
gin.SetMode(gin.DebugMode)
pprof.Register(r)
}
r.Use(natGateway)
2024-10-19 11:14:53 -04:00
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
r.Use(mygin.RecordPath)
2019-12-08 03:59:58 -05:00
routers(r)
2021-08-10 08:13:17 -04:00
page404 := func(c *gin.Context) {
mygin.ShowErrorPage(c, mygin.ErrInfo{
Code: http.StatusNotFound,
Title: "该页面不存在",
Msg: "该页面内容可能已着陆火星",
Link: "/",
Btn: "返回首页",
}, true)
}
r.NoRoute(page404)
r.NoMethod(page404)
srv := &http.Server{
2022-06-22 00:53:21 -04:00
Addr: fmt.Sprintf(":%d", port),
ReadHeaderTimeout: time.Second * 5,
Handler: r,
}
return srv
2019-12-08 03:59:58 -05:00
}
func routers(r *gin.Engine) {
2024-10-19 12:09:16 -04:00
authMiddleware, err := jwt.New(initParams())
if err != nil {
log.Fatal("JWT Error:" + err.Error())
}
// register middleware
r.Use(handlerMiddleWare(authMiddleware))
r.POST("/login", authMiddleware.LoginHandler)
auth := r.Group("/auth", authMiddleware.MiddlewareFunc())
auth.GET("/refresh_token", authMiddleware.RefreshHandler)
2019-12-08 03:59:58 -05:00
// 通用页面
cp := commonPage{r: r}
2019-12-08 03:59:58 -05:00
cp.serve()
// 游客页面
gp := guestPage{r}
gp.serve()
// 会员页面
mp := &memberPage{r}
mp.serve()
// API
api := r.Group("api")
{
ma := &memberAPI{api}
ma.serve()
}
}
2024-07-14 07:41:50 -04:00
func natGateway(c *gin.Context) {
natConfig := singleton.GetNATConfigByDomain(c.Request.Host)
if natConfig == nil {
return
}
singleton.ServerLock.RLock()
server := singleton.ServerList[natConfig.ServerID]
singleton.ServerLock.RUnlock()
if server == nil || server.TaskStream == nil {
c.Writer.WriteString("server not found or not connected")
c.Abort()
return
}
streamId, err := uuid.GenerateUUID()
if err != nil {
c.Writer.WriteString(fmt.Sprintf("stream id error: %v", err))
c.Abort()
return
}
rpc.NezhaHandlerSingleton.CreateStream(streamId)
defer rpc.NezhaHandlerSingleton.CloseStream(streamId)
taskData, err := utils.Json.Marshal(model.TaskNAT{
2024-07-14 07:41:50 -04:00
StreamID: streamId,
Host: natConfig.Host,
})
if err != nil {
c.Writer.WriteString(fmt.Sprintf("task data error: %v", err))
c.Abort()
return
}
if err := server.TaskStream.Send(&proto.Task{
Type: model.TaskTypeNAT,
Data: string(taskData),
}); err != nil {
c.Writer.WriteString(fmt.Sprintf("send task error: %v", err))
c.Abort()
return
}
w, err := utils.NewRequestWrapper(c.Request, c.Writer)
if err != nil {
c.Writer.WriteString(fmt.Sprintf("request wrapper error: %v", err))
c.Abort()
return
}
if err := rpc.NezhaHandlerSingleton.UserConnected(streamId, w); err != nil {
c.Writer.WriteString(fmt.Sprintf("user connected error: %v", err))
c.Abort()
return
}
rpc.NezhaHandlerSingleton.StartStream(streamId, time.Second*10)
c.Abort()
}