diff options
| author | Ho3ein <ho3ein.sanaei@gmail.com> | 2023-05-31 09:17:02 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-31 09:17:02 +0300 |
| commit | 94fad02737d82817ca69f1f05872b49e769a0cb4 (patch) | |
| tree | 55e7ead217c5a3e82791c0edae6ad44de1ba524f /web/middleware | |
| parent | 8442836512d82b705e404bc1749e3000115ba550 (diff) | |
| parent | d694e6eafccad246c63264714897316f671d6428 (diff) | |
Merge pull request #545 from hamid-gh98/main
🔀 New Feature + Fix URLs + Some Improvements 🛠️🌐
Diffstat (limited to 'web/middleware')
| -rw-r--r-- | web/middleware/domainValidator.go | 21 | ||||
| -rw-r--r-- | web/middleware/redirect.go | 34 |
2 files changed, 55 insertions, 0 deletions
diff --git a/web/middleware/domainValidator.go b/web/middleware/domainValidator.go new file mode 100644 index 00000000..3adb0f0f --- /dev/null +++ b/web/middleware/domainValidator.go @@ -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() + } +} diff --git a/web/middleware/redirect.go b/web/middleware/redirect.go new file mode 100644 index 00000000..e3dc8ada --- /dev/null +++ b/web/middleware/redirect.go @@ -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() + } +} |
