| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 | package middlewareimport (	"encoding/base64"	"net/http"	"path"	"strings"	"github.com/0xJacky/Nginx-UI/internal/user"	"github.com/0xJacky/Nginx-UI/settings"	"github.com/gin-gonic/gin"	"github.com/uozi-tech/cosy/logger")// getToken from header, cookie or queryfunc getToken(c *gin.Context) (token string) {	if token = c.GetHeader("Authorization"); token != "" {		return	}	if token = c.Query("token"); token != "" {		tokenBytes, _ := base64.StdEncoding.DecodeString(token)		return string(tokenBytes)	}	if token, _ = c.Cookie("token"); token != "" {		return token	}	return ""}// getXNodeID from header or queryfunc getXNodeID(c *gin.Context) (xNodeID string) {	if xNodeID = c.GetHeader("X-Node-ID"); xNodeID != "" {		return xNodeID	}	return c.Query("x_node_id")}// AuthRequired is a middleware that checks if the user is authenticatedfunc AuthRequired() gin.HandlerFunc {	return func(c *gin.Context) {		abortWithAuthFailure := func() {			c.AbortWithStatusJSON(http.StatusForbidden, gin.H{				"message": "Authorization failed",			})		}		xNodeID := getXNodeID(c)		if xNodeID != "" {			c.Set("ProxyNodeID", xNodeID)		}		if token := c.GetHeader("X-Node-Secret"); token != "" && token == settings.NodeSettings.Secret {			c.Set("Secret", token)			c.Next()			return		}		if token := c.Query("node_secret"); token != "" && token == settings.NodeSettings.Secret {			c.Set("Secret", token)			c.Next()			return		}		token := getToken(c)		if token == "" {			abortWithAuthFailure()			return		}		u, ok := user.GetTokenUser(token)		if !ok {			abortWithAuthFailure()			return		}		c.Set("user", u)		c.Next()	}}type ServerFileSystemType struct {	http.FileSystem}func (f ServerFileSystemType) Exists(prefix string, _path string) bool {	file, err := f.Open(path.Join(prefix, _path))	if file != nil {		defer func(file http.File) {			err = file.Close()			if err != nil {				logger.Error("file not found", err)			}		}(file)	}	return err == nil}// CacheJs is a middleware that send header to client to cache js filefunc CacheJs() gin.HandlerFunc {	return func(c *gin.Context) {		if strings.Contains(c.Request.URL.String(), "js") {			c.Header("Cache-Control", "max-age: 1296000")			if c.Request.Header.Get("If-Modified-Since") == settings.LastModified {				c.AbortWithStatus(http.StatusNotModified)			}			c.Header("Last-Modified", settings.LastModified)		}	}}
 |