service_matcher-go/ftp/ftp.go

97 lines
1.9 KiB
Go
Raw Normal View History

2018-08-13 07:48:32 +00:00
package ftp
import (
"strings"
2018-08-15 07:17:18 +00:00
osm "git.loafle.net/overflow/service_matcher-go"
2018-08-13 07:48:32 +00:00
)
// FTP Status codes, defined in RFC 959
const (
statusReadyServer = "120"
statusOK = "200"
statusNewConnectOK = "220"
statusSystemNameOK = "215"
statusCloseConnect = "221"
statusUnkownCMD = "202"
statusTlsUseOK = "234"
statusCloseControlConnect = "421"
statusSyntaxErr = "500"
statusParamSyntaxErr = "501"
statusNotUseCMD = "502"
statusIncorrectCMD = "503"
statusNotLoggedIn = "530"
statusTlsNotUse = "534"
statusNeedUserId = "332"
)
type FTPMatcher struct {
2018-08-15 07:17:18 +00:00
osm.Matchers
meta osm.Metadata
2018-08-13 07:48:32 +00:00
}
func (ftp *FTPMatcher) Key() string {
return "FTP"
}
func (ftp *FTPMatcher) Name() string {
return "FTP"
}
2018-08-15 07:17:18 +00:00
func (ftp *FTPMatcher) Meta() osm.Metadata {
2018-08-13 07:48:32 +00:00
return ftp.meta
}
func (ftp *FTPMatcher) IsPrePacket() bool {
return true
}
func (ftp *FTPMatcher) HasResponse(index int) bool {
return true
}
2018-08-15 07:17:18 +00:00
func (ftp *FTPMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool {
2018-08-13 07:48:32 +00:00
return false
}
2018-08-15 07:17:18 +00:00
func (ftp *FTPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error {
2018-08-13 07:48:32 +00:00
2018-09-03 06:42:56 +00:00
if packet == nil || !packet.Valid() {
2018-08-15 07:17:18 +00:00
return osm.NoPacketReceivedError()
2018-08-13 07:48:32 +00:00
}
2018-09-03 06:42:56 +00:00
str := strings.Split(string(packet.Bytes()), "\r\n")[0]
2018-08-13 07:48:32 +00:00
if len(str) < 4 {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
code := str[:3]
if index == 0 {
switch code {
case statusNewConnectOK, statusReadyServer:
ftp.meta["comment"] = str[4:]
return nil
}
} else if index == 1 {
switch code {
case statusCloseConnect, statusSyntaxErr:
return nil
}
}
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
2018-08-15 07:17:18 +00:00
func NewMatcher() osm.Matcher {
2018-08-13 07:48:32 +00:00
m := &FTPMatcher{}
2018-08-15 07:17:18 +00:00
m.meta = osm.NewMetadata()
2018-08-13 07:48:32 +00:00
quitStr := "QUIT\r\n"
quitByte := make([]byte, len(quitStr))
copy(quitByte[:], quitStr)
2018-08-15 07:17:18 +00:00
m.AddPacket(osm.NewPacket(quitByte, len(quitStr)))
2018-08-13 07:48:32 +00:00
return m
}