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
+15 -2
View File
@@ -55,7 +55,7 @@
@click="() => (collapsed = !collapsed)" @click="() => (collapsed = !collapsed)"
/> />
<!-- <transition name="list1">--> <!-- <transition name="list1">-->
<div v-if="$route.path.includes('logs')" style="float: right; width:45% ;padding: 12px 0;line-height: 24px;"> <div v-if="isLogMode" style="float: right; width:45% ;padding: 12px 0;line-height: 24px;">
<a-row :gutter="24" type="flex"> <a-row :gutter="24" type="flex">
<a-col :span="21"> <a-col :span="21">
<a-form-model v-show="showSettings" ref="settings" layout="inline"> <a-form-model v-show="showSettings" ref="settings" layout="inline">
@@ -111,6 +111,11 @@ export default {
openKeys: ['logs', "rules"], openKeys: ['logs', "rules"],
}; };
}, },
computed: {
isLogMode() {
return this.$route.path.includes('logs')
}
},
methods: { methods: {
timing() { timing() {
if (this.timer !== null) { if (this.timer !== null) {
@@ -122,7 +127,7 @@ export default {
} }
}, },
mounted() { mounted() {
if (this.autoRefresh) { if (this.autoRefresh && this.isLogMode) {
this.timing() this.timing()
} }
}, },
@@ -138,6 +143,14 @@ export default {
} }
localStorage.setItem('autoRefresh', val) localStorage.setItem('autoRefresh', val)
}, },
isLogMode(val) {
if (!val) {
clearInterval(this.timer)
} else {
this.timing()
}
localStorage.setItem('autoRefresh', val)
},
refreshInterval(val) { refreshInterval(val) {
clearInterval(this.timer) clearInterval(this.timer)
this.timing() this.timing()
+34 -1
View File
@@ -39,6 +39,18 @@
type="search" type="search"
:style="{ color: filtered ? '#108ee9' : undefined }" :style="{ color: filtered ? '#108ee9' : undefined }"
/> />
<span slot="method" slot-scope="method">
<a-tag v-if="method" :color="method==='UPLOAD'?'#eb2f96':'#02a7ff'">{{ method }}</a-tag>
</span>
<span slot="file" slot-scope="file">
<a-tag
v-if="file.id"
color="#f5222d"
><a target="_blank" :href="'/revsuit/api/file/ftp/'+file.id">TRUE</a> </a-tag>
<a-tag v-else color="#722ed1">
FALSE
</a-tag>
</span>
<span slot="time" slot-scope="time"> <span slot="time" slot-scope="time">
{{ new Date(time).format("yyyy-MM-dd hh:mm:ss") }} {{ new Date(time).format("yyyy-MM-dd hh:mm:ss") }}
@@ -65,7 +77,7 @@ import FilterDropdown from '@/components/FilterDropdown'
const colors = { const colors = {
"CRASHED": "#f50", "CRASHED": "#f50",
"FINISHED": "#87d068" "FINISHED": "#52c41a"
} }
const columns = [ const columns = [
@@ -128,6 +140,24 @@ const columns = [
filterIcon: 'filterIcon', filterIcon: 'filterIcon',
}, },
}, },
{
title: 'METHOD',
dataIndex: 'method',
key: 'method',
scopedSlots: {
customRender: 'method',
filterDropdown: 'selectDropdown',
filterIcon: 'filterIcon',
},
},
{
title: 'FILE',
dataIndex: 'file',
key: 'file',
scopedSlots: {
customRender: 'file'
},
},
{ {
title: 'STATUS', title: 'STATUS',
dataIndex: 'status', dataIndex: 'status',
@@ -169,6 +199,9 @@ export default {
}; };
}, },
methods: { methods: {
aa(file) {
console.log(file)
},
handleTableChange(pagination, filters, sorter) { handleTableChange(pagination, filters, sorter) {
const pager = {...this.pagination}; const pager = {...this.pagination};
pager.current = pagination.current; pager.current = pagination.current;
+1 -1
View File
@@ -50,7 +50,7 @@
<span slot="loadData" slot-scope="loadData"> <span slot="loadData" slot-scope="loadData">
<a-tag v-if="loadData" <a-tag v-if="loadData"
color="#eb2f96" color="#eb2f96"
>True</a-tag><a-tag v-else color="#f5222d">False</a-tag> >TRUE</a-tag><a-tag v-else color="#f5222d">FALSE</a-tag>
</span> </span>
<span slot="fileNum" slot-scope="files"> <span slot="fileNum" slot-scope="files">
<a-tag v-if="files.length>=3" <a-tag v-if="files.length>=3"
+29 -8
View File
@@ -20,7 +20,8 @@
<span slot="label"> <span slot="label">
Pasv Address Pasv Address
<a-tooltip <a-tooltip
title="Support template such as ${user}/${password}/${varname}"> title="1. Support template such as ${user}/${password}/${varname}.
2.For setting the rebind mode, please use ',' to separate addresses.">
<a-icon type="question-circle-o"/> <a-icon type="question-circle-o"/>
</a-tooltip> </a-tooltip>
</span> </span>
@@ -33,6 +34,26 @@
</a-form-model-item> </a-form-model-item>
</a-col> </a-col>
</a-row> </a-row>
<a-row :gutter="24">
<a-col :span="24">
<a-form-model-item>
<span slot="label">
Data
<a-tooltip
title="1. The data to be returned when the client executes the download request.
2. Please use base64 encoding.
3. Support template such as ${user}/${password}/${varname}">
<a-icon type="question-circle-o"/>
</a-tooltip>
</span>
<a-textarea v-model="form.data"
placeholder="The data to be returned when the client executes the download request. Please use base64 encoding."
:readOnly="formReadOnly"
:auto-size="{ minRows: 10, maxRows: 30 }"
/>
</a-form-model-item>
</a-col>
</a-row>
</a-form-model> </a-form-model>
<div <div
:style="{ :style="{
@@ -132,11 +153,6 @@
</a-table> </a-table>
</div> </div>
</template> </template>
<style scoped>
#add-rule {
margin-bottom: 10px;
}
</style>
<script> <script>
import {getFtpRule, upsertFtpRule, deleteFtpRule} from '@/api/rule' import {getFtpRule, upsertFtpRule, deleteFtpRule} from '@/api/rule'
@@ -344,7 +360,7 @@ export default {
handleCancel() { handleCancel() {
this.form = {} this.form = {}
this.closeDrawer() this.closeDrawer()
} },
}, },
mounted() { mounted() {
this.fetch({page: "1"}); this.fetch({page: "1"});
@@ -358,4 +374,9 @@ export default {
BasicRule, BasicRule,
} }
} }
</script> </script>
<style scoped>
#add-rule {
margin-bottom: 10px;
}
</style>
+29 -15
View File
@@ -1,16 +1,13 @@
package mysql package file
import ( import (
"fmt" "fmt"
"strings"
"github.com/gabriel-vasile/mimetype" "github.com/gabriel-vasile/mimetype"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/li4n0/revsuit/internal/database" "github.com/li4n0/revsuit/internal/database"
) )
const FILE_SPEARATOR = ";"
type File struct { type File struct {
ID uint `gorm:"primarykey" form:"id" json:"id"` ID uint `gorm:"primarykey" form:"id" json:"id"`
RecordID uint `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-"` RecordID uint `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-"`
@@ -18,15 +15,20 @@ type File struct {
Content []byte `json:"-"` Content []byte `json:"-"`
} }
//func (File) TableName() string { type MySQLFile File
// return "mysql_files"
//} func (MySQLFile) TableName() string {
return "mysql_files"
}
type FTPFile File
func (FTPFile) TableName() string {
return "ftp_files"
}
func GetFile(c *gin.Context) { func GetFile(c *gin.Context) {
var ( var file File
file File
mysqlRecord Record
)
id := c.Param("id") id := c.Param("id")
if id == "" { if id == "" {
c.JSON(400, gin.H{ c.JSON(400, gin.H{
@@ -37,8 +39,21 @@ func GetFile(c *gin.Context) {
return return
} }
database.DB.Model(&file).Where("id = ?", id).Find(&file) recordType := c.Param("record_type")
database.DB.Model(&mysqlRecord).Where("id = ?", file.RecordID).Find(&mysqlRecord) if id == "" {
c.JSON(400, gin.H{
"status": "failed",
"error": fmt.Errorf("param record_type missed"),
"result": nil,
})
return
}
if recordType == "mysql" {
database.DB.Table("mysql_files").Where("id = ?", id).Find(&file)
} else if recordType == "ftp" {
database.DB.Table("ftp_files").Where("id = ?", id).Find(&file)
}
if file.Content == nil || len(file.Content) == 0 { if file.Content == nil || len(file.Content) == 0 {
c.JSON(400, gin.H{ c.JSON(400, gin.H{
"status": "failed", "status": "failed",
@@ -51,8 +66,7 @@ func GetFile(c *gin.Context) {
c.Header("Content-Type", mime.String()) c.Header("Content-Type", mime.String())
c.Header("Content-Disposition", c.Header("Content-Disposition",
fmt.Sprintf( fmt.Sprintf(
"filename=%s_%s_%d", "filename=%s_%d",
strings.Replace(mysqlRecord.RemoteIP, ".", "_", -1),
file.Name, file.Name,
file.ID, file.ID,
), ),
+12 -4
View File
@@ -1,12 +1,20 @@
package rule package rule
import "strings" import (
"strings"
)
func CompileTpl(tpl string, vars map[string]string) (compiled string) { // CompileTpl receive []byte or string type tpl and variables map.
compiled = tpl // Return the template after variable substitution
func CompileTpl(tpl interface{}, vars map[string]string) (compiled string) {
switch v := tpl.(type) {
case string:
compiled = v
case []byte:
compiled = string(v)
}
for n, v := range vars { for n, v := range vars {
compiled = strings.ReplaceAll(compiled, "${"+n+"}", v) compiled = strings.ReplaceAll(compiled, "${"+n+"}", v)
} }
return compiled return compiled
} }
+185 -67
View File
@@ -3,6 +3,8 @@ package ftp
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"go/types"
"io"
"net" "net"
"strconv" "strconv"
"strings" "strings"
@@ -10,16 +12,19 @@ import (
"time" "time"
"github.com/li4n0/revsuit/internal/database" "github.com/li4n0/revsuit/internal/database"
"github.com/li4n0/revsuit/internal/file"
"github.com/li4n0/revsuit/internal/qqwry" "github.com/li4n0/revsuit/internal/qqwry"
"github.com/li4n0/revsuit/internal/recycler" "github.com/li4n0/revsuit/internal/recycler"
"github.com/li4n0/revsuit/internal/rule" "github.com/li4n0/revsuit/internal/rule"
"github.com/patrickmn/go-cache"
log "unknwon.dev/clog/v2" log "unknwon.dev/clog/v2"
) )
type Server struct { type Server struct {
Config Config
rules []*Rule rules []*Rule
rulesLock sync.RWMutex rulesLock sync.RWMutex
dataChannel chan map[string]interface{}
} }
type Status string type Status string
@@ -29,14 +34,22 @@ const (
FINISHED Status = "FINISHED" FINISHED Status = "FINISHED"
) )
type Method = string
const (
DOWNLOAD Method = "DOWNLOAD"
UPLOAD Method = "UPLOAD"
)
var ( var (
server *Server server *Server
once sync.Once once sync.Once
rebindingCache = cache.New(5*time.Second, 10*time.Second)
) )
func GetServer() *Server { func GetServer() *Server {
once.Do(func() { once.Do(func() {
server = &Server{rulesLock: sync.RWMutex{}} server = &Server{rulesLock: sync.RWMutex{}, dataChannel: make(chan map[string]interface{}, 10)}
}) })
return server return server
} }
@@ -54,7 +67,40 @@ func (s *Server) updateRules() error {
return db.Order("rank desc").Find(&s.rules).Error return db.Order("rank desc").Find(&s.rules).Error
} }
func (s *Server) authenticate(user, password string) (_rule *Rule, flag, flagGroup string, vars map[string]string) {
for _, _rule := range s.getRules() {
for _, s := range []string{user, password} {
flag, flagGroup, vars = _rule.Match(s)
if flag != "" {
vars["user"] = user
vars["password"] = password
return _rule, flag, flagGroup, vars
}
}
}
return _rule, flag, flagGroup, vars
}
func (s *Server) getPasvAddressFromCache(ip, pasvAddressTpl string) (pasvAddress string) {
if strings.Contains(pasvAddressTpl, ",") {
values, ok := rebindingCache.Get(ip)
if !ok {
rebindingCache.Set(ip, strings.Split(pasvAddressTpl, ","), cache.DefaultExpiration)
values = strings.Split(pasvAddressTpl, ",")
}
//Choose and delete first address
pasvAddress = values.([]string)[0]
if len(values.([]string)) > 1 {
rebindingCache.Set(ip, values.([]string)[1:len(values.([]string))], cache.DefaultExpiration)
} else {
rebindingCache.Delete(ip)
}
}
return pasvAddress
}
func (s *Server) handleConnection(conn net.Conn) { func (s *Server) handleConnection(conn net.Conn) {
log.Trace("New FTP connection from addr [%s]", conn.RemoteAddr())
defer func() { defer func() {
_ = conn.Close() _ = conn.Close()
if err := recover(); err != nil { if err := recover(); err != nil {
@@ -70,13 +116,18 @@ func (s *Server) handleConnection(conn net.Conn) {
log.Warn("FTP write connection error:%v", err) log.Warn("FTP write connection error:%v", err)
} }
ip := strings.Split(conn.RemoteAddr().String(), ":")[0] ip, port, _ := net.SplitHostPort(conn.RemoteAddr().String())
dataPort, _ := strconv.Atoi(port)
clientPasvConnAddress := fmt.Sprintf("%s:%d", ip, dataPort+1)
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
var user, password, path, flag, flagGroup string var user, password, method, flag, flagGroup, pasvAddress, filename string
status := CRASHED status := CRASHED
var matchedRule *Rule path := "/"
uploadData := make([]byte, 0)
var _rule *Rule
var vars map[string]string var vars map[string]string
var isRedirect bool
loop: loop:
for { for {
@@ -88,81 +139,125 @@ loop:
buf.Write(data[:n]) buf.Write(data[:n])
if buf.Len() > 4 { if buf.Len() > 4 {
cmd := string(buf.Bytes()[:4]) frags := strings.SplitN(strings.TrimRight(buf.String(), "\r\n"), " ", 2)
var cmd = frags[0]
var args string
if len(frags) > 1 {
args = frags[1]
}
log.Trace("FTP connection[%s] exec command: %s", conn.RemoteAddr(), strings.TrimRight(buf.String(), "\r\n"))
if _rule == nil && cmd != "USER" && cmd != "PASS" {
_, _ = conn.Write([]byte("332 Need account for login.\r\n"))
break loop
}
switch cmd { switch cmd {
case "USER": case "USER":
user = strings.TrimRight(string(buf.Bytes()[5:]), "\r\n") user = args
_, _ = conn.Write([]byte("331 password please - version check\r\n")) _, _ = conn.Write([]byte("331 password please - version check\r\n"))
case "PASS": case "PASS":
password = strings.TrimRight(string(buf.Bytes()[5:]), "\r\n") password = args
_, _ = conn.Write([]byte("230 User logged in\r\n")) if _rule, flag, flagGroup, vars = s.authenticate(user, password); _rule == nil {
_, _ = conn.Write([]byte("331 please specify the password\r\n"))
for _, _rule := range s.getRules() { break loop
for _, s := range []string{user, password} {
flag, flagGroup, vars = _rule.Match(s)
if flag != "" {
vars["user"] = user
vars["password"] = password
break
}
}
if flag == "" {
continue
}
matchedRule = _rule
} }
case "QUIT": log.Trace("FTP connection[%s] matched rule[rule_name: %s, flag: %s]", conn.RemoteAddr(), _rule.Name, flag)
_, _ = conn.Write([]byte("221 Goodbye.\r\n")) _, _ = conn.Write([]byte("230 User logged in\r\n"))
case "RETR":
path += "/" + strings.TrimRight(string(buf.Bytes()[5:]), "\r\n") if pasvAddress = s.getPasvAddressFromCache(ip, _rule.PasvAddress); pasvAddress == "" {
_, _ = conn.Write([]byte("451 Nope\r\n")) pasvAddress = fmt.Sprintf("%s:%d", s.PasvIP, s.PasvPort)
_, _ = conn.Write([]byte("221 Goodbye.\r\n")) }
status = FINISHED
break loop isRedirect = rule.CompileTpl(pasvAddress, vars) != fmt.Sprintf("%s:%d", s.PasvIP, s.PasvPort)
case "SIZE":
path += strings.TrimLeft(args, "/")
if _rule == nil || isRedirect || len(_rule.Data) == 0 {
_, _ = conn.Write([]byte(fmt.Sprintf("550 %s: No such file or directory.\r\n", args)))
break
}
_, _ = conn.Write([]byte(fmt.Sprintf("213 %d\r\n", len(_rule.Data))))
case "EPSV", "EPRT", "PORT": case "EPSV", "EPRT", "PORT":
// refuse to use EPSV/EPRT/PORT in order to make the client to use PASV mode. // refuse to use EPSV/EPRT/PORT in order to make the client to use PASV mode.
_, _ = conn.Write([]byte(fmt.Sprintf("500 '%s': command not understood.\r\n", cmd))) _, _ = conn.Write([]byte(fmt.Sprintf("500 '%s': command not understood.\r\n", cmd)))
case "PASV": case "PASV":
// return rule's pasv_address or default pasv address //Just so that ide does not prompt that there may be a nil value
ret := fmt.Sprintf("227 Entering Passive Mode (%s,%v,%d)\r\n", strings.ReplaceAll(s.PasvIP, ".", ","), float64(s.PasvPort/256), s.PasvPort%256) if _rule != nil {
// return rule's pasv_address or default pasv address
if matchedRule != nil && matchedRule.PasvAddress != "" { pasvAddress := rule.CompileTpl(pasvAddress, vars)
pasvAddress := rule.CompileTpl(matchedRule.PasvAddress, vars)
pasvIP, pasvPort, err := net.SplitHostPort(pasvAddress) pasvIP, pasvPort, err := net.SplitHostPort(pasvAddress)
if err != nil { if err != nil {
log.Warn("FTP failed to split rule[id%d] pasv_address(%s) :%s", matchedRule.ID, pasvAddress, err) log.Warn("FTP failed to split rule[id%d] pasv_address(%s) :%s", _rule.ID, pasvAddress, err)
break break
} }
port, err := strconv.Atoi(pasvPort) port, err := strconv.Atoi(pasvPort)
if err != nil { if err != nil {
log.Warn("FTP failed to convert rule[id%d] pasv_port(%s) :%s", matchedRule.ID, pasvPort, err) log.Warn("FTP failed to convert rule[id%d] pasv_port(%s) :%s", _rule.ID, pasvPort, err)
break break
} }
ret = fmt.Sprintf("227 Entering Passive Mode (%s,%v,%d)\r\n", strings.ReplaceAll(pasvIP, ".", ","), float64(port/256), port%256) ret := fmt.Sprintf("227 Entering Passive Mode (%s,%v,%d)\r\n", strings.ReplaceAll(pasvIP, ".", ","), float64(port/256), port%256)
_, _ = conn.Write([]byte(ret))
if isRedirect {
log.Trace("FTP connection[%s] will be redirect[pasv_address: %s]", conn.RemoteAddr(), pasvAddress)
}
} }
_, _ = conn.Write([]byte(ret)) case "RETR":
//Just so that ide does not prompt that there may be a nil value
if _rule != nil {
filename = args
method = DOWNLOAD
//send data to client
_, _ = conn.Write([]byte(fmt.Sprintf("150 Opening BINARY mode data connection for '%s' (%d bytes).\r\n", filename, len(_rule.Data))))
s.dataChannel <- map[string]interface{}{clientPasvConnAddress: []byte(rule.CompileTpl(_rule.Data, vars))}
_, _ = conn.Write([]byte("226 Transfer complete.\r\n"))
}
case "STOR":
filename = args
method = UPLOAD
_, _ = conn.Write([]byte(fmt.Sprintf("150 Opening BINARY mode data connection for '%s'.\r\n", filename)))
//only could read data send to local pasv server.
if !isRedirect {
dataChannel := make(chan []byte)
s.dataChannel <- map[string]interface{}{clientPasvConnAddress: dataChannel}
uploadData = <-dataChannel
log.Trace("FTP connection[%s] uploaded %d bytes", conn.RemoteAddr(), len(uploadData))
}
_, _ = conn.Write([]byte("226 Transfer complete.\r\n"))
case "QUIT":
_, _ = conn.Write([]byte("221 Goodbye.\r\n"))
status = FINISHED
break loop
case "CWD":
_, _ = conn.Write([]byte("250 Directory successfully changed.\r\n"))
path += strings.TrimRight(args, "\r\n") + "/"
case "PWD":
_, _ = conn.Write([]byte(fmt.Sprintf("257 \"%s\" is the current directory\r\n", path)))
default: default:
cmd = string(buf.Bytes()[:3]) _, _ = conn.Write([]byte("230 more data please!\r\n"))
if cmd == "CWD" {
_, _ = conn.Write([]byte("250 Directory successfully changed.\r\n"))
path += "/" + strings.TrimRight(string(buf.Bytes()[4:]), "\r\n")
} else if cmd == "PWD" {
_, _ = conn.Write([]byte("257 \"/\" is the current directory\r\n"))
} else {
_, _ = conn.Write([]byte("230 more data please!\r\n"))
}
} }
} }
buf = &bytes.Buffer{} buf = &bytes.Buffer{}
} }
if matchedRule != nil { if _rule != nil {
_rule := matchedRule
area := qqwry.Area(ip) area := qqwry.Area(ip)
var r *Record
var err error
// create new record // create new record
r, err := NewRecord(_rule, flag, user, password, path, ip, area, status) ftpFile := &file.FTPFile{}
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 { if err != nil {
log.Warn("FTP record[rule_id:%d] created failed :%s", _rule.ID, err) log.Warn("FTP record[rule_id:%d] created failed :%s", _rule.ID, err)
return return
@@ -193,19 +288,35 @@ loop:
} }
} }
func (s *Server) handlePasvConnection(conn net.Conn, data map[string]interface{}) {
remoteAddress := conn.RemoteAddr().String()
switch v := data[remoteAddress].(type) {
case types.Nil:
s.dataChannel <- data
return
case []byte:
_, err := conn.Write(v)
if err != nil {
log.Warn("FTP PASV server sent data to connection[%s] failed with error: %s", remoteAddress, err)
}
log.Trace("FTP PASV server has sent data to connection[%s]", remoteAddress)
case chan []byte:
buf, err := io.ReadAll(conn)
if err != nil {
log.Warn("FTP PASV server received data from connection[%s] failed with error: %s", remoteAddress, err)
}
v <- buf
log.Trace("FTP PASV server has received data from connection[%s]", remoteAddress)
}
_ = conn.Close()
}
func (s *Server) Run() { func (s *Server) Run() {
if err := s.updateRules(); err != nil { if err := s.updateRules(); err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
// run pasv server
// run server
log.Info("Starting FTP Server at %v", s.Addr)
listener, err := net.Listen("tcp", s.Addr)
if err != nil {
log.Fatal(err.Error())
}
go func() { go func() {
pasvAddress := fmt.Sprintf("%s:%d", strings.Split(s.Addr, ":")[0], s.PasvPort) pasvAddress := fmt.Sprintf("%s:%d", strings.Split(s.Addr, ":")[0], s.PasvPort)
log.Info("Start to listen FTP PASV port at %v", pasvAddress) log.Info("Start to listen FTP PASV port at %v", pasvAddress)
@@ -213,16 +324,23 @@ func (s *Server) Run() {
if err != nil { if err != nil {
log.Fatal("FTP failed to listen on pasv port : %v", err) log.Fatal("FTP failed to listen on pasv port : %v", err)
} }
for { for data := range s.dataChannel {
tcpConn, err := listener.Accept() tcpConn, err := listener.Accept()
if err != nil { if err != nil {
log.Warn("FTP accept connection error: %v", err) log.Warn("FTP accept connection error: %v", err)
continue continue
} }
_ = tcpConn.Close() s.handlePasvConnection(tcpConn, data)
} }
}() }()
// run ftp server
log.Info("Starting FTP Server at %v", s.Addr)
listener, err := net.Listen("tcp", s.Addr)
if err != nil {
log.Fatal(err.Error())
}
for { for {
tcpConn, err := listener.Accept() tcpConn, err := listener.Accept()
if err != nil { if err != nil {
+15 -7
View File
@@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/li4n0/revsuit/internal/database" "github.com/li4n0/revsuit/internal/database"
"github.com/li4n0/revsuit/internal/file"
"github.com/li4n0/revsuit/internal/notice" "github.com/li4n0/revsuit/internal/notice"
"github.com/li4n0/revsuit/internal/record" "github.com/li4n0/revsuit/internal/record"
) )
@@ -13,12 +14,14 @@ import (
var _ record.Record = (*Record)(nil) var _ record.Record = (*Record)(nil)
type Record struct { type Record struct {
User string `form:"user" json:"user"`
Password string `form:"password" json:"password"`
Path string `form:"path" json:"path"`
Status Status `form:"status" json:"status"`
record.BaseRecord record.BaseRecord
Rule Rule `gorm:"foreignKey:RuleName;references:Name;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" form:"-" json:"-" notice:"-"` 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 `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 { func (Record) TableName() string {
@@ -29,7 +32,7 @@ func (r Record) Notice() {
notice.Notice(r) notice.Notice(r)
} }
func NewRecord(rule *Rule, flag, user, password, path, ip, area string, status Status) (r *Record, err error) { func NewRecord(rule *Rule, flag, user, password, method, path, ip, area string, file *file.FTPFile, status Status) (r *Record, err error) {
r = &Record{ r = &Record{
BaseRecord: record.BaseRecord{ BaseRecord: record.BaseRecord{
Flag: flag, Flag: flag,
@@ -38,9 +41,11 @@ func NewRecord(rule *Rule, flag, user, password, path, ip, area string, status S
RequestTime: time.Now(), RequestTime: time.Now(),
}, },
Path: path, Path: path,
Method: method,
User: user, User: user,
Password: password, Password: password,
Status: status, Status: status,
File: file,
Rule: *rule, Rule: *rule,
} }
return r, database.DB.Create(r).Error return r, database.DB.Create(r).Error
@@ -76,6 +81,9 @@ func ListRecords(c *gin.Context) {
if ftpRecord.Path != "" { if ftpRecord.Path != "" {
db.Where("path like ?", "%"+ftpRecord.Path+"%") db.Where("path like ?", "%"+ftpRecord.Path+"%")
} }
if ftpRecord.Method != "" {
db.Where("method = ?", ftpRecord.Method)
}
if ftpRecord.Status != "" { if ftpRecord.Status != "" {
db.Where("status = ?", ftpRecord.Status) db.Where("status = ?", ftpRecord.Status)
} }
@@ -100,7 +108,7 @@ func ListRecords(c *gin.Context) {
order = "desc" order = "desc"
} }
if err := db.Order("id" + " " + order).Count(&count).Offset((page - 1) * 10).Limit(10).Find(&res).Error; err != nil { if err := db.Preload("File").Order("id " + order).Count(&count).Offset((page - 1) * 10).Limit(10).Find(&res).Error; err != nil {
c.JSON(400, gin.H{ c.JSON(400, gin.H{
"status": "failed", "status": "failed",
"error": err, "error": err,
+2
View File
@@ -14,6 +14,7 @@ import (
type Rule struct { type Rule struct {
rule.BaseRule rule.BaseRule
PasvAddress string `gorm:"pasv_address" json:"pasv_address" form:"pasv_address"` PasvAddress string `gorm:"pasv_address" json:"pasv_address" form:"pasv_address"`
Data []byte `json:"data" form:"data"`
} }
func (Rule) TableName() string { func (Rule) TableName() string {
@@ -44,6 +45,7 @@ func (r *Rule) CreateOrUpdate() (err error) {
"flag_format", "flag_format",
"rank", "rank",
"pasv_address", "pasv_address",
"data",
"push_to_client", "push_to_client",
"notice", "notice",
}), }),
+21 -17
View File
@@ -7,8 +7,10 @@ import (
"regexp" "regexp"
"strings" "strings"
"sync" "sync"
"time"
"github.com/li4n0/revsuit/internal/database" "github.com/li4n0/revsuit/internal/database"
"github.com/li4n0/revsuit/internal/file"
"github.com/li4n0/revsuit/internal/qqwry" "github.com/li4n0/revsuit/internal/qqwry"
"github.com/li4n0/revsuit/pkg/mysql/vmysql" "github.com/li4n0/revsuit/pkg/mysql/vmysql"
log "unknwon.dev/clog/v2" log "unknwon.dev/clog/v2"
@@ -54,21 +56,29 @@ func (s *Server) updateRules() error {
// NewConnection is part of the mysql.Handler interface. // NewConnection is part of the mysql.Handler interface.
func (s *Server) NewConnection(c *vmysql.Conn) { 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() c.RecycleReadPacket()
var ( var (
user = c.User user = c.User
schema = c.SchemaName schema = c.SchemaName
validated bool validated bool
flag string
) )
for _, _rule := range s.getRules() { for _, _rule := range s.getRules() {
userFlag, _, _ := _rule.Match(user) flag, _, _ = _rule.Match(user)
schemaFlag, _, _ := _rule.Match(schema) if flag == "" {
if userFlag == "" && schemaFlag == "" { flag, _, _ = _rule.Match(schema)
}
if flag == "" {
continue 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) s.connRulePool.Store(c.ConnectionID, _rule)
validated = true validated = true
break break
@@ -117,17 +127,17 @@ func (s *Server) ConnectionClosed(c *vmysql.Conn) {
ip := strings.Split(c.RemoteAddr().String(), ":")[0] ip := strings.Split(c.RemoteAddr().String(), ":")[0]
filenames := strings.Split(_rule.Files, FILE_SPEARATOR) filenames := strings.Split(_rule.Files, ",")
files := make([]File, 0) files := make([]file.MySQLFile, 0)
for _, filename := range filenames { for _, filename := range filenames {
if len(c.Files[filename]) != 0 { 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) r, err := newRecord(_rule, flag, user, clientName, clientOS, ip, qqwry.Area(ip), supportLoadLocalData, files)
if err != nil { 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 return
} }
log.Info("MySQL record[id:%d rule:%s remote_ip:%s] has been created", r.ID, _rule.Name, ip) 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 { if _rule.Notice {
go func() { go func() {
r.Notice() 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 // mysql LOAD DATA LOCAL
if !c.SupportLoadDataLocal { // 客户端不支持读取本地文件且没有开启总是读取,直接返回错误 if !c.SupportLoadDataLocal {
log.Trace("MySQL Client not support LOAD DATA LOCAL, return error directly") log.Trace("MySQL Client not support LOAD DATA LOCAL, return error directly")
c.WriteErrorResponse( c.WriteErrorResponse(
fmt.Sprintf( fmt.Sprintf(
@@ -224,7 +234,7 @@ func (s *Server) ComQuery(c *vmysql.Conn, query string, callback func(*sqltypes.
return nil return nil
} }
files := strings.Split(_rule.Files, ";") files := strings.Split(_rule.Files, ",")
if c.Files == nil { if c.Files == nil {
c.Files = make(map[string][]byte) c.Files = make(map[string][]byte)
} }
@@ -238,12 +248,6 @@ func (s *Server) ComQuery(c *vmysql.Conn, query string, callback func(*sqltypes.
} else { } else {
c.Files[filename] = data 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/gin-gonic/gin"
"github.com/li4n0/revsuit/internal/database" "github.com/li4n0/revsuit/internal/database"
"github.com/li4n0/revsuit/internal/file"
"github.com/li4n0/revsuit/internal/notice" "github.com/li4n0/revsuit/internal/notice"
"github.com/li4n0/revsuit/internal/record" "github.com/li4n0/revsuit/internal/record"
) )
@@ -14,12 +15,12 @@ var _ record.Record = (*Record)(nil)
type Record struct { type Record struct {
record.BaseRecord record.BaseRecord
Username string `gorm:"index" form:"username" json:"username" notice:"username"` Username string `gorm:"index" form:"username" json:"username" notice:"username"`
ClientName string `gorm:"index" form:"client_name" json:"client_name" notice:"client_name"` 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"` 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"` LoadLocalData bool `gorm:"index" form:"load_local_data" json:"load_local_data" notice:"load_local_data"`
Files []File `form:"-" json:"files" notice:"-"` Files []file.MySQLFile `form:"-" json:"files" notice:"-"`
Rule Rule `gorm:"foreignKey:RuleName;references:Name;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" form:"-" json:"-" notice:"-"` Rule Rule `gorm:"foreignKey:RuleName;references:Name;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" form:"-" json:"-" notice:"-"`
} }
func (Record) TableName() string { func (Record) TableName() string {
@@ -30,7 +31,7 @@ func (r Record) Notice() {
notice.Notice(r) 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{ r = &Record{
BaseRecord: record.BaseRecord{ BaseRecord: record.BaseRecord{
Flag: flag, Flag: flag,
+9 -9
View File
@@ -73,7 +73,7 @@ type Conn struct {
// conn is the underlying network connection. // conn is the underlying network connection.
// Calling Close() on the Conn will close this connection. // Calling Close() on the Conn will close this connection.
// If there are any ongoing reads or writes, they may get interrupted. // 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. // For server-side connections, listener points to the server object.
listener *Listener listener *Listener
@@ -167,7 +167,7 @@ var writersPool = sync.Pool{New: func() interface{} { return bufio.NewWriterSize
// size for reads. // size for reads.
func newServerConn(conn net.Conn, listener *Listener) *Conn { func newServerConn(conn net.Conn, listener *Listener) *Conn {
c := &Conn{ c := &Conn{
conn: conn, Conn: conn,
listener: listener, listener: listener,
closed: sync2.NewAtomicBool(false), closed: sync2.NewAtomicBool(false),
} }
@@ -181,7 +181,7 @@ func newServerConn(conn net.Conn, listener *Listener) *Conn {
// be terminated by a call to flush. // be terminated by a call to flush.
func (c *Conn) startWriterBuffering() { func (c *Conn) startWriterBuffering() {
c.bufferedWriter = writersPool.Get().(*bufio.Writer) c.bufferedWriter = writersPool.Get().(*bufio.Writer)
c.bufferedWriter.Reset(c.conn) c.bufferedWriter.Reset(c.Conn)
} }
// flush flushes the written data to the socket. // flush flushes the written data to the socket.
@@ -206,7 +206,7 @@ func (c *Conn) getWriter() io.Writer {
if c.bufferedWriter != nil { if c.bufferedWriter != nil {
return c.bufferedWriter return c.bufferedWriter
} }
return c.conn return c.Conn
} }
// getReader returns reader for connection. It can be *bufio.Reader or net.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 { if c.bufferedReader != nil {
return c.bufferedReader return c.bufferedReader
} }
return c.conn return c.Conn
} }
func (c *Conn) readHeaderFrom(r io.Reader) (int, error) { 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. // This function usually shouldn't be used - use readEphemeralPacket.
func (c *Conn) readEphemeralPacketDirect() ([]byte, error) { func (c *Conn) readEphemeralPacketDirect() ([]byte, error) {
if c.currentEphemeralPolicy != ephemeralUnused { 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) length, err := c.readHeaderFrom(r)
if err != nil { if err != nil {
@@ -577,7 +577,7 @@ func (c *Conn) recycleWritePacket() {
// RemoteAddr returns the underlying socket RemoteAddr(). // RemoteAddr returns the underlying socket RemoteAddr().
func (c *Conn) RemoteAddr() net.Addr { func (c *Conn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr() return c.Conn.RemoteAddr()
} }
// ID returns the MySQL connection ID for this connection. // ID returns the MySQL connection ID for this connection.
@@ -594,7 +594,7 @@ func (c *Conn) String() string {
// routine to interrupt the current connection. // routine to interrupt the current connection.
func (c *Conn) Close() { func (c *Conn) Close() {
if c.closed.CompareAndSwap(false, true) { 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() c.RecycleReadPacket()
if con, ok := c.conn.(*tls.Conn); ok { if con, ok := c.Conn.(*tls.Conn); ok {
connState := con.ConnectionState() connState := con.ConnectionState()
tlsVerStr := tlsVersionToString(connState.Version) tlsVerStr := tlsVersionToString(connState.Version)
if tlsVerStr != "" { if tlsVerStr != "" {
@@ -619,8 +619,8 @@ func (l *Listener) parseClientHandshakePacket(c *Conn, firstTime bool, data []by
// Check for SSL. // Check for SSL.
if firstTime && l.TLSConfig != nil && clientFlags&CapabilityClientSSL > 0 { if firstTime && l.TLSConfig != nil && clientFlags&CapabilityClientSSL > 0 {
// Need to switch to TLS, and then re-read the packet. // Need to switch to TLS, and then re-read the packet.
conn := tls.Server(c.conn, l.TLSConfig) conn := tls.Server(c.Conn, l.TLSConfig)
c.conn = conn c.Conn = conn
c.bufferedReader.Reset(conn) c.bufferedReader.Reset(conn)
c.Capabilities |= CapabilityClientSSL c.Capabilities |= CapabilityClientSSL
return "", "", nil, nil return "", "", nil, nil
+2 -1
View File
@@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/li4n0/revsuit/frontend" "github.com/li4n0/revsuit/frontend"
"github.com/li4n0/revsuit/internal/file"
"github.com/li4n0/revsuit/pkg/dns" "github.com/li4n0/revsuit/pkg/dns"
"github.com/li4n0/revsuit/pkg/ftp" "github.com/li4n0/revsuit/pkg/ftp"
"github.com/li4n0/revsuit/pkg/mysql" "github.com/li4n0/revsuit/pkg/mysql"
@@ -101,6 +102,6 @@ func (revsuit *Revsuit) registerHttpRouter() {
// init file router group // init file router group
fileGroup := revsuit.http.ApiGroup.Group("/file") fileGroup := revsuit.http.ApiGroup.Group("/file")
fileGroup.GET("/mysql/:id", mysql.GetFile) fileGroup.GET("/:record_type/:id", file.GetFile)
} }
+9 -4
View File
@@ -3,6 +3,7 @@ package server
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/li4n0/revsuit/internal/database" "github.com/li4n0/revsuit/internal/database"
"github.com/li4n0/revsuit/internal/file"
"github.com/li4n0/revsuit/internal/notice" "github.com/li4n0/revsuit/internal/notice"
"github.com/li4n0/revsuit/pkg/dns" "github.com/li4n0/revsuit/pkg/dns"
"github.com/li4n0/revsuit/pkg/ftp" "github.com/li4n0/revsuit/pkg/ftp"
@@ -53,7 +54,7 @@ func initDatabase(dsn string) {
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
err = database.DB.AutoMigrate(&mysql.File{}) err = database.DB.AutoMigrate(&file.MySQLFile{})
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
@@ -73,13 +74,17 @@ func initDatabase(dsn string) {
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
err = database.DB.AutoMigrate(&file.FTPFile{})
if err != nil {
log.Fatal(err.Error())
}
} }
func initLog(level string) (logLevel log.Level) { func initLog(level string) (logLevel log.Level) {
switch level { switch level {
case "debug": case "debug", "trace":
gin.SetMode(gin.DebugMode) gin.SetMode(gin.DebugMode)
database.DB.Logger.LogMode(logger.Info) database.DB.Logger.LogMode(logger.Info)
logLevel = log.LevelTrace logLevel = log.LevelTrace
@@ -87,7 +92,7 @@ func initLog(level string) (logLevel log.Level) {
gin.SetMode(gin.DebugMode) gin.SetMode(gin.DebugMode)
database.DB.Logger.LogMode(logger.Info) database.DB.Logger.LogMode(logger.Info)
logLevel = log.LevelInfo logLevel = log.LevelInfo
case "warning": case "warning", "warn":
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
database.DB.Logger.LogMode(logger.Warn) database.DB.Logger.LogMode(logger.Warn)
logLevel = log.LevelWarn logLevel = log.LevelWarn
@@ -133,8 +138,8 @@ func initNotice(nc noticeConfig) {
func New(c *Config) *Revsuit { func New(c *Config) *Revsuit {
initDatabase(c.Database)
logLevel := initLog(c.LogLevel) logLevel := initLog(c.LogLevel)
initDatabase(c.Database)
initNotice(c.Notice) initNotice(c.Notice)
s := &Revsuit{ s := &Revsuit{