feat: support deleting logs based on filtering results (#35)

* feat: support deleting logs based on filtering results

* fix(record): fix the problem of wrong number of parameters when outputting delete logs
This commit is contained in:
Li4n0
2021-07-22 22:46:11 +08:00
committed by GitHub
parent 206808e83a
commit 9863821dbc
22 changed files with 322 additions and 32 deletions
+29 -1
View File
@@ -1,6 +1,7 @@
package dns
import (
"net/http"
"strconv"
"time"
@@ -8,6 +9,8 @@ import (
"github.com/li4n0/revsuit/internal/database"
"github.com/li4n0/revsuit/internal/notice"
"github.com/li4n0/revsuit/internal/record"
"gorm.io/gorm"
log "unknwon.dev/clog/v2"
)
var _ record.Record = (*Record)(nil)
@@ -41,7 +44,7 @@ func newRecord(rule *Rule, flag, domain, remoteIp, ipArea string) (r *Record, er
return r, database.DB.Create(r).Error
}
func ListRecords(c *gin.Context) {
func Records(c *gin.Context) {
var (
dnsRecord Record
res []Record
@@ -80,6 +83,31 @@ func ListRecords(c *gin.Context) {
db.Where("rule_name = ?", dnsRecord.RuleName)
}
//Delete records
if c.Request.Method == http.MethodDelete {
if err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&res).Error; err != nil {
c.JSON(400, gin.H{
"status": "failed",
"error": err.Error(),
"data": nil,
})
return
}
if database.Driver == database.Sqlite {
db.Exec("VACUUM")
}
c.JSON(200, gin.H{
"status": "succeed",
"error": nil,
})
log.Info("%d dns records deleted by %s", db.RowsAffected, c.Request.RemoteAddr)
return
}
//List records
page, err := strconv.Atoi(c.Query("page"))
if err != nil {
c.JSON(400, gin.H{
+28 -1
View File
@@ -1,6 +1,7 @@
package ftp
import (
"net/http"
"strconv"
"time"
@@ -10,6 +11,7 @@ import (
"github.com/li4n0/revsuit/internal/ipinfo"
"github.com/li4n0/revsuit/internal/notice"
"github.com/li4n0/revsuit/internal/record"
"gorm.io/gorm"
log "unknwon.dev/clog/v2"
)
@@ -53,7 +55,7 @@ func NewRecord(rule *Rule, flag, user, password, method, path, ip, area string,
return r, database.DB.Create(r).Error
}
func ListRecords(c *gin.Context) {
func Records(c *gin.Context) {
var (
ftpRecord Record
res []Record
@@ -105,6 +107,31 @@ func ListRecords(c *gin.Context) {
db.Where("rule_name = ?", ftpRecord.RuleName)
}
//Delete records
if c.Request.Method == http.MethodDelete {
if err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&res).Error; err != nil {
c.JSON(400, gin.H{
"status": "failed",
"error": err.Error(),
"data": nil,
})
return
}
if database.Driver == database.Sqlite {
db.Exec("VACUUM")
}
c.JSON(200, gin.H{
"status": "succeed",
"error": nil,
})
log.Info("%d ftp records deleted by %s", db.RowsAffected, c.Request.RemoteAddr)
return
}
//List records
page, err := strconv.Atoi(c.Query("page"))
if err != nil {
c.JSON(400, gin.H{
+28 -1
View File
@@ -1,6 +1,7 @@
package mysql
import (
"net/http"
"strconv"
"time"
@@ -9,6 +10,8 @@ import (
"github.com/li4n0/revsuit/internal/file"
"github.com/li4n0/revsuit/internal/notice"
"github.com/li4n0/revsuit/internal/record"
"gorm.io/gorm"
log "unknwon.dev/clog/v2"
)
var _ record.Record = (*Record)(nil)
@@ -51,7 +54,7 @@ func newRecord(rule *Rule, flag, username, schema, clientName, clientOS, remoteI
return r, database.DB.Create(r).Error
}
func ListRecords(c *gin.Context) {
func Records(c *gin.Context) {
var (
mysqlRecord Record
res []Record
@@ -103,6 +106,30 @@ func ListRecords(c *gin.Context) {
}
}
//Delete records
if c.Request.Method == http.MethodDelete {
if err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&res).Error; err != nil {
c.JSON(400, gin.H{
"status": "failed",
"error": err.Error(),
"data": nil,
})
return
}
if database.Driver == database.Sqlite {
db.Exec("VACUUM")
}
c.JSON(200, gin.H{
"status": "succeed",
"error": nil,
})
log.Info("%d mysql records deleted by %s", db.RowsAffected, c.Request.RemoteAddr)
return
}
//List records
page, err := strconv.Atoi(c.Query("page"))
if err != nil {
c.JSON(400, gin.H{
+28 -1
View File
@@ -1,6 +1,7 @@
package rhttp
import (
"net/http"
"strconv"
"time"
@@ -8,6 +9,8 @@ import (
"github.com/li4n0/revsuit/internal/database"
"github.com/li4n0/revsuit/internal/notice"
"github.com/li4n0/revsuit/internal/record"
"gorm.io/gorm"
log "unknwon.dev/clog/v2"
)
var _ record.Record = (*Record)(nil)
@@ -44,7 +47,7 @@ func NewRecord(rule *Rule, flag, method, url, ip, area, raw string) (r *Record,
return r, database.DB.Create(r).Error
}
func ListRecords(c *gin.Context) {
func Records(c *gin.Context) {
var (
httpRecord Record
res []Record
@@ -87,6 +90,30 @@ func ListRecords(c *gin.Context) {
db.Where("rule_name = ?", httpRecord.RuleName)
}
//Delete records
if c.Request.Method == http.MethodDelete {
if err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&res).Error; err != nil {
c.JSON(400, gin.H{
"status": "failed",
"error": err.Error(),
"data": nil,
})
return
}
if database.Driver == database.Sqlite {
db.Exec("VACUUM")
}
c.JSON(200, gin.H{
"status": "succeed",
"error": nil,
})
log.Info("%d http records deleted by %s", db.RowsAffected, c.Request.RemoteAddr)
return
}
//List records
page, err := strconv.Atoi(c.Query("page"))
if err != nil {
c.JSON(400, gin.H{
+29 -1
View File
@@ -1,6 +1,7 @@
package rmi
import (
"net/http"
"strconv"
"time"
@@ -8,6 +9,8 @@ import (
"github.com/li4n0/revsuit/internal/database"
"github.com/li4n0/revsuit/internal/notice"
"github.com/li4n0/revsuit/internal/record"
"gorm.io/gorm"
log "unknwon.dev/clog/v2"
)
var _ record.Record = (*Record)(nil)
@@ -40,7 +43,7 @@ func NewRecord(rule *Rule, flag, path, ip, area string) (r *Record, err error) {
return r, database.DB.Create(r).Error
}
func ListRecords(c *gin.Context) {
func Records(c *gin.Context) {
var (
rmiRecord Record
res []Record
@@ -80,6 +83,31 @@ func ListRecords(c *gin.Context) {
db.Where("rule_name = ?", rmiRecord.RuleName)
}
//Delete records
if c.Request.Method == http.MethodDelete {
if err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&res).Error; err != nil {
c.JSON(400, gin.H{
"status": "failed",
"error": err.Error(),
"data": nil,
})
return
}
if database.Driver == database.Sqlite {
db.Exec("VACUUM")
}
c.JSON(200, gin.H{
"status": "succeed",
"error": nil,
})
log.Info("%d rmi records deleted by %s", db.RowsAffected, c.Request.RemoteAddr)
return
}
//List records
page, err := strconv.Atoi(c.Query("page"))
if err != nil {
c.JSON(400, gin.H{
+10 -5
View File
@@ -77,19 +77,24 @@ func (revsuit *Revsuit) registerHttpRouter() {
recordGroup := revsuit.http.ApiGroup.Group("/record")
httpGroup := recordGroup.Group("/http")
httpGroup.GET("", rhttp.ListRecords)
httpGroup.GET("", rhttp.Records)
httpGroup.DELETE("", rhttp.Records)
dnsGroup := recordGroup.Group("/dns")
dnsGroup.GET("", dns.ListRecords)
dnsGroup.GET("", dns.Records)
dnsGroup.DELETE("", dns.Records)
mysqlGroup := recordGroup.Group("/mysql")
mysqlGroup.GET("", mysql.ListRecords)
mysqlGroup.GET("", mysql.Records)
mysqlGroup.DELETE("", mysql.Records)
rmiGroup := recordGroup.Group("/rmi")
rmiGroup.GET("", rmi.ListRecords)
rmiGroup.GET("", rmi.Records)
rmiGroup.DELETE("", rmi.Records)
ftpGroup := recordGroup.Group("/ftp")
ftpGroup.GET("", ftp.ListRecords)
ftpGroup.GET("", ftp.Records)
ftpGroup.DELETE("", ftp.Records)
// init rule router group
ruleGroup := revsuit.http.ApiGroup.Group("/rule")
+2 -2
View File
@@ -18,7 +18,7 @@ import (
log "unknwon.dev/clog/v2"
)
const VERSION = "0.1.7"
const VERSION = "0.2.0"
type Revsuit struct {
config *Config
@@ -60,7 +60,7 @@ func initDatabase(dsn string) {
Level: log.LevelInfo,
})
err := database.InitDB("sqlite", dsn)
err := database.InitDB(database.Sqlite, dsn)
if err != nil {
log.Fatal(err.Error())
}