service_matcher-go/smtp/smtp.go
2018-09-12 13:26:27 +09:00

77 lines
1.3 KiB
Go

package smtp
import (
"strings"
osm "git.loafle.net/overflow/service_matcher-go"
)
type SmtpMatcher struct {
osm.Matchers
}
func (m *SmtpMatcher) Key() string {
return "SMTP"
}
func (m *SmtpMatcher) Type() string {
return "MAIL"
}
func (m *SmtpMatcher) Name(matchCtx *osm.MatchCtx) string {
return "SMTP"
}
func (m *SmtpMatcher) IsPrePacket() bool {
return true
}
func (m *SmtpMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool {
return true
}
func (m *SmtpMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
return false
}
func (m *SmtpMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error {
if packet == nil {
return osm.NoPacketReceivedError()
}
buf := string(packet.Bytes())
if len(buf) == 0 || len(buf) < 5 {
return osm.NotMatchedError()
}
splits := strings.Split(buf, "\r\n")
splits = strings.Split(buf, " ")
if index == 0 {
if splits[0] == "220" {
return nil
}
} else if index == 1 {
if splits[0] == "250" {
return nil
}
} else if index == 2 {
if splits[0] == "221" {
return nil
}
}
return osm.NotMatchedError()
}
func NewMatcher() osm.Matcher {
m := &SmtpMatcher{}
b := []byte("helo test\r\n")
m.AddPacket(osm.NewPacket(b, len(b)))
b = []byte("quit\r\n")
m.AddPacket(osm.NewPacket(b, len(b)))
return m
}