-
Notifications
You must be signed in to change notification settings - Fork 62
/
record.go
219 lines (194 loc) · 5.31 KB
/
record.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package ftp
import (
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/li4n0/revsuit/internal/database"
"github.com/li4n0/revsuit/internal/file"
"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"
)
var _ record.Record = (*Record)(nil)
type Record struct {
record.BaseRecord
User string `form:"user" json:"user"`
Password string `form:"password" json:"password"`
Path string `form:"path" json:"path"`
Method Method `form:"method" json:"method"`
Status Status `form:"status" json:"status"`
File *file.FTPFile `gorm:"foreignKey:RecordID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" form:"file" json:"file" notice:"-"`
Rule Rule `gorm:"foreignKey:RuleName;references:Name;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" form:"-" json:"-" notice:"-"`
}
func (Record) TableName() string {
return "ftp_records"
}
func (r Record) Notice() {
notice.Notice(r)
}
func NewRecord(rule *Rule, flag, user, password, method, path, ip, area string, file *file.FTPFile, status Status) (r *Record, err error) {
r = &Record{
BaseRecord: record.BaseRecord{
Flag: flag,
RemoteIP: ip,
IpArea: area,
RequestTime: time.Now(),
},
Path: path,
Method: method,
User: user,
Password: password,
Status: status,
File: file,
Rule: *rule,
}
// sqlite db-level lock to prevent too much write operation lead to error of `database is locked` #54
if database.Driver == database.Sqlite {
database.Locker.Lock()
defer database.Locker.Unlock()
}
return r, database.DB.Create(r).Error
}
func Records(c *gin.Context) {
var (
ftpRecord Record
res []Record
count int64
order = c.Query("order")
pageSize = 10
)
if c.Query("pageSize") != "" {
if n, err := strconv.Atoi(c.Query("pageSize")); err == nil {
if n > 0 && n < 100 {
pageSize = n
}
}
}
if err := c.ShouldBind(&ftpRecord); err != nil {
c.JSON(400, gin.H{
"status": "failed",
"error": err.Error(),
"result": nil,
})
return
}
db := database.DB.Model(&ftpRecord)
if ftpRecord.Flag != "" {
db.Where("flag = ?", ftpRecord.Flag)
}
if ftpRecord.User != "" {
db.Where("user like ?", "%"+ftpRecord.User+"%")
}
if ftpRecord.Password != "" {
db.Where("password like ?", "%"+ftpRecord.Password+"%")
}
if ftpRecord.Path != "" {
db.Where("path like ?", "%"+ftpRecord.Path+"%")
}
if ftpRecord.Method != "" {
db.Where("method = ?", ftpRecord.Method)
}
if ftpRecord.Status != "" {
db.Where("status = ?", ftpRecord.Status)
}
if ftpRecord.RemoteIP != "" {
db.Where("remote_ip = ?", ftpRecord.RemoteIP)
}
if ftpRecord.RuleName != "" {
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{
"status": "failed",
"error": err.Error(),
"result": nil,
})
return
}
if order != "asc" {
order = "desc"
}
if err := db.Preload("File").Order("id " + order).Count(&count).Offset((page - 1) * pageSize).Limit(pageSize).Find(&res).Error; err != nil {
c.JSON(400, gin.H{
"status": "failed",
"error": err.Error(),
"data": nil,
})
return
}
c.JSON(200, gin.H{
"status": "succeed",
"error": nil,
"result": gin.H{"count": count, "data": res},
})
}
func createRecord(_rule *Rule, flag, flagGroup, user, password, method, path, filename, ip string, uploadData []byte, status Status) {
// create new record
area := ipinfo.Area(ip)
var ftpFile *file.FTPFile
var r *Record
var err error
if len(uploadData) != 0 {
ftpFile = &file.FTPFile{
Name: filename,
Content: uploadData,
}
}
r, err = NewRecord(_rule, flag, user, password, method, path, ip, area, ftpFile, status)
if err != nil {
log.Warn("FTP record[rule_id:%d] created failed :%s", _rule.ID, err)
return
}
log.Info("FTP record[id:%d rule:%s remote_ip:%s] has been created", r.ID, _rule.Name, ip)
//only send to client or notify user when this connection recorded first time.
var count int64
if flagGroup != "" {
database.DB.Where("rule_name=? and (user like ? or password like ?)", _rule.Name, "%"+flagGroup+"%", "%"+flagGroup+"%").Model(&Record{}).Count(&count)
}
if count <= 1 {
if _rule.PushToClient {
r.PushToClient()
if flagGroup != "" {
log.Trace("FTP record[id:%d, flagGroup:%s] has been put to client message queue", r.ID, flagGroup)
} else {
log.Trace("FTP record[id:%d] has been put to client message queue", r.ID)
}
}
//send notice
if _rule.Notice {
go func() {
r.Notice()
if flagGroup != "" {
log.Trace("FTP record[id:%d, flagGroup:%s] notice has been sent", r.ID, flagGroup)
} else {
log.Trace("FTP record[id:%d] notice has been sent", r.ID)
}
}()
}
}
}