service_matcher-go/smtp/smtp.go
2018-08-13 16:48:32 +09:00

72 lines
1.1 KiB
Go

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