feat(ftp): support upload, download and PASV rebind (#13)

Co-authored-by: E99p1ant <[email protected]>
This commit is contained in:
Li4n0
2021-05-05 12:59:50 +08:00
committed by GitHub
co-authored by E99p1ant
parent 3d8507e682
commit b39ee101f9
15 changed files with 374 additions and 146 deletions
-62
View File
@@ -1,62 +0,0 @@
package mysql
import (
"fmt"
"strings"
"github.com/gabriel-vasile/mimetype"
"github.com/gin-gonic/gin"
"github.com/li4n0/revsuit/internal/database"
)
const FILE_SPEARATOR = ";"
type File struct {
ID uint `gorm:"primarykey" form:"id" json:"id"`
RecordID uint `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-"`
Name string `json:"name"`
Content []byte `json:"-"`
}
//func (File) TableName() string {
// return "mysql_files"
//}
func GetFile(c *gin.Context) {
var (
file File
mysqlRecord Record
)
id := c.Param("id")
if id == "" {
c.JSON(400, gin.H{
"status": "failed",
"error": fmt.Errorf("param id missed").Error(),
"result": nil,
})
return
}
database.DB.Model(&file).Where("id = ?", id).Find(&file)
database.DB.Model(&mysqlRecord).Where("id = ?", file.RecordID).Find(&mysqlRecord)
if file.Content == nil || len(file.Content) == 0 {
c.JSON(400, gin.H{
"status": "failed",
"error": fmt.Errorf("file not found").Error(),
"result": nil,
})
return
} else {
mime := mimetype.Detect(file.Content)
c.Header("Content-Type", mime.String())
c.Header("Content-Disposition",
fmt.Sprintf(
"filename=%s_%s_%d",
strings.Replace(mysqlRecord.RemoteIP, ".", "_", -1),
file.Name,
file.ID,
),
)
c.String(200, string(file.Content))
}
}
+21 -17
View File
@@ -7,8 +7,10 @@ import (
"regexp"
"strings"
"sync"
"time"
"github.com/li4n0/revsuit/internal/database"
"github.com/li4n0/revsuit/internal/file"
"github.com/li4n0/revsuit/internal/qqwry"
"github.com/li4n0/revsuit/pkg/mysql/vmysql"
log "unknwon.dev/clog/v2"
@@ -54,21 +56,29 @@ func (s *Server) updateRules() error {
// NewConnection is part of the mysql.Handler interface.
func (s *Server) NewConnection(c *vmysql.Conn) {
log.Trace("New MySQL client from addr [%s] logged in with username [%s], ID [%d]", c.RemoteAddr(), c.User, c.ConnectionID)
log.Trace("New MySQL connection from addr [%s] logged [%s] in with username [%s], ID [%d]", c.RemoteAddr(), c.SchemaName, c.User, c.ConnectionID)
if err := c.Conn.SetDeadline(time.Now().Add(time.Second * 30)); err != nil {
log.Warn("MySQL set connection deadline error:%v", err)
}
c.RecycleReadPacket()
var (
user = c.User
schema = c.SchemaName
validated bool
flag string
)
for _, _rule := range s.getRules() {
userFlag, _, _ := _rule.Match(user)
schemaFlag, _, _ := _rule.Match(schema)
if userFlag == "" && schemaFlag == "" {
flag, _, _ = _rule.Match(user)
if flag == "" {
flag, _, _ = _rule.Match(schema)
}
if flag == "" {
continue
}
log.Trace("MySQL connection[id: %d] matched rule[rule_name: %s, flag: %s]", c.ConnectionID, _rule.Name, flag)
s.connRulePool.Store(c.ConnectionID, _rule)
validated = true
break
@@ -117,17 +127,17 @@ func (s *Server) ConnectionClosed(c *vmysql.Conn) {
ip := strings.Split(c.RemoteAddr().String(), ":")[0]
filenames := strings.Split(_rule.Files, FILE_SPEARATOR)
files := make([]File, 0)
filenames := strings.Split(_rule.Files, ",")
files := make([]file.MySQLFile, 0)
for _, filename := range filenames {
if len(c.Files[filename]) != 0 {
files = append(files, File{Name: filename, Content: c.Files[filename]})
files = append(files, file.MySQLFile{Name: filename, Content: c.Files[filename]})
}
}
r, err := newRecord(_rule, flag, user, clientName, clientOS, ip, qqwry.Area(ip), supportLoadLocalData, files)
if err != nil {
log.Warn("MySQL record(rule_id:%s) created failed :%s", _rule.Name, err)
log.Warn("MySQL record[rule_id: %s] created failed: %s", _rule.Name, err)
return
}
log.Info("MySQL record[id:%d rule:%s remote_ip:%s] has been created", r.ID, _rule.Name, ip)
@@ -151,7 +161,7 @@ func (s *Server) ConnectionClosed(c *vmysql.Conn) {
if _rule.Notice {
go func() {
r.Notice()
log.Trace("MySQL record[id%d] notice has been sent", r.ID)
log.Trace("MySQL record[id: %d] notice has been sent", r.ID)
}()
}
@@ -211,7 +221,7 @@ func (s *Server) ComQuery(c *vmysql.Conn, query string, callback func(*sqltypes.
}
// mysql LOAD DATA LOCAL
if !c.SupportLoadDataLocal { // 客户端不支持读取本地文件且没有开启总是读取,直接返回错误
if !c.SupportLoadDataLocal {
log.Trace("MySQL Client not support LOAD DATA LOCAL, return error directly")
c.WriteErrorResponse(
fmt.Sprintf(
@@ -224,7 +234,7 @@ func (s *Server) ComQuery(c *vmysql.Conn, query string, callback func(*sqltypes.
return nil
}
files := strings.Split(_rule.Files, ";")
files := strings.Split(_rule.Files, ",")
if c.Files == nil {
c.Files = make(map[string][]byte)
}
@@ -238,12 +248,6 @@ func (s *Server) ComQuery(c *vmysql.Conn, query string, callback func(*sqltypes.
} else {
c.Files[filename] = data
}
c.WriteErrorResponse(fmt.Sprintf(
"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 1",
strings.ReplaceAll(
strings.ReplaceAll(query, "%", "%%"),
"'", "\\'"),
))
}
}
+8 -7
View File
@@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/li4n0/revsuit/internal/database"
"github.com/li4n0/revsuit/internal/file"
"github.com/li4n0/revsuit/internal/notice"
"github.com/li4n0/revsuit/internal/record"
)
@@ -14,12 +15,12 @@ var _ record.Record = (*Record)(nil)
type Record struct {
record.BaseRecord
Username string `gorm:"index" form:"username" json:"username" notice:"username"`
ClientName string `gorm:"index" form:"client_name" json:"client_name" notice:"client_name"`
ClientOS string `gorm:"index" form:"client_os" json:"client_os" notice:"client_os"`
LoadLocalData bool `gorm:"index" form:"load_local_data" json:"load_local_data" notice:"load_local_data"`
Files []File `form:"-" json:"files" notice:"-"`
Rule Rule `gorm:"foreignKey:RuleName;references:Name;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" form:"-" json:"-" notice:"-"`
Username string `gorm:"index" form:"username" json:"username" notice:"username"`
ClientName string `gorm:"index" form:"client_name" json:"client_name" notice:"client_name"`
ClientOS string `gorm:"index" form:"client_os" json:"client_os" notice:"client_os"`
LoadLocalData bool `gorm:"index" form:"load_local_data" json:"load_local_data" notice:"load_local_data"`
Files []file.MySQLFile `form:"-" json:"files" notice:"-"`
Rule Rule `gorm:"foreignKey:RuleName;references:Name;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" form:"-" json:"-" notice:"-"`
}
func (Record) TableName() string {
@@ -30,7 +31,7 @@ func (r Record) Notice() {
notice.Notice(r)
}
func newRecord(rule *Rule, flag, username, clientName, clientOS, remoteIp, ipArea string, supportLoadLocalData bool, files []File) (r *Record, err error) {
func newRecord(rule *Rule, flag, username, clientName, clientOS, remoteIp, ipArea string, supportLoadLocalData bool, files []file.MySQLFile) (r *Record, err error) {
r = &Record{
BaseRecord: record.BaseRecord{
Flag: flag,
+9 -9
View File
@@ -73,7 +73,7 @@ type Conn struct {
// conn is the underlying network connection.
// Calling Close() on the Conn will close this connection.
// If there are any ongoing reads or writes, they may get interrupted.
conn net.Conn
Conn net.Conn
// For server-side connections, listener points to the server object.
listener *Listener
@@ -167,7 +167,7 @@ var writersPool = sync.Pool{New: func() interface{} { return bufio.NewWriterSize
// size for reads.
func newServerConn(conn net.Conn, listener *Listener) *Conn {
c := &Conn{
conn: conn,
Conn: conn,
listener: listener,
closed: sync2.NewAtomicBool(false),
}
@@ -181,7 +181,7 @@ func newServerConn(conn net.Conn, listener *Listener) *Conn {
// be terminated by a call to flush.
func (c *Conn) startWriterBuffering() {
c.bufferedWriter = writersPool.Get().(*bufio.Writer)
c.bufferedWriter.Reset(c.conn)
c.bufferedWriter.Reset(c.Conn)
}
// flush flushes the written data to the socket.
@@ -206,7 +206,7 @@ func (c *Conn) getWriter() io.Writer {
if c.bufferedWriter != nil {
return c.bufferedWriter
}
return c.conn
return c.Conn
}
// getReader returns reader for connection. It can be *bufio.Reader or net.Conn
@@ -215,7 +215,7 @@ func (c *Conn) getReader() io.Reader {
if c.bufferedReader != nil {
return c.bufferedReader
}
return c.conn
return c.Conn
}
func (c *Conn) readHeaderFrom(r io.Reader) (int, error) {
@@ -359,10 +359,10 @@ func (c *Conn) readUploadFileEphemeralPacket() []byte {
// This function usually shouldn't be used - use readEphemeralPacket.
func (c *Conn) readEphemeralPacketDirect() ([]byte, error) {
if c.currentEphemeralPolicy != ephemeralUnused {
panic(vterrors.Errorf(vtrpc.Code_INTERNAL, "readEphemeralPacketDirect: unexpected currentEphemeralPolicy: %v", c.currentEphemeralPolicy))
return nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "readEphemeralPacketDirect: unexpected currentEphemeralPolicy: %v", c.currentEphemeralPolicy)
}
var r io.Reader = c.conn
var r io.Reader = c.Conn
length, err := c.readHeaderFrom(r)
if err != nil {
@@ -577,7 +577,7 @@ func (c *Conn) recycleWritePacket() {
// RemoteAddr returns the underlying socket RemoteAddr().
func (c *Conn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
return c.Conn.RemoteAddr()
}
// ID returns the MySQL connection ID for this connection.
@@ -594,7 +594,7 @@ func (c *Conn) String() string {
// routine to interrupt the current connection.
func (c *Conn) Close() {
if c.closed.CompareAndSwap(false, true) {
c.conn.Close()
c.Conn.Close()
}
}
+3 -3
View File
@@ -317,7 +317,7 @@ func (l *Listener) handle(conn net.Conn, connectionID uint32, acceptTime time.Ti
}
c.RecycleReadPacket()
if con, ok := c.conn.(*tls.Conn); ok {
if con, ok := c.Conn.(*tls.Conn); ok {
connState := con.ConnectionState()
tlsVerStr := tlsVersionToString(connState.Version)
if tlsVerStr != "" {
@@ -619,8 +619,8 @@ func (l *Listener) parseClientHandshakePacket(c *Conn, firstTime bool, data []by
// Check for SSL.
if firstTime && l.TLSConfig != nil && clientFlags&CapabilityClientSSL > 0 {
// Need to switch to TLS, and then re-read the packet.
conn := tls.Server(c.conn, l.TLSConfig)
c.conn = conn
conn := tls.Server(c.Conn, l.TLSConfig)
c.Conn = conn
c.bufferedReader.Reset(conn)
c.Capabilities |= CapabilityClientSSL
return "", "", nil, nil