Files

30 lines
506 B
Go

package middleware
import (
"net"
"net/http"
"github.com/gin-gonic/gin"
)
func LocalOnlyHandler() gin.HandlerFunc {
return func(c *gin.Context) {
host, _, err := net.SplitHostPort(c.Request.RemoteAddr)
if err != nil {
host = c.ClientIP()
}
if host != "127.0.0.1" && host != "::1" {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"code": http.StatusForbidden,
"type": "no",
"message": "local access only",
"data": nil,
})
return
}
c.Next()
}
}