Подготовить HY2XS к production-сборке

This commit is contained in:
2026-04-25 23:13:12 +05:00
commit 84a4e94567
277 changed files with 26513 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
package middleware
import (
"github.com/gin-gonic/gin"
"hy2xs-admin/model/constant"
"hy2xs-admin/model/vo"
"hy2xs-admin/service"
"hy2xs-admin/util"
)
func AdminHandler() gin.HandlerFunc {
return func(c *gin.Context) {
myClaims, err := service.ParseToken(service.GetToken(c))
if err != nil {
vo.Fail(err.Error(), c)
c.Abort()
return
}
if !util.ArrContain(myClaims.AccountBo.Roles, "admin") {
vo.Fail(constant.ForbiddenError, c)
c.Abort()
return
}
c.Next()
}
}
+33
View File
@@ -0,0 +1,33 @@
package middleware
import (
"errors"
"github.com/robfig/cron/v3"
"github.com/sirupsen/logrus"
"hy2xs-admin/dao"
"hy2xs-admin/model/constant"
"hy2xs-admin/service"
"time"
)
func InitCron() error {
loc := time.Now().Location()
c := cron.New(cron.WithLocation(loc))
_, err := c.AddFunc("@every 30s", service.CronHandleAccount)
if err != nil {
logrus.Errorf("cron add func CronHandleAccount err: %v", err)
return errors.New("cron add func CronHandleAccount err")
}
resetTrafficCron, err := dao.GetConfig("key = ?", constant.ResetTrafficCron)
if err != nil {
return err
}
if *resetTrafficCron.Value != "" {
_, err := c.AddFunc(*resetTrafficCron.Value, service.CronResetTraffic)
if err != nil {
logrus.Errorf("cron add func CronResetTraffic err: %v", err)
}
}
c.Start()
return nil
}
+25
View File
@@ -0,0 +1,25 @@
package middleware
import (
"github.com/gin-gonic/gin"
"hy2xs-admin/model/vo"
"net/http"
"regexp"
)
func FilterHandler() gin.HandlerFunc {
return func(c *gin.Context) {
matched, err := regexp.MatchString(`(?i)fofa|shodan|curl|wget`, c.Request.UserAgent())
if err != nil {
vo.Fail("Internal error", c)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
if matched {
vo.Fail("Forbidden: Scanning tools are not allowed", c)
c.AbortWithStatus(http.StatusForbidden)
return
}
c.Next()
}
}
+38
View File
@@ -0,0 +1,38 @@
package middleware
import (
"github.com/gin-gonic/gin"
"hy2xs-admin/model/constant"
"hy2xs-admin/model/vo"
"hy2xs-admin/service"
"strings"
)
func JWTHandler() gin.HandlerFunc {
return func(c *gin.Context) {
authHeader := c.Request.Header.Get("Authorization")
if authHeader == "" {
vo.Fail(constant.UnauthorizedError, c)
c.Abort()
return
}
parts := strings.SplitN(authHeader, " ", 2)
if !(len(parts) == 2 && parts[0] == "Bearer") {
vo.Fail(constant.IllegalTokenError, c)
c.Abort()
return
}
myClaims, err := service.ParseToken(parts[1])
if err != nil {
vo.Fail(err.Error(), c)
c.Abort()
return
}
if myClaims.AccountBo.Deleted != 0 {
vo.Fail("this account has been disabled", c)
c.Abort()
return
}
c.Next()
}
}
+43
View File
@@ -0,0 +1,43 @@
package middleware
import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"hy2xs-admin/model/constant"
"time"
)
func InitLog() {
logrus.SetOutput(&lumberjack.Logger{
Filename: constant.SystemLogPath,
MaxSize: 1,
MaxBackups: 2,
MaxAge: 30,
Compress: true,
LocalTime: true,
})
logrus.SetFormatter(&logrus.JSONFormatter{TimestampFormat: "2006-01-02 15:04:05"})
logrus.SetLevel(logrus.WarnLevel)
}
func LogHandler() gin.HandlerFunc {
return func(c *gin.Context) {
startTime := time.Now()
endTime := time.Now()
statusCode := c.Writer.Status()
latencyTime := endTime.Sub(startTime)
clientIP := c.ClientIP()
reqMethod := c.Request.Method
reqUri := c.Request.RequestURI
logrus.WithFields(logrus.Fields{
"statusCode": statusCode,
"latencyTime": latencyTime,
"clientIP": clientIP,
"reqMethod": reqMethod,
"reqUri": reqUri,
}).Info()
c.Next()
}
}
+26
View File
@@ -0,0 +1,26 @@
package middleware
import (
"github.com/didip/tollbooth"
"github.com/didip/tollbooth/limiter"
"github.com/gin-gonic/gin"
"hy2xs-admin/model/vo"
)
var limit *limiter.Limiter
func init() {
limit = tollbooth.NewLimiter(5, nil)
}
func RateLimiterHandler() gin.HandlerFunc {
return func(c *gin.Context) {
httpError := tollbooth.LimitByRequest(limit, c.Writer, c.Request)
if httpError != nil {
vo.Fail("click too fast", c)
c.Abort()
return
}
c.Next()
}
}