middleware.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. if token := c.GetHeader("X-Node-Secret"); token != "" && token == settings.NodeSettings.Secret {
  46. c.Set("Secret", token)
  47. c.Next()
  48. return
  49. }
  50. if token := c.Query("node_secret"); token != "" && token == settings.NodeSettings.Secret {
  51. c.Set("Secret", token)
  52. c.Next()
  53. return
  54. }
  55. token := getToken(c)
  56. if token == "" {
  57. abortWithAuthFailure()
  58. return
  59. }
  60. u, ok := user.GetTokenUser(token)
  61. if !ok {
  62. abortWithAuthFailure()
  63. return
  64. }
  65. c.Set("user", u)
  66. c.Next()
  67. }
  68. }
  69. type ServerFileSystemType struct {
  70. http.FileSystem
  71. }
  72. func (f ServerFileSystemType) Exists(prefix string, _path string) bool {
  73. file, err := f.Open(path.Join(prefix, _path))
  74. if file != nil {
  75. defer func(file http.File) {
  76. err = file.Close()
  77. if err != nil {
  78. logger.Error("file not found", err)
  79. }
  80. }(file)
  81. }
  82. return err == nil
  83. }
  84. // CacheJs is a middleware that send header to client to cache js file
  85. func CacheJs() gin.HandlerFunc {
  86. return func(c *gin.Context) {
  87. if strings.Contains(c.Request.URL.String(), "js") {
  88. c.Header("Cache-Control", "max-age: 1296000")
  89. if c.Request.Header.Get("If-Modified-Since") == settings.LastModified {
  90. c.AbortWithStatus(http.StatusNotModified)
  91. }
  92. c.Header("Last-Modified", settings.LastModified)
  93. }
  94. }
  95. }