middleware.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package router
  2. import (
  3. "encoding/base64"
  4. "github.com/0xJacky/Nginx-UI/app"
  5. "github.com/0xJacky/Nginx-UI/internal/logger"
  6. "github.com/0xJacky/Nginx-UI/internal/user"
  7. "github.com/0xJacky/Nginx-UI/model"
  8. "github.com/0xJacky/Nginx-UI/settings"
  9. "github.com/gin-contrib/static"
  10. "github.com/gin-gonic/gin"
  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. buf := make([]byte, 1024)
  22. runtime.Stack(buf, false)
  23. logger.Errorf("%s\n%s", err, buf)
  24. c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
  25. "message": err.(error).Error(),
  26. })
  27. }
  28. }()
  29. c.Next()
  30. }
  31. }
  32. func authRequired() gin.HandlerFunc {
  33. return func(c *gin.Context) {
  34. abortWithAuthFailure := func() {
  35. c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
  36. "message": "Authorization failed",
  37. })
  38. }
  39. token := c.GetHeader("Authorization")
  40. if token == "" {
  41. if token = c.GetHeader("X-Node-Secret"); token != "" && token == settings.ServerSettings.NodeSecret {
  42. c.Set("NodeSecret", token)
  43. c.Next()
  44. return
  45. } else {
  46. c.Set("ProxyNodeID", c.Query("x_node_id"))
  47. tokenBytes, _ := base64.StdEncoding.DecodeString(c.Query("token"))
  48. token = string(tokenBytes)
  49. if token == "" {
  50. abortWithAuthFailure()
  51. return
  52. }
  53. }
  54. }
  55. u, ok := user.GetTokenUser(token)
  56. if !ok {
  57. abortWithAuthFailure()
  58. return
  59. }
  60. c.Set("user", u)
  61. if nodeID := c.GetHeader("X-Node-ID"); nodeID != "" {
  62. c.Set("ProxyNodeID", nodeID)
  63. }
  64. c.Next()
  65. }
  66. }
  67. func required2FA() gin.HandlerFunc {
  68. return func(c *gin.Context) {
  69. u, ok := c.Get("user")
  70. if !ok {
  71. c.Next()
  72. return
  73. }
  74. cUser := u.(*model.Auth)
  75. if !cUser.EnabledOTP() {
  76. c.Next()
  77. return
  78. }
  79. ssid := c.GetHeader("X-Secure-Session-ID")
  80. if ssid == "" {
  81. ssid = c.Query("X-Secure-Session-ID")
  82. }
  83. if ssid == "" {
  84. c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
  85. "message": "Secure Session ID is empty",
  86. })
  87. return
  88. }
  89. if user.VerifySecureSessionID(ssid, cUser.ID) {
  90. c.Next()
  91. return
  92. }
  93. c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
  94. "message": "Secure Session ID is invalid",
  95. })
  96. return
  97. }
  98. }
  99. type serverFileSystemType struct {
  100. http.FileSystem
  101. }
  102. func (f serverFileSystemType) Exists(prefix string, _path string) bool {
  103. file, err := f.Open(path.Join(prefix, _path))
  104. if file != nil {
  105. defer func(file http.File) {
  106. err = file.Close()
  107. if err != nil {
  108. logger.Error("file not found", err)
  109. }
  110. }(file)
  111. }
  112. return err == nil
  113. }
  114. func mustFS(dir string) (serverFileSystem static.ServeFileSystem) {
  115. sub, err := fs.Sub(app.DistFS, path.Join("dist", dir))
  116. if err != nil {
  117. logger.Error(err)
  118. return
  119. }
  120. serverFileSystem = serverFileSystemType{
  121. http.FS(sub),
  122. }
  123. return
  124. }
  125. func cacheJs() gin.HandlerFunc {
  126. return func(c *gin.Context) {
  127. if strings.Contains(c.Request.URL.String(), "js") {
  128. c.Header("Cache-Control", "max-age: 1296000")
  129. if c.Request.Header.Get("If-Modified-Since") == settings.LastModified {
  130. c.AbortWithStatus(http.StatusNotModified)
  131. }
  132. c.Header("Last-Modified", settings.LastModified)
  133. }
  134. }
  135. }