service_matcher-go/smtp/smtp.go

77 lines
1.3 KiB
Go
Raw Normal View History

2018-08-13 07:48:32 +00:00
package smtp
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
)
type SmtpMatcher struct {
2018-08-15 07:17:18 +00:00
osm.Matchers
2018-08-13 07:48:32 +00:00
}
func (t *SmtpMatcher) Key() string {
return "SMTP"
}
2018-09-03 06:42:56 +00:00
func (t *SmtpMatcher) Name() string {
2018-08-13 07:48:32 +00:00
return "SMTP"
}
2018-09-03 06:42:56 +00:00
func (t *SmtpMatcher) Meta() osm.Metadata {
return nil
}
2018-08-13 07:48:32 +00:00
func (t *SmtpMatcher) IsPrePacket() bool {
return true
}
func (t *SmtpMatcher) HasResponse(index int) bool {
return true
}
2018-08-15 07:17:18 +00:00
func (t *SmtpMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool {
2018-08-13 07:48:32 +00:00
return false
}
2018-09-03 06:42:56 +00:00
func (t *SmtpMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error {
2018-08-13 07:48:32 +00:00
if packet == nil {
2018-09-03 06:42:56 +00:00
return osm.NoPacketReceivedError()
2018-08-13 07:48:32 +00:00
}
2018-09-03 06:42:56 +00:00
buf := string(packet.Bytes())
2018-08-13 07:48:32 +00:00
if len(buf) == 0 || len(buf) < 5 {
2018-09-03 06:42:56 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
splits := strings.Split(buf, "\r\n")
splits = strings.Split(buf, " ")
if index == 0 {
if splits[0] == "220" {
2018-09-03 06:42:56 +00:00
return nil
2018-08-13 07:48:32 +00:00
}
} else if index == 1 {
if splits[0] == "250" {
2018-09-03 06:42:56 +00:00
return nil
2018-08-13 07:48:32 +00:00
}
} else if index == 2 {
if splits[0] == "221" {
2018-09-03 06:42:56 +00:00
return nil
2018-08-13 07:48:32 +00:00
}
}
2018-09-03 06:42:56 +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 := &SmtpMatcher{}
b := []byte("helo test\r\n")
2018-08-15 07:17:18 +00:00
m.AddPacket(osm.NewPacket(b, len(b)))
2018-08-13 07:48:32 +00:00
b = []byte("quit\r\n")
2018-08-15 07:17:18 +00:00
m.AddPacket(osm.NewPacket(b, len(b)))
2018-08-13 07:48:32 +00:00
return m
}