release revsuit vBeta0.1.0 (#15)

Co-authored-by: E99p1ant <[email protected]>
This commit is contained in:
Li4n0
2021-05-16 12:11:35 +08:00
committed by GitHub
co-authored by E99p1ant
parent b39ee101f9
commit 5b63e90390
98 changed files with 2357 additions and 623 deletions
+38 -9
View File
@@ -1,6 +1,7 @@
package rhttp
import (
"context"
"math/rand"
"net/http"
"net/http/httputil"
@@ -8,6 +9,7 @@ import (
"strconv"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/li4n0/revsuit/internal/database"
@@ -23,6 +25,8 @@ type Server struct {
ApiGroup *gin.RouterGroup
rules []*Rule
rulesLock sync.RWMutex
httpServer http.Server
}
const (
@@ -86,14 +90,38 @@ func (s *Server) updateRules() error {
return db.Order("rank desc").Find(&s.rules).Error
}
func (s *Server) startHttpServer() {
log.Info("Starting HTTP Server at %s, token:%s", s.Addr, s.Token)
s.httpServer = http.Server{
Addr: s.Addr,
Handler: s.Router,
}
err := s.httpServer.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatal(err.Error())
}
}
func (s *Server) stopHttpServer() {
log.Info("HTTP Server is stopping...")
err := s.httpServer.Shutdown(context.TODO())
if err != nil {
log.Fatal(err.Error())
}
}
func (s *Server) Restart() {
//only need to stop http server, because it will start in a loop
s.stopHttpServer()
}
func (s *Server) Run() {
if err := s.updateRules(); err != nil {
log.Warn(err.Error())
}
log.Info("Starting HTTP Server at %s, token:%s", s.Addr, s.Token)
err := s.Router.Run(s.Addr)
if err != nil {
log.Fatal(err.Error())
for {
s.startHttpServer()
time.Sleep(2 * time.Second)
}
}
@@ -167,21 +195,22 @@ func (s *Server) Receive(c *gin.Context) {
if _rule.PushToClient {
if flagGroup != "" {
var count int64
database.DB.Where("rule_name=? and raw like ?", _rule.Name, "%"+flagGroup+"%").Model(&Record{}).Count(&count)
database.DB.Where("rule_name=? and raw_request like ?", _rule.Name, "%"+flagGroup+"%").Model(&Record{}).Count(&count)
if count <= 1 {
r.PushToClient()
log.Trace("HTTP record[id%d] has been put to client message queue", r.ID)
log.Trace("HTTP record[id:%d, flagGroup:%s] has been put to client message queue", r.ID, flagGroup)
}
} else {
r.PushToClient()
log.Trace("HTTP record[id:%d, flag:%s] has been put to client message queue", r.ID, r.Flag)
}
r.PushToClient()
log.Trace("HTTP record[id%d] has been put to client message queue", r.ID)
}
//send notice
if _rule.Notice {
go func() {
r.Notice()
log.Trace("HTTP record[id%d] notice has been sent", r.ID)
log.Trace("HTTP record[id:%d] notice has been sent", r.ID)
}()
}