mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
Update middleware.go
This commit is contained in:
parent
72739fb07f
commit
b8d5d03572
1 changed files with 44 additions and 62 deletions
|
@ -1,84 +1,66 @@
|
||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"github.com/0xJacky/Nginx-UI/frontend"
|
"github.com/0xJacky/Nginx-UI/frontend"
|
||||||
"github.com/0xJacky/Nginx-UI/server/model"
|
"github.com/0xJacky/Nginx-UI/server/model"
|
||||||
"github.com/gin-contrib/static"
|
"github.com/gin-contrib/static"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func authRequired() gin.HandlerFunc {
|
func authRequired() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
token := c.GetHeader("Authorization")
|
token := c.GetHeader("Authorization")
|
||||||
if token == "" {
|
if token == "" {
|
||||||
tmp, _ := base64.StdEncoding.DecodeString(c.Query("token"))
|
tmp, _ := base64.StdEncoding.DecodeString(c.Query("token"))
|
||||||
token = string(tmp)
|
token = string(tmp)
|
||||||
if token == "" {
|
if token == "" {
|
||||||
c.JSON(http.StatusForbidden, gin.H{
|
c.JSON(http.StatusForbidden, gin.H{
|
||||||
"message": "auth fail",
|
"message": "auth fail",
|
||||||
})
|
})
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
n := model.CheckToken(token)
|
n := model.CheckToken(token)
|
||||||
|
|
||||||
if n < 1 {
|
if n < 1 {
|
||||||
c.JSON(http.StatusForbidden, gin.H{
|
c.JSON(http.StatusForbidden, gin.H{
|
||||||
"message": "auth fail",
|
"message": "auth fail",
|
||||||
})
|
})
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type serverFileSystemType struct {
|
type serverFileSystemType struct {
|
||||||
http.FileSystem
|
http.FileSystem
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f serverFileSystemType) Exists(prefix string, _path string) bool {
|
func (f serverFileSystemType) Exists(prefix string, _path string) bool {
|
||||||
_, err := f.Open(path.Join(prefix, _path))
|
_, err := f.Open(path.Join(prefix, _path))
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustFS(dir string) (serverFileSystem static.ServeFileSystem) {
|
func mustFS(dir string) (serverFileSystem static.ServeFileSystem) {
|
||||||
|
|
||||||
sub, err := fs.Sub(frontend.DistFS, path.Join("dist", dir))
|
sub, err := fs.Sub(frontend.DistFS, path.Join("dist", dir))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
serverFileSystem = serverFileSystemType{
|
serverFileSystem = serverFileSystemType{
|
||||||
http.FS(sub),
|
http.FS(sub),
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
|
||||||
|
|
||||||
// tryStatic Static returns a middleware handler that serves static files in the given directory.
|
|
||||||
func tryStatic(urlPrefix string, fs static.ServeFileSystem) gin.HandlerFunc {
|
|
||||||
fileserver := http.FileServer(fs)
|
|
||||||
if urlPrefix != "" {
|
|
||||||
fileserver = http.StripPrefix(urlPrefix, fileserver)
|
|
||||||
}
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
if fs.Exists(urlPrefix, c.Request.URL.Path) {
|
|
||||||
fileserver.ServeHTTP(c.Writer, c.Request)
|
|
||||||
if strings.Contains(c.Request.URL.Path, ".js") {
|
|
||||||
c.Writer.Header().Set("content-type", "application/javascript")
|
|
||||||
}
|
|
||||||
c.Abort()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue