middleware.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package middleware
  2. import (
  3. "encoding/base64"
  4. "net/http"
  5. "path"
  6. "strings"
  7. "github.com/0xJacky/Nginx-UI/internal/user"
  8. "github.com/0xJacky/Nginx-UI/settings"
  9. "github.com/gin-gonic/gin"
  10. "github.com/uozi-tech/cosy/logger"
  11. )
  12. // getToken from header, cookie or query
  13. func getToken(c *gin.Context) (token string) {
  14. if token = c.GetHeader("Authorization"); token != "" {
  15. return
  16. }
  17. if token = c.Query("token"); token != "" {
  18. tokenBytes, _ := base64.StdEncoding.DecodeString(token)
  19. return string(tokenBytes)
  20. }
  21. if token, _ = c.Cookie("token"); token != "" {
  22. return token
  23. }
  24. return ""
  25. }
  26. // getXNodeID from header or query
  27. func getXNodeID(c *gin.Context) (xNodeID string) {
  28. if xNodeID = c.GetHeader("X-Node-ID"); xNodeID != "" {
  29. return xNodeID
  30. }
  31. return c.Query("x_node_id")
  32. }
  33. // AuthRequired is a middleware that checks if the user is authenticated
  34. func AuthRequired() gin.HandlerFunc {
  35. return func(c *gin.Context) {
  36. abortWithAuthFailure := func() {
  37. c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
  38. "message": "Authorization failed",
  39. })
  40. }
  41. xNodeID := getXNodeID(c)
  42. if xNodeID != "" {
  43. c.Set("ProxyNodeID", xNodeID)
  44. }
  45. initUser := user.GetInitUser(c)
  46. if token := c.GetHeader("X-Node-Secret"); token != "" && token == settings.NodeSettings.Secret {
  47. c.Set("Secret", token)
  48. c.Set("user", initUser)
  49. c.Next()
  50. return
  51. }
  52. if token := c.Query("node_secret"); token != "" && token == settings.NodeSettings.Secret {
  53. c.Set("Secret", token)
  54. c.Set("user", initUser)
  55. c.Next()
  56. return
  57. }
  58. token := getToken(c)
  59. if token == "" {
  60. abortWithAuthFailure()
  61. return
  62. }
  63. u, ok := user.GetTokenUser(token)
  64. if !ok {
  65. abortWithAuthFailure()
  66. return
  67. }
  68. c.Set("user", u)
  69. c.Next()
  70. }
  71. }
  72. type ServerFileSystemType struct {
  73. http.FileSystem
  74. }
  75. func (f ServerFileSystemType) Exists(prefix string, _path string) bool {
  76. file, err := f.Open(path.Join(prefix, _path))
  77. if file != nil {
  78. defer func(file http.File) {
  79. err = file.Close()
  80. if err != nil {
  81. logger.Error("file not found", err)
  82. }
  83. }(file)
  84. }
  85. return err == nil
  86. }
  87. // CacheJs is a middleware that send header to client to cache js file
  88. func CacheJs() gin.HandlerFunc {
  89. return func(c *gin.Context) {
  90. if strings.Contains(c.Request.URL.String(), "js") {
  91. c.Header("Cache-Control", "max-age: 1296000")
  92. if c.Request.Header.Get("If-Modified-Since") == settings.LastModified {
  93. c.AbortWithStatus(http.StatusNotModified)
  94. }
  95. c.Header("Last-Modified", settings.LastModified)
  96. }
  97. }
  98. }