mirror of
https://github.com/Li4n0/revsuit.git
synced 2026-09-26 08:31:52 +08:00
feat(ftp): support receive ftp connection (#6)
Co-authored-by: E99p1ant <[email protected]>
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package ftp
|
||||
|
||||
type Config struct {
|
||||
Enable bool
|
||||
Addr string
|
||||
PasvIP string `yaml:"pasv_ip"`
|
||||
PasvPort int `yaml:"pasv_port"`
|
||||
}
|
||||
+230
@@ -0,0 +1,230 @@
|
||||
package ftp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/li4n0/revsuit/internal/database"
|
||||
"github.com/li4n0/revsuit/internal/qqwry"
|
||||
"github.com/li4n0/revsuit/internal/rule"
|
||||
log "unknwon.dev/clog/v2"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Config
|
||||
rules []*Rule
|
||||
rulesLock sync.RWMutex
|
||||
}
|
||||
|
||||
type Status string
|
||||
|
||||
const (
|
||||
CRASHED Status = "CRASHED"
|
||||
FINISHED Status = "FINISHED"
|
||||
)
|
||||
|
||||
var (
|
||||
server *Server
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
func GetServer() *Server {
|
||||
once.Do(func() {
|
||||
server = &Server{rulesLock: sync.RWMutex{}}
|
||||
})
|
||||
return server
|
||||
}
|
||||
|
||||
func (s *Server) getRules() []*Rule {
|
||||
defer s.rulesLock.RUnlock()
|
||||
s.rulesLock.RLock()
|
||||
return s.rules
|
||||
}
|
||||
|
||||
func (s *Server) updateRules() error {
|
||||
db := database.DB.Model(new(Rule))
|
||||
s.rulesLock.Lock()
|
||||
db.Order("rank desc").Find(&s.rules)
|
||||
s.rulesLock.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) handleConnection(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
|
||||
if err := conn.SetDeadline(time.Now().Add(time.Second * 30)); err != nil {
|
||||
log.Error("FTP set connection deadline error:%v", err.Error())
|
||||
}
|
||||
|
||||
if _, err := conn.Write([]byte("220 (vsFTPd 3.0.2)\r\n")); err != nil {
|
||||
log.Error("FTP write connection error:%v", err.Error())
|
||||
}
|
||||
|
||||
ip := strings.Split(conn.RemoteAddr().String(), ":")[0]
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
var user, password, path, flag, flagGroup string
|
||||
status := CRASHED
|
||||
var matchedRule *Rule
|
||||
var vars map[string]string
|
||||
|
||||
loop:
|
||||
for {
|
||||
data := make([]byte, 2048)
|
||||
n, err := conn.Read(data)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
buf.Write(data[:n])
|
||||
|
||||
if buf.Len() > 4 {
|
||||
cmd := string(buf.Bytes()[:4])
|
||||
switch cmd {
|
||||
case "USER":
|
||||
user = strings.TrimRight(string(buf.Bytes()[5:]), "\r\n")
|
||||
_, _ = conn.Write([]byte("331 password please - version check\r\n"))
|
||||
case "PASS":
|
||||
password = strings.TrimRight(string(buf.Bytes()[5:]), "\r\n")
|
||||
_, _ = conn.Write([]byte("230 User logged in\r\n"))
|
||||
|
||||
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
|
||||
break
|
||||
}
|
||||
}
|
||||
if flag == "" {
|
||||
continue
|
||||
}
|
||||
matchedRule = _rule
|
||||
}
|
||||
|
||||
case "QUIT":
|
||||
_, _ = conn.Write([]byte("221 Goodbye.\r\n"))
|
||||
case "RETR":
|
||||
path += "/" + strings.TrimRight(string(buf.Bytes()[5:]), "\r\n")
|
||||
_, _ = conn.Write([]byte("451 Nope\r\n"))
|
||||
_, _ = conn.Write([]byte("221 Goodbye.\r\n"))
|
||||
status = FINISHED
|
||||
break loop
|
||||
case "EPSV", "EPRT", "PORT":
|
||||
// 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)))
|
||||
case "PASV":
|
||||
// return rule's pasv_address or default pasv address
|
||||
ret := fmt.Sprintf("227 Entering Passive Mode (%s,%v,%d)\r\n", strings.ReplaceAll(s.PasvIP, ".", ","), float64(s.PasvPort/256), s.PasvPort%256)
|
||||
|
||||
if matchedRule != nil && matchedRule.PasvAddress != "" {
|
||||
pasvAddress := rule.CompileTpl(matchedRule.PasvAddress, vars)
|
||||
pasvIP, pasvPort, err := net.SplitHostPort(pasvAddress)
|
||||
if err != nil {
|
||||
log.Warn("FTP failed to split rule[id%d] pasv_address(%s) :%s", matchedRule.ID, pasvAddress, err.Error())
|
||||
break
|
||||
}
|
||||
port, err := strconv.Atoi(pasvPort)
|
||||
if err != nil {
|
||||
log.Warn("FTP failed to convert rule[id%d] pasv_port(%s) :%s", matchedRule.ID, pasvPort, err.Error())
|
||||
break
|
||||
}
|
||||
ret = fmt.Sprintf("227 Entering Passive Mode (%s,%v,%d)\r\n", strings.ReplaceAll(pasvIP, ".", ","), float64(port/256), port%256)
|
||||
}
|
||||
_, _ = conn.Write([]byte(ret))
|
||||
default:
|
||||
cmd = string(buf.Bytes()[:3])
|
||||
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{}
|
||||
}
|
||||
|
||||
if matchedRule != nil {
|
||||
_rule := matchedRule
|
||||
area := qqwry.Area(ip)
|
||||
|
||||
// create new record
|
||||
r, err := NewRecord(_rule, flag, user, password, path, ip, area, status)
|
||||
if err != nil {
|
||||
log.Error("FTP record[rule_id:%d] created failed :%s", _rule.ID, err.Error())
|
||||
return
|
||||
}
|
||||
log.Info("FTP record[id:%d rule:%s remote_ip:%s] has been created", r.ID, _rule.Name, ip)
|
||||
|
||||
//only send to client when this connection recorded first time.
|
||||
if _rule.PushToClient {
|
||||
if flagGroup != "" {
|
||||
var count int64
|
||||
database.DB.Where("rule_name=? and raw like ?", _rule.Name, "%"+flagGroup+"%").Model(&Record{}).Count(&count)
|
||||
if count <= 1 {
|
||||
r.PushToClient()
|
||||
log.Trace("FTP record[id%d] has been put to client message queue", r.ID)
|
||||
}
|
||||
}
|
||||
r.PushToClient()
|
||||
log.Trace("FTP record[id%d] has been put to client message queue", r.ID)
|
||||
}
|
||||
|
||||
//send notice
|
||||
if _rule.Notice {
|
||||
go func() {
|
||||
r.Notice()
|
||||
log.Trace("FTP record[id%d] notice has been sent", r.ID)
|
||||
}()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Run() {
|
||||
if err := s.updateRules(); err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
// 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() {
|
||||
pasvAddress := fmt.Sprintf("%s:%d", strings.Split(s.Addr, ":")[0], s.PasvPort)
|
||||
log.Info("Start to listen FTP PASV port at %v", pasvAddress)
|
||||
listener, err := net.Listen("tcp", pasvAddress)
|
||||
if err != nil {
|
||||
log.Fatal("FTP failed to listen on pasv port : %v", err)
|
||||
}
|
||||
for {
|
||||
tcpConn, err := listener.Accept()
|
||||
if err != nil {
|
||||
log.Error("FTP accept connection error: %v", err)
|
||||
continue
|
||||
}
|
||||
_ = tcpConn.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
tcpConn, err := listener.Accept()
|
||||
if err != nil {
|
||||
log.Error("FTP accept connection error: %v", err)
|
||||
continue
|
||||
}
|
||||
go s.handleConnection(tcpConn)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package ftp
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/li4n0/revsuit/internal/database"
|
||||
"github.com/li4n0/revsuit/internal/notice"
|
||||
"github.com/li4n0/revsuit/internal/record"
|
||||
)
|
||||
|
||||
var _ record.Record = (*Record)(nil)
|
||||
|
||||
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
|
||||
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, path, ip, area string, status Status) (r *Record, err error) {
|
||||
r = &Record{
|
||||
BaseRecord: record.BaseRecord{
|
||||
Flag: flag,
|
||||
RemoteIP: ip,
|
||||
IpArea: area,
|
||||
RequestTime: time.Now(),
|
||||
},
|
||||
Path: path,
|
||||
User: user,
|
||||
Password: password,
|
||||
Status: status,
|
||||
Rule: *rule,
|
||||
}
|
||||
err = database.DB.Create(r).Error
|
||||
return r, err
|
||||
}
|
||||
|
||||
func ListRecords(c *gin.Context) {
|
||||
var (
|
||||
ftpRecord Record
|
||||
res []Record
|
||||
count int64
|
||||
order = c.Query("order")
|
||||
)
|
||||
|
||||
if err := c.ShouldBind(&ftpRecord); err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err,
|
||||
"result": nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
db := database.DB.Model(&ftpRecord)
|
||||
if ftpRecord.Flag != "" {
|
||||
db.Where("flag = ?", ftpRecord.Flag)
|
||||
}
|
||||
if ftpRecord.Path != "" {
|
||||
db.Where("path like ?", "%"+ftpRecord.Path+"%")
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
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.Order("id" + " " + order).Count(&count).Offset((page - 1) * 10).Limit(10).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},
|
||||
})
|
||||
}
|
||||
+194
@@ -0,0 +1,194 @@
|
||||
package ftp
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/li4n0/revsuit/internal/database"
|
||||
"github.com/li4n0/revsuit/internal/rule"
|
||||
"gorm.io/gorm/clause"
|
||||
log "unknwon.dev/clog/v2"
|
||||
)
|
||||
|
||||
// FTP rule struct
|
||||
type Rule struct {
|
||||
rule.BaseRule
|
||||
PasvAddress string `gorm:"pasv_address" json:"pasv_address" form:"pasv_address"`
|
||||
}
|
||||
|
||||
func (Rule) TableName() string {
|
||||
return "ftp_rules"
|
||||
}
|
||||
|
||||
// NewRule creates a new ftp rule struct
|
||||
func NewRule(name, flagFormat, pasvAddress string, pushToClient, notice bool) *Rule {
|
||||
return &Rule{
|
||||
BaseRule: rule.BaseRule{
|
||||
Name: name,
|
||||
FlagFormat: flagFormat,
|
||||
PushToClient: pushToClient,
|
||||
Notice: notice,
|
||||
},
|
||||
PasvAddress: pasvAddress,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateOrUpdate creates or updates the ftp rule in database and ruleSet
|
||||
func (r *Rule) CreateOrUpdate() (err error) {
|
||||
db := database.DB.Model(r)
|
||||
err = db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "id"}},
|
||||
DoUpdates: clause.AssignmentColumns(
|
||||
[]string{
|
||||
"name",
|
||||
"flag_format",
|
||||
"rank",
|
||||
"pasv_address",
|
||||
"push_to_client",
|
||||
"notice",
|
||||
}),
|
||||
}).Create(r).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return GetServer().updateRules()
|
||||
}
|
||||
|
||||
// Delete deletes the ftp rule in database and ruleSet
|
||||
func (r *Rule) Delete() (err error) {
|
||||
db := database.DB.Model(r)
|
||||
err = db.Delete(r).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return GetServer().updateRules()
|
||||
}
|
||||
|
||||
// ListRules lists all ftp rules those satisfy the filter
|
||||
func ListRules(c *gin.Context) {
|
||||
var (
|
||||
ftpRule Rule
|
||||
res []Rule
|
||||
count int64
|
||||
order = c.Query("order")
|
||||
)
|
||||
|
||||
if err := c.ShouldBind(&ftpRule); err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err,
|
||||
"result": nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
db := database.DB.Model(&ftpRule)
|
||||
if ftpRule.Name != "" {
|
||||
db.Where("name = ?", ftpRule.Name)
|
||||
}
|
||||
db.Count(&count)
|
||||
|
||||
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.Order("rank desc").Order("id" + " " + order).Count(&count).Offset((page - 1) * 10).Limit(10).Find(&res).Error; err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err,
|
||||
"data": nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"status": "succeed",
|
||||
"error": nil,
|
||||
"result": gin.H{"count": count, "data": res},
|
||||
})
|
||||
}
|
||||
|
||||
// Create or update ftp rule from user submit
|
||||
func UpsertRules(c *gin.Context) {
|
||||
var (
|
||||
ftpRule Rule
|
||||
update bool
|
||||
)
|
||||
|
||||
if err := c.ShouldBind(&ftpRule); err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"data": nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if ftpRule.ID != 0 {
|
||||
update = true
|
||||
}
|
||||
|
||||
if err := ftpRule.CreateOrUpdate(); err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"result": nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if update {
|
||||
log.Trace("FTP rule[id%d] has been updated", ftpRule.ID)
|
||||
} else {
|
||||
log.Trace("FTP rule[id%d] has been created", ftpRule.ID)
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"status": "succeed",
|
||||
"error": nil,
|
||||
"result": nil,
|
||||
})
|
||||
}
|
||||
|
||||
// Delete ftp rule from user submit
|
||||
func DeleteRules(c *gin.Context) {
|
||||
var ftpRule Rule
|
||||
|
||||
if err := c.ShouldBind(&ftpRule); err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"data": nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if err := ftpRule.Delete(); err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"data": nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
log.Trace("FTP rule[id%d] has been deleted", ftpRule.ID)
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"status": "succeed",
|
||||
"error": nil,
|
||||
"data": nil,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user