mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-03-01 01:20:49 +03:00
create and move middlewares to seperate folder
This commit is contained in:
parent
8442836512
commit
a2d8c98b0d
21
web/middleware/domainValidator.go
Normal file
21
web/middleware/domainValidator.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func DomainValidatorMiddleware(domain string) gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
host := strings.Split(c.Request.Host, ":")[0]
|
||||||
|
|
||||||
|
if host != domain {
|
||||||
|
c.AbortWithStatus(http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
34
web/middleware/redirect.go
Normal file
34
web/middleware/redirect.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RedirectMiddleware(basePath string) gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
// Redirect from old '/xui' path to '/panel'
|
||||||
|
redirects := map[string]string{
|
||||||
|
"panel/API": "panel/api",
|
||||||
|
"xui/API": "panel/api",
|
||||||
|
"xui": "panel",
|
||||||
|
}
|
||||||
|
|
||||||
|
path := c.Request.URL.Path
|
||||||
|
for from, to := range redirects {
|
||||||
|
from, to = basePath+from, basePath+to
|
||||||
|
|
||||||
|
if strings.HasPrefix(path, from) {
|
||||||
|
newPath := to + path[len(from):]
|
||||||
|
|
||||||
|
c.Redirect(http.StatusMovedPermanently, newPath)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user