middleware.go 2.6 KB

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