feat: add ip whitelist

This commit is contained in:
Jacky 2024-07-20 10:37:19 +08:00
parent 7a9aa3a33b
commit 3b937ee0f4
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
17 changed files with 1026 additions and 927 deletions

25
router/ip.go Normal file
View file

@ -0,0 +1,25 @@
package router
import (
"github.com/0xJacky/Nginx-UI/settings"
"github.com/gin-gonic/gin"
"github.com/samber/lo"
"net/http"
)
func ipWhiteList() gin.HandlerFunc {
return func(c *gin.Context) {
clientIP := c.ClientIP()
if len(settings.AuthSettings.IPWhiteList) == 0 || clientIP == "127.0.0.1" {
c.Next()
return
}
if !lo.Contains(settings.AuthSettings.IPWhiteList, clientIP) {
c.AbortWithStatus(http.StatusForbidden)
return
}
c.Next()
}
}