middleware.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package router
  2. import (
  3. "encoding/base64"
  4. "github.com/0xJacky/Nginx-UI/frontend"
  5. "github.com/0xJacky/Nginx-UI/server/internal/logger"
  6. "github.com/0xJacky/Nginx-UI/server/model"
  7. "github.com/0xJacky/Nginx-UI/server/settings"
  8. "github.com/gin-contrib/static"
  9. "github.com/gin-gonic/gin"
  10. "github.com/spf13/cast"
  11. "io/fs"
  12. "net/http"
  13. "path"
  14. "runtime"
  15. "strings"
  16. )
  17. func recovery() gin.HandlerFunc {
  18. return func(c *gin.Context) {
  19. defer func() {
  20. if err := recover(); err != nil {
  21. errorAction := "panic"
  22. if action, ok := c.Get("maybe_error"); ok {
  23. errorActionMsg := cast.ToString(action)
  24. if errorActionMsg != "" {
  25. errorAction = errorActionMsg
  26. }
  27. }
  28. buf := make([]byte, 1024)
  29. runtime.Stack(buf, false)
  30. logger.Errorf("%s\n%s", err, buf)
  31. c.JSON(http.StatusInternalServerError, gin.H{
  32. "message": err.(error).Error(),
  33. "error": errorAction,
  34. })
  35. }
  36. }()
  37. c.Next()
  38. }
  39. }
  40. func authRequired() gin.HandlerFunc {
  41. return func(c *gin.Context) {
  42. abortWithAuthFailure := func() {
  43. c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
  44. "message": "Authorization failed",
  45. })
  46. }
  47. token := c.GetHeader("Authorization")
  48. if token == "" {
  49. if token = c.GetHeader("X-Node-Secret"); token != "" && token == settings.ServerSettings.NodeSecret {
  50. c.Set("NodeSecret", token)
  51. c.Next()
  52. return
  53. } else {
  54. c.Set("ProxyNodeID", c.Query("x_node_id"))
  55. tokenBytes, _ := base64.StdEncoding.DecodeString(c.Query("token"))
  56. token = string(tokenBytes)
  57. if token == "" {
  58. abortWithAuthFailure()
  59. return
  60. }
  61. }
  62. }
  63. if model.CheckToken(token) < 1 {
  64. abortWithAuthFailure()
  65. return
  66. }
  67. if nodeID := c.GetHeader("X-Node-ID"); nodeID != "" {
  68. c.Set("ProxyNodeID", nodeID)
  69. }
  70. c.Next()
  71. }
  72. }
  73. type serverFileSystemType struct {
  74. http.FileSystem
  75. }
  76. func (f serverFileSystemType) Exists(prefix string, _path string) bool {
  77. file, err := f.Open(path.Join(prefix, _path))
  78. if file != nil {
  79. defer func(file http.File) {
  80. err = file.Close()
  81. if err != nil {
  82. logger.Error("file not found", err)
  83. }
  84. }(file)
  85. }
  86. return err == nil
  87. }
  88. func mustFS(dir string) (serverFileSystem static.ServeFileSystem) {
  89. sub, err := fs.Sub(frontend.DistFS, path.Join("dist", dir))
  90. if err != nil {
  91. logger.Error(err)
  92. return
  93. }
  94. serverFileSystem = serverFileSystemType{
  95. http.FS(sub),
  96. }
  97. return
  98. }
  99. func cacheJs() gin.HandlerFunc {
  100. return func(c *gin.Context) {
  101. if strings.Contains(c.Request.URL.String(), "js") {
  102. c.Header("Cache-Control", "max-age: 1296000")
  103. if c.Request.Header.Get("If-Modified-Since") == settings.LastModified {
  104. c.AbortWithStatus(http.StatusNotModified)
  105. }
  106. c.Header("Last-Modified", settings.LastModified)
  107. }
  108. }
  109. }