mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-11-29 10:52:54 +00:00
34 lines
738 B
Go
34 lines
738 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"github.com/mhsanaei/3x-ui/v2/web/service"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func ApiAuth() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
apiKey := c.GetHeader("Api-Key")
|
|
if apiKey == "" {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "API key is required"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
settingService := service.SettingService{}
|
|
panelAPIKey, err := settingService.GetAPIKey()
|
|
if err != nil || panelAPIKey == "" {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "API key not configured on the panel"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
if apiKey != panelAPIKey {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid API key"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|